blob: 65bb50076632f3b92bbc988b73e927f13ce73f76 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070013
14my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080015my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070016
Joe Perches000d1cc12011-07-25 17:13:25 -070017my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070018
19use Getopt::Long qw(:config no_auto_abbrev);
20
21my $quiet = 0;
22my $tree = 1;
23my $chk_signoff = 1;
24my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070025my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070026my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080027my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070028my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070030my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070031my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070033my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080034my $summary = 1;
35my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080036my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070037my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070038my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070039my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080040my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070041my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080042my %debug;
Joe Perches34456862013-07-03 15:05:34 -070043my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070044my %use_type = ();
45my @use = ();
46my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070047my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070048my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070049my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080050my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070051my $ignore_perl_version = 0;
52my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070053my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070054my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070055my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070056my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070057my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070058my $typedefsfile = "";
Joe Perches57230292015-06-25 15:03:03 -070059my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070060my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070061
62sub help {
63 my ($exitcode) = @_;
64
65 print << "EOM";
66Usage: $P [OPTION]... [FILE]...
67Version: $V
68
69Options:
70 -q, --quiet quiet
71 --no-tree run without a kernel tree
72 --no-signoff do not check for 'Signed-off-by' line
73 --patch treat FILE as patchfile (default)
74 --emacs emacs compile window format
75 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070076 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070077 -g, --git treat FILE as a single commit or git revision range
78 single git commit with:
79 <rev>
80 <rev>^
81 <rev>~n
82 multiple git commits with:
83 <rev1>..<rev2>
84 <rev1>...<rev2>
85 <rev>-<count>
86 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070087 -f, --file treat FILE as regular source file
88 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070089 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070090 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070091 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070092 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080093 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070094 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070095 --root=PATH PATH to the kernel tree root
96 --no-summary suppress the per-file summary
97 --mailback only produce a report in case of warnings/errors
98 --summary-file include the filename in summary
99 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
100 'values', 'possible', 'type', and 'attr' (default
101 is all off)
102 --test-only=WORD report only warnings/errors containing WORD
103 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700104 --fix EXPERIMENTAL - may create horrible results
105 If correctable single-line errors exist, create
106 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
107 with potential errors corrected to the preferred
108 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800109 --fix-inplace EXPERIMENTAL - may create horrible results
110 Is the same as --fix, but overwrites the input
111 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700112 --ignore-perl-version override checking of perl version. expect
113 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700114 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700115 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700116 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700117 --typedefsfile Read additional types from this file
Joe Perches57230292015-06-25 15:03:03 -0700118 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700119 -h, --help, --version display this help and exit
120
121When FILE is - read standard input.
122EOM
123
124 exit($exitcode);
125}
126
Joe Perches3beb42e2016-05-20 17:04:14 -0700127sub uniq {
128 my %seen;
129 return grep { !$seen{$_}++ } @_;
130}
131
132sub list_types {
133 my ($exitcode) = @_;
134
135 my $count = 0;
136
137 local $/ = undef;
138
139 open(my $script, '<', abs_path($P)) or
140 die "$P: Can't read '$P' $!\n";
141
142 my $text = <$script>;
143 close($script);
144
145 my @types = ();
146 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
147 push (@types, $_);
148 }
149 @types = sort(uniq(@types));
150 print("#\tMessage type\n\n");
151 foreach my $type (@types) {
152 print(++$count . "\t" . $type . "\n");
153 }
154
155 exit($exitcode);
156}
157
Joe Perches000d1cc12011-07-25 17:13:25 -0700158my $conf = which_conf($configuration_file);
159if (-f $conf) {
160 my @conf_args;
161 open(my $conffile, '<', "$conf")
162 or warn "$P: Can't find a readable $configuration_file file $!\n";
163
164 while (<$conffile>) {
165 my $line = $_;
166
167 $line =~ s/\s*\n?$//g;
168 $line =~ s/^\s*//g;
169 $line =~ s/\s+/ /g;
170
171 next if ($line =~ m/^\s*#/);
172 next if ($line =~ m/^\s*$/);
173
174 my @words = split(" ", $line);
175 foreach my $word (@words) {
176 last if ($word =~ m/^#/);
177 push (@conf_args, $word);
178 }
179 }
180 close($conffile);
181 unshift(@ARGV, @conf_args) if @conf_args;
182}
183
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700184GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700186 'tree!' => \$tree,
187 'signoff!' => \$chk_signoff,
188 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800190 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700191 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700192 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700193 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'subjective!' => \$check,
195 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700196 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700197 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700198 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700199 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800200 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700201 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800203 'summary!' => \$summary,
204 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800205 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700206 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800207 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700208 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800209 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700210 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700211 'codespell!' => \$codespell,
212 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700213 'typedefsfile=s' => \$typedefsfile,
Joe Perches57230292015-06-25 15:03:03 -0700214 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700215 'h|help' => \$help,
216 'version' => \$help
217) or help(1);
218
219help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700220
Joe Perches3beb42e2016-05-20 17:04:14 -0700221list_types(0) if ($list_types);
222
Joe Perches9624b8d2014-01-23 15:54:44 -0800223$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700224$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800225
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700226my $exit = 0;
227
Dave Hansend62a2012013-09-11 14:23:56 -0700228if ($^V && $^V lt $minimum_perl_version) {
229 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
230 if (!$ignore_perl_version) {
231 exit(1);
232 }
233}
234
Allen Hubbe45107ff2016-08-02 14:04:47 -0700235#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700237 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700238}
239
Joe Perches91bfe482013-09-11 14:23:59 -0700240sub hash_save_array_words {
241 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700242
Joe Perches91bfe482013-09-11 14:23:59 -0700243 my @array = split(/,/, join(',', @$arrayRef));
244 foreach my $word (@array) {
245 $word =~ s/\s*\n?$//g;
246 $word =~ s/^\s*//g;
247 $word =~ s/\s+/ /g;
248 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700249
Joe Perches91bfe482013-09-11 14:23:59 -0700250 next if ($word =~ m/^\s*#/);
251 next if ($word =~ m/^\s*$/);
252
253 $hashRef->{$word}++;
254 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700255}
256
Joe Perches91bfe482013-09-11 14:23:59 -0700257sub hash_show_words {
258 my ($hashRef, $prefix) = @_;
259
Joe Perches3c816e42015-06-25 15:03:29 -0700260 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700261 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700262 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700263 print " $word";
264 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700265 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700266 }
267}
268
269hash_save_array_words(\%ignore_type, \@ignore);
270hash_save_array_words(\%use_type, \@use);
271
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800272my $dbg_values = 0;
273my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700274my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700275my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800276for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800277 ## no critic
278 eval "\${dbg_$key} = '$debug{$key}';";
279 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800280}
281
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700282my $rpt_cleaners = 0;
283
Andy Whitcroft8905a672007-11-28 16:21:06 -0800284if ($terse) {
285 $emacs = 1;
286 $quiet++;
287}
288
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700289if ($tree) {
290 if (defined $root) {
291 if (!top_of_kernel_tree($root)) {
292 die "$P: $root: --root does not point at a valid tree\n";
293 }
294 } else {
295 if (top_of_kernel_tree('.')) {
296 $root = '.';
297 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
298 top_of_kernel_tree($1)) {
299 $root = $1;
300 }
301 }
302
303 if (!defined $root) {
304 print "Must be run from the top-level dir. of a kernel tree\n";
305 exit(2);
306 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700307}
308
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700309my $emitted_corrupt = 0;
310
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700311our $Ident = qr{
312 [A-Za-z_][A-Za-z\d_]*
313 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
314 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700315our $Storage = qr{extern|static|asmlinkage};
316our $Sparse = qr{
317 __user|
318 __kernel|
319 __force|
320 __iomem|
321 __must_check|
322 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800323 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700324 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800325 __rcu|
326 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700327 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800328our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
329our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
330our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
331our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
332our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700333
Wolfram Sang52131292010-03-05 13:43:51 -0800334# Notes to $Attribute:
335# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700336our $Attribute = qr{
337 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700338 __percpu|
339 __nocast|
340 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200341 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700342 __packed__|
343 __packed2__|
344 __naked|
345 __maybe_unused|
346 __always_unused|
347 __noreturn|
348 __used|
349 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800350 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700351 __noclone|
352 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700353 __read_mostly|
354 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700355 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700356 ____cacheline_aligned|
357 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800358 ____cacheline_internodealigned_in_smp|
359 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700360 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700361our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700362our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
364our $Lval = qr{$Ident(?:$Member)*};
365
Joe Perches95e2c602013-07-03 15:05:20 -0700366our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
367our $Binary = qr{(?i)0b[01]+$Int_type?};
368our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
369our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700370our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800371our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800372our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
373our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
374our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800375our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700376our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800377our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700378our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700379our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700380our $Operators = qr{
381 <=|>=|==|!=|
382 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700383 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 }x;
385
Joe Perches91cb5192014-04-03 14:49:32 -0700386our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
387
Joe Perchesab7e23f2015-04-16 12:44:22 -0700388our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700390our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700391our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700393our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800394our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700395our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396
Joe Perches15662b32011-10-31 17:13:12 -0700397our $NON_ASCII_UTF8 = qr{
398 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700399 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
400 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
401 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
402 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
403 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
404 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
405}x;
406
Joe Perches15662b32011-10-31 17:13:12 -0700407our $UTF8 = qr{
408 [\x09\x0A\x0D\x20-\x7E] # ASCII
409 | $NON_ASCII_UTF8
410}x;
411
Joe Perchese6176fa2015-06-25 15:02:49 -0700412our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800413our $typeOtherOSTypedefs = qr{(?x:
414 u_(?:char|short|int|long) | # bsd
415 u(?:nchar|short|int|long) # sysv
416)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700417our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700418 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700419 atomic_t
420)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700421our $typeTypedefs = qr{(?x:
422 $typeC99Typedefs\b|
423 $typeOtherOSTypedefs\b|
424 $typeKernelTypedefs\b
425)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700426
Joe Perches6d32f7a2015-11-06 16:31:37 -0800427our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
428
Joe Perches691e6692010-03-05 13:43:51 -0800429our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800430 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700431 (?:[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 -0700432 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700433 panic|
Joe Perches06668722013-11-12 15:10:07 -0800434 MODULE_[A-Z_]+|
435 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800436)};
437
Joe Perches20112472011-07-25 17:13:23 -0700438our $signature_tags = qr{(?xi:
439 Signed-off-by:|
440 Acked-by:|
441 Tested-by:|
442 Reviewed-by:|
443 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700444 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700445 To:|
446 Cc:
447)};
448
Joe Perches18130872014-08-06 16:11:22 -0700449our @typeListMisordered = (
450 qr{char\s+(?:un)?signed},
451 qr{int\s+(?:(?:un)?signed\s+)?short\s},
452 qr{int\s+short(?:\s+(?:un)?signed)},
453 qr{short\s+int(?:\s+(?:un)?signed)},
454 qr{(?:un)?signed\s+int\s+short},
455 qr{short\s+(?:un)?signed},
456 qr{long\s+int\s+(?:un)?signed},
457 qr{int\s+long\s+(?:un)?signed},
458 qr{long\s+(?:un)?signed\s+int},
459 qr{int\s+(?:un)?signed\s+long},
460 qr{int\s+(?:un)?signed},
461 qr{int\s+long\s+long\s+(?:un)?signed},
462 qr{long\s+long\s+int\s+(?:un)?signed},
463 qr{long\s+long\s+(?:un)?signed\s+int},
464 qr{long\s+long\s+(?:un)?signed},
465 qr{long\s+(?:un)?signed},
466);
467
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468our @typeList = (
469 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700470 qr{(?:(?:un)?signed\s+)?char},
471 qr{(?:(?:un)?signed\s+)?short\s+int},
472 qr{(?:(?:un)?signed\s+)?short},
473 qr{(?:(?:un)?signed\s+)?int},
474 qr{(?:(?:un)?signed\s+)?long\s+int},
475 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
476 qr{(?:(?:un)?signed\s+)?long\s+long},
477 qr{(?:(?:un)?signed\s+)?long},
478 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800479 qr{float},
480 qr{double},
481 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800482 qr{struct\s+$Ident},
483 qr{union\s+$Ident},
484 qr{enum\s+$Ident},
485 qr{${Ident}_t},
486 qr{${Ident}_handler},
487 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700488 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489);
Joe Perches938224b2016-01-20 14:59:15 -0800490
491our $C90_int_types = qr{(?x:
492 long\s+long\s+int\s+(?:un)?signed|
493 long\s+long\s+(?:un)?signed\s+int|
494 long\s+long\s+(?:un)?signed|
495 (?:(?:un)?signed\s+)?long\s+long\s+int|
496 (?:(?:un)?signed\s+)?long\s+long|
497 int\s+long\s+long\s+(?:un)?signed|
498 int\s+(?:(?:un)?signed\s+)?long\s+long|
499
500 long\s+int\s+(?:un)?signed|
501 long\s+(?:un)?signed\s+int|
502 long\s+(?:un)?signed|
503 (?:(?:un)?signed\s+)?long\s+int|
504 (?:(?:un)?signed\s+)?long|
505 int\s+long\s+(?:un)?signed|
506 int\s+(?:(?:un)?signed\s+)?long|
507
508 int\s+(?:un)?signed|
509 (?:(?:un)?signed\s+)?int
510)};
511
Alex Dowad485ff232015-06-25 15:02:52 -0700512our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700513our @typeListWithAttr = (
514 @typeList,
515 qr{struct\s+$InitAttribute\s+$Ident},
516 qr{union\s+$InitAttribute\s+$Ident},
517);
518
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700519our @modifierList = (
520 qr{fastcall},
521);
Alex Dowad485ff232015-06-25 15:02:52 -0700522our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523
Joe Perches24358802014-04-03 14:49:13 -0700524our @mode_permission_funcs = (
525 ["module_param", 3],
526 ["module_param_(?:array|named|string)", 4],
527 ["module_param_array_named", 5],
528 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
529 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700530 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
531 ["IIO_DEV_ATTR_[A-Z_]+", 1],
532 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
533 ["SENSOR_TEMPLATE(?:_2|)", 3],
534 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700535);
536
Joe Perches515a2352014-04-03 14:49:24 -0700537#Create a search pattern for all these functions to speed up a loop below
538our $mode_perms_search = "";
539foreach my $entry (@mode_permission_funcs) {
540 $mode_perms_search .= '|' if ($mode_perms_search ne "");
541 $mode_perms_search .= $entry->[0];
542}
543
Joe Perchesb392c642015-04-16 12:44:16 -0700544our $mode_perms_world_writable = qr{
545 S_IWUGO |
546 S_IWOTH |
547 S_IRWXUGO |
548 S_IALLUGO |
549 0[0-7][0-7][2367]
550}x;
551
Joe Perchesf90774e2016-10-11 13:51:47 -0700552our %mode_permission_string_types = (
553 "S_IRWXU" => 0700,
554 "S_IRUSR" => 0400,
555 "S_IWUSR" => 0200,
556 "S_IXUSR" => 0100,
557 "S_IRWXG" => 0070,
558 "S_IRGRP" => 0040,
559 "S_IWGRP" => 0020,
560 "S_IXGRP" => 0010,
561 "S_IRWXO" => 0007,
562 "S_IROTH" => 0004,
563 "S_IWOTH" => 0002,
564 "S_IXOTH" => 0001,
565 "S_IRWXUGO" => 0777,
566 "S_IRUGO" => 0444,
567 "S_IWUGO" => 0222,
568 "S_IXUGO" => 0111,
569);
570
571#Create a search pattern for all these strings to speed up a loop below
572our $mode_perms_string_search = "";
573foreach my $entry (keys %mode_permission_string_types) {
574 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
575 $mode_perms_string_search .= $entry;
576}
577
Wolfram Sang7840a942010-08-09 17:20:57 -0700578our $allowed_asm_includes = qr{(?x:
579 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700580 memory|
581 time|
582 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700583)};
584# memory.h: ARM has a custom one
585
Kees Cook66b47b42014-10-13 15:51:57 -0700586# Load common spelling mistakes and build regular expression list.
587my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700588my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700589
Joe Perches36061e32014-12-10 15:51:43 -0800590if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800591 while (<$spelling>) {
592 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700593
Joe Perches36061e32014-12-10 15:51:43 -0800594 $line =~ s/\s*\n?$//g;
595 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700596
Joe Perches36061e32014-12-10 15:51:43 -0800597 next if ($line =~ m/^\s*#/);
598 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700599
Joe Perches36061e32014-12-10 15:51:43 -0800600 my ($suspect, $fix) = split(/\|\|/, $line);
601
Joe Perches36061e32014-12-10 15:51:43 -0800602 $spelling_fix{$suspect} = $fix;
603 }
604 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800605} else {
606 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700607}
Kees Cook66b47b42014-10-13 15:51:57 -0700608
Joe Perchesebfd7d62015-04-16 12:44:14 -0700609if ($codespell) {
610 if (open(my $spelling, '<', $codespellfile)) {
611 while (<$spelling>) {
612 my $line = $_;
613
614 $line =~ s/\s*\n?$//g;
615 $line =~ s/^\s*//g;
616
617 next if ($line =~ m/^\s*#/);
618 next if ($line =~ m/^\s*$/);
619 next if ($line =~ m/, disabled/i);
620
621 $line =~ s/,.*$//;
622
623 my ($suspect, $fix) = split(/->/, $line);
624
625 $spelling_fix{$suspect} = $fix;
626 }
627 close($spelling);
628 } else {
629 warn "No codespell typos will be found - file '$codespellfile': $!\n";
630 }
631}
632
633$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
634
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700635sub read_words {
636 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700637
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700638 if (open(my $words, '<', $file)) {
639 while (<$words>) {
640 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700641
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700642 $line =~ s/\s*\n?$//g;
643 $line =~ s/^\s*//g;
644
645 next if ($line =~ m/^\s*#/);
646 next if ($line =~ m/^\s*$/);
647 if ($line =~ /\s/) {
648 print("$file: '$line' invalid - ignored\n");
649 next;
650 }
651
652 $$wordsRef .= '|' if ($$wordsRef ne "");
653 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700654 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700655 close($file);
656 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700657 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700658
659 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700660}
661
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700662my $const_structs = "";
663read_words(\$const_structs, $conststructsfile)
664 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
665
666my $typeOtherTypedefs = "";
667if (length($typedefsfile)) {
668 read_words(\$typeOtherTypedefs, $typedefsfile)
669 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
670}
671$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
672
Andy Whitcroft8905a672007-11-28 16:21:06 -0800673sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700674 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
675 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700676 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700677 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700678 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700679 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700680 (?:$typeTypedefs\b)|
681 (?:${all}\b)
682 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800683 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700684 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800685 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800686 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700687 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700688 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800689 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700690 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800691 }x;
Joe Perches18130872014-08-06 16:11:22 -0700692 $NonptrTypeMisordered = qr{
693 (?:$Modifier\s+|const\s+)*
694 (?:
695 (?:${Misordered}\b)
696 )
697 (?:\s+$Modifier|\s+const)*
698 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700699 $NonptrTypeWithAttr = qr{
700 (?:$Modifier\s+|const\s+)*
701 (?:
702 (?:typeof|__typeof__)\s*\([^\)]*\)|
703 (?:$typeTypedefs\b)|
704 (?:${allWithAttr}\b)
705 )
706 (?:\s+$Modifier|\s+const)*
707 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800708 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700709 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700710 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700711 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712 }x;
Joe Perches18130872014-08-06 16:11:22 -0700713 $TypeMisordered = qr{
714 $NonptrTypeMisordered
715 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
716 (?:\s+$Inline|\s+$Modifier)*
717 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700718 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700719 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800720}
721build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700722
Joe Perches7d2367a2011-07-25 17:13:22 -0700723our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700724
725# Using $balanced_parens, $LvalOrFunc, or $FuncArg
726# requires at least perl version v5.10.0
727# Any use must be runtime checked with $^V
728
729our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700730our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800731our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700732
Joe Perchesf8422302014-08-06 16:11:31 -0700733our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700734 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700735 (?:$Storage\s+)?LIST_HEAD\s*\(|
736 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
737)};
738
Joe Perches7d2367a2011-07-25 17:13:22 -0700739sub deparenthesize {
740 my ($string) = @_;
741 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700742
743 while ($string =~ /^\s*\(.*\)\s*$/) {
744 $string =~ s@^\s*\(\s*@@;
745 $string =~ s@\s*\)\s*$@@;
746 }
747
Joe Perches7d2367a2011-07-25 17:13:22 -0700748 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700749
Joe Perches7d2367a2011-07-25 17:13:22 -0700750 return $string;
751}
752
Joe Perches34456862013-07-03 15:05:34 -0700753sub seed_camelcase_file {
754 my ($file) = @_;
755
756 return if (!(-f $file));
757
758 local $/;
759
760 open(my $include_file, '<', "$file")
761 or warn "$P: Can't read '$file' $!\n";
762 my $text = <$include_file>;
763 close($include_file);
764
765 my @lines = split('\n', $text);
766
767 foreach my $line (@lines) {
768 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
769 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
770 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800771 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
772 $camelcase{$1} = 1;
773 } 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 -0700774 $camelcase{$1} = 1;
775 }
776 }
777}
778
Joe Perches85b0ee12016-10-11 13:51:44 -0700779sub is_maintained_obsolete {
780 my ($filename) = @_;
781
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800782 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700783
Joe Perches0616efa2016-10-11 13:52:02 -0700784 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 -0700785
786 return $status =~ /obsolete/i;
787}
788
Joe Perches34456862013-07-03 15:05:34 -0700789my $camelcase_seeded = 0;
790sub seed_camelcase_includes {
791 return if ($camelcase_seeded);
792
793 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700794 my $camelcase_cache = "";
795 my @include_files = ();
796
797 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700798
Richard Genoud3645e322014-02-10 14:25:32 -0800799 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700800 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
801 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700802 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700803 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700804 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700805 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700806 @include_files = split('\n', $files);
807 foreach my $file (@include_files) {
808 my $date = POSIX::strftime("%Y%m%d%H%M",
809 localtime((stat $file)[9]));
810 $last_mod_date = $date if ($last_mod_date < $date);
811 }
812 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700813 }
Joe Perchesc707a812013-07-08 16:00:43 -0700814
815 if ($camelcase_cache ne "" && -f $camelcase_cache) {
816 open(my $camelcase_file, '<', "$camelcase_cache")
817 or warn "$P: Can't read '$camelcase_cache' $!\n";
818 while (<$camelcase_file>) {
819 chomp;
820 $camelcase{$_} = 1;
821 }
822 close($camelcase_file);
823
824 return;
825 }
826
Richard Genoud3645e322014-02-10 14:25:32 -0800827 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700828 $files = `git ls-files "include/*.h"`;
829 @include_files = split('\n', $files);
830 }
831
Joe Perches34456862013-07-03 15:05:34 -0700832 foreach my $file (@include_files) {
833 seed_camelcase_file($file);
834 }
Joe Perches351b2a12013-07-03 15:05:36 -0700835
Joe Perchesc707a812013-07-08 16:00:43 -0700836 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700837 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700838 open(my $camelcase_file, '>', "$camelcase_cache")
839 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700840 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
841 print $camelcase_file ("$_\n");
842 }
843 close($camelcase_file);
844 }
Joe Perches34456862013-07-03 15:05:34 -0700845}
846
Joe Perchesd311cd42014-08-06 16:10:57 -0700847sub git_commit_info {
848 my ($commit, $id, $desc) = @_;
849
850 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
851
852 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
853 $output =~ s/^\s*//gm;
854 my @lines = split("\n", $output);
855
Joe Perches0d7835f2015-02-13 14:38:35 -0800856 return ($id, $desc) if ($#lines < 0);
857
Joe Perchesd311cd42014-08-06 16:10:57 -0700858 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
859# Maybe one day convert this block of bash into something that returns
860# all matching commit ids, but it's very slow...
861#
862# echo "checking commits $1..."
863# git rev-list --remotes | grep -i "^$1" |
864# while read line ; do
865# git log --format='%H %s' -1 $line |
866# echo "commit $(cut -c 1-12,41-)"
867# done
868 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
869 } else {
870 $id = substr($lines[0], 0, 12);
871 $desc = substr($lines[0], 41);
872 }
873
874 return ($id, $desc);
875}
876
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700877$chk_signoff = 0 if ($file);
878
Andy Whitcroft00df3442007-06-08 13:47:06 -0700879my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800880my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700881my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700882my @fixed_inserted = ();
883my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700884my $fixlinenr = -1;
885
Du, Changbin4a593c32016-05-20 17:04:16 -0700886# If input is git commits, extract all commits from the commit expressions.
887# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
888die "$P: No git repository found\n" if ($git && !-e ".git");
889
890if ($git) {
891 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700892 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700893 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700894 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
895 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700896 } elsif ($commit_expr =~ m/\.\./) {
897 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700898 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700899 $git_range = "-1 $commit_expr";
900 }
901 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
902 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700903 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
904 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700905 my $sha1 = $1;
906 my $subject = $2;
907 unshift(@commits, $sha1);
908 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700909 }
910 }
911 die "$P: no git commits after extraction!\n" if (@commits == 0);
912 @ARGV = @commits;
913}
914
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800915my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700916for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800917 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700918 if ($git) {
919 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
920 die "$P: $filename: git format-patch failed - $!\n";
921 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800922 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700923 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800924 } elsif ($filename eq '-') {
925 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700926 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800927 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700928 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700929 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800930 if ($filename eq '-') {
931 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700932 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700933 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800934 } else {
935 $vname = $filename;
936 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800937 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700938 chomp;
939 push(@rawlines, $_);
940 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800941 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700942
943 if ($#ARGV > 0 && $quiet == 0) {
944 print '-' x length($vname) . "\n";
945 print "$vname\n";
946 print '-' x length($vname) . "\n";
947 }
948
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800949 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700950 $exit = 1;
951 }
952 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800953 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700954 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700955 @fixed_inserted = ();
956 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700957 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700958 @modifierListFile = ();
959 @typeListFile = ();
960 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700961}
962
Joe Perchesd8469f12015-06-25 15:03:00 -0700963if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700964 hash_show_words(\%use_type, "Used");
965 hash_show_words(\%ignore_type, "Ignored");
966
Joe Perchesd8469f12015-06-25 15:03:00 -0700967 if ($^V lt 5.10.0) {
968 print << "EOM"
969
970NOTE: perl $^V is not modern enough to detect all possible issues.
971 An upgrade to at least perl v5.10.0 is suggested.
972EOM
973 }
974 if ($exit) {
975 print << "EOM"
976
977NOTE: If any of the errors are false positives, please report
978 them to the maintainer, see CHECKPATCH in MAINTAINERS.
979EOM
980 }
981}
982
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700983exit($exit);
984
985sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700986 my ($root) = @_;
987
988 my @tree_check = (
989 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
990 "README", "Documentation", "arch", "include", "drivers",
991 "fs", "init", "ipc", "kernel", "lib", "scripts",
992 );
993
994 foreach my $check (@tree_check) {
995 if (! -e $root . '/' . $check) {
996 return 0;
997 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700998 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700999 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001000}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001001
Joe Perches20112472011-07-25 17:13:23 -07001002sub parse_email {
1003 my ($formatted_email) = @_;
1004
1005 my $name = "";
1006 my $address = "";
1007 my $comment = "";
1008
1009 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1010 $name = $1;
1011 $address = $2;
1012 $comment = $3 if defined $3;
1013 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1014 $address = $1;
1015 $comment = $2 if defined $2;
1016 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1017 $address = $1;
1018 $comment = $2 if defined $2;
1019 $formatted_email =~ s/$address.*$//;
1020 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001021 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001022 $name =~ s/^\"|\"$//g;
1023 # If there's a name left after stripping spaces and
1024 # leading quotes, and the address doesn't have both
1025 # leading and trailing angle brackets, the address
1026 # is invalid. ie:
1027 # "joe smith joe@smith.com" bad
1028 # "joe smith <joe@smith.com" bad
1029 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1030 $name = "";
1031 $address = "";
1032 $comment = "";
1033 }
1034 }
1035
Joe Perches3705ce52013-07-03 15:05:31 -07001036 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001037 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001038 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001039 $address =~ s/^\<|\>$//g;
1040
1041 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1042 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1043 $name = "\"$name\"";
1044 }
1045
1046 return ($name, $address, $comment);
1047}
1048
1049sub format_email {
1050 my ($name, $address) = @_;
1051
1052 my $formatted_email;
1053
Joe Perches3705ce52013-07-03 15:05:31 -07001054 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001055 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001056 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001057
1058 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1059 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1060 $name = "\"$name\"";
1061 }
1062
1063 if ("$name" eq "") {
1064 $formatted_email = "$address";
1065 } else {
1066 $formatted_email = "$name <$address>";
1067 }
1068
1069 return $formatted_email;
1070}
1071
Joe Perchesd311cd42014-08-06 16:10:57 -07001072sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001073 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001074
Joe Perchesbd474ca2014-08-06 16:11:10 -07001075 foreach my $path (split(/:/, $ENV{PATH})) {
1076 if (-e "$path/$bin") {
1077 return "$path/$bin";
1078 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001079 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001080
Joe Perchesbd474ca2014-08-06 16:11:10 -07001081 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001082}
1083
Joe Perches000d1cc12011-07-25 17:13:25 -07001084sub which_conf {
1085 my ($conf) = @_;
1086
1087 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1088 if (-e "$path/$conf") {
1089 return "$path/$conf";
1090 }
1091 }
1092
1093 return "";
1094}
1095
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001096sub expand_tabs {
1097 my ($str) = @_;
1098
1099 my $res = '';
1100 my $n = 0;
1101 for my $c (split(//, $str)) {
1102 if ($c eq "\t") {
1103 $res .= ' ';
1104 $n++;
1105 for (; ($n % 8) != 0; $n++) {
1106 $res .= ' ';
1107 }
1108 next;
1109 }
1110 $res .= $c;
1111 $n++;
1112 }
1113
1114 return $res;
1115}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001117 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001118 return $res;
1119}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001120
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001121sub line_stats {
1122 my ($line) = @_;
1123
1124 # Drop the diff line leader and expand tabs
1125 $line =~ s/^.//;
1126 $line = expand_tabs($line);
1127
1128 # Pick the indent from the front of the line.
1129 my ($white) = ($line =~ /^(\s*)/);
1130
1131 return (length($line), length($white));
1132}
1133
Andy Whitcroft773647a2008-03-28 14:15:58 -07001134my $sanitise_quote = '';
1135
1136sub sanitise_line_reset {
1137 my ($in_comment) = @_;
1138
1139 if ($in_comment) {
1140 $sanitise_quote = '*/';
1141 } else {
1142 $sanitise_quote = '';
1143 }
1144}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001145sub sanitise_line {
1146 my ($line) = @_;
1147
1148 my $res = '';
1149 my $l = '';
1150
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001151 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001152 my $off = 0;
1153 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001154
Andy Whitcroft773647a2008-03-28 14:15:58 -07001155 # Always copy over the diff marker.
1156 $res = substr($line, 0, 1);
1157
1158 for ($off = 1; $off < length($line); $off++) {
1159 $c = substr($line, $off, 1);
1160
1161 # Comments we are wacking completly including the begin
1162 # and end, all to $;.
1163 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1164 $sanitise_quote = '*/';
1165
1166 substr($res, $off, 2, "$;$;");
1167 $off++;
1168 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001170 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001171 $sanitise_quote = '';
1172 substr($res, $off, 2, "$;$;");
1173 $off++;
1174 next;
1175 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001176 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1177 $sanitise_quote = '//';
1178
1179 substr($res, $off, 2, $sanitise_quote);
1180 $off++;
1181 next;
1182 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001183
1184 # A \ in a string means ignore the next character.
1185 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1186 $c eq "\\") {
1187 substr($res, $off, 2, 'XX');
1188 $off++;
1189 next;
1190 }
1191 # Regular quotes.
1192 if ($c eq "'" || $c eq '"') {
1193 if ($sanitise_quote eq '') {
1194 $sanitise_quote = $c;
1195
1196 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001197 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001198 } elsif ($sanitise_quote eq $c) {
1199 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001200 }
1201 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001202
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001203 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001204 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1205 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001206 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1207 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001208 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1209 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001210 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001211 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001212 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 }
1214
Daniel Walker113f04a2009-09-21 17:04:35 -07001215 if ($sanitise_quote eq '//') {
1216 $sanitise_quote = '';
1217 }
1218
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001219 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001220 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001221 my $clean = 'X' x length($1);
1222 $res =~ s@\<.*\>@<$clean>@;
1223
1224 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001225 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001226 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001227 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001228 }
1229
Joe Perchesdadf6802016-08-02 14:04:33 -07001230 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1231 my $match = $1;
1232 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1233 }
1234
Andy Whitcroft00df3442007-06-08 13:47:06 -07001235 return $res;
1236}
1237
Joe Perchesa6962d72013-04-29 16:18:13 -07001238sub get_quoted_string {
1239 my ($line, $rawline) = @_;
1240
Joe Perches33acb542015-06-25 15:02:54 -07001241 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001242 return substr($rawline, $-[0], $+[0] - $-[0]);
1243}
1244
Andy Whitcroft8905a672007-11-28 16:21:06 -08001245sub ctx_statement_block {
1246 my ($linenr, $remain, $off) = @_;
1247 my $line = $linenr - 1;
1248 my $blk = '';
1249 my $soff = $off;
1250 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001251 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001252
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001253 my $loff = 0;
1254
Andy Whitcroft8905a672007-11-28 16:21:06 -08001255 my $type = '';
1256 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001257 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001258 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001259 my $c;
1260 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001261
1262 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001263 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001264 @stack = (['', 0]) if ($#stack == -1);
1265
Andy Whitcroft773647a2008-03-28 14:15:58 -07001266 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001267 # If we are about to drop off the end, pull in more
1268 # context.
1269 if ($off >= $len) {
1270 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001271 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001272 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001273 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001274 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001275 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001276 $len = length($blk);
1277 $line++;
1278 last;
1279 }
1280 # Bail if there is no further context.
1281 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001282 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001283 last;
1284 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001285 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1286 $level++;
1287 $type = '#';
1288 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001289 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001290 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001291 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001292 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001293
Andy Whitcroft773647a2008-03-28 14:15:58 -07001294 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001295
1296 # Handle nested #if/#else.
1297 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1298 push(@stack, [ $type, $level ]);
1299 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1300 ($type, $level) = @{$stack[$#stack - 1]};
1301 } elsif ($remainder =~ /^#\s*endif\b/) {
1302 ($type, $level) = @{pop(@stack)};
1303 }
1304
Andy Whitcroft8905a672007-11-28 16:21:06 -08001305 # Statement ends at the ';' or a close '}' at the
1306 # outermost level.
1307 if ($level == 0 && $c eq ';') {
1308 last;
1309 }
1310
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001311 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001312 if ($level == 0 && $coff_set == 0 &&
1313 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1314 $remainder =~ /^(else)(?:\s|{)/ &&
1315 $remainder !~ /^else\s+if\b/) {
1316 $coff = $off + length($1) - 1;
1317 $coff_set = 1;
1318 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1319 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001320 }
1321
Andy Whitcroft8905a672007-11-28 16:21:06 -08001322 if (($type eq '' || $type eq '(') && $c eq '(') {
1323 $level++;
1324 $type = '(';
1325 }
1326 if ($type eq '(' && $c eq ')') {
1327 $level--;
1328 $type = ($level != 0)? '(' : '';
1329
1330 if ($level == 0 && $coff < $soff) {
1331 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001332 $coff_set = 1;
1333 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001334 }
1335 }
1336 if (($type eq '' || $type eq '{') && $c eq '{') {
1337 $level++;
1338 $type = '{';
1339 }
1340 if ($type eq '{' && $c eq '}') {
1341 $level--;
1342 $type = ($level != 0)? '{' : '';
1343
1344 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001345 if (substr($blk, $off + 1, 1) eq ';') {
1346 $off++;
1347 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001348 last;
1349 }
1350 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001351 # Preprocessor commands end at the newline unless escaped.
1352 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1353 $level--;
1354 $type = '';
1355 $off++;
1356 last;
1357 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358 $off++;
1359 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001360 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001361 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001362 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001363 $line++;
1364 $remain--;
1365 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001366
1367 my $statement = substr($blk, $soff, $off - $soff + 1);
1368 my $condition = substr($blk, $soff, $coff - $soff + 1);
1369
1370 #warn "STATEMENT<$statement>\n";
1371 #warn "CONDITION<$condition>\n";
1372
Andy Whitcroft773647a2008-03-28 14:15:58 -07001373 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001374
1375 return ($statement, $condition,
1376 $line, $remain + 1, $off - $loff + 1, $level);
1377}
1378
Andy Whitcroftcf655042008-03-04 14:28:20 -08001379sub statement_lines {
1380 my ($stmt) = @_;
1381
1382 # Strip the diff line prefixes and rip blank lines at start and end.
1383 $stmt =~ s/(^|\n)./$1/g;
1384 $stmt =~ s/^\s*//;
1385 $stmt =~ s/\s*$//;
1386
1387 my @stmt_lines = ($stmt =~ /\n/g);
1388
1389 return $#stmt_lines + 2;
1390}
1391
1392sub statement_rawlines {
1393 my ($stmt) = @_;
1394
1395 my @stmt_lines = ($stmt =~ /\n/g);
1396
1397 return $#stmt_lines + 2;
1398}
1399
1400sub statement_block_size {
1401 my ($stmt) = @_;
1402
1403 $stmt =~ s/(^|\n)./$1/g;
1404 $stmt =~ s/^\s*{//;
1405 $stmt =~ s/}\s*$//;
1406 $stmt =~ s/^\s*//;
1407 $stmt =~ s/\s*$//;
1408
1409 my @stmt_lines = ($stmt =~ /\n/g);
1410 my @stmt_statements = ($stmt =~ /;/g);
1411
1412 my $stmt_lines = $#stmt_lines + 2;
1413 my $stmt_statements = $#stmt_statements + 1;
1414
1415 if ($stmt_lines > $stmt_statements) {
1416 return $stmt_lines;
1417 } else {
1418 return $stmt_statements;
1419 }
1420}
1421
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001422sub ctx_statement_full {
1423 my ($linenr, $remain, $off) = @_;
1424 my ($statement, $condition, $level);
1425
1426 my (@chunks);
1427
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001429 ($statement, $condition, $linenr, $remain, $off, $level) =
1430 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001431 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001432 push(@chunks, [ $condition, $statement ]);
1433 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1434 return ($level, $linenr, @chunks);
1435 }
1436
1437 # Pull in the following conditional/block pairs and see if they
1438 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001439 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 ($statement, $condition, $linenr, $remain, $off, $level) =
1441 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001442 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001444 #print "C: push\n";
1445 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001446 }
1447
1448 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001449}
1450
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001451sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001452 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001453 my $line;
1454 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001455 my $blk = '';
1456 my @o;
1457 my @c;
1458 my @res = ();
1459
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001460 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001461 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001462 for ($line = $start; $remain > 0; $line++) {
1463 next if ($rawlines[$line] =~ /^-/);
1464 $remain--;
1465
1466 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001467
1468 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001469 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001470 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001471 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001472 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001473 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001474 $level = pop(@stack);
1475 }
1476
Andy Whitcroft01464f32010-10-26 14:23:19 -07001477 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001478 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1479 if ($off > 0) {
1480 $off--;
1481 next;
1482 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001483
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001484 if ($c eq $close && $level > 0) {
1485 $level--;
1486 last if ($level == 0);
1487 } elsif ($c eq $open) {
1488 $level++;
1489 }
1490 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001491
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001492 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001493 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494 }
1495
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001496 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001497 }
1498
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001499 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001500}
1501sub ctx_block_outer {
1502 my ($linenr, $remain) = @_;
1503
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001504 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1505 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001506}
1507sub ctx_block {
1508 my ($linenr, $remain) = @_;
1509
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001510 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1511 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001512}
1513sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001514 my ($linenr, $remain, $off) = @_;
1515
1516 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1517 return @r;
1518}
1519sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001520 my ($linenr, $remain) = @_;
1521
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001522 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001523}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001524sub ctx_statement_level {
1525 my ($linenr, $remain, $off) = @_;
1526
1527 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1528}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001529
1530sub ctx_locate_comment {
1531 my ($first_line, $end_line) = @_;
1532
1533 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001534 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001535 return $current_comment if (defined $current_comment);
1536
1537 # Look through the context and try and figure out if there is a
1538 # comment.
1539 my $in_comment = 0;
1540 $current_comment = '';
1541 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001542 my $line = $rawlines[$linenr - 1];
1543 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001544 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1545 $in_comment = 1;
1546 }
1547 if ($line =~ m@/\*@) {
1548 $in_comment = 1;
1549 }
1550 if (!$in_comment && $current_comment ne '') {
1551 $current_comment = '';
1552 }
1553 $current_comment .= $line . "\n" if ($in_comment);
1554 if ($line =~ m@\*/@) {
1555 $in_comment = 0;
1556 }
1557 }
1558
1559 chomp($current_comment);
1560 return($current_comment);
1561}
1562sub ctx_has_comment {
1563 my ($first_line, $end_line) = @_;
1564 my $cmt = ctx_locate_comment($first_line, $end_line);
1565
Andy Whitcroft00df3442007-06-08 13:47:06 -07001566 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001567 ##print "CMMT: $cmt\n";
1568
1569 return ($cmt ne '');
1570}
1571
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001572sub raw_line {
1573 my ($linenr, $cnt) = @_;
1574
1575 my $offset = $linenr - 1;
1576 $cnt++;
1577
1578 my $line;
1579 while ($cnt) {
1580 $line = $rawlines[$offset++];
1581 next if (defined($line) && $line =~ /^-/);
1582 $cnt--;
1583 }
1584
1585 return $line;
1586}
1587
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001588sub cat_vet {
1589 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001590 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001591
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001592 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001593 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1594 $res .= $1;
1595 if ($2 ne '') {
1596 $coded = sprintf("^%c", unpack('C', $2) + 64);
1597 $res .= $coded;
1598 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001599 }
1600 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001601
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001602 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001603}
1604
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001605my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001607my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001608my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609
1610sub annotate_reset {
1611 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001612 $av_pending = '_';
1613 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001614 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001615}
1616
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001617sub annotate_values {
1618 my ($stream, $type) = @_;
1619
1620 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001621 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001622 my $cur = $stream;
1623
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001624 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001625
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001626 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001627 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001628 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001629 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001630 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001631 print "WS($1)\n" if ($dbg_values > 1);
1632 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001633 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001634 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001635 }
1636
Florian Micklerc023e4732011-01-12 16:59:58 -08001637 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001638 print "CAST($1)\n" if ($dbg_values > 1);
1639 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001640 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001641
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001642 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001643 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001644 $type = 'T';
1645
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001646 } elsif ($cur =~ /^($Modifier)\s*/) {
1647 print "MODIFIER($1)\n" if ($dbg_values > 1);
1648 $type = 'T';
1649
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001650 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001651 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001652 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001653 push(@av_paren_type, $type);
1654 if ($2 ne '') {
1655 $av_pending = 'N';
1656 }
1657 $type = 'E';
1658
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001659 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001660 print "UNDEF($1)\n" if ($dbg_values > 1);
1661 $av_preprocessor = 1;
1662 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001663
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001664 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001666 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001667
1668 push(@av_paren_type, $type);
1669 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001670 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001671
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001672 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1674 $av_preprocessor = 1;
1675
1676 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1677
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001678 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001679
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001680 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001681 print "PRE_END($1)\n" if ($dbg_values > 1);
1682
1683 $av_preprocessor = 1;
1684
1685 # Assume all arms of the conditional end as this
1686 # one does, and continue as if the #endif was not here.
1687 pop(@av_paren_type);
1688 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001689 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001690
1691 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001693
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001694 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1695 print "ATTR($1)\n" if ($dbg_values > 1);
1696 $av_pending = $type;
1697 $type = 'N';
1698
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001699 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001700 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001701 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001702 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703 }
1704 $type = 'N';
1705
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001706 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001707 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001708 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 $type = 'N';
1710
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001711 } elsif ($cur =~/^(case)/o) {
1712 print "CASE($1)\n" if ($dbg_values > 1);
1713 $av_pend_colon = 'C';
1714 $type = 'N';
1715
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001716 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001717 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 $type = 'N';
1719
1720 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001721 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001722 push(@av_paren_type, $av_pending);
1723 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 $type = 'N';
1725
1726 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001727 my $new_type = pop(@av_paren_type);
1728 if ($new_type ne '_') {
1729 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001730 print "PAREN('$1') -> $type\n"
1731 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001732 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001733 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734 }
1735
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001736 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001737 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001738 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001739 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001740
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001741 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1742 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001743 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001744 } elsif ($type eq 'E') {
1745 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001746 }
1747 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1748 $type = 'V';
1749
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001751 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001752 $type = 'V';
1753
1754 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001755 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001756 $type = 'N';
1757
Andy Whitcroftcf655042008-03-04 14:28:20 -08001758 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001759 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001760 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001761 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001762
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001763 } elsif ($cur =~/^(,)/) {
1764 print "COMMA($1)\n" if ($dbg_values > 1);
1765 $type = 'C';
1766
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001767 } elsif ($cur =~ /^(\?)/o) {
1768 print "QUESTION($1)\n" if ($dbg_values > 1);
1769 $type = 'N';
1770
1771 } elsif ($cur =~ /^(:)/o) {
1772 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1773
1774 substr($var, length($res), 1, $av_pend_colon);
1775 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1776 $type = 'E';
1777 } else {
1778 $type = 'N';
1779 }
1780 $av_pend_colon = 'O';
1781
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001782 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001783 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001784 $type = 'N';
1785
Andy Whitcroft0d413862008-10-15 22:02:16 -07001786 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001787 my $variant;
1788
1789 print "OPV($1)\n" if ($dbg_values > 1);
1790 if ($type eq 'V') {
1791 $variant = 'B';
1792 } else {
1793 $variant = 'U';
1794 }
1795
1796 substr($var, length($res), 1, $variant);
1797 $type = 'N';
1798
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001799 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 if ($1 ne '++' && $1 ne '--') {
1802 $type = 'N';
1803 }
1804
1805 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001806 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001807 }
1808 if (defined $1) {
1809 $cur = substr($cur, length($1));
1810 $res .= $type x length($1);
1811 }
1812 }
1813
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001814 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815}
1816
Andy Whitcroft8905a672007-11-28 16:21:06 -08001817sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001818 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001819 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001820 ^(?:
1821 $Modifier|
1822 $Storage|
1823 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001824 DEFINE_\S+
1825 )$|
1826 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001827 goto|
1828 return|
1829 case|
1830 else|
1831 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001832 do|
1833 \#|
1834 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001835 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001836 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001837 )}x;
1838 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1839 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001840 # Check for modifiers.
1841 $possible =~ s/\s*$Storage\s*//g;
1842 $possible =~ s/\s*$Sparse\s*//g;
1843 if ($possible =~ /^\s*$/) {
1844
1845 } elsif ($possible =~ /\s/) {
1846 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001847 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001848 if ($modifier !~ $notPermitted) {
1849 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001850 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001851 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001852 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001853
1854 } else {
1855 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001856 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001857 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001858 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001859 } else {
1860 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001861 }
1862}
1863
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001864my $prefix = '';
1865
Joe Perches000d1cc12011-07-25 17:13:25 -07001866sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001867 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001868
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001869 $type =~ tr/[a-z]/[A-Z]/;
1870
Joe Perchescbec18a2014-04-03 14:49:19 -07001871 return defined $use_type{$type} if (scalar keys %use_type > 0);
1872
1873 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001874}
1875
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001876sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001877 my ($level, $type, $msg) = @_;
1878
1879 if (!show_type($type) ||
1880 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001881 return 0;
1882 }
Joe Perches57230292015-06-25 15:03:03 -07001883 my $output = '';
1884 if (-t STDOUT && $color) {
1885 if ($level eq 'ERROR') {
1886 $output .= RED;
1887 } elsif ($level eq 'WARNING') {
1888 $output .= YELLOW;
1889 } else {
1890 $output .= GREEN;
1891 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001892 }
Joe Perches57230292015-06-25 15:03:03 -07001893 $output .= $prefix . $level . ':';
1894 if ($show_types) {
1895 $output .= BLUE if (-t STDOUT && $color);
1896 $output .= "$type:";
1897 }
1898 $output .= RESET if (-t STDOUT && $color);
1899 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001900
1901 if ($showfile) {
1902 my @lines = split("\n", $output, -1);
1903 splice(@lines, 1, 1);
1904 $output = join("\n", @lines);
1905 }
Joe Perches57230292015-06-25 15:03:03 -07001906 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001907
Joe Perches57230292015-06-25 15:03:03 -07001908 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001909
1910 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001911}
Joe Perchescbec18a2014-04-03 14:49:19 -07001912
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001913sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001914 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001915}
Joe Perches000d1cc12011-07-25 17:13:25 -07001916
Joe Perchesd752fcc2014-08-06 16:11:05 -07001917sub fixup_current_range {
1918 my ($lineRef, $offset, $length) = @_;
1919
1920 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1921 my $o = $1;
1922 my $l = $2;
1923 my $no = $o + $offset;
1924 my $nl = $l + $length;
1925 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1926 }
1927}
1928
1929sub fix_inserted_deleted_lines {
1930 my ($linesRef, $insertedRef, $deletedRef) = @_;
1931
1932 my $range_last_linenr = 0;
1933 my $delta_offset = 0;
1934
1935 my $old_linenr = 0;
1936 my $new_linenr = 0;
1937
1938 my $next_insert = 0;
1939 my $next_delete = 0;
1940
1941 my @lines = ();
1942
1943 my $inserted = @{$insertedRef}[$next_insert++];
1944 my $deleted = @{$deletedRef}[$next_delete++];
1945
1946 foreach my $old_line (@{$linesRef}) {
1947 my $save_line = 1;
1948 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001949 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001950 $delta_offset = 0;
1951 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1952 $range_last_linenr = $new_linenr;
1953 fixup_current_range(\$line, $delta_offset, 0);
1954 }
1955
1956 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1957 $deleted = @{$deletedRef}[$next_delete++];
1958 $save_line = 0;
1959 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1960 }
1961
1962 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1963 push(@lines, ${$inserted}{'LINE'});
1964 $inserted = @{$insertedRef}[$next_insert++];
1965 $new_linenr++;
1966 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1967 }
1968
1969 if ($save_line) {
1970 push(@lines, $line);
1971 $new_linenr++;
1972 }
1973
1974 $old_linenr++;
1975 }
1976
1977 return @lines;
1978}
1979
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001980sub fix_insert_line {
1981 my ($linenr, $line) = @_;
1982
1983 my $inserted = {
1984 LINENR => $linenr,
1985 LINE => $line,
1986 };
1987 push(@fixed_inserted, $inserted);
1988}
1989
1990sub fix_delete_line {
1991 my ($linenr, $line) = @_;
1992
1993 my $deleted = {
1994 LINENR => $linenr,
1995 LINE => $line,
1996 };
1997
1998 push(@fixed_deleted, $deleted);
1999}
2000
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002001sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002002 my ($type, $msg) = @_;
2003
2004 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002005 our $clean = 0;
2006 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002007 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 }
Joe Perches3705ce52013-07-03 15:05:31 -07002009 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002010}
2011sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002012 my ($type, $msg) = @_;
2013
2014 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002015 our $clean = 0;
2016 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002017 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002018 }
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002020}
2021sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002022 my ($type, $msg) = @_;
2023
2024 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002025 our $clean = 0;
2026 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002027 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002028 }
Joe Perches3705ce52013-07-03 15:05:31 -07002029 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002030}
2031
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002032sub check_absolute_file {
2033 my ($absolute, $herecurr) = @_;
2034 my $file = $absolute;
2035
2036 ##print "absolute<$absolute>\n";
2037
2038 # See if any suffix of this path is a path within the tree.
2039 while ($file =~ s@^[^/]*/@@) {
2040 if (-f "$root/$file") {
2041 ##print "file<$file>\n";
2042 last;
2043 }
2044 }
2045 if (! -f _) {
2046 return 0;
2047 }
2048
2049 # It is, so see if the prefix is acceptable.
2050 my $prefix = $absolute;
2051 substr($prefix, -length($file)) = '';
2052
2053 ##print "prefix<$prefix>\n";
2054 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002055 WARN("USE_RELATIVE_PATH",
2056 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002057 }
2058}
2059
Joe Perches3705ce52013-07-03 15:05:31 -07002060sub trim {
2061 my ($string) = @_;
2062
Joe Perchesb34c6482013-09-11 14:24:01 -07002063 $string =~ s/^\s+|\s+$//g;
2064
2065 return $string;
2066}
2067
2068sub ltrim {
2069 my ($string) = @_;
2070
2071 $string =~ s/^\s+//;
2072
2073 return $string;
2074}
2075
2076sub rtrim {
2077 my ($string) = @_;
2078
2079 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002080
2081 return $string;
2082}
2083
Joe Perches52ea8502013-11-12 15:10:09 -08002084sub string_find_replace {
2085 my ($string, $find, $replace) = @_;
2086
2087 $string =~ s/$find/$replace/g;
2088
2089 return $string;
2090}
2091
Joe Perches3705ce52013-07-03 15:05:31 -07002092sub tabify {
2093 my ($leading) = @_;
2094
2095 my $source_indent = 8;
2096 my $max_spaces_before_tab = $source_indent - 1;
2097 my $spaces_to_tab = " " x $source_indent;
2098
2099 #convert leading spaces to tabs
2100 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2101 #Remove spaces before a tab
2102 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2103
2104 return "$leading";
2105}
2106
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002107sub pos_last_openparen {
2108 my ($line) = @_;
2109
2110 my $pos = 0;
2111
2112 my $opens = $line =~ tr/\(/\(/;
2113 my $closes = $line =~ tr/\)/\)/;
2114
2115 my $last_openparen = 0;
2116
2117 if (($opens == 0) || ($closes >= $opens)) {
2118 return -1;
2119 }
2120
2121 my $len = length($line);
2122
2123 for ($pos = 0; $pos < $len; $pos++) {
2124 my $string = substr($line, $pos);
2125 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2126 $pos += length($1) - 1;
2127 } elsif (substr($line, $pos, 1) eq '(') {
2128 $last_openparen = $pos;
2129 } elsif (index($string, '(') == -1) {
2130 last;
2131 }
2132 }
2133
Joe Perches91cb5192014-04-03 14:49:32 -07002134 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002135}
2136
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002137sub process {
2138 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002139
2140 my $linenr=0;
2141 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002142 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002143 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002144 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002145
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002146 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002147 my $indent;
2148 my $previndent=0;
2149 my $stashindent=0;
2150
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002151 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002152 my $signoff = 0;
2153 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002154 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002155 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002156 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002157 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002158 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002159 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002160 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002161 my $non_utf8_charset = 0;
2162
Joe Perches365dd4e2014-08-06 16:10:42 -07002163 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002164 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002165
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002166 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002167 our $cnt_lines = 0;
2168 our $cnt_error = 0;
2169 our $cnt_warn = 0;
2170 our $cnt_chk = 0;
2171
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002172 # Trace the real file/line as we go.
2173 my $realfile = '';
2174 my $realline = 0;
2175 my $realcnt = 0;
2176 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002177 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002178 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002179 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002180 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002181 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002182
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002183 my $prev_values = 'E';
2184
2185 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002186 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002187 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002188 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002189 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002190
Joe Perches7e51f192013-09-11 14:23:57 -07002191 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002192
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002193 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002194 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002195 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196 my @setup_docs = ();
2197 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002198
Joe Perchesd8b07712013-11-12 15:10:06 -08002199 my $camelcase_file_seeded = 0;
2200
Andy Whitcroft773647a2008-03-28 14:15:58 -07002201 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002202 my $line;
2203 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002204 $linenr++;
2205 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002206
Joe Perches3705ce52013-07-03 15:05:31 -07002207 push(@fixed, $rawline) if ($fix);
2208
Andy Whitcroft773647a2008-03-28 14:15:58 -07002209 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002210 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002211 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002212 $setup_docs = 1;
2213 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002214 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002215 }
Joe Perches77cb8542017-02-24 15:01:28 -08002216 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2217 my $context = $4;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218 $realline=$1-1;
2219 if (defined $2) {
2220 $realcnt=$3+1;
2221 } else {
2222 $realcnt=1+1;
2223 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002224 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002225
Joe Perches77cb8542017-02-24 15:01:28 -08002226 if ($context =~ /\b(\w+)\s*\(/) {
2227 $context_function = $1;
2228 } else {
2229 undef $context_function;
2230 }
2231
Andy Whitcroft773647a2008-03-28 14:15:58 -07002232 # Guestimate if this is a continuing comment. Run
2233 # the context looking for a comment "edge". If this
2234 # edge is a close comment then we must be in a comment
2235 # at context start.
2236 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002237 my $cnt = $realcnt;
2238 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2239 next if (defined $rawlines[$ln - 1] &&
2240 $rawlines[$ln - 1] =~ /^-/);
2241 $cnt--;
2242 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002243 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002244 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2245 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2246 ($edge) = $1;
2247 last;
2248 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249 }
2250 if (defined $edge && $edge eq '*/') {
2251 $in_comment = 1;
2252 }
2253
2254 # Guestimate if this is a continuing comment. If this
2255 # is the start of a diff block and this line starts
2256 # ' *' then it is very likely a comment.
2257 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002258 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002259 {
2260 $in_comment = 1;
2261 }
2262
2263 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2264 sanitise_line_reset($in_comment);
2265
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002266 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002268 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002269 $line = sanitise_line($rawline);
2270 }
2271 push(@lines, $line);
2272
2273 if ($realcnt > 1) {
2274 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2275 } else {
2276 $realcnt = 0;
2277 }
2278
2279 #print "==>$rawline\n";
2280 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002281
2282 if ($setup_docs && $line =~ /^\+/) {
2283 push(@setup_docs, $line);
2284 }
2285 }
2286
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002287 $prefix = '';
2288
Andy Whitcroft773647a2008-03-28 14:15:58 -07002289 $realcnt = 0;
2290 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002291 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002292 foreach my $line (@lines) {
2293 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002294 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002295 my $sline = $line; #copy of $line
2296 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002297
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002298 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002299
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002300#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002301 if (!$in_commit_log &&
2302 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002303 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002304 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002305 $realline=$1-1;
2306 if (defined $2) {
2307 $realcnt=$3+1;
2308 } else {
2309 $realcnt=1+1;
2310 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002311 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002312 $prev_values = 'E';
2313
Andy Whitcroft773647a2008-03-28 14:15:58 -07002314 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002315 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002316 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002317 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002318 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002319
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002320# track the line number as we move through the hunk, note that
2321# new versions of GNU diff omit the leading space on completely
2322# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002323 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002324 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002325 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002326
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002327 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002328 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002329
2330 # Track the previous line.
2331 ($prevline, $stashline) = ($stashline, $line);
2332 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2334
Andy Whitcroft773647a2008-03-28 14:15:58 -07002335 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002336
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002337 } elsif ($realcnt == 1) {
2338 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002339 }
2340
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002341 my $hunk_line = ($realcnt != 0);
2342
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002343 $here = "#$linenr: " if (!$file);
2344 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002345
Joe Perches2ac73b42014-06-04 16:12:05 -07002346 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002347 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002348 if ($line =~ /^diff --git.*?(\S+)$/) {
2349 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002350 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002351 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002352 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002353 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002354 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002355 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002356 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002357
2358 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002359 if (!$file && $tree && $p1_prefix ne '' &&
2360 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002361 WARN("PATCH_PREFIX",
2362 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002363 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002364
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002365 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002366 ERROR("MODIFIED_INCLUDE_ASM",
2367 "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 -07002368 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002369 $found_file = 1;
2370 }
2371
Joe Perches34d88152015-06-25 15:03:05 -07002372#make up the handle for any error we report on this line
2373 if ($showfile) {
2374 $prefix = "$realfile:$realline: "
2375 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002376 if ($file) {
2377 $prefix = "$filename:$realline: ";
2378 } else {
2379 $prefix = "$filename:$linenr: ";
2380 }
Joe Perches34d88152015-06-25 15:03:05 -07002381 }
2382
Joe Perches2ac73b42014-06-04 16:12:05 -07002383 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002384 if (is_maintained_obsolete($realfile)) {
2385 WARN("OBSOLETE",
2386 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2387 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002388 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002389 $check = 1;
2390 } else {
2391 $check = $check_orig;
2392 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002393 next;
2394 }
2395
Randy Dunlap389834b2007-06-08 13:47:03 -07002396 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002397
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002398 my $hereline = "$here\n$rawline\n";
2399 my $herecurr = "$here\n$rawline\n";
2400 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002401
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002402 $cnt_lines++ if ($realcnt != 0);
2403
Joe Perchese518e9a2015-06-25 15:03:27 -07002404# Check if the commit log has what seems like a diff which can confuse patch
2405 if ($in_commit_log && !$commit_log_has_diff &&
2406 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2407 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2408 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2409 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2410 ERROR("DIFF_IN_COMMIT_MSG",
2411 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2412 $commit_log_has_diff = 1;
2413 }
2414
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002415# Check for incorrect file permissions
2416 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2417 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002418 if ($realfile !~ m@scripts/@ &&
2419 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002420 ERROR("EXECUTE_PERMISSIONS",
2421 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002422 }
2423 }
2424
Joe Perches20112472011-07-25 17:13:23 -07002425# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002426 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002427 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002428 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002429 }
2430
Joe Perchese0d975b2014-12-10 15:51:49 -08002431# Check if MAINTAINERS is being updated. If so, there's probably no need to
2432# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2433 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2434 $reported_maintainer_file = 1;
2435 }
2436
Joe Perches20112472011-07-25 17:13:23 -07002437# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002438 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002439 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002440 my $space_before = $1;
2441 my $sign_off = $2;
2442 my $space_after = $3;
2443 my $email = $4;
2444 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2445
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002446 if ($sign_off !~ /$signature_tags/) {
2447 WARN("BAD_SIGN_OFF",
2448 "Non-standard signature: $sign_off\n" . $herecurr);
2449 }
Joe Perches20112472011-07-25 17:13:23 -07002450 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002451 if (WARN("BAD_SIGN_OFF",
2452 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002454 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002455 "$ucfirst_sign_off $email";
2456 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002457 }
Joe Perches20112472011-07-25 17:13:23 -07002458 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002459 if (WARN("BAD_SIGN_OFF",
2460 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2461 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002462 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002463 "$ucfirst_sign_off $email";
2464 }
2465
Joe Perches20112472011-07-25 17:13:23 -07002466 }
2467 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002468 if (WARN("BAD_SIGN_OFF",
2469 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2470 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002471 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002472 "$ucfirst_sign_off $email";
2473 }
Joe Perches20112472011-07-25 17:13:23 -07002474 }
2475
2476 my ($email_name, $email_address, $comment) = parse_email($email);
2477 my $suggested_email = format_email(($email_name, $email_address));
2478 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002479 ERROR("BAD_SIGN_OFF",
2480 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002481 } else {
2482 my $dequoted = $suggested_email;
2483 $dequoted =~ s/^"//;
2484 $dequoted =~ s/" </ </;
2485 # Don't force email to have quotes
2486 # Allow just an angle bracketed address
2487 if ("$dequoted$comment" ne $email &&
2488 "<$email_address>$comment" ne $email &&
2489 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002490 WARN("BAD_SIGN_OFF",
2491 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002492 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002493 }
Joe Perches7e51f192013-09-11 14:23:57 -07002494
2495# Check for duplicate signatures
2496 my $sig_nospace = $line;
2497 $sig_nospace =~ s/\s//g;
2498 $sig_nospace = lc($sig_nospace);
2499 if (defined $signatures{$sig_nospace}) {
2500 WARN("BAD_SIGN_OFF",
2501 "Duplicate signature\n" . $herecurr);
2502 } else {
2503 $signatures{$sig_nospace} = 1;
2504 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002505 }
2506
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002507# Check email subject for common tools that don't need to be mentioned
2508 if ($in_header_lines &&
2509 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2510 WARN("EMAIL_SUBJECT",
2511 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2512 }
2513
Joe Perches9b3189e2014-06-04 16:12:10 -07002514# Check for old stable address
2515 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2516 ERROR("STABLE_ADDRESS",
2517 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2518 }
2519
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002520# Check for unwanted Gerrit info
2521 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2522 ERROR("GERRIT_CHANGE_ID",
2523 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2524 }
2525
Joe Perches369c8dd2015-11-06 16:31:34 -08002526# Check if the commit log is in a possible stack dump
2527 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2528 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2529 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2530 # timestamp
2531 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2532 # stack dump address
2533 $commit_log_possible_stack_dump = 1;
2534 }
2535
Joe Perches2a076f42015-04-16 12:44:28 -07002536# Check for line lengths > 75 in commit log, warn once
2537 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002538 length($line) > 75 &&
2539 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2540 # file delta changes
2541 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2542 # filename then :
2543 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2544 # A Fixes: or Link: line
2545 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002546 WARN("COMMIT_LOG_LONG_LINE",
2547 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2548 $commit_log_long_line = 1;
2549 }
2550
Joe Perchesbf4daf12015-09-09 15:37:50 -07002551# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002552 if ($in_commit_log && $commit_log_possible_stack_dump &&
2553 $line =~ /^\s*$/) {
2554 $commit_log_possible_stack_dump = 0;
2555 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002556
Joe Perches0d7835f2015-02-13 14:38:35 -08002557# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002558 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002559 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002560 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002561 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002562 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002563 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2564 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002565 my $init_char = "c";
2566 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002567 my $short = 1;
2568 my $long = 0;
2569 my $case = 1;
2570 my $space = 1;
2571 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002572 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002573 my $id = '0123456789ab';
2574 my $orig_desc = "commit description";
2575 my $description = "";
2576
Joe Perchesfe043ea2015-09-09 15:37:25 -07002577 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2578 $init_char = $1;
2579 $orig_commit = lc($2);
2580 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2581 $orig_commit = lc($1);
2582 }
2583
Joe Perches0d7835f2015-02-13 14:38:35 -08002584 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2585 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2586 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2587 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2588 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2589 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002590 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002591 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2592 defined $rawlines[$linenr] &&
2593 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2594 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002595 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002596 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2597 defined $rawlines[$linenr] &&
2598 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2599 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2600 $orig_desc = $1;
2601 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2602 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002603 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002604 }
2605
2606 ($id, $description) = git_commit_info($orig_commit,
2607 $id, $orig_desc);
2608
Joe Perches19c146a2015-02-13 14:39:00 -08002609 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002610 ERROR("GIT_COMMIT_ID",
2611 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2612 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002613 }
2614
Joe Perches13f19372014-08-06 16:10:59 -07002615# Check for added, moved or deleted files
2616 if (!$reported_maintainer_file && !$in_commit_log &&
2617 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2618 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2619 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2620 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002621 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002622 $reported_maintainer_file = 1;
2623 WARN("FILE_PATH_CHANGES",
2624 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2625 }
2626
Andy Whitcroft00df3442007-06-08 13:47:06 -07002627# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002628 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002629 ERROR("CORRUPTED_PATCH",
2630 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002631 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002632 }
2633
2634# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2635 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002636 $rawline !~ m/^$UTF8*$/) {
2637 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2638
2639 my $blank = copy_spacing($rawline);
2640 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2641 my $hereptr = "$hereline$ptr\n";
2642
Joe Perches34d99212011-07-25 17:13:26 -07002643 CHK("INVALID_UTF8",
2644 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002645 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002646
Joe Perches15662b32011-10-31 17:13:12 -07002647# Check if it's the start of a commit log
2648# (not a header line and we haven't seen the patch filename)
2649 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002650 !($rawline =~ /^\s+(?:\S|$)/ ||
2651 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002652 $in_header_lines = 0;
2653 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002654 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002655 }
2656
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002657# Check if there is UTF-8 in a commit log when a mail header has explicitly
2658# declined it, i.e defined some charset where it is missing.
2659 if ($in_header_lines &&
2660 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2661 $1 !~ /utf-8/i) {
2662 $non_utf8_charset = 1;
2663 }
2664
2665 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002666 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002667 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002668 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2669 }
2670
Joe Perchesd6430f72016-12-12 16:46:28 -08002671# Check for absolute kernel paths in commit message
2672 if ($tree && $in_commit_log) {
2673 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2674 my $file = $1;
2675
2676 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2677 check_absolute_file($1, $herecurr)) {
2678 #
2679 } else {
2680 check_absolute_file($file, $herecurr);
2681 }
2682 }
2683 }
2684
Kees Cook66b47b42014-10-13 15:51:57 -07002685# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002686 if (defined($misspellings) &&
2687 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002688 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002689 my $typo = $1;
2690 my $typo_fix = $spelling_fix{lc($typo)};
2691 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2692 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2693 my $msg_type = \&WARN;
2694 $msg_type = \&CHK if ($file);
2695 if (&{$msg_type}("TYPO_SPELLING",
2696 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2697 $fix) {
2698 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2699 }
2700 }
2701 }
2702
Andy Whitcroft306708542008-10-15 22:02:28 -07002703# ignore non-hunk lines and lines being removed
2704 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002705
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002706#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002707 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002708 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002709 if (ERROR("DOS_LINE_ENDINGS",
2710 "DOS line endings\n" . $herevet) &&
2711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002712 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002713 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002714 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2715 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002716 if (ERROR("TRAILING_WHITESPACE",
2717 "trailing whitespace\n" . $herevet) &&
2718 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002719 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002720 }
2721
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002722 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002723 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002724
Josh Triplett4783f892013-11-12 15:10:12 -08002725# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002726 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002727 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002728 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2729 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002730 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2731 my $msg_type = \&ERROR;
2732 $msg_type = \&CHK if ($file);
2733 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002734 "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 -08002735 }
2736
Andi Kleen33549572010-05-24 14:33:29 -07002737# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002738# Only applies when adding the entry originally, after that we do not have
2739# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002740 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002741 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002742 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002743 my $cnt = $realcnt;
2744 my $ln = $linenr + 1;
2745 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002746 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002747 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002748 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002749 $f = $lines[$ln - 1];
2750 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2751 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002752
2753 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002754 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002755
Joe Perches8d73e0e2014-08-06 16:10:46 -07002756 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002757 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002758 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002759 $length = -1;
2760 }
2761
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002762 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002763 $f =~ s/#.*//;
2764 $f =~ s/^\s+//;
2765 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002766 if ($f =~ /^\s*config\s/) {
2767 $is_end = 1;
2768 last;
2769 }
Andi Kleen33549572010-05-24 14:33:29 -07002770 $length++;
2771 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002772 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2773 WARN("CONFIG_DESCRIPTION",
2774 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2775 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002776 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002777 }
2778
Christoph Jaeger327953e2015-02-13 14:38:29 -08002779# discourage the use of boolean for type definition attributes of Kconfig options
2780 if ($realfile =~ /Kconfig/ &&
2781 $line =~ /^\+\s*\bboolean\b/) {
2782 WARN("CONFIG_TYPE_BOOLEAN",
2783 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2784 }
2785
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002786 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2787 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2788 my $flag = $1;
2789 my $replacement = {
2790 'EXTRA_AFLAGS' => 'asflags-y',
2791 'EXTRA_CFLAGS' => 'ccflags-y',
2792 'EXTRA_CPPFLAGS' => 'cppflags-y',
2793 'EXTRA_LDFLAGS' => 'ldflags-y',
2794 };
2795
2796 WARN("DEPRECATED_VARIABLE",
2797 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2798 }
2799
Rob Herringbff5da42014-01-23 15:54:51 -08002800# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002801 if (defined $root &&
2802 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2803 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2804
Rob Herringbff5da42014-01-23 15:54:51 -08002805 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2806
Florian Vaussardcc933192014-04-03 14:49:27 -07002807 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2808 my $vp_file = $dt_path . "vendor-prefixes.txt";
2809
Rob Herringbff5da42014-01-23 15:54:51 -08002810 foreach my $compat (@compats) {
2811 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002812 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2813 my $compat3 = $compat;
2814 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2815 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002816 if ( $? >> 8 ) {
2817 WARN("UNDOCUMENTED_DT_STRING",
2818 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2819 }
2820
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002821 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2822 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002823 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002824 if ( $? >> 8 ) {
2825 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002826 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002827 }
2828 }
2829 }
2830
Andy Whitcroft5368df202008-10-15 22:02:27 -07002831# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002832 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002833
Joe Perches47e0c882015-06-25 15:02:57 -07002834# line length limit (with some exclusions)
2835#
2836# There are a few types of lines that may extend beyond $max_line_length:
2837# logging functions like pr_info that end in a string
2838# lines with a single string
2839# #defines that are a single string
2840#
2841# There are 3 different line length message types:
2842# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2843# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2844# LONG_LINE all other lines longer than $max_line_length
2845#
2846# if LONG_LINE is ignored, the other 2 types are also ignored
2847#
2848
Joe Perchesb4749e92015-07-17 16:24:01 -07002849 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07002850 my $msg_type = "LONG_LINE";
2851
2852 # Check the allowed long line types first
2853
2854 # logging functions that end in a string that starts
2855 # before $max_line_length
2856 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2857 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2858 $msg_type = "";
2859
2860 # lines with only strings (w/ possible termination)
2861 # #defines with only strings
2862 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2863 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2864 $msg_type = "";
2865
Joe Perchesd560a5f2016-08-02 14:04:31 -07002866 # EFI_GUID is another special case
2867 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2868 $msg_type = "";
2869
Joe Perches47e0c882015-06-25 15:02:57 -07002870 # Otherwise set the alternate message types
2871
2872 # a comment starts before $max_line_length
2873 } elsif ($line =~ /($;[\s$;]*)$/ &&
2874 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2875 $msg_type = "LONG_LINE_COMMENT"
2876
2877 # a quoted string starts before $max_line_length
2878 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2879 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2880 $msg_type = "LONG_LINE_STRING"
2881 }
2882
2883 if ($msg_type ne "" &&
2884 (show_type("LONG_LINE") || show_type($msg_type))) {
2885 WARN($msg_type,
2886 "line over $max_line_length characters\n" . $herecurr);
2887 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002888 }
2889
Andy Whitcroft8905a672007-11-28 16:21:06 -08002890# check for adding lines without a newline.
2891 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002892 WARN("MISSING_EOF_NEWLINE",
2893 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002894 }
2895
Mike Frysinger42e41c52009-09-21 17:04:40 -07002896# Blackfin: use hi/lo macros
2897 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2898 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2899 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002900 ERROR("LO_MACRO",
2901 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002902 }
2903 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2904 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002905 ERROR("HI_MACRO",
2906 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002907 }
2908 }
2909
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002910# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002911 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002912
2913# at the beginning of a line any tabs must come first and anything
2914# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002915 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2916 $rawline =~ /^\+\s* \s*/) {
2917 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002918 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002919 if (ERROR("CODE_INDENT",
2920 "code indent should use tabs where possible\n" . $herevet) &&
2921 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002922 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002923 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002924 }
2925
Alberto Panizzo08e44362010-03-05 13:43:54 -08002926# check for space before tabs.
2927 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2928 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002929 if (WARN("SPACE_BEFORE_TAB",
2930 "please, no space before tabs\n" . $herevet) &&
2931 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002932 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002933 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002934 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002935 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002936 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002937 }
2938
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002939# check for && or || at the start of a line
2940 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2941 CHK("LOGICAL_CONTINUATIONS",
2942 "Logical continuations should be on the previous line\n" . $hereprev);
2943 }
2944
Joe Perchesa91e8992016-05-20 17:04:05 -07002945# check indentation starts on a tab stop
2946 if ($^V && $^V ge 5.10.0 &&
2947 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2948 my $indent = length($1);
2949 if ($indent % 8) {
2950 if (WARN("TABSTOP",
2951 "Statements should start on a tabstop\n" . $herecurr) &&
2952 $fix) {
2953 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2954 }
2955 }
2956 }
2957
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002958# check multi-line statement indentation matches previous line
2959 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002960 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002961 $prevline =~ /^\+(\t*)(.*)$/;
2962 my $oldindent = $1;
2963 my $rest = $2;
2964
2965 my $pos = pos_last_openparen($rest);
2966 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002967 $line =~ /^(\+| )([ \t]*)/;
2968 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002969
2970 my $goodtabindent = $oldindent .
2971 "\t" x ($pos / 8) .
2972 " " x ($pos % 8);
2973 my $goodspaceindent = $oldindent . " " x $pos;
2974
2975 if ($newindent ne $goodtabindent &&
2976 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002977
2978 if (CHK("PARENTHESIS_ALIGNMENT",
2979 "Alignment should match open parenthesis\n" . $hereprev) &&
2980 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002981 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002982 s/^\+[ \t]*/\+$goodtabindent/;
2983 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002984 }
2985 }
2986 }
2987
Joe Perches6ab3a972015-04-16 12:44:05 -07002988# check for space after cast like "(int) foo" or "(struct foo) bar"
2989# avoid checking a few false positives:
2990# "sizeof(<type>)" or "__alignof__(<type>)"
2991# function pointer declarations like "(*foo)(int) = bar;"
2992# structure definitions like "(struct foo) { 0 };"
2993# multiline macros that define functions
2994# known attributes or the __attribute__ keyword
2995 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2996 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002997 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002998 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002999 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003000 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003001 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003002 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003003 }
3004
Joe Perches86406b12015-09-09 15:37:41 -07003005# Block comment styles
3006# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003007 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003008 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003009 $rawline =~ /^\+[ \t]*\*/ &&
3010 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003011 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3012 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3013 }
3014
Joe Perches86406b12015-09-09 15:37:41 -07003015# Block comments use * on subsequent lines
3016 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3017 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003018 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003019 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003020 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003021 WARN("BLOCK_COMMENT_STYLE",
3022 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003023 }
3024
Joe Perches86406b12015-09-09 15:37:41 -07003025# Block comments use */ on trailing lines
3026 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003027 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3028 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3029 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003030 WARN("BLOCK_COMMENT_STYLE",
3031 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003032 }
3033
Joe Perches08eb9b82016-10-11 13:51:50 -07003034# Block comment * alignment
3035 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003036 $line =~ /^\+[ \t]*$;/ && #leading comment
3037 $rawline =~ /^\+[ \t]*\*/ && #leading *
3038 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003039 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003040 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3041 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003042 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003043 if (defined($1)) {
3044 $oldindent = expand_tabs($1);
3045 } else {
3046 $prevrawline =~ m@^\+(.*/?)\*@;
3047 $oldindent = expand_tabs($1);
3048 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003049 $rawline =~ m@^\+([ \t]*)\*@;
3050 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003051 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003052 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003053 WARN("BLOCK_COMMENT_STYLE",
3054 "Block comments should align the * on each line\n" . $hereprev);
3055 }
3056 }
3057
Joe Perches7f619192014-08-06 16:10:39 -07003058# check for missing blank lines after struct/union declarations
3059# with exceptions for various attributes and macros
3060 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3061 $line =~ /^\+/ &&
3062 !($line =~ /^\+\s*$/ ||
3063 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3064 $line =~ /^\+\s*MODULE_/i ||
3065 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3066 $line =~ /^\+[a-z_]*init/ ||
3067 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3068 $line =~ /^\+\s*DECLARE/ ||
3069 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003070 if (CHK("LINE_SPACING",
3071 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3072 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003073 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003074 }
Joe Perches7f619192014-08-06 16:10:39 -07003075 }
3076
Joe Perches365dd4e2014-08-06 16:10:42 -07003077# check for multiple consecutive blank lines
3078 if ($prevline =~ /^[\+ ]\s*$/ &&
3079 $line =~ /^\+\s*$/ &&
3080 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003081 if (CHK("LINE_SPACING",
3082 "Please don't use multiple blank lines\n" . $hereprev) &&
3083 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003084 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003085 }
3086
Joe Perches365dd4e2014-08-06 16:10:42 -07003087 $last_blank_line = $linenr;
3088 }
3089
Joe Perches3b617e32014-04-03 14:49:28 -07003090# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003091 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3092 # actual declarations
3093 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003094 # function pointer declarations
3095 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003096 # foo bar; where foo is some local typedef or #define
3097 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3098 # known declaration macros
3099 $prevline =~ /^\+\s+$declaration_macros/) &&
3100 # for "else if" which can look like "$Ident $Ident"
3101 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3102 # other possible extensions of declaration lines
3103 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3104 # not starting a section or a macro "\" extended line
3105 $prevline =~ /(?:\{\s*|\\)$/) &&
3106 # looks like a declaration
3107 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003108 # function pointer declarations
3109 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003110 # foo bar; where foo is some local typedef or #define
3111 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3112 # known declaration macros
3113 $sline =~ /^\+\s+$declaration_macros/ ||
3114 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003115 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003116 # start or end of block or continuation of declaration
3117 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3118 # bitfield continuation
3119 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3120 # other possible extensions of declaration lines
3121 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3122 # indentation of previous and current line are the same
3123 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003124 if (WARN("LINE_SPACING",
3125 "Missing a blank line after declarations\n" . $hereprev) &&
3126 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003127 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003128 }
Joe Perches3b617e32014-04-03 14:49:28 -07003129 }
3130
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003131# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003132# Exceptions:
3133# 1) within comments
3134# 2) indented preprocessor commands
3135# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003136 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003137 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003138 if (WARN("LEADING_SPACE",
3139 "please, no spaces at the start of a line\n" . $herevet) &&
3140 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003141 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003142 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003143 }
3144
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003145# check we are in a valid C source file if not then ignore this hunk
3146 next if ($realfile !~ /\.(h|c)$/);
3147
Joe Perches4dbed762017-05-08 15:55:39 -07003148# check if this appears to be the start function declaration, save the name
3149 if ($sline =~ /^\+\{\s*$/ &&
3150 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3151 $context_function = $1;
3152 }
3153
3154# check if this appears to be the end of function declaration
3155 if ($sline =~ /^\+\}\s*$/) {
3156 undef $context_function;
3157 }
3158
Joe Perches032a4c02014-08-06 16:10:29 -07003159# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003160# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003161# if the previous line is a break or return and is indented 1 tab more...
3162 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3163 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003164 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3165 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3166 defined $lines[$linenr] &&
3167 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003168 WARN("UNNECESSARY_ELSE",
3169 "else is not generally useful after a break or return\n" . $hereprev);
3170 }
3171 }
3172
Joe Perchesc00df192014-08-06 16:11:01 -07003173# check indentation of a line with a break;
3174# if the previous line is a goto or return and is indented the same # of tabs
3175 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3176 my $tabs = $1;
3177 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3178 WARN("UNNECESSARY_BREAK",
3179 "break is not useful after a goto or return\n" . $hereprev);
3180 }
3181 }
3182
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003183# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003184 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003185 WARN("CVS_KEYWORD",
3186 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003187 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003188
Mike Frysinger42e41c52009-09-21 17:04:40 -07003189# Blackfin: don't use __builtin_bfin_[cs]sync
3190 if ($line =~ /__builtin_bfin_csync/) {
3191 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003192 ERROR("CSYNC",
3193 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003194 }
3195 if ($line =~ /__builtin_bfin_ssync/) {
3196 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003197 ERROR("SSYNC",
3198 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003199 }
3200
Joe Perches56e77d72013-02-21 16:44:14 -08003201# check for old HOTPLUG __dev<foo> section markings
3202 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3203 WARN("HOTPLUG_SECTION",
3204 "Using $1 is unnecessary\n" . $herecurr);
3205 }
3206
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003207# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003208 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3209 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003210#print "LINE<$line>\n";
3211 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003212 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003213 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003214 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003215 $stat =~ s/\n./\n /g;
3216 $cond =~ s/\n./\n /g;
3217
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003218#print "linenr<$linenr> <$stat>\n";
3219 # If this statement has no statement boundaries within
3220 # it there is no point in retrying a statement scan
3221 # until we hit end of it.
3222 my $frag = $stat; $frag =~ s/;+\s*$//;
3223 if ($frag !~ /(?:{|;)/) {
3224#print "skip<$line_nr_next>\n";
3225 $suppress_statement = $line_nr_next;
3226 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003227
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003228 # Find the real next line.
3229 $realline_next = $line_nr_next;
3230 if (defined $realline_next &&
3231 (!defined $lines[$realline_next - 1] ||
3232 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3233 $realline_next++;
3234 }
3235
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003236 my $s = $stat;
3237 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003238
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003239 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003240 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003241
3242 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003243 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003244
Andy Whitcroft463f2862009-09-21 17:04:34 -07003245 } elsif ($s =~ /^.\s*else\b/s) {
3246
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003247 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003248 } 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 -07003249 my $type = $1;
3250 $type =~ s/\s+/ /g;
3251 possible($type, "A:" . $s);
3252
Andy Whitcroft8905a672007-11-28 16:21:06 -08003253 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003254 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003255 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003256 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003257
3258 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003259 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003260 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003261 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003262
3263 # Check for any sort of function declaration.
3264 # int foo(something bar, other baz);
3265 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003266 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 -08003267 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003268
Andy Whitcroftcf655042008-03-04 14:28:20 -08003269 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003270 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003271 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003272
Andy Whitcroft8905a672007-11-28 16:21:06 -08003273 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003274 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003275
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003276 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003277 }
3278 }
3279 }
3280
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003281 }
3282
Andy Whitcroft653d4872007-06-23 17:16:34 -07003283#
3284# Checks which may be anchored in the context.
3285#
3286
3287# Check for switch () and associated case and default
3288# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003289 if ($line=~/\bswitch\s*\(.*\)/) {
3290 my $err = '';
3291 my $sep = '';
3292 my @ctx = ctx_block_outer($linenr, $realcnt);
3293 shift(@ctx);
3294 for my $ctx (@ctx) {
3295 my ($clen, $cindent) = line_stats($ctx);
3296 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3297 $indent != $cindent) {
3298 $err .= "$sep$ctx\n";
3299 $sep = '';
3300 } else {
3301 $sep = "[...]\n";
3302 }
3303 }
3304 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003305 ERROR("SWITCH_CASE_INDENT_LEVEL",
3306 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003307 }
3308 }
3309
3310# if/while/etc brace do not go on next line, unless defining a do while loop,
3311# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003312 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 -07003313 my $pre_ctx = "$1$2";
3314
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003315 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003316
3317 if ($line =~ /^\+\t{6,}/) {
3318 WARN("DEEP_INDENTATION",
3319 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3320 }
3321
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003322 my $ctx_cnt = $realcnt - $#ctx - 1;
3323 my $ctx = join("\n", @ctx);
3324
Andy Whitcroft548596d2008-07-23 21:29:01 -07003325 my $ctx_ln = $linenr;
3326 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003327
Andy Whitcroft548596d2008-07-23 21:29:01 -07003328 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3329 defined $lines[$ctx_ln - 1] &&
3330 $lines[$ctx_ln - 1] =~ /^-/)) {
3331 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3332 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003333 $ctx_ln++;
3334 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003335
Andy Whitcroft53210162008-07-23 21:29:03 -07003336 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3337 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003338
Joe Perchesd752fcc2014-08-06 16:11:05 -07003339 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003340 ERROR("OPEN_BRACE",
3341 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003342 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003343 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003344 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3345 $ctx =~ /\)\s*\;\s*$/ &&
3346 defined $lines[$ctx_ln - 1])
3347 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003348 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3349 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003350 WARN("TRAILING_SEMICOLON",
3351 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003352 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003353 }
3354 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003355 }
3356
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003357# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003358 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003359 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3360 ctx_statement_block($linenr, $realcnt, 0)
3361 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003362 my ($s, $c) = ($stat, $cond);
3363
3364 substr($s, 0, length($c), '');
3365
Joe Perches9f5af482015-09-09 15:37:30 -07003366 # remove inline comments
3367 $s =~ s/$;/ /g;
3368 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003369
3370 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003371 my @newlines = ($c =~ /\n/gs);
3372 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003373
Joe Perches9f5af482015-09-09 15:37:30 -07003374 # Make sure we remove the line prefixes as we have
3375 # none on the first line, and are going to readd them
3376 # where necessary.
3377 $s =~ s/\n./\n/gs;
3378 while ($s =~ /\n\s+\\\n/) {
3379 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3380 }
3381
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003382 # We want to check the first line inside the block
3383 # starting at the end of the conditional, so remove:
3384 # 1) any blank line termination
3385 # 2) any opening brace { on end of the line
3386 # 3) any do (...) {
3387 my $continuation = 0;
3388 my $check = 0;
3389 $s =~ s/^.*\bdo\b//;
3390 $s =~ s/^\s*{//;
3391 if ($s =~ s/^\s*\\//) {
3392 $continuation = 1;
3393 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003394 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003395 $check = 1;
3396 $cond_lines++;
3397 }
3398
3399 # Also ignore a loop construct at the end of a
3400 # preprocessor statement.
3401 if (($prevline =~ /^.\s*#\s*define\s/ ||
3402 $prevline =~ /\\\s*$/) && $continuation == 0) {
3403 $check = 0;
3404 }
3405
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003406 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003407 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003408 while ($cond_ptr != $cond_lines) {
3409 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003410
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003411 # If we see an #else/#elif then the code
3412 # is not linear.
3413 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3414 $check = 0;
3415 }
3416
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003417 # Ignore:
3418 # 1) blank lines, they should be at 0,
3419 # 2) preprocessor lines, and
3420 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003421 if ($continuation ||
3422 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003423 $s =~ /^\s*#\s*?/ ||
3424 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003425 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003426 if ($s =~ s/^.*?\n//) {
3427 $cond_lines++;
3428 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003429 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003430 }
3431
3432 my (undef, $sindent) = line_stats("+" . $s);
3433 my $stat_real = raw_line($linenr, $cond_lines);
3434
3435 # Check if either of these lines are modified, else
3436 # this is not this patch's fault.
3437 if (!defined($stat_real) ||
3438 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3439 $check = 0;
3440 }
3441 if (defined($stat_real) && $cond_lines > 1) {
3442 $stat_real = "[...]\n$stat_real";
3443 }
3444
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003445 #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 -07003446
Joe Perches9f5af482015-09-09 15:37:30 -07003447 if ($check && $s ne '' &&
3448 (($sindent % 8) != 0 ||
3449 ($sindent < $indent) ||
3450 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003451 WARN("SUSPECT_CODE_INDENT",
3452 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003453 }
3454 }
3455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003456 # Track the 'values' across context and added lines.
3457 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003458 my ($curr_values, $curr_vars) =
3459 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003460 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003461 if ($dbg_values) {
3462 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003463 print "$linenr > .$outline\n";
3464 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003465 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003466 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003467 $prev_values = substr($curr_values, -1);
3468
Andy Whitcroft00df3442007-06-08 13:47:06 -07003469#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003470 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003471
Joe Perches11ca40a2016-12-12 16:46:31 -08003472# check for dereferences that span multiple lines
3473 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3474 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3475 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3476 my $ref = $1;
3477 $line =~ /^.\s*($Lval)/;
3478 $ref .= $1;
3479 $ref =~ s/\s//g;
3480 WARN("MULTILINE_DEREFERENCE",
3481 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3482 }
3483
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003484# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003485 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 -07003486 my $type = $1;
3487 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003488 $var = "" if (!defined $var);
3489 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003490 my $sign = $1;
3491 my $pointer = $2;
3492
3493 $pointer = "" if (!defined $pointer);
3494
3495 if (WARN("UNSPECIFIED_INT",
3496 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3497 $fix) {
3498 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003499 my $comp_pointer = $pointer;
3500 $comp_pointer =~ s/\s//g;
3501 $decl .= $comp_pointer;
3502 $decl = rtrim($decl) if ($var eq "");
3503 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003504 }
3505 }
3506 }
3507
Andy Whitcroft653d4872007-06-23 17:16:34 -07003508# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003509 if ($dbg_type) {
3510 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003511 ERROR("TEST_TYPE",
3512 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003513 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003514 ERROR("TEST_NOT_TYPE",
3515 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003516 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003517 next;
3518 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003519# TEST: allow direct testing of the attribute matcher.
3520 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003521 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003522 ERROR("TEST_ATTR",
3523 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003524 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003525 ERROR("TEST_NOT_ATTR",
3526 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003527 }
3528 next;
3529 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003530
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003531# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003532 if ($line =~ /^.\s*{/ &&
3533 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003534 if (ERROR("OPEN_BRACE",
3535 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003536 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3537 fix_delete_line($fixlinenr - 1, $prevrawline);
3538 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003539 my $fixedline = $prevrawline;
3540 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003541 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003542 $fixedline = $line;
3543 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003544 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003545 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003546 }
3547
Andy Whitcroft653d4872007-06-23 17:16:34 -07003548#
3549# Checks which are anchored on the added line.
3550#
3551
3552# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003553 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003554 my $path = $1;
3555 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003556 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003557 "malformed #include filename\n" . $herecurr);
3558 }
3559 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3560 ERROR("UAPI_INCLUDE",
3561 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003562 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003563 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003564
3565# no C99 // comments
3566 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003567 if (ERROR("C99_COMMENTS",
3568 "do not use C99 // comments\n" . $herecurr) &&
3569 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003570 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003571 if ($line =~ /\/\/(.*)$/) {
3572 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003573 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003574 }
3575 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003576 }
3577 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003578 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003579 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003580
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003581# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3582# the whole statement.
3583#print "APW <$lines[$realline_next - 1]>\n";
3584 if (defined $realline_next &&
3585 exists $lines[$realline_next - 1] &&
3586 !defined $suppress_export{$realline_next} &&
3587 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3588 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003589 # Handle definitions which produce identifiers with
3590 # a prefix:
3591 # XXX(foo);
3592 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003593 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003594 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003595 $name =~ /^${Ident}_$2/) {
3596#print "FOO C name<$name>\n";
3597 $suppress_export{$realline_next} = 1;
3598
3599 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003600 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003601 ^.DEFINE_$Ident\(\Q$name\E\)|
3602 ^.DECLARE_$Ident\(\Q$name\E\)|
3603 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003604 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3605 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003606 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003607#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3608 $suppress_export{$realline_next} = 2;
3609 } else {
3610 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003611 }
3612 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003613 if (!defined $suppress_export{$linenr} &&
3614 $prevline =~ /^.\s*$/ &&
3615 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3616 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3617#print "FOO B <$lines[$linenr - 1]>\n";
3618 $suppress_export{$linenr} = 2;
3619 }
3620 if (defined $suppress_export{$linenr} &&
3621 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003622 WARN("EXPORT_SYMBOL",
3623 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003624 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003625
Joe Eloff5150bda2010-08-09 17:21:00 -07003626# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003627 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003628 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003629 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003630 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003631 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003632 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003633 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003634# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003635 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003636 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003637 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003638 $herecurr) &&
3639 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003640 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003641 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003642 }
3643
Joe Perches18130872014-08-06 16:11:22 -07003644# check for misordered declarations of char/short/int/long with signed/unsigned
3645 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3646 my $tmp = trim($1);
3647 WARN("MISORDERED_TYPE",
3648 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3649 }
3650
Joe Perchescb710ec2010-10-26 14:23:20 -07003651# check for static const char * arrays.
3652 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003653 WARN("STATIC_CONST_CHAR_ARRAY",
3654 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003655 $herecurr);
3656 }
3657
3658# check for static char foo[] = "bar" declarations.
3659 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003660 WARN("STATIC_CONST_CHAR_ARRAY",
3661 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003662 $herecurr);
3663 }
3664
Joe Perchesab7e23f2015-04-16 12:44:22 -07003665# check for const <foo> const where <foo> is not a pointer or array type
3666 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3667 my $found = $1;
3668 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3669 WARN("CONST_CONST",
3670 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3671 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3672 WARN("CONST_CONST",
3673 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3674 }
3675 }
3676
Joe Perches9b0fa602014-04-03 14:49:18 -07003677# check for non-global char *foo[] = {"bar", ...} declarations.
3678 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3679 WARN("STATIC_CONST_CHAR_ARRAY",
3680 "char * array declaration might be better as static const\n" .
3681 $herecurr);
3682 }
3683
Joe Perchesb598b672015-04-16 12:44:36 -07003684# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3685 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3686 my $array = $1;
3687 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3688 my $array_div = $1;
3689 if (WARN("ARRAY_SIZE",
3690 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3691 $fix) {
3692 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3693 }
3694 }
3695 }
3696
Joe Perchesb36190c2014-01-27 17:07:18 -08003697# check for function declarations without arguments like "int foo()"
3698 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3699 if (ERROR("FUNCTION_WITHOUT_ARGS",
3700 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3701 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003702 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003703 }
3704 }
3705
Andy Whitcroft653d4872007-06-23 17:16:34 -07003706# check for new typedefs, only function parameters and sparse annotations
3707# make sense.
3708 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003709 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003710 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003711 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003712 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003713 WARN("NEW_TYPEDEFS",
3714 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003715 }
3716
3717# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003718 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003719 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3720 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003721 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003722
Andy Whitcroft65863862009-01-06 14:41:21 -08003723 # Should start with a space.
3724 $to =~ s/^(\S)/ $1/;
3725 # Should not end with a space.
3726 $to =~ s/\s+$//;
3727 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003728 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003729 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003730
Joe Perches3705ce52013-07-03 15:05:31 -07003731## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003732 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003733 if (ERROR("POINTER_LOCATION",
3734 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3735 $fix) {
3736 my $sub_from = $ident;
3737 my $sub_to = $ident;
3738 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003739 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003740 s@\Q$sub_from\E@$sub_to@;
3741 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003742 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003743 }
3744 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3745 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003746 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003747
Andy Whitcroft65863862009-01-06 14:41:21 -08003748 # Should start with a space.
3749 $to =~ s/^(\S)/ $1/;
3750 # Should not end with a space.
3751 $to =~ s/\s+$//;
3752 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003753 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003754 }
3755 # Modifiers should have spaces.
3756 $to =~ s/(\b$Modifier$)/$1 /;
3757
Joe Perches3705ce52013-07-03 15:05:31 -07003758## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003759 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003760 if (ERROR("POINTER_LOCATION",
3761 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3762 $fix) {
3763
3764 my $sub_from = $match;
3765 my $sub_to = $match;
3766 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003767 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003768 s@\Q$sub_from\E@$sub_to@;
3769 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003770 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003771 }
3772
Joe Perches9d3e3c72015-09-09 15:37:27 -07003773# avoid BUG() or BUG_ON()
3774 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3775 my $msg_type = \&WARN;
3776 $msg_type = \&CHK if ($file);
3777 &{$msg_type}("AVOID_BUG",
3778 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3779 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003780
Joe Perches9d3e3c72015-09-09 15:37:27 -07003781# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003782 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003783 WARN("LINUX_VERSION_CODE",
3784 "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 -08003785 }
3786
Joe Perches17441222011-06-15 15:08:17 -07003787# check for uses of printk_ratelimit
3788 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003789 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003790 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003791 }
3792
Andy Whitcroft00df3442007-06-08 13:47:06 -07003793# printk should use KERN_* levels. Note that follow on printk's on the
3794# same line do not need a level, so we use the current block context
3795# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003796# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003797# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003798 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003799 my $ok = 0;
3800 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3801 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003802 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003803 # with "\n" ignore it, else it is to blame
3804 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3805 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3806 $ok = 1;
3807 }
3808 last;
3809 }
3810 }
3811 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003812 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3813 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003814 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003815 }
3816
Joe Perches243f3802012-05-31 16:26:09 -07003817 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3818 my $orig = $1;
3819 my $level = lc($orig);
3820 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003821 my $level2 = $level;
3822 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003823 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003824 "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 -07003825 }
3826
3827 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003828 if (WARN("PREFER_PR_LEVEL",
3829 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3830 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003831 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003832 s/\bpr_warning\b/pr_warn/;
3833 }
Joe Perches243f3802012-05-31 16:26:09 -07003834 }
3835
Joe Perchesdc139312013-02-21 16:44:13 -08003836 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3837 my $orig = $1;
3838 my $level = lc($orig);
3839 $level = "warn" if ($level eq "warning");
3840 $level = "dbg" if ($level eq "debug");
3841 WARN("PREFER_DEV_LEVEL",
3842 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3843 }
3844
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003845# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3846# number of false positives, but assembly files are not checked, so at
3847# least the arch entry code will not trigger this warning.
3848 if ($line =~ /\bENOSYS\b/) {
3849 WARN("ENOSYS",
3850 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3851 }
3852
Andy Whitcroft653d4872007-06-23 17:16:34 -07003853# function brace can't be on same line, except for #defines of do while,
3854# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003855 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003856 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003857 if (ERROR("OPEN_BRACE",
3858 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3859 $fix) {
3860 fix_delete_line($fixlinenr, $rawline);
3861 my $fixed_line = $rawline;
3862 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3863 my $line1 = $1;
3864 my $line2 = $2;
3865 fix_insert_line($fixlinenr, ltrim($line1));
3866 fix_insert_line($fixlinenr, "\+{");
3867 if ($line2 !~ /^\s*$/) {
3868 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3869 }
3870 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003871 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003872
Andy Whitcroft8905a672007-11-28 16:21:06 -08003873# open braces for enum, union and struct go on the same line.
3874 if ($line =~ /^.\s*{/ &&
3875 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003876 if (ERROR("OPEN_BRACE",
3877 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3878 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3879 fix_delete_line($fixlinenr - 1, $prevrawline);
3880 fix_delete_line($fixlinenr, $rawline);
3881 my $fixedline = rtrim($prevrawline) . " {";
3882 fix_insert_line($fixlinenr, $fixedline);
3883 $fixedline = $rawline;
3884 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3885 if ($fixedline !~ /^\+\s*$/) {
3886 fix_insert_line($fixlinenr, $fixedline);
3887 }
3888 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003889 }
3890
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003891# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003892 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3893 if (WARN("SPACING",
3894 "missing space after $1 definition\n" . $herecurr) &&
3895 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003896 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003897 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3898 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003899 }
3900
Joe Perches31070b52014-01-23 15:54:49 -08003901# Function pointer declarations
3902# check spacing between type, funcptr, and args
3903# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003904 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003905 my $declare = $1;
3906 my $pre_pointer_space = $2;
3907 my $post_pointer_space = $3;
3908 my $funcname = $4;
3909 my $post_funcname_space = $5;
3910 my $pre_args_space = $6;
3911
Joe Perches91f72e92014-04-03 14:49:12 -07003912# the $Declare variable will capture all spaces after the type
3913# so check it for a missing trailing missing space but pointer return types
3914# don't need a space so don't warn for those.
3915 my $post_declare_space = "";
3916 if ($declare =~ /(\s+)$/) {
3917 $post_declare_space = $1;
3918 $declare = rtrim($declare);
3919 }
3920 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003921 WARN("SPACING",
3922 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003923 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003924 }
3925
3926# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003927# This test is not currently implemented because these declarations are
3928# equivalent to
3929# int foo(int bar, ...)
3930# and this is form shouldn't/doesn't generate a checkpatch warning.
3931#
3932# elsif ($declare =~ /\s{2,}$/) {
3933# WARN("SPACING",
3934# "Multiple spaces after return type\n" . $herecurr);
3935# }
Joe Perches31070b52014-01-23 15:54:49 -08003936
3937# unnecessary space "type ( *funcptr)(args...)"
3938 if (defined $pre_pointer_space &&
3939 $pre_pointer_space =~ /^\s/) {
3940 WARN("SPACING",
3941 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3942 }
3943
3944# unnecessary space "type (* funcptr)(args...)"
3945 if (defined $post_pointer_space &&
3946 $post_pointer_space =~ /^\s/) {
3947 WARN("SPACING",
3948 "Unnecessary space before function pointer name\n" . $herecurr);
3949 }
3950
3951# unnecessary space "type (*funcptr )(args...)"
3952 if (defined $post_funcname_space &&
3953 $post_funcname_space =~ /^\s/) {
3954 WARN("SPACING",
3955 "Unnecessary space after function pointer name\n" . $herecurr);
3956 }
3957
3958# unnecessary space "type (*funcptr) (args...)"
3959 if (defined $pre_args_space &&
3960 $pre_args_space =~ /^\s/) {
3961 WARN("SPACING",
3962 "Unnecessary space before function pointer arguments\n" . $herecurr);
3963 }
3964
3965 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003966 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003967 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003968 }
3969 }
3970
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003971# check for spacing round square brackets; allowed:
3972# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003973# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3974# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003975 while ($line =~ /(.*?\s)\[/g) {
3976 my ($where, $prefix) = ($-[1], $1);
3977 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003978 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003979 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003980 if (ERROR("BRACKET_SPACE",
3981 "space prohibited before open square bracket '['\n" . $herecurr) &&
3982 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003983 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003984 s/^(\+.*?)\s+\[/$1\[/;
3985 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003986 }
3987 }
3988
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003989# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003990 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003991 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003992 my $ctx_before = substr($line, 0, $-[1]);
3993 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003994
3995 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003996 if ($name =~ /^(?:
3997 if|for|while|switch|return|case|
3998 volatile|__volatile__|
3999 __attribute__|format|__extension__|
4000 asm|__asm__)$/x)
4001 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004002 # cpp #define statements have non-optional spaces, ie
4003 # if there is a space between the name and the open
4004 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004005 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004006
4007 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004008 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004009
4010 # If this whole things ends with a type its most
4011 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004012 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004013
4014 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004015 if (WARN("SPACING",
4016 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4017 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004018 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004019 s/\b$name\s+\(/$name\(/;
4020 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004021 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004022 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004023
Andy Whitcroft653d4872007-06-23 17:16:34 -07004024# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004025 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004026 my $fixed_line = "";
4027 my $line_fixed = 0;
4028
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004029 my $ops = qr{
4030 <<=|>>=|<=|>=|==|!=|
4031 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4032 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004033 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004034 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004035 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004036 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004037
4038## print("element count: <" . $#elements . ">\n");
4039## foreach my $el (@elements) {
4040## print("el: <$el>\n");
4041## }
4042
4043 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004044 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004045
Joe Perches3705ce52013-07-03 15:05:31 -07004046 foreach my $el (@elements) {
4047 push(@fix_elements, substr($rawline, $off, length($el)));
4048 $off += length($el);
4049 }
4050
4051 $off = 0;
4052
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004053 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004054 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004055
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004056 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004057
4058 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4059
4060## print("n: <$n> good: <$good>\n");
4061
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004062 $off += length($elements[$n]);
4063
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004064 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004065 my $ca = substr($opline, 0, $off);
4066 my $cc = '';
4067 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4068 $cc = substr($opline, $off + length($elements[$n + 1]));
4069 }
4070 my $cb = "$ca$;$cc";
4071
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004072 my $a = '';
4073 $a = 'V' if ($elements[$n] ne '');
4074 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004075 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004076 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4077 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004078 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004079
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004080 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004081
4082 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004083 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004084 $c = 'V' if ($elements[$n + 2] ne '');
4085 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004086 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004087 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4088 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004089 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004090 } else {
4091 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004092 }
4093
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004094 my $ctx = "${a}x${c}";
4095
4096 my $at = "(ctx:$ctx)";
4097
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004098 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004099 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004100
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004101 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004102 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004103
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004104 # Get the full operator variant.
4105 my $opv = $op . substr($curr_vars, $off, 1);
4106
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004107 # Ignore operators passed as parameters.
4108 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004109 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004110
Andy Whitcroftcf655042008-03-04 14:28:20 -08004111# # Ignore comments
4112# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004113
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004114 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004115 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004116 if ($ctx !~ /.x[WEBC]/ &&
4117 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004118 if (ERROR("SPACING",
4119 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004120 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004121 $line_fixed = 1;
4122 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004123 }
4124
4125 # // is a comment
4126 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004127
Joe Perchesb00e4812014-04-03 14:49:33 -07004128 # : when part of a bitfield
4129 } elsif ($opv eq ':B') {
4130 # skip the bitfield test for now
4131
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004132 # No spaces for:
4133 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004134 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004135 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004136 if (ERROR("SPACING",
4137 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004138 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004139 if (defined $fix_elements[$n + 2]) {
4140 $fix_elements[$n + 2] =~ s/^\s+//;
4141 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004142 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004143 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004144 }
4145
Joe Perches23810972014-12-10 15:51:32 -08004146 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004147 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004148 my $rtrim_before = 0;
4149 my $space_after = 0;
4150 if ($ctx =~ /Wx./) {
4151 if (ERROR("SPACING",
4152 "space prohibited before that '$op' $at\n" . $hereptr)) {
4153 $line_fixed = 1;
4154 $rtrim_before = 1;
4155 }
4156 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004157 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004158 if (ERROR("SPACING",
4159 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004160 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004161 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004162 $space_after = 1;
4163 }
4164 }
4165 if ($rtrim_before || $space_after) {
4166 if ($rtrim_before) {
4167 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4168 } else {
4169 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4170 }
4171 if ($space_after) {
4172 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004173 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004174 }
4175
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004176 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004177 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004178 #warn "'*' is part of type\n";
4179
4180 # unary operators should have a space before and
4181 # none after. May be left adjacent to another
4182 # unary operator, or a cast
4183 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004184 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004185 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004186 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004187 if (ERROR("SPACING",
4188 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004189 if ($n != $last_after + 2) {
4190 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4191 $line_fixed = 1;
4192 }
Joe Perches3705ce52013-07-03 15:05:31 -07004193 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004194 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004195 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004196 # A unary '*' may be const
4197
4198 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004199 if (ERROR("SPACING",
4200 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004201 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004202 if (defined $fix_elements[$n + 2]) {
4203 $fix_elements[$n + 2] =~ s/^\s+//;
4204 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004205 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004206 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004207 }
4208
4209 # unary ++ and unary -- are allowed no space on one side.
4210 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004211 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004212 if (ERROR("SPACING",
4213 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004214 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004215 $line_fixed = 1;
4216 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004217 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004218 if ($ctx =~ /Wx[BE]/ ||
4219 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004220 if (ERROR("SPACING",
4221 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004222 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004223 $line_fixed = 1;
4224 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004225 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004226 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004227 if (ERROR("SPACING",
4228 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004229 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004230 if (defined $fix_elements[$n + 2]) {
4231 $fix_elements[$n + 2] =~ s/^\s+//;
4232 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004233 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004234 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004235 }
4236
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004237 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004238 } elsif ($op eq '<<' or $op eq '>>' or
4239 $op eq '&' or $op eq '^' or $op eq '|' or
4240 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004241 $op eq '*' or $op eq '/' or
4242 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004243 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004244 if ($check) {
4245 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4246 if (CHK("SPACING",
4247 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4248 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4249 $fix_elements[$n + 2] =~ s/^\s+//;
4250 $line_fixed = 1;
4251 }
4252 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4253 if (CHK("SPACING",
4254 "space preferred before that '$op' $at\n" . $hereptr)) {
4255 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4256 $line_fixed = 1;
4257 }
4258 }
4259 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004260 if (ERROR("SPACING",
4261 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004262 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4263 if (defined $fix_elements[$n + 2]) {
4264 $fix_elements[$n + 2] =~ s/^\s+//;
4265 }
Joe Perches3705ce52013-07-03 15:05:31 -07004266 $line_fixed = 1;
4267 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004268 }
4269
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004270 # A colon needs no spaces before when it is
4271 # terminating a case value or a label.
4272 } elsif ($opv eq ':C' || $opv eq ':L') {
4273 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004274 if (ERROR("SPACING",
4275 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004276 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004277 $line_fixed = 1;
4278 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004279 }
4280
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004281 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004282 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004283 my $ok = 0;
4284
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004285 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004286 if (($op eq '<' &&
4287 $cc =~ /^\S+\@\S+>/) ||
4288 ($op eq '>' &&
4289 $ca =~ /<\S+\@\S+$/))
4290 {
4291 $ok = 1;
4292 }
4293
Joe Perchese0df7e12015-04-16 12:44:53 -07004294 # for asm volatile statements
4295 # ignore a colon with another
4296 # colon immediately before or after
4297 if (($op eq ':') &&
4298 ($ca =~ /:$/ || $cc =~ /^:/)) {
4299 $ok = 1;
4300 }
4301
Joe Perches84731622013-11-12 15:10:05 -08004302 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004303 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004304 my $msg_type = \&ERROR;
4305 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4306
4307 if (&{$msg_type}("SPACING",
4308 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004309 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4310 if (defined $fix_elements[$n + 2]) {
4311 $fix_elements[$n + 2] =~ s/^\s+//;
4312 }
Joe Perches3705ce52013-07-03 15:05:31 -07004313 $line_fixed = 1;
4314 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004315 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004316 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004317 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004318
4319## print("n: <$n> GOOD: <$good>\n");
4320
4321 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004322 }
Joe Perches3705ce52013-07-03 15:05:31 -07004323
4324 if (($#elements % 2) == 0) {
4325 $fixed_line = $fixed_line . $fix_elements[$#elements];
4326 }
4327
Joe Perches194f66f2014-08-06 16:11:03 -07004328 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4329 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004330 }
4331
4332
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004333 }
4334
Joe Perches786b6322013-07-03 15:05:32 -07004335# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004336 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004337 if (WARN("SPACING",
4338 "space prohibited before semicolon\n" . $herecurr) &&
4339 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004340 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004341 s/^(\+.*\S)\s+;/$1;/;
4342 }
4343 }
4344
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004345# check for multiple assignments
4346 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004347 CHK("MULTIPLE_ASSIGNMENTS",
4348 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004349 }
4350
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004351## # check for multiple declarations, allowing for a function declaration
4352## # continuation.
4353## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4354## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4355##
4356## # Remove any bracketed sections to ensure we do not
4357## # falsly report the parameters of functions.
4358## my $ln = $line;
4359## while ($ln =~ s/\([^\(\)]*\)//g) {
4360## }
4361## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004362## WARN("MULTIPLE_DECLARATION",
4363## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004364## }
4365## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004366
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004367#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004368 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004369 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004370 if (ERROR("SPACING",
4371 "space required before the open brace '{'\n" . $herecurr) &&
4372 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004373 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004374 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004375 }
4376
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004377## # check for blank lines before declarations
4378## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4379## $prevrawline =~ /^.\s*$/) {
4380## WARN("SPACING",
4381## "No blank lines before declarations\n" . $hereprev);
4382## }
4383##
4384
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004385# closing brace should have a space following it when it has anything
4386# on the line
4387 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004388 if (ERROR("SPACING",
4389 "space required after that close brace '}'\n" . $herecurr) &&
4390 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004391 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004392 s/}((?!(?:,|;|\)))\S)/} $1/;
4393 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004394 }
4395
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004396# check spacing on square brackets
4397 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004398 if (ERROR("SPACING",
4399 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4400 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004401 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004402 s/\[\s+/\[/;
4403 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004404 }
4405 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004406 if (ERROR("SPACING",
4407 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4408 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004409 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004410 s/\s+\]/\]/;
4411 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004412 }
4413
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004414# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004415 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4416 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004417 if (ERROR("SPACING",
4418 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4419 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004420 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004421 s/\(\s+/\(/;
4422 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004423 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004424 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004425 $line !~ /for\s*\(.*;\s+\)/ &&
4426 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004427 if (ERROR("SPACING",
4428 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4429 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004430 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004431 s/\s+\)/\)/;
4432 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004433 }
4434
Joe Perchese2826fd2014-08-06 16:10:48 -07004435# check unnecessary parentheses around addressof/dereference single $Lvals
4436# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4437
4438 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004439 my $var = $1;
4440 if (CHK("UNNECESSARY_PARENTHESES",
4441 "Unnecessary parentheses around $var\n" . $herecurr) &&
4442 $fix) {
4443 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4444 }
4445 }
4446
4447# check for unnecessary parentheses around function pointer uses
4448# ie: (foo->bar)(); should be foo->bar();
4449# but not "if (foo->bar) (" to avoid some false positives
4450 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4451 my $var = $2;
4452 if (CHK("UNNECESSARY_PARENTHESES",
4453 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4454 $fix) {
4455 my $var2 = deparenthesize($var);
4456 $var2 =~ s/\s//g;
4457 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4458 }
4459 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004460
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004461#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004462 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004463 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004464 if (WARN("INDENTED_LABEL",
4465 "labels should not be indented\n" . $herecurr) &&
4466 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004467 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004468 s/^(.)\s+/$1/;
4469 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004470 }
4471
Joe Perches5b9553a2014-04-03 14:49:21 -07004472# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004473 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004474 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004475 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004476 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4477 my $value = $1;
4478 $value = deparenthesize($value);
4479 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4480 ERROR("RETURN_PARENTHESES",
4481 "return is not a function, parentheses are not required\n" . $herecurr);
4482 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004483 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004484 ERROR("SPACING",
4485 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004486 }
4487 }
Joe Perches507e5142013-11-12 15:10:13 -08004488
Joe Perchesb43ae212014-06-23 13:22:07 -07004489# unnecessary return in a void function
4490# at end-of-function, with the previous line a single leading tab, then return;
4491# and the line before that not a goto label target like "out:"
4492 if ($sline =~ /^[ \+]}\s*$/ &&
4493 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4494 $linenr >= 3 &&
4495 $lines[$linenr - 3] =~ /^[ +]/ &&
4496 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004497 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004498 "void function return statements are not generally useful\n" . $hereprev);
4499 }
Joe Perches9819cf22014-06-04 16:12:09 -07004500
Joe Perches189248d2014-01-23 15:54:47 -08004501# if statements using unnecessary parentheses - ie: if ((foo == bar))
4502 if ($^V && $^V ge 5.10.0 &&
4503 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4504 my $openparens = $1;
4505 my $count = $openparens =~ tr@\(@\(@;
4506 my $msg = "";
4507 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4508 my $comp = $4; #Not $1 because of $LvalOrFunc
4509 $msg = " - maybe == should be = ?" if ($comp eq "==");
4510 WARN("UNNECESSARY_PARENTHESES",
4511 "Unnecessary parentheses$msg\n" . $herecurr);
4512 }
4513 }
4514
Joe Perchesc5595fa2015-09-09 15:37:58 -07004515# comparisons with a constant or upper case identifier on the left
4516# avoid cases like "foo + BAR < baz"
4517# only fix matches surrounded by parentheses to avoid incorrect
4518# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4519 if ($^V && $^V ge 5.10.0 &&
4520 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4521 my $lead = $1;
4522 my $const = $2;
4523 my $comp = $3;
4524 my $to = $4;
4525 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004526 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004527 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4528 WARN("CONSTANT_COMPARISON",
4529 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4530 $fix) {
4531 if ($comp eq "<") {
4532 $newcomp = ">";
4533 } elsif ($comp eq "<=") {
4534 $newcomp = ">=";
4535 } elsif ($comp eq ">") {
4536 $newcomp = "<";
4537 } elsif ($comp eq ">=") {
4538 $newcomp = "<=";
4539 }
4540 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4541 }
4542 }
4543
Joe Perchesf34e4a42015-04-16 12:44:19 -07004544# Return of what appears to be an errno should normally be negative
4545 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004546 my $name = $1;
4547 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004548 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004549 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004550 }
4551 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004552
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004553# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004554 if ($line =~ /\b(if|while|for|switch)\(/) {
4555 if (ERROR("SPACING",
4556 "space required before the open parenthesis '('\n" . $herecurr) &&
4557 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004558 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004559 s/\b(if|while|for|switch)\(/$1 \(/;
4560 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004561 }
4562
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004563# Check for illegal assignment in if conditional -- and check for trailing
4564# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004565 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004566 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4567 ctx_statement_block($linenr, $realcnt, 0)
4568 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004569 my ($stat_next) = ctx_statement_block($line_nr_next,
4570 $remain_next, $off_next);
4571 $stat_next =~ s/\n./\n /g;
4572 ##print "stat<$stat> stat_next<$stat_next>\n";
4573
4574 if ($stat_next =~ /^\s*while\b/) {
4575 # If the statement carries leading newlines,
4576 # then count those as offsets.
4577 my ($whitespace) =
4578 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4579 my $offset =
4580 statement_rawlines($whitespace) - 1;
4581
4582 $suppress_whiletrailers{$line_nr_next +
4583 $offset} = 1;
4584 }
4585 }
4586 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004587 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004588 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004589 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004590
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004591 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004592 ERROR("ASSIGN_IN_IF",
4593 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004594 }
4595
4596 # Find out what is on the end of the line after the
4597 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004598 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004599 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004600 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004601 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4602 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004603 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004604 # Find out how long the conditional actually is.
4605 my @newlines = ($c =~ /\n/gs);
4606 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004607 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004608
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004609 $stat_real = raw_line($linenr, $cond_lines)
4610 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004611 if (defined($stat_real) && $cond_lines > 1) {
4612 $stat_real = "[...]\n$stat_real";
4613 }
4614
Joe Perches000d1cc12011-07-25 17:13:25 -07004615 ERROR("TRAILING_STATEMENTS",
4616 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004617 }
4618 }
4619
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004620# Check for bitwise tests written as boolean
4621 if ($line =~ /
4622 (?:
4623 (?:\[|\(|\&\&|\|\|)
4624 \s*0[xX][0-9]+\s*
4625 (?:\&\&|\|\|)
4626 |
4627 (?:\&\&|\|\|)
4628 \s*0[xX][0-9]+\s*
4629 (?:\&\&|\|\||\)|\])
4630 )/x)
4631 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004632 WARN("HEXADECIMAL_BOOLEAN_TEST",
4633 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004634 }
4635
Andy Whitcroft8905a672007-11-28 16:21:06 -08004636# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004637 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4638 my $s = $1;
4639 $s =~ s/$;//g; # Remove any comments
4640 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004641 ERROR("TRAILING_STATEMENTS",
4642 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004643 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004644 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004645# if should not continue a brace
4646 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004647 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004648 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004649 $herecurr);
4650 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004651# case and default should not have general statements after them
4652 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4653 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004654 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004655 \s*return\s+
4656 )/xg)
4657 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004658 ERROR("TRAILING_STATEMENTS",
4659 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004660 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004661
4662 # Check for }<nl>else {, these must be at the same
4663 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004664 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4665 $previndent == $indent) {
4666 if (ERROR("ELSE_AFTER_BRACE",
4667 "else should follow close brace '}'\n" . $hereprev) &&
4668 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4669 fix_delete_line($fixlinenr - 1, $prevrawline);
4670 fix_delete_line($fixlinenr, $rawline);
4671 my $fixedline = $prevrawline;
4672 $fixedline =~ s/}\s*$//;
4673 if ($fixedline !~ /^\+\s*$/) {
4674 fix_insert_line($fixlinenr, $fixedline);
4675 }
4676 $fixedline = $rawline;
4677 $fixedline =~ s/^(.\s*)else/$1} else/;
4678 fix_insert_line($fixlinenr, $fixedline);
4679 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004680 }
4681
Joe Perches8b8856f2014-08-06 16:11:14 -07004682 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4683 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004684 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4685
4686 # Find out what is on the end of the line after the
4687 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004688 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004689 $s =~ s/\n.*//g;
4690
4691 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004692 if (ERROR("WHILE_AFTER_BRACE",
4693 "while should follow close brace '}'\n" . $hereprev) &&
4694 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4695 fix_delete_line($fixlinenr - 1, $prevrawline);
4696 fix_delete_line($fixlinenr, $rawline);
4697 my $fixedline = $prevrawline;
4698 my $trailing = $rawline;
4699 $trailing =~ s/^\+//;
4700 $trailing = trim($trailing);
4701 $fixedline =~ s/}\s*$/} $trailing/;
4702 fix_insert_line($fixlinenr, $fixedline);
4703 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004704 }
4705 }
4706
Joe Perches95e2c602013-07-03 15:05:20 -07004707#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004708 while ($line =~ m{($Constant|$Lval)}g) {
4709 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004710
4711#gcc binary extension
4712 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004713 if (WARN("GCC_BINARY_CONSTANT",
4714 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4715 $fix) {
4716 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004717 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004718 s/\b$var\b/$hexval/;
4719 }
Joe Perches95e2c602013-07-03 15:05:20 -07004720 }
4721
4722#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004723 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004724 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004725#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004726 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004727#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004728 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4729#Ignore some three character SI units explicitly, like MiB and KHz
4730 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004731 while ($var =~ m{($Ident)}g) {
4732 my $word = $1;
4733 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004734 if ($check) {
4735 seed_camelcase_includes();
4736 if (!$file && !$camelcase_file_seeded) {
4737 seed_camelcase_file($realfile);
4738 $camelcase_file_seeded = 1;
4739 }
4740 }
Joe Perches7e781f62013-09-11 14:23:55 -07004741 if (!defined $camelcase{$word}) {
4742 $camelcase{$word} = 1;
4743 CHK("CAMELCASE",
4744 "Avoid CamelCase: <$word>\n" . $herecurr);
4745 }
Joe Perches34456862013-07-03 15:05:34 -07004746 }
Joe Perches323c1262012-12-17 16:02:07 -08004747 }
4748 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004749
4750#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004751 if ($line =~ /\#\s*define.*\\\s+$/) {
4752 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4753 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4754 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004755 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004756 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004757 }
4758
Fabian Frederick0e212e02015-04-16 12:44:25 -07004759# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4760# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004761 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004762 my $file = "$1.h";
4763 my $checkfile = "include/linux/$file";
4764 if (-f "$root/$checkfile" &&
4765 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004766 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004767 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004768 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4769 if ($asminclude > 0) {
4770 if ($realfile =~ m{^arch/}) {
4771 CHK("ARCH_INCLUDE_LINUX",
4772 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4773 } else {
4774 WARN("INCLUDE_LINUX",
4775 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4776 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004777 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004778 }
4779 }
4780
Andy Whitcroft653d4872007-06-23 17:16:34 -07004781# multi-statement macros should be enclosed in a do while loop, grab the
4782# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004783# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004784 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4785 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004786 my $ln = $linenr;
4787 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004788 my ($off, $dstat, $dcond, $rest);
4789 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004790 my $has_flow_statement = 0;
4791 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004792 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004793 ctx_statement_block($linenr, $realcnt, 0);
4794 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004795 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004796 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004797
Joe Perches08a28432014-10-13 15:51:55 -07004798 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004799 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004800
Joe Perchesf59b64b2016-10-11 13:52:08 -07004801 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4802 my $define_args = $1;
4803 my $define_stmt = $dstat;
4804 my @def_args = ();
4805
4806 if (defined $define_args && $define_args ne "") {
4807 $define_args = substr($define_args, 1, length($define_args) - 2);
4808 $define_args =~ s/\s*//g;
4809 @def_args = split(",", $define_args);
4810 }
4811
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004812 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004813 $dstat =~ s/\\\n.//g;
4814 $dstat =~ s/^\s*//s;
4815 $dstat =~ s/\s*$//s;
4816
4817 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004818 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4819 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004820 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004821 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004822 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004823
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004824 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004825 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4826 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004827 {
4828 }
4829
Joe Perches42e15292016-03-15 14:58:01 -07004830 # Make asm volatile uses seem like a generic function
4831 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4832
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004833 my $exceptions = qr{
4834 $Declare|
4835 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004836 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004837 DECLARE_PER_CPU|
4838 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004839 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004840 union|
4841 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004842 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004843 ^\"|\"$|
4844 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004845 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004846 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004847
4848 $ctx =~ s/\n*$//;
4849 my $herectx = $here . "\n";
4850 my $stmt_cnt = statement_rawlines($ctx);
4851
4852 for (my $n = 0; $n < $stmt_cnt; $n++) {
4853 $herectx .= raw_line($linenr, $n) . "\n";
4854 }
4855
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004856 if ($dstat ne '' &&
4857 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4858 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004859 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004860 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004861 $dstat !~ /$exceptions/ &&
4862 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004863 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004864 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004865 $dstat !~ /^for\s*$Constant$/ && # for (...)
4866 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4867 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004868 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004869 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004870 {
Joe Perchese7955562017-05-08 15:55:48 -07004871 if ($dstat =~ /^\s*if\b/) {
4872 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4873 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4874 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004875 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4876 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4877 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004878 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004879 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004880 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07004881
4882 }
Joe Perches52076492016-10-11 13:52:14 -07004883
4884 # Make $define_stmt single line, comment-free, etc
4885 my @stmt_array = split('\n', $define_stmt);
4886 my $first = 1;
4887 $define_stmt = "";
4888 foreach my $l (@stmt_array) {
4889 $l =~ s/\\$//;
4890 if ($first) {
4891 $define_stmt = $l;
4892 $first = 0;
4893 } elsif ($l =~ /^[\+ ]/) {
4894 $define_stmt .= substr($l, 1);
4895 }
4896 }
4897 $define_stmt =~ s/$;//g;
4898 $define_stmt =~ s/\s+/ /g;
4899 $define_stmt = trim($define_stmt);
4900
Joe Perchesf59b64b2016-10-11 13:52:08 -07004901# check if any macro arguments are reused (ignore '...' and 'type')
4902 foreach my $arg (@def_args) {
4903 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07004904 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07004905 my $tmp = $define_stmt;
4906 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches52076492016-10-11 13:52:14 -07004907 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004908 $tmp =~ s/\b$arg\s*\#\#//g;
4909 my $use_cnt = $tmp =~ s/\b$arg\b//g;
4910 if ($use_cnt > 1) {
4911 CHK("MACRO_ARG_REUSE",
4912 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07004913 }
4914# check if any macro arguments may have other precedence issues
4915 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4916 ((defined($1) && $1 ne ',') ||
4917 (defined($2) && $2 ne ','))) {
4918 CHK("MACRO_ARG_PRECEDENCE",
4919 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07004920 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004921 }
Joe Perches5023d342012-12-17 16:01:47 -08004922
Joe Perches08a28432014-10-13 15:51:55 -07004923# check for macros with flow control, but without ## concatenation
4924# ## concatenation is commonly a macro that defines a function so ignore those
4925 if ($has_flow_statement && !$has_arg_concat) {
4926 my $herectx = $here . "\n";
4927 my $cnt = statement_rawlines($ctx);
4928
4929 for (my $n = 0; $n < $cnt; $n++) {
4930 $herectx .= raw_line($linenr, $n) . "\n";
4931 }
4932 WARN("MACRO_WITH_FLOW_CONTROL",
4933 "Macros with flow control statements should be avoided\n" . "$herectx");
4934 }
4935
Joe Perches481eb482012-12-17 16:01:56 -08004936# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004937
4938 } else {
4939 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004940 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4941 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004942 $line =~ /^\+.*\\$/) {
4943 WARN("LINE_CONTINUATIONS",
4944 "Avoid unnecessary line continuations\n" . $herecurr);
4945 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004946 }
4947
Joe Perchesb13edf72012-07-30 14:41:24 -07004948# do {} while (0) macro tests:
4949# single-statement macros do not need to be enclosed in do while (0) loop,
4950# macro should not end with a semicolon
4951 if ($^V && $^V ge 5.10.0 &&
4952 $realfile !~ m@/vmlinux.lds.h$@ &&
4953 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4954 my $ln = $linenr;
4955 my $cnt = $realcnt;
4956 my ($off, $dstat, $dcond, $rest);
4957 my $ctx = '';
4958 ($dstat, $dcond, $ln, $cnt, $off) =
4959 ctx_statement_block($linenr, $realcnt, 0);
4960 $ctx = $dstat;
4961
4962 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004963 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004964
4965 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4966 my $stmts = $2;
4967 my $semis = $3;
4968
4969 $ctx =~ s/\n*$//;
4970 my $cnt = statement_rawlines($ctx);
4971 my $herectx = $here . "\n";
4972
4973 for (my $n = 0; $n < $cnt; $n++) {
4974 $herectx .= raw_line($linenr, $n) . "\n";
4975 }
4976
Joe Perchesac8e97f2012-08-21 16:15:53 -07004977 if (($stmts =~ tr/;/;/) == 1 &&
4978 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004979 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4980 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4981 }
4982 if (defined $semis && $semis ne "") {
4983 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4984 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4985 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004986 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4987 $ctx =~ s/\n*$//;
4988 my $cnt = statement_rawlines($ctx);
4989 my $herectx = $here . "\n";
4990
4991 for (my $n = 0; $n < $cnt; $n++) {
4992 $herectx .= raw_line($linenr, $n) . "\n";
4993 }
4994
4995 WARN("TRAILING_SEMICOLON",
4996 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004997 }
4998 }
4999
Mike Frysinger080ba922009-01-06 14:41:25 -08005000# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5001# all assignments may have only one of the following with an assignment:
5002# .
5003# ALIGN(...)
5004# VMLINUX_SYMBOL(...)
5005 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 WARN("MISSING_VMLINUX_SYMBOL",
5007 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005008 }
5009
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005010# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005011 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5012 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005013 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005014 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005015 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5016 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005017 my @allowed = ();
5018 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005019 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005020 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005021 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005022 for my $chunk (@chunks) {
5023 my ($cond, $block) = @{$chunk};
5024
Andy Whitcroft773647a2008-03-28 14:15:58 -07005025 # If the condition carries leading newlines, then count those as offsets.
5026 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5027 my $offset = statement_rawlines($whitespace) - 1;
5028
Joe Perchesaad4f612012-03-23 15:02:19 -07005029 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005030 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5031
5032 # We have looked at and allowed this specific line.
5033 $suppress_ifbraces{$ln + $offset} = 1;
5034
5035 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005036 $ln += statement_rawlines($block) - 1;
5037
Andy Whitcroft773647a2008-03-28 14:15:58 -07005038 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005039
5040 $seen++ if ($block =~ /^\s*{/);
5041
Joe Perchesaad4f612012-03-23 15:02:19 -07005042 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005043 if (statement_lines($cond) > 1) {
5044 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005045 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005046 }
5047 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005048 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005049 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005050 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005051 if (statement_block_size($block) > 1) {
5052 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005053 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005054 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005055 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005056 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005057 if ($seen) {
5058 my $sum_allowed = 0;
5059 foreach (@allowed) {
5060 $sum_allowed += $_;
5061 }
5062 if ($sum_allowed == 0) {
5063 WARN("BRACES",
5064 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5065 } elsif ($sum_allowed != $allow &&
5066 $seen != $allow) {
5067 CHK("BRACES",
5068 "braces {} should be used on all arms of this statement\n" . $herectx);
5069 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005070 }
5071 }
5072 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005073 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005074 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005075 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005076
Andy Whitcroftcf655042008-03-04 14:28:20 -08005077 # Check the pre-context.
5078 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5079 #print "APW: ALLOWED: pre<$1>\n";
5080 $allowed = 1;
5081 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005082
5083 my ($level, $endln, @chunks) =
5084 ctx_statement_full($linenr, $realcnt, $-[0]);
5085
Andy Whitcroftcf655042008-03-04 14:28:20 -08005086 # Check the condition.
5087 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005088 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005089 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005090 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005091 }
5092 if (statement_lines($cond) > 1) {
5093 #print "APW: ALLOWED: cond<$cond>\n";
5094 $allowed = 1;
5095 }
5096 if ($block =~/\b(?:if|for|while)\b/) {
5097 #print "APW: ALLOWED: block<$block>\n";
5098 $allowed = 1;
5099 }
5100 if (statement_block_size($block) > 1) {
5101 #print "APW: ALLOWED: lines block<$block>\n";
5102 $allowed = 1;
5103 }
5104 # Check the post-context.
5105 if (defined $chunks[1]) {
5106 my ($cond, $block) = @{$chunks[1]};
5107 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005108 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005109 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005110 if ($block =~ /^\s*\{/) {
5111 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5112 $allowed = 1;
5113 }
5114 }
5115 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005116 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005117 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005118
Andy Whitcroftf0556632008-10-15 22:02:23 -07005119 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005120 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005121 }
5122
Joe Perches000d1cc12011-07-25 17:13:25 -07005123 WARN("BRACES",
5124 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005125 }
5126 }
5127
Joe Perchese4c5bab2017-02-24 15:01:41 -08005128# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005129 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5130 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005131 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5132 }
5133
Joe Perches0979ae62012-12-17 16:01:59 -08005134# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005135 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005136 if (CHK("BRACES",
5137 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5138 $fix && $prevrawline =~ /^\+/) {
5139 fix_delete_line($fixlinenr - 1, $prevrawline);
5140 }
Joe Perches0979ae62012-12-17 16:01:59 -08005141 }
Joe Perches77b9a532013-07-03 15:05:29 -07005142 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005143 if (CHK("BRACES",
5144 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5145 $fix) {
5146 fix_delete_line($fixlinenr, $rawline);
5147 }
Joe Perches0979ae62012-12-17 16:01:59 -08005148 }
5149
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005150# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005151 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5152 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005153 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005154 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005155 }
5156
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005157# Check for user-visible strings broken across lines, which breaks the ability
5158# to grep for the string. Make exceptions when the previous string ends in a
5159# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5160# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005161 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005162 $prevline =~ /"\s*$/ &&
5163 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5164 if (WARN("SPLIT_STRING",
5165 "quoted string split across lines\n" . $hereprev) &&
5166 $fix &&
5167 $prevrawline =~ /^\+.*"\s*$/ &&
5168 $last_coalesced_string_linenr != $linenr - 1) {
5169 my $extracted_string = get_quoted_string($line, $rawline);
5170 my $comma_close = "";
5171 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5172 $comma_close = $1;
5173 }
5174
5175 fix_delete_line($fixlinenr - 1, $prevrawline);
5176 fix_delete_line($fixlinenr, $rawline);
5177 my $fixedline = $prevrawline;
5178 $fixedline =~ s/"\s*$//;
5179 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5180 fix_insert_line($fixlinenr - 1, $fixedline);
5181 $fixedline = $rawline;
5182 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5183 if ($fixedline !~ /\+\s*$/) {
5184 fix_insert_line($fixlinenr, $fixedline);
5185 }
5186 $last_coalesced_string_linenr = $linenr;
5187 }
5188 }
5189
5190# check for missing a space in a string concatenation
5191 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5192 WARN('MISSING_SPACE',
5193 "break quoted strings at a space character\n" . $hereprev);
5194 }
5195
Joe Perchese4b7d302017-05-08 15:55:51 -07005196# check for an embedded function name in a string when the function is known
5197# This does not work very well for -f --file checking as it depends on patch
5198# context providing the function name or a single line form for in-file
5199# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005200 if ($line =~ /^\+.*$String/ &&
5201 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005202 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5203 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005204 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005205 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005206 }
5207
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005208# check for spaces before a quoted newline
5209 if ($rawline =~ /^.*\".*\s\\n/) {
5210 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5211 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5212 $fix) {
5213 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5214 }
5215
5216 }
5217
Joe Perchesf17dba42014-10-13 15:51:51 -07005218# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005219 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005220 CHK("CONCATENATED_STRING",
5221 "Concatenated strings should use spaces between elements\n" . $herecurr);
5222 }
5223
Joe Perches90ad30e2014-12-10 15:51:59 -08005224# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005225 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005226 WARN("STRING_FRAGMENTS",
5227 "Consecutive strings are generally better as a single string\n" . $herecurr);
5228 }
5229
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005230# check for non-standard and hex prefixed decimal printf formats
5231 my $show_L = 1; #don't show the same defect twice
5232 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005233 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005234 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005235 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005236 # check for %L
5237 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005238 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005239 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5240 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005241 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005242 # check for %Z
5243 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5244 WARN("PRINTF_Z",
5245 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5246 $show_Z = 0;
5247 }
5248 # check for 0x<decimal>
5249 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5250 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005251 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5252 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005253 }
5254
5255# check for line continuations in quoted strings with odd counts of "
5256 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5257 WARN("LINE_CONTINUATIONS",
5258 "Avoid line continuations in quoted strings\n" . $herecurr);
5259 }
5260
Andy Whitcroft00df3442007-06-08 13:47:06 -07005261# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005262 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005263 CHK("REDUNDANT_CODE",
5264 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005265 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005266 }
5267
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005268# check for needless "if (<foo>) fn(<foo>)" uses
5269 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005270 my $tested = quotemeta($1);
5271 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5272 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5273 my $func = $1;
5274 if (WARN('NEEDLESS_IF',
5275 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5276 $fix) {
5277 my $do_fix = 1;
5278 my $leading_tabs = "";
5279 my $new_leading_tabs = "";
5280 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5281 $leading_tabs = $1;
5282 } else {
5283 $do_fix = 0;
5284 }
5285 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5286 $new_leading_tabs = $1;
5287 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5288 $do_fix = 0;
5289 }
5290 } else {
5291 $do_fix = 0;
5292 }
5293 if ($do_fix) {
5294 fix_delete_line($fixlinenr - 1, $prevrawline);
5295 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5296 }
5297 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005298 }
5299 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005300
Joe Perchesebfdc402014-08-06 16:10:27 -07005301# check for unnecessary "Out of Memory" messages
5302 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5303 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5304 (defined $1 || defined $3) &&
5305 $linenr > 3) {
5306 my $testval = $2;
5307 my $testline = $lines[$linenr - 3];
5308
5309 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5310# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5311
5312 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5313 WARN("OOM_MESSAGE",
5314 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5315 }
5316 }
5317
Joe Perchesf78d98f2014-10-13 15:52:01 -07005318# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005319 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005320 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5321 my $level = $1;
5322 if (WARN("UNNECESSARY_KERN_LEVEL",
5323 "Possible unnecessary $level\n" . $herecurr) &&
5324 $fix) {
5325 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5326 }
5327 }
5328
Joe Perches45c55e92017-02-24 15:01:31 -08005329# check for logging continuations
5330 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5331 WARN("LOGGING_CONTINUATION",
5332 "Avoid logging continuation uses where feasible\n" . $herecurr);
5333 }
5334
Joe Perchesabb08a52014-12-10 15:51:46 -08005335# check for mask then right shift without a parentheses
5336 if ($^V && $^V ge 5.10.0 &&
5337 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5338 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5339 WARN("MASK_THEN_SHIFT",
5340 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5341 }
5342
Joe Perchesb75ac612014-12-10 15:52:02 -08005343# check for pointer comparisons to NULL
5344 if ($^V && $^V ge 5.10.0) {
5345 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5346 my $val = $1;
5347 my $equal = "!";
5348 $equal = "" if ($4 eq "!=");
5349 if (CHK("COMPARISON_TO_NULL",
5350 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5351 $fix) {
5352 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5353 }
5354 }
5355 }
5356
Joe Perches8716de32013-09-11 14:24:05 -07005357# check for bad placement of section $InitAttribute (e.g.: __initdata)
5358 if ($line =~ /(\b$InitAttribute\b)/) {
5359 my $attr = $1;
5360 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5361 my $ptr = $1;
5362 my $var = $2;
5363 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5364 ERROR("MISPLACED_INIT",
5365 "$attr should be placed after $var\n" . $herecurr)) ||
5366 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5367 WARN("MISPLACED_INIT",
5368 "$attr should be placed after $var\n" . $herecurr))) &&
5369 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005370 $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 -07005371 }
5372 }
5373 }
5374
Joe Perchese970b8842013-11-12 15:10:10 -08005375# check for $InitAttributeData (ie: __initdata) with const
5376 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5377 my $attr = $1;
5378 $attr =~ /($InitAttributePrefix)(.*)/;
5379 my $attr_prefix = $1;
5380 my $attr_type = $2;
5381 if (ERROR("INIT_ATTRIBUTE",
5382 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5383 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005384 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005385 s/$InitAttributeData/${attr_prefix}initconst/;
5386 }
5387 }
5388
5389# check for $InitAttributeConst (ie: __initconst) without const
5390 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5391 my $attr = $1;
5392 if (ERROR("INIT_ATTRIBUTE",
5393 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5394 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005395 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005396 /(^\+\s*(?:static\s+))/;
5397 $lead = rtrim($1);
5398 $lead = "$lead " if ($lead !~ /^\+$/);
5399 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005400 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005401 }
5402 }
5403
Joe Perchesc17893c2015-04-16 12:44:42 -07005404# check for __read_mostly with const non-pointer (should just be const)
5405 if ($line =~ /\b__read_mostly\b/ &&
5406 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5407 if (ERROR("CONST_READ_MOSTLY",
5408 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5409 $fix) {
5410 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5411 }
5412 }
5413
Joe Perchesfbdb8132014-04-03 14:49:14 -07005414# don't use __constant_<foo> functions outside of include/uapi/
5415 if ($realfile !~ m@^include/uapi/@ &&
5416 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5417 my $constant_func = $1;
5418 my $func = $constant_func;
5419 $func =~ s/^__constant_//;
5420 if (WARN("CONSTANT_CONVERSION",
5421 "$constant_func should be $func\n" . $herecurr) &&
5422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005423 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005424 }
5425 }
5426
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005427# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005428 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005429 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005430 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005431 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005432 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005433 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5434 }
5435 if ($delay > 2000) {
5436 WARN("LONG_UDELAY",
5437 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005438 }
5439 }
5440
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005441# warn about unexpectedly long msleep's
5442 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5443 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005444 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005445 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005446 }
5447 }
5448
Joe Perches36ec1932013-07-03 15:05:25 -07005449# check for comparisons of jiffies
5450 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5451 WARN("JIFFIES_COMPARISON",
5452 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5453 }
5454
Joe Perches9d7a34a2013-07-03 15:05:26 -07005455# check for comparisons of get_jiffies_64()
5456 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5457 WARN("JIFFIES_COMPARISON",
5458 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5459 }
5460
Andy Whitcroft00df3442007-06-08 13:47:06 -07005461# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005462# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005463# print "#ifdef in C files should be avoided\n";
5464# print "$herecurr";
5465# $clean = 0;
5466# }
5467
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005468# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005469 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005470 if (ERROR("SPACING",
5471 "exactly one space required after that #$1\n" . $herecurr) &&
5472 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005473 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005474 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5475 }
5476
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005477 }
5478
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005479# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005480 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5481 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005482 my $which = $1;
5483 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005484 CHK("UNCOMMENTED_DEFINITION",
5485 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005486 }
5487 }
5488# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005489
5490 my $barriers = qr{
5491 mb|
5492 rmb|
5493 wmb|
5494 read_barrier_depends
5495 }x;
5496 my $barrier_stems = qr{
5497 mb__before_atomic|
5498 mb__after_atomic|
5499 store_release|
5500 load_acquire|
5501 store_mb|
5502 (?:$barriers)
5503 }x;
5504 my $all_barriers = qr{
5505 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005506 smp_(?:$barrier_stems)|
5507 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005508 }x;
5509
5510 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005511 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005512 WARN("MEMORY_BARRIER",
5513 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005514 }
5515 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005516
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005517 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5518
5519 if ($realfile !~ m@^include/asm-generic/@ &&
5520 $realfile !~ m@/barrier\.h$@ &&
5521 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5522 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5523 WARN("MEMORY_BARRIER",
5524 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5525 }
5526
Joe Perchescb426e92015-06-25 15:02:46 -07005527# check for waitqueue_active without a comment.
5528 if ($line =~ /\bwaitqueue_active\s*\(/) {
5529 if (!ctx_has_comment($first_line, $linenr)) {
5530 WARN("WAITQUEUE_ACTIVE",
5531 "waitqueue_active without comment\n" . $herecurr);
5532 }
5533 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005534
5535# Check for expedited grace periods that interrupt non-idle non-nohz
5536# online CPUs. These expedited can therefore degrade real-time response
5537# if used carelessly, and should be avoided where not absolutely
5538# needed. It is always OK to use synchronize_rcu_expedited() and
5539# synchronize_sched_expedited() at boot time (before real-time applications
5540# start) and in error situations where real-time response is compromised in
5541# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5542# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5543# Of course, nothing comes for free, and srcu_read_lock() and
5544# srcu_read_unlock() do contain full memory barriers in payment for
5545# synchronize_srcu_expedited() non-interruption properties.
5546 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5547 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5548 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5549
5550 }
5551
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005552# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005553 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005554 CHK("ARCH_DEFINES",
5555 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005556 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005557
Tobias Klauserd4977c72010-05-24 14:33:30 -07005558# Check that the storage class is at the beginning of a declaration
5559 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005560 WARN("STORAGE_CLASS",
5561 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005562 }
5563
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005564# check the location of the inline attribute, that it is between
5565# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005566 if ($line =~ /\b$Type\s+$Inline\b/ ||
5567 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005568 ERROR("INLINE_LOCATION",
5569 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005570 }
5571
Andy Whitcroft8905a672007-11-28 16:21:06 -08005572# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005573 if ($realfile !~ m@\binclude/uapi/@ &&
5574 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005575 if (WARN("INLINE",
5576 "plain inline is preferred over $1\n" . $herecurr) &&
5577 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005578 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005579
5580 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005581 }
5582
Joe Perches3d130fd2011-01-12 17:00:00 -08005583# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005584 if ($realfile !~ m@\binclude/uapi/@ &&
5585 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005586 WARN("PREFER_PACKED",
5587 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005588 }
5589
Joe Perches39b7e282011-07-25 17:13:24 -07005590# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005591 if ($realfile !~ m@\binclude/uapi/@ &&
5592 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005593 WARN("PREFER_ALIGNED",
5594 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005595 }
5596
Joe Perches5f14d3b2012-01-10 15:09:52 -08005597# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005598 if ($realfile !~ m@\binclude/uapi/@ &&
5599 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005600 if (WARN("PREFER_PRINTF",
5601 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5602 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005603 $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 -07005604
5605 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005606 }
5607
Joe Perches6061d942012-03-23 15:02:16 -07005608# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005609 if ($realfile !~ m@\binclude/uapi/@ &&
5610 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005611 if (WARN("PREFER_SCANF",
5612 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5613 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005614 $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 -07005615 }
Joe Perches6061d942012-03-23 15:02:16 -07005616 }
5617
Joe Perches619a9082014-12-10 15:51:35 -08005618# Check for __attribute__ weak, or __weak declarations (may have link issues)
5619 if ($^V && $^V ge 5.10.0 &&
5620 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5621 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5622 $line =~ /\b__weak\b/)) {
5623 ERROR("WEAK_DECLARATION",
5624 "Using weak declarations can have unintended link defects\n" . $herecurr);
5625 }
5626
Tomas Winklerfd39f902016-12-12 16:46:34 -08005627# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005628 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005629 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005630 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5631 my $type = $1;
5632 if ($type =~ /\b($typeC99Typedefs)\b/) {
5633 $type = $1;
5634 my $kernel_type = 'u';
5635 $kernel_type = 's' if ($type =~ /^_*[si]/);
5636 $type =~ /(\d+)/;
5637 $kernel_type .= $1;
5638 if (CHK("PREFER_KERNEL_TYPES",
5639 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5640 $fix) {
5641 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5642 }
5643 }
5644 }
5645
Joe Perches938224b2016-01-20 14:59:15 -08005646# check for cast of C90 native int or longer types constants
5647 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5648 my $cast = $1;
5649 my $const = $2;
5650 if (WARN("TYPECAST_INT_CONSTANT",
5651 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5652 $fix) {
5653 my $suffix = "";
5654 my $newconst = $const;
5655 $newconst =~ s/${Int_type}$//;
5656 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5657 if ($cast =~ /\blong\s+long\b/) {
5658 $suffix .= 'LL';
5659 } elsif ($cast =~ /\blong\b/) {
5660 $suffix .= 'L';
5661 }
5662 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5663 }
5664 }
5665
Joe Perches8f53a9b2010-03-05 13:43:48 -08005666# check for sizeof(&)
5667 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005668 WARN("SIZEOF_ADDRESS",
5669 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005670 }
5671
Joe Perches66c80b62012-07-30 14:41:22 -07005672# check for sizeof without parenthesis
5673 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005674 if (WARN("SIZEOF_PARENTHESIS",
5675 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5676 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005677 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005678 }
Joe Perches66c80b62012-07-30 14:41:22 -07005679 }
5680
Joe Perches88982fe2012-12-17 16:02:00 -08005681# check for struct spinlock declarations
5682 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5683 WARN("USE_SPINLOCK_T",
5684 "struct spinlock should be spinlock_t\n" . $herecurr);
5685 }
5686
Joe Perchesa6962d72013-04-29 16:18:13 -07005687# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005688 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005689 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005690 $fmt =~ s/%%//g;
5691 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005692 if (WARN("PREFER_SEQ_PUTS",
5693 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5694 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005695 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005696 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005697 }
5698 }
5699
Joe Perches0b523762017-05-08 15:55:36 -07005700 # check for vsprintf extension %p<foo> misuses
5701 if ($^V && $^V ge 5.10.0 &&
5702 defined $stat &&
5703 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5704 $1 !~ /^_*volatile_*$/) {
5705 my $bad_extension = "";
5706 my $lc = $stat =~ tr@\n@@;
5707 $lc = $lc + $linenr;
5708 for (my $count = $linenr; $count <= $lc; $count++) {
5709 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5710 $fmt =~ s/%%//g;
5711 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGN]).)/) {
5712 $bad_extension = $1;
5713 last;
5714 }
5715 }
5716 if ($bad_extension ne "") {
5717 my $stat_real = raw_line($linenr, 0);
5718 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5719 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5720 }
5721 WARN("VSPRINTF_POINTER_EXTENSION",
5722 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5723 }
5724 }
5725
Andy Whitcroft554e1652012-01-10 15:09:57 -08005726# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005727 if ($^V && $^V ge 5.10.0 &&
5728 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005729 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005730
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005731 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005732 my $ms_val = $7;
5733 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005734
Andy Whitcroft554e1652012-01-10 15:09:57 -08005735 if ($ms_size =~ /^(0x|)0$/i) {
5736 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005737 "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 -08005738 } elsif ($ms_size =~ /^(0x|)1$/i) {
5739 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005740 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5741 }
5742 }
5743
Joe Perches98a9bba2014-01-23 15:54:52 -08005744# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf333195d2016-10-11 13:51:53 -07005745# if ($^V && $^V ge 5.10.0 &&
5746# defined $stat &&
5747# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5748# if (WARN("PREFER_ETHER_ADDR_COPY",
5749# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5750# $fix) {
5751# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5752# }
5753# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005754
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005755# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf333195d2016-10-11 13:51:53 -07005756# if ($^V && $^V ge 5.10.0 &&
5757# defined $stat &&
5758# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5759# WARN("PREFER_ETHER_ADDR_EQUAL",
5760# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5761# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005762
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005763# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5764# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf333195d2016-10-11 13:51:53 -07005765# if ($^V && $^V ge 5.10.0 &&
5766# defined $stat &&
5767# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5768#
5769# my $ms_val = $7;
5770#
5771# if ($ms_val =~ /^(?:0x|)0+$/i) {
5772# if (WARN("PREFER_ETH_ZERO_ADDR",
5773# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5774# $fix) {
5775# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5776# }
5777# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5778# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5779# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5780# $fix) {
5781# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5782# }
5783# }
5784# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005785
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005786# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005787 if ($^V && $^V ge 5.10.0 &&
5788 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005789 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005790 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005791 my $call = $1;
5792 my $cast1 = deparenthesize($2);
5793 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005794 my $cast2 = deparenthesize($7);
5795 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005796 my $cast;
5797
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005798 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005799 $cast = "$cast1 or $cast2";
5800 } elsif ($cast1 ne "") {
5801 $cast = $cast1;
5802 } else {
5803 $cast = $cast2;
5804 }
5805 WARN("MINMAX",
5806 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005807 }
5808 }
5809
Joe Perches4a273192012-07-30 14:41:20 -07005810# check usleep_range arguments
5811 if ($^V && $^V ge 5.10.0 &&
5812 defined $stat &&
5813 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5814 my $min = $1;
5815 my $max = $7;
5816 if ($min eq $max) {
5817 WARN("USLEEP_RANGE",
5818 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5819 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5820 $min > $max) {
5821 WARN("USLEEP_RANGE",
5822 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5823 }
5824 }
5825
Joe Perches823b7942013-11-12 15:10:15 -08005826# check for naked sscanf
5827 if ($^V && $^V ge 5.10.0 &&
5828 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005829 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005830 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5831 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5832 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5833 my $lc = $stat =~ tr@\n@@;
5834 $lc = $lc + $linenr;
5835 my $stat_real = raw_line($linenr, 0);
5836 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5837 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5838 }
5839 WARN("NAKED_SSCANF",
5840 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5841 }
5842
Joe Perchesafc819a2014-06-04 16:12:08 -07005843# check for simple sscanf that should be kstrto<foo>
5844 if ($^V && $^V ge 5.10.0 &&
5845 defined $stat &&
5846 $line =~ /\bsscanf\b/) {
5847 my $lc = $stat =~ tr@\n@@;
5848 $lc = $lc + $linenr;
5849 my $stat_real = raw_line($linenr, 0);
5850 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5851 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5852 }
5853 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5854 my $format = $6;
5855 my $count = $format =~ tr@%@%@;
5856 if ($count == 1 &&
5857 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5858 WARN("SSCANF_TO_KSTRTO",
5859 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5860 }
5861 }
5862 }
5863
Joe Perches70dc8a42013-09-11 14:23:58 -07005864# check for new externs in .h files.
5865 if ($realfile =~ /\.h$/ &&
5866 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005867 if (CHK("AVOID_EXTERNS",
5868 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005869 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005870 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005871 }
5872 }
5873
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005874# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005875 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005876 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005877 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005878 my $function_name = $1;
5879 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005880
5881 my $s = $stat;
5882 if (defined $cond) {
5883 substr($s, 0, length($cond), '');
5884 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005885 if ($s =~ /^\s*;/ &&
5886 $function_name ne 'uninitialized_var')
5887 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005888 WARN("AVOID_EXTERNS",
5889 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005890 }
5891
5892 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005893 WARN("FUNCTION_ARGUMENTS",
5894 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005895 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005896
5897 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5898 $stat =~ /^.\s*extern\s+/)
5899 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005900 WARN("AVOID_EXTERNS",
5901 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005902 }
5903
Joe Perchesca0d8922016-10-11 13:52:16 -07005904 if ($realfile =~ /\.[ch]$/ && defined $stat &&
5905 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5906 $1 ne "void") {
5907 my $args = trim($1);
5908 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5909 my $arg = trim($1);
5910 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5911 WARN("FUNCTION_ARGUMENTS",
5912 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5913 }
5914 }
5915 }
5916
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005917# checks for new __setup's
5918 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5919 my $name = $1;
5920
5921 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005922 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005923 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005924 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005925 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005926
5927# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005928 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005929 WARN("UNNECESSARY_CASTS",
5930 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005931 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005932
Joe Perchesa640d252013-07-03 15:05:21 -07005933# alloc style
5934# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5935 if ($^V && $^V ge 5.10.0 &&
5936 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5937 CHK("ALLOC_SIZEOF_STRUCT",
5938 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5939 }
5940
Joe Perches60a55362014-06-04 16:12:07 -07005941# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5942 if ($^V && $^V ge 5.10.0 &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005943 defined $stat &&
5944 $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 -07005945 my $oldfunc = $3;
5946 my $a1 = $4;
5947 my $a2 = $10;
5948 my $newfunc = "kmalloc_array";
5949 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005950 my $r1 = $a1;
5951 my $r2 = $a2;
5952 if ($a1 =~ /^sizeof\s*\S/) {
5953 $r1 = $a2;
5954 $r2 = $a1;
5955 }
5956 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5957 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005958 my $ctx = '';
5959 my $herectx = $here . "\n";
5960 my $cnt = statement_rawlines($stat);
5961 for (my $n = 0; $n < $cnt; $n++) {
5962 $herectx .= raw_line($linenr, $n) . "\n";
5963 }
Joe Perches60a55362014-06-04 16:12:07 -07005964 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005965 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
5966 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07005967 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005968 $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 -07005969 }
5970 }
5971 }
5972
Joe Perches972fdea2013-04-29 16:18:12 -07005973# check for krealloc arg reuse
5974 if ($^V && $^V ge 5.10.0 &&
5975 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5976 WARN("KREALLOC_ARG_REUSE",
5977 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5978 }
5979
Joe Perches5ce59ae2013-02-21 16:44:18 -08005980# check for alloc argument mismatch
5981 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5982 WARN("ALLOC_ARRAY_ARGS",
5983 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5984 }
5985
Joe Perchescaf2a542011-01-12 16:59:56 -08005986# check for multiple semicolons
5987 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005988 if (WARN("ONE_SEMICOLON",
5989 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5990 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005991 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005992 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005993 }
5994
Tomas Winklercec3aaa2016-08-02 14:04:39 -07005995# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5996 if ($realfile !~ m@^include/uapi/@ &&
5997 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08005998 my $ull = "";
5999 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6000 if (CHK("BIT_MACRO",
6001 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6002 $fix) {
6003 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6004 }
6005 }
6006
Joe Perches2d632742016-05-20 17:04:00 -07006007# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6008 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6009 my $config = $1;
6010 if (WARN("PREFER_IS_ENABLED",
6011 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6012 $fix) {
6013 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6014 }
6015 }
6016
Joe Perchese81f2392014-08-06 16:11:25 -07006017# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006018 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6019 my $has_break = 0;
6020 my $has_statement = 0;
6021 my $count = 0;
6022 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006023 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006024 $prevline--;
6025 my $rline = $rawlines[$prevline - 1];
6026 my $fline = $lines[$prevline - 1];
6027 last if ($fline =~ /^\@\@/);
6028 next if ($fline =~ /^\-/);
6029 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6030 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6031 next if ($fline =~ /^.[\s$;]*$/);
6032 $has_statement = 1;
6033 $count++;
6034 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6035 }
6036 if (!$has_break && $has_statement) {
6037 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006038 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006039 }
6040 }
6041
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006042# check for switch/default statements without a break;
6043 if ($^V && $^V ge 5.10.0 &&
6044 defined $stat &&
6045 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6046 my $ctx = '';
6047 my $herectx = $here . "\n";
6048 my $cnt = statement_rawlines($stat);
6049 for (my $n = 0; $n < $cnt; $n++) {
6050 $herectx .= raw_line($linenr, $n) . "\n";
6051 }
6052 WARN("DEFAULT_NO_BREAK",
6053 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006054 }
6055
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006056# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006057 if ($line =~ /\b__FUNCTION__\b/) {
6058 if (WARN("USE_FUNC",
6059 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6060 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006061 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006062 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006063 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006064
Joe Perches62ec8182015-02-13 14:38:18 -08006065# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6066 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6067 ERROR("DATE_TIME",
6068 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6069 }
6070
Joe Perches2c924882012-03-23 15:02:20 -07006071# check for use of yield()
6072 if ($line =~ /\byield\s*\(\s*\)/) {
6073 WARN("YIELD",
6074 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6075 }
6076
Joe Perches179f8f42013-07-03 15:05:30 -07006077# check for comparisons against true and false
6078 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6079 my $lead = $1;
6080 my $arg = $2;
6081 my $test = $3;
6082 my $otype = $4;
6083 my $trail = $5;
6084 my $op = "!";
6085
6086 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6087
6088 my $type = lc($otype);
6089 if ($type =~ /^(?:true|false)$/) {
6090 if (("$test" eq "==" && "$type" eq "true") ||
6091 ("$test" eq "!=" && "$type" eq "false")) {
6092 $op = "";
6093 }
6094
6095 CHK("BOOL_COMPARISON",
6096 "Using comparison to $otype is error prone\n" . $herecurr);
6097
6098## maybe suggesting a correct construct would better
6099## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6100
6101 }
6102 }
6103
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006104# check for semaphores initialized locked
6105 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006106 WARN("CONSIDER_COMPLETION",
6107 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006108 }
Joe Perches6712d852012-03-23 15:02:20 -07006109
Joe Perches67d0a072011-10-31 17:13:10 -07006110# recommend kstrto* over simple_strto* and strict_strto*
6111 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006112 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006113 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006114 }
Joe Perches6712d852012-03-23 15:02:20 -07006115
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006116# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006117 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006118 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006119 "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 -07006120 }
Joe Perches6712d852012-03-23 15:02:20 -07006121
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006122# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006123# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006124 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006125 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006126 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006127 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006128 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006129
6130# use of NR_CPUS is usually wrong
6131# ignore definitions of NR_CPUS and usage to define arrays as likely right
6132 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006133 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6134 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006135 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6136 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6137 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006138 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006139 WARN("NR_CPUS",
6140 "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 -07006141 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006142
Joe Perches52ea8502013-11-12 15:10:09 -08006143# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6144 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6145 ERROR("DEFINE_ARCH_HAS",
6146 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6147 }
6148
Joe Perchesacd93622015-02-13 14:38:38 -08006149# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6150 if ($^V && $^V ge 5.10.0 &&
6151 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6152 WARN("LIKELY_MISUSE",
6153 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6154 }
6155
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006156# whine mightly about in_atomic
6157 if ($line =~ /\bin_atomic\s*\(/) {
6158 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006159 ERROR("IN_ATOMIC",
6160 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006161 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006162 WARN("IN_ATOMIC",
6163 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006164 }
6165 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006166
Joe Perches481aea52016-05-20 17:04:08 -07006167# whine about ACCESS_ONCE
6168 if ($^V && $^V ge 5.10.0 &&
6169 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6170 my $par = $1;
6171 my $eq = $2;
6172 my $fun = $3;
6173 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6174 if (defined($eq)) {
6175 if (WARN("PREFER_WRITE_ONCE",
6176 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6177 $fix) {
6178 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6179 }
6180 } else {
6181 if (WARN("PREFER_READ_ONCE",
6182 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6183 $fix) {
6184 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6185 }
6186 }
6187 }
6188
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006189# check for mutex_trylock_recursive usage
6190 if ($line =~ /mutex_trylock_recursive/) {
6191 ERROR("LOCKING",
6192 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6193 }
6194
Peter Zijlstra1704f472010-03-19 01:37:42 +01006195# check for lockdep_set_novalidate_class
6196 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6197 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6198 if ($realfile !~ m@^kernel/lockdep@ &&
6199 $realfile !~ m@^include/linux/lockdep@ &&
6200 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006201 ERROR("LOCKDEP",
6202 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006203 }
6204 }
Dave Jones88f88312011-01-12 16:59:59 -08006205
Joe Perchesb392c642015-04-16 12:44:16 -07006206 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6207 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006208 WARN("EXPORTED_WORLD_WRITABLE",
6209 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006210 }
Joe Perches24358802014-04-03 14:49:13 -07006211
Joe Perches515a2352014-04-03 14:49:24 -07006212# Mode permission misuses where it seems decimal should be octal
6213# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6214 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006215 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006216 $line =~ /$mode_perms_search/) {
6217 foreach my $entry (@mode_permission_funcs) {
6218 my $func = $entry->[0];
6219 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006220
Joe Perches459cf0a2016-10-11 13:52:19 -07006221 my $lc = $stat =~ tr@\n@@;
6222 $lc = $lc + $linenr;
6223 my $stat_real = raw_line($linenr, 0);
6224 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6225 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6226 }
6227
Joe Perches515a2352014-04-03 14:49:24 -07006228 my $skip_args = "";
6229 if ($arg_pos > 1) {
6230 $arg_pos--;
6231 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6232 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006233 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006234 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006235 my $val = $1;
6236 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006237 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6238 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006239 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006240 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006241 }
6242 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006243 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006244 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006245 }
Joe Perches24358802014-04-03 14:49:13 -07006246 }
6247 }
6248 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006249
Joe Perches459cf0a2016-10-11 13:52:19 -07006250# check for uses of S_<PERMS> that could be octal for readability
6251 if ($line =~ /\b$mode_perms_string_search\b/) {
6252 my $val = "";
6253 my $oval = "";
6254 my $to = 0;
6255 my $curpos = 0;
6256 my $lastpos = 0;
6257 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6258 $curpos = pos($line);
6259 my $match = $2;
6260 my $omatch = $1;
6261 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6262 $lastpos = $curpos;
6263 $to |= $mode_permission_string_types{$match};
6264 $val .= '\s*\|\s*' if ($val ne "");
6265 $val .= $match;
6266 $oval .= $omatch;
6267 }
6268 $oval =~ s/^\s*\|\s*//;
6269 $oval =~ s/\s*\|\s*$//;
6270 my $octal = sprintf("%04o", $to);
6271 if (WARN("SYMBOLIC_PERMS",
6272 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6273 $fix) {
6274 $fixed[$fixlinenr] =~ s/$val/$octal/;
6275 }
6276 }
6277
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006278# validate content of MODULE_LICENSE against list from include/linux/module.h
6279 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6280 my $extracted_string = get_quoted_string($line, $rawline);
6281 my $valid_licenses = qr{
6282 GPL|
6283 GPL\ v2|
6284 GPL\ and\ additional\ rights|
6285 Dual\ BSD/GPL|
6286 Dual\ MIT/GPL|
6287 Dual\ MPL/GPL|
6288 Proprietary
6289 }x;
6290 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6291 WARN("MODULE_LICENSE",
6292 "unknown module license " . $extracted_string . "\n" . $herecurr);
6293 }
6294 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006295 }
6296
6297 # If we have no input at all, then there is nothing to report on
6298 # so just keep quiet.
6299 if ($#rawlines == -1) {
6300 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006301 }
6302
Andy Whitcroft8905a672007-11-28 16:21:06 -08006303 # In mailback mode only produce a report in the negative, for
6304 # things that appear to be patches.
6305 if ($mailback && ($clean == 1 || !$is_patch)) {
6306 exit(0);
6307 }
6308
6309 # This is not a patch, and we are are in 'no-patch' mode so
6310 # just keep quiet.
6311 if (!$chk_patch && !$is_patch) {
6312 exit(0);
6313 }
6314
Joe Perches06330fc2015-06-25 15:03:11 -07006315 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006316 ERROR("NOT_UNIFIED_DIFF",
6317 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006318 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006319 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006320 ERROR("MISSING_SIGN_OFF",
6321 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006322 }
6323
Andy Whitcroft8905a672007-11-28 16:21:06 -08006324 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006325 if ($summary && !($clean == 1 && $quiet == 1)) {
6326 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006327 print "total: $cnt_error errors, $cnt_warn warnings, " .
6328 (($check)? "$cnt_chk checks, " : "") .
6329 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006330 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006331
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006332 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006333 # If there were any defects found and not already fixing them
6334 if (!$clean and !$fix) {
6335 print << "EOM"
6336
6337NOTE: For some of the reported defects, checkpatch may be able to
6338 mechanically convert to the typical style using --fix or --fix-inplace.
6339EOM
6340 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006341 # If there were whitespace errors which cleanpatch can fix
6342 # then suggest that.
6343 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006344 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006345 print << "EOM"
6346
6347NOTE: Whitespace errors detected.
6348 You may wish to use scripts/cleanpatch or scripts/cleanfile
6349EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006350 }
6351 }
6352
Joe Perchesd752fcc2014-08-06 16:11:05 -07006353 if ($clean == 0 && $fix &&
6354 ("@rawlines" ne "@fixed" ||
6355 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006356 my $newfile = $filename;
6357 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006358 my $linecount = 0;
6359 my $f;
6360
Joe Perchesd752fcc2014-08-06 16:11:05 -07006361 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6362
Joe Perches3705ce52013-07-03 15:05:31 -07006363 open($f, '>', $newfile)
6364 or die "$P: Can't open $newfile for write\n";
6365 foreach my $fixed_line (@fixed) {
6366 $linecount++;
6367 if ($file) {
6368 if ($linecount > 3) {
6369 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006370 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006371 }
6372 } else {
6373 print $f $fixed_line . "\n";
6374 }
6375 }
6376 close($f);
6377
6378 if (!$quiet) {
6379 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006380
Joe Perches3705ce52013-07-03 15:05:31 -07006381Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6382
6383Do _NOT_ trust the results written to this file.
6384Do _NOT_ submit these changes without inspecting them for correctness.
6385
6386This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6387No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006388EOM
6389 }
6390 }
6391
Joe Perchesd8469f12015-06-25 15:03:00 -07006392 if ($quiet == 0) {
6393 print "\n";
6394 if ($clean == 1) {
6395 print "$vname has no obvious style problems and is ready for submission.\n";
6396 } else {
6397 print "$vname has style problems, please review.\n";
6398 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006399 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006400 return $clean;
6401}