blob: cdfa754abd5b89ed2b1fb07c1651545aa5d96ce5 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
David Keitel72bf3e82016-03-23 20:52:44 -070014use constant BEFORE_SHORTTEXT => 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -080015use constant IN_SHORTTEXT_BLANKLINE => 1;
16use constant IN_SHORTTEXT => 2;
17use constant AFTER_SHORTTEXT => 3;
18use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel72bf3e82016-03-23 20:52:44 -070019use constant SHORTTEXT_LIMIT => 75;
20
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080022my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070023
Joe Perches000d1cc12011-07-25 17:13:25 -070024my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070025
26use Getopt::Long qw(:config no_auto_abbrev);
27
28my $quiet = 0;
29my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080032my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070033my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070036my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070038my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070039my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070040my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070041my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080042my $summary = 1;
43my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070045my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070046my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070047my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080048my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070049my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080050my %debug;
Joe Perches34456862013-07-03 15:05:34 -070051my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070052my %use_type = ();
53my @use = ();
54my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070055my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070056my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070057my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080058my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070059my $ignore_perl_version = 0;
60my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070061my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070062my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070063my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070064my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070065my $conststructsfile = "$D/const_structs.checkpatch";
Joe Perches57230292015-06-25 15:03:03 -070066my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070067my $allow_c99_comments = 1;
Maya Erezec77bd62017-02-26 09:14:43 +020068my $qca_sign_off = 0;
69my $codeaurora_sign_off = 0;
Hannes Eder77f5b102009-09-21 17:04:37 -070070
71sub help {
72 my ($exitcode) = @_;
73
74 print << "EOM";
75Usage: $P [OPTION]... [FILE]...
76Version: $V
77
78Options:
79 -q, --quiet quiet
80 --no-tree run without a kernel tree
81 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080082 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070083 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800103 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700104 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
111 is all off)
112 --test-only=WORD report only warnings/errors containing WORD
113 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
118 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700122 --ignore-perl-version override checking of perl version. expect
123 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700125 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700126 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700127 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Joe Perches3beb42e2016-05-20 17:04:14 -0700136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
155 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
156 push (@types, $_);
157 }
158 @types = sort(uniq(@types));
159 print("#\tMessage type\n\n");
160 foreach my $type (@types) {
161 print(++$count . "\t" . $type . "\n");
162 }
163
164 exit($exitcode);
165}
166
Joe Perches000d1cc12011-07-25 17:13:25 -0700167my $conf = which_conf($configuration_file);
168if (-f $conf) {
169 my @conf_args;
170 open(my $conffile, '<', "$conf")
171 or warn "$P: Can't find a readable $configuration_file file $!\n";
172
173 while (<$conffile>) {
174 my $line = $_;
175
176 $line =~ s/\s*\n?$//g;
177 $line =~ s/^\s*//g;
178 $line =~ s/\s+/ /g;
179
180 next if ($line =~ m/^\s*#/);
181 next if ($line =~ m/^\s*$/);
182
183 my @words = split(" ", $line);
184 foreach my $word (@words) {
185 last if ($word =~ m/^#/);
186 push (@conf_args, $word);
187 }
188 }
189 close($conffile);
190 unshift(@ARGV, @conf_args) if @conf_args;
191}
192
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700193GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700195 'tree!' => \$tree,
196 'signoff!' => \$chk_signoff,
197 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800198 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800200 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700201 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700202 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700203 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 'subjective!' => \$check,
205 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700206 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700207 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700208 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700209 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800210 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700211 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213 'summary!' => \$summary,
214 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800215 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700216 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800217 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700218 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700220 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700221 'codespell!' => \$codespell,
222 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700223 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700224 'h|help' => \$help,
225 'version' => \$help
226) or help(1);
227
228help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700229
Joe Perches3beb42e2016-05-20 17:04:14 -0700230list_types(0) if ($list_types);
231
Joe Perches9624b8d2014-01-23 15:54:44 -0800232$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700233$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800234
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700235my $exit = 0;
236
Dave Hansend62a2012013-09-11 14:23:56 -0700237if ($^V && $^V lt $minimum_perl_version) {
238 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
239 if (!$ignore_perl_version) {
240 exit(1);
241 }
242}
243
Allen Hubbe45107ff2016-08-02 14:04:47 -0700244#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700245if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700246 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700247}
248
Joe Perches91bfe482013-09-11 14:23:59 -0700249sub hash_save_array_words {
250 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700251
Joe Perches91bfe482013-09-11 14:23:59 -0700252 my @array = split(/,/, join(',', @$arrayRef));
253 foreach my $word (@array) {
254 $word =~ s/\s*\n?$//g;
255 $word =~ s/^\s*//g;
256 $word =~ s/\s+/ /g;
257 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700258
Joe Perches91bfe482013-09-11 14:23:59 -0700259 next if ($word =~ m/^\s*#/);
260 next if ($word =~ m/^\s*$/);
261
262 $hashRef->{$word}++;
263 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700264}
265
Joe Perches91bfe482013-09-11 14:23:59 -0700266sub hash_show_words {
267 my ($hashRef, $prefix) = @_;
268
Joe Perches3c816e42015-06-25 15:03:29 -0700269 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700270 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700271 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700272 print " $word";
273 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700274 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700275 }
276}
277
278hash_save_array_words(\%ignore_type, \@ignore);
279hash_save_array_words(\%use_type, \@use);
280
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281my $dbg_values = 0;
282my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700283my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700284my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800285for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 ## no critic
287 eval "\${dbg_$key} = '$debug{$key}';";
288 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800289}
290
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700291my $rpt_cleaners = 0;
292
Andy Whitcroft8905a672007-11-28 16:21:06 -0800293if ($terse) {
294 $emacs = 1;
295 $quiet++;
296}
297
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298if ($tree) {
299 if (defined $root) {
300 if (!top_of_kernel_tree($root)) {
301 die "$P: $root: --root does not point at a valid tree\n";
302 }
303 } else {
304 if (top_of_kernel_tree('.')) {
305 $root = '.';
306 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
307 top_of_kernel_tree($1)) {
308 $root = $1;
309 }
310 }
311
312 if (!defined $root) {
313 print "Must be run from the top-level dir. of a kernel tree\n";
314 exit(2);
315 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700316}
317
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318my $emitted_corrupt = 0;
319
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700320our $Ident = qr{
321 [A-Za-z_][A-Za-z\d_]*
322 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
323 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700324our $Storage = qr{extern|static|asmlinkage};
325our $Sparse = qr{
326 __user|
327 __kernel|
328 __force|
329 __iomem|
330 __must_check|
331 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800332 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700333 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800334 __rcu|
335 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700336 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800337our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
338our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
339our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
340our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
341our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700342
Wolfram Sang52131292010-03-05 13:43:51 -0800343# Notes to $Attribute:
344# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Attribute = qr{
346 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700347 __percpu|
348 __nocast|
349 __safe|
350 __bitwise__|
351 __packed__|
352 __packed2__|
353 __naked|
354 __maybe_unused|
355 __always_unused|
356 __noreturn|
357 __used|
358 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800359 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700360 __noclone|
361 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700362 __read_mostly|
363 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700364 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700365 ____cacheline_aligned|
366 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800367 ____cacheline_internodealigned_in_smp|
368 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700369 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700370our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700371our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
373our $Lval = qr{$Ident(?:$Member)*};
374
Joe Perches95e2c602013-07-03 15:05:20 -0700375our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
376our $Binary = qr{(?i)0b[01]+$Int_type?};
377our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
378our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700379our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800380our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800381our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
382our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
383our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800384our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700385our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800386our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700387our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700388our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389our $Operators = qr{
390 <=|>=|==|!=|
391 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700392 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 }x;
394
Joe Perches91cb5192014-04-03 14:49:32 -0700395our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
396
Joe Perchesab7e23f2015-04-16 12:44:22 -0700397our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700399our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700400our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700402our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700404our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405
Joe Perches15662b32011-10-31 17:13:12 -0700406our $NON_ASCII_UTF8 = qr{
407 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700408 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
409 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
410 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
411 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
412 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
413 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
414}x;
415
Joe Perches15662b32011-10-31 17:13:12 -0700416our $UTF8 = qr{
417 [\x09\x0A\x0D\x20-\x7E] # ASCII
418 | $NON_ASCII_UTF8
419}x;
420
Joe Perchese6176fa2015-06-25 15:02:49 -0700421our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800422our $typeOtherOSTypedefs = qr{(?x:
423 u_(?:char|short|int|long) | # bsd
424 u(?:nchar|short|int|long) # sysv
425)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700426our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700427 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700428 atomic_t
429)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700430our $typeTypedefs = qr{(?x:
431 $typeC99Typedefs\b|
432 $typeOtherOSTypedefs\b|
433 $typeKernelTypedefs\b
434)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700435
Joe Perches6d32f7a2015-11-06 16:31:37 -0800436our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
437
Joe Perches691e6692010-03-05 13:43:51 -0800438our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700439 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700440 (?:[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 -0700441 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700442 panic|
Joe Perches06668722013-11-12 15:10:07 -0800443 MODULE_[A-Z_]+|
444 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800445)};
446
Joe Perches20112472011-07-25 17:13:23 -0700447our $signature_tags = qr{(?xi:
448 Signed-off-by:|
449 Acked-by:|
450 Tested-by:|
451 Reviewed-by:|
452 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700453 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700454 To:|
455 Cc:
456)};
457
Joe Perches18130872014-08-06 16:11:22 -0700458our @typeListMisordered = (
459 qr{char\s+(?:un)?signed},
460 qr{int\s+(?:(?:un)?signed\s+)?short\s},
461 qr{int\s+short(?:\s+(?:un)?signed)},
462 qr{short\s+int(?:\s+(?:un)?signed)},
463 qr{(?:un)?signed\s+int\s+short},
464 qr{short\s+(?:un)?signed},
465 qr{long\s+int\s+(?:un)?signed},
466 qr{int\s+long\s+(?:un)?signed},
467 qr{long\s+(?:un)?signed\s+int},
468 qr{int\s+(?:un)?signed\s+long},
469 qr{int\s+(?:un)?signed},
470 qr{int\s+long\s+long\s+(?:un)?signed},
471 qr{long\s+long\s+int\s+(?:un)?signed},
472 qr{long\s+long\s+(?:un)?signed\s+int},
473 qr{long\s+long\s+(?:un)?signed},
474 qr{long\s+(?:un)?signed},
475);
476
Andy Whitcroft8905a672007-11-28 16:21:06 -0800477our @typeList = (
478 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700479 qr{(?:(?:un)?signed\s+)?char},
480 qr{(?:(?:un)?signed\s+)?short\s+int},
481 qr{(?:(?:un)?signed\s+)?short},
482 qr{(?:(?:un)?signed\s+)?int},
483 qr{(?:(?:un)?signed\s+)?long\s+int},
484 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
485 qr{(?:(?:un)?signed\s+)?long\s+long},
486 qr{(?:(?:un)?signed\s+)?long},
487 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488 qr{float},
489 qr{double},
490 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800491 qr{struct\s+$Ident},
492 qr{union\s+$Ident},
493 qr{enum\s+$Ident},
494 qr{${Ident}_t},
495 qr{${Ident}_handler},
496 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700497 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498);
Joe Perches938224b2016-01-20 14:59:15 -0800499
500our $C90_int_types = qr{(?x:
501 long\s+long\s+int\s+(?:un)?signed|
502 long\s+long\s+(?:un)?signed\s+int|
503 long\s+long\s+(?:un)?signed|
504 (?:(?:un)?signed\s+)?long\s+long\s+int|
505 (?:(?:un)?signed\s+)?long\s+long|
506 int\s+long\s+long\s+(?:un)?signed|
507 int\s+(?:(?:un)?signed\s+)?long\s+long|
508
509 long\s+int\s+(?:un)?signed|
510 long\s+(?:un)?signed\s+int|
511 long\s+(?:un)?signed|
512 (?:(?:un)?signed\s+)?long\s+int|
513 (?:(?:un)?signed\s+)?long|
514 int\s+long\s+(?:un)?signed|
515 int\s+(?:(?:un)?signed\s+)?long|
516
517 int\s+(?:un)?signed|
518 (?:(?:un)?signed\s+)?int
519)};
520
Alex Dowad485ff232015-06-25 15:02:52 -0700521our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700522our @typeListWithAttr = (
523 @typeList,
524 qr{struct\s+$InitAttribute\s+$Ident},
525 qr{union\s+$InitAttribute\s+$Ident},
526);
527
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700528our @modifierList = (
529 qr{fastcall},
530);
Alex Dowad485ff232015-06-25 15:02:52 -0700531our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532
Joe Perches24358802014-04-03 14:49:13 -0700533our @mode_permission_funcs = (
534 ["module_param", 3],
535 ["module_param_(?:array|named|string)", 4],
536 ["module_param_array_named", 5],
537 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
538 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700539 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
540 ["IIO_DEV_ATTR_[A-Z_]+", 1],
541 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
542 ["SENSOR_TEMPLATE(?:_2|)", 3],
543 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700544);
545
Joe Perches515a2352014-04-03 14:49:24 -0700546#Create a search pattern for all these functions to speed up a loop below
547our $mode_perms_search = "";
548foreach my $entry (@mode_permission_funcs) {
549 $mode_perms_search .= '|' if ($mode_perms_search ne "");
550 $mode_perms_search .= $entry->[0];
551}
552
Joe Perchesb392c642015-04-16 12:44:16 -0700553our $mode_perms_world_writable = qr{
554 S_IWUGO |
555 S_IWOTH |
556 S_IRWXUGO |
557 S_IALLUGO |
558 0[0-7][0-7][2367]
559}x;
560
Joe Perchesf90774e2016-10-11 13:51:47 -0700561our %mode_permission_string_types = (
562 "S_IRWXU" => 0700,
563 "S_IRUSR" => 0400,
564 "S_IWUSR" => 0200,
565 "S_IXUSR" => 0100,
566 "S_IRWXG" => 0070,
567 "S_IRGRP" => 0040,
568 "S_IWGRP" => 0020,
569 "S_IXGRP" => 0010,
570 "S_IRWXO" => 0007,
571 "S_IROTH" => 0004,
572 "S_IWOTH" => 0002,
573 "S_IXOTH" => 0001,
574 "S_IRWXUGO" => 0777,
575 "S_IRUGO" => 0444,
576 "S_IWUGO" => 0222,
577 "S_IXUGO" => 0111,
578);
579
580#Create a search pattern for all these strings to speed up a loop below
581our $mode_perms_string_search = "";
582foreach my $entry (keys %mode_permission_string_types) {
583 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
584 $mode_perms_string_search .= $entry;
585}
586
Wolfram Sang7840a942010-08-09 17:20:57 -0700587our $allowed_asm_includes = qr{(?x:
588 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700589 memory|
590 time|
591 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700592)};
593# memory.h: ARM has a custom one
594
Kees Cook66b47b42014-10-13 15:51:57 -0700595# Load common spelling mistakes and build regular expression list.
596my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700597my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700598
Joe Perches36061e32014-12-10 15:51:43 -0800599if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800600 while (<$spelling>) {
601 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700602
Joe Perches36061e32014-12-10 15:51:43 -0800603 $line =~ s/\s*\n?$//g;
604 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700605
Joe Perches36061e32014-12-10 15:51:43 -0800606 next if ($line =~ m/^\s*#/);
607 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700608
Joe Perches36061e32014-12-10 15:51:43 -0800609 my ($suspect, $fix) = split(/\|\|/, $line);
610
Joe Perches36061e32014-12-10 15:51:43 -0800611 $spelling_fix{$suspect} = $fix;
612 }
613 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800614} else {
615 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700616}
Kees Cook66b47b42014-10-13 15:51:57 -0700617
Joe Perchesebfd7d62015-04-16 12:44:14 -0700618if ($codespell) {
619 if (open(my $spelling, '<', $codespellfile)) {
620 while (<$spelling>) {
621 my $line = $_;
622
623 $line =~ s/\s*\n?$//g;
624 $line =~ s/^\s*//g;
625
626 next if ($line =~ m/^\s*#/);
627 next if ($line =~ m/^\s*$/);
628 next if ($line =~ m/, disabled/i);
629
630 $line =~ s/,.*$//;
631
632 my ($suspect, $fix) = split(/->/, $line);
633
634 $spelling_fix{$suspect} = $fix;
635 }
636 close($spelling);
637 } else {
638 warn "No codespell typos will be found - file '$codespellfile': $!\n";
639 }
640}
641
642$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
643
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700644my $const_structs = "";
645if (open(my $conststructs, '<', $conststructsfile)) {
646 while (<$conststructs>) {
647 my $line = $_;
648
649 $line =~ s/\s*\n?$//g;
650 $line =~ s/^\s*//g;
651
652 next if ($line =~ m/^\s*#/);
653 next if ($line =~ m/^\s*$/);
654 if ($line =~ /\s/) {
655 print("$conststructsfile: '$line' invalid - ignored\n");
656 next;
657 }
658
659 $const_structs .= '|' if ($const_structs ne "");
660 $const_structs .= $line;
661 }
662 close($conststructsfile);
663} else {
664 warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
665}
666
Andy Whitcroft8905a672007-11-28 16:21:06 -0800667sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700668 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
669 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700670 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700671 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700672 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700673 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700674 (?:$typeTypedefs\b)|
675 (?:${all}\b)
676 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800677 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700678 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800679 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800680 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700681 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700682 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800683 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700684 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800685 }x;
Joe Perches18130872014-08-06 16:11:22 -0700686 $NonptrTypeMisordered = qr{
687 (?:$Modifier\s+|const\s+)*
688 (?:
689 (?:${Misordered}\b)
690 )
691 (?:\s+$Modifier|\s+const)*
692 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700693 $NonptrTypeWithAttr = qr{
694 (?:$Modifier\s+|const\s+)*
695 (?:
696 (?:typeof|__typeof__)\s*\([^\)]*\)|
697 (?:$typeTypedefs\b)|
698 (?:${allWithAttr}\b)
699 )
700 (?:\s+$Modifier|\s+const)*
701 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800702 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700703 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700704 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700705 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800706 }x;
Joe Perches18130872014-08-06 16:11:22 -0700707 $TypeMisordered = qr{
708 $NonptrTypeMisordered
709 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
710 (?:\s+$Inline|\s+$Modifier)*
711 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700712 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700713 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800714}
715build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700716
Joe Perches7d2367a2011-07-25 17:13:22 -0700717our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700718
719# Using $balanced_parens, $LvalOrFunc, or $FuncArg
720# requires at least perl version v5.10.0
721# Any use must be runtime checked with $^V
722
723our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700724our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800725our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700726
Joe Perchesf8422302014-08-06 16:11:31 -0700727our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700728 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700729 (?:$Storage\s+)?LIST_HEAD\s*\(|
730 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
731)};
732
Joe Perches7d2367a2011-07-25 17:13:22 -0700733sub deparenthesize {
734 my ($string) = @_;
735 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700736
737 while ($string =~ /^\s*\(.*\)\s*$/) {
738 $string =~ s@^\s*\(\s*@@;
739 $string =~ s@\s*\)\s*$@@;
740 }
741
Joe Perches7d2367a2011-07-25 17:13:22 -0700742 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700743
Joe Perches7d2367a2011-07-25 17:13:22 -0700744 return $string;
745}
746
Joe Perches34456862013-07-03 15:05:34 -0700747sub seed_camelcase_file {
748 my ($file) = @_;
749
750 return if (!(-f $file));
751
752 local $/;
753
754 open(my $include_file, '<', "$file")
755 or warn "$P: Can't read '$file' $!\n";
756 my $text = <$include_file>;
757 close($include_file);
758
759 my @lines = split('\n', $text);
760
761 foreach my $line (@lines) {
762 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
763 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
764 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800765 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
766 $camelcase{$1} = 1;
767 } 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 -0700768 $camelcase{$1} = 1;
769 }
770 }
771}
772
Joe Perches85b0ee12016-10-11 13:51:44 -0700773sub is_maintained_obsolete {
774 my ($filename) = @_;
775
776 return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
777
Joe Perches0616efa2016-10-11 13:52:02 -0700778 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 -0700779
780 return $status =~ /obsolete/i;
781}
782
Joe Perches34456862013-07-03 15:05:34 -0700783my $camelcase_seeded = 0;
784sub seed_camelcase_includes {
785 return if ($camelcase_seeded);
786
787 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700788 my $camelcase_cache = "";
789 my @include_files = ();
790
791 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700792
Richard Genoud3645e322014-02-10 14:25:32 -0800793 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700794 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
795 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700796 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700797 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700798 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700799 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700800 @include_files = split('\n', $files);
801 foreach my $file (@include_files) {
802 my $date = POSIX::strftime("%Y%m%d%H%M",
803 localtime((stat $file)[9]));
804 $last_mod_date = $date if ($last_mod_date < $date);
805 }
806 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700807 }
Joe Perchesc707a812013-07-08 16:00:43 -0700808
809 if ($camelcase_cache ne "" && -f $camelcase_cache) {
810 open(my $camelcase_file, '<', "$camelcase_cache")
811 or warn "$P: Can't read '$camelcase_cache' $!\n";
812 while (<$camelcase_file>) {
813 chomp;
814 $camelcase{$_} = 1;
815 }
816 close($camelcase_file);
817
818 return;
819 }
820
Richard Genoud3645e322014-02-10 14:25:32 -0800821 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700822 $files = `git ls-files "include/*.h"`;
823 @include_files = split('\n', $files);
824 }
825
Joe Perches34456862013-07-03 15:05:34 -0700826 foreach my $file (@include_files) {
827 seed_camelcase_file($file);
828 }
Joe Perches351b2a12013-07-03 15:05:36 -0700829
Joe Perchesc707a812013-07-08 16:00:43 -0700830 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700831 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700832 open(my $camelcase_file, '>', "$camelcase_cache")
833 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700834 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
835 print $camelcase_file ("$_\n");
836 }
837 close($camelcase_file);
838 }
Joe Perches34456862013-07-03 15:05:34 -0700839}
840
Joe Perchesd311cd42014-08-06 16:10:57 -0700841sub git_commit_info {
842 my ($commit, $id, $desc) = @_;
843
844 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
845
846 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
847 $output =~ s/^\s*//gm;
848 my @lines = split("\n", $output);
849
Joe Perches0d7835f2015-02-13 14:38:35 -0800850 return ($id, $desc) if ($#lines < 0);
851
Joe Perchesd311cd42014-08-06 16:10:57 -0700852 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
853# Maybe one day convert this block of bash into something that returns
854# all matching commit ids, but it's very slow...
855#
856# echo "checking commits $1..."
857# git rev-list --remotes | grep -i "^$1" |
858# while read line ; do
859# git log --format='%H %s' -1 $line |
860# echo "commit $(cut -c 1-12,41-)"
861# done
862 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
863 } else {
864 $id = substr($lines[0], 0, 12);
865 $desc = substr($lines[0], 41);
866 }
867
868 return ($id, $desc);
869}
870
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700871$chk_signoff = 0 if ($file);
872
Andy Whitcroft00df3442007-06-08 13:47:06 -0700873my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700875my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700876my @fixed_inserted = ();
877my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700878my $fixlinenr = -1;
879
Du, Changbin4a593c32016-05-20 17:04:16 -0700880# If input is git commits, extract all commits from the commit expressions.
881# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
882die "$P: No git repository found\n" if ($git && !-e ".git");
883
884if ($git) {
885 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700886 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700887 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700888 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
889 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700890 } elsif ($commit_expr =~ m/\.\./) {
891 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700892 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700893 $git_range = "-1 $commit_expr";
894 }
895 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
896 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700897 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
898 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700899 my $sha1 = $1;
900 my $subject = $2;
901 unshift(@commits, $sha1);
902 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700903 }
904 }
905 die "$P: no git commits after extraction!\n" if (@commits == 0);
906 @ARGV = @commits;
907}
908
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800909my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700910for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800911 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700912 if ($git) {
913 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
914 die "$P: $filename: git format-patch failed - $!\n";
915 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800916 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700917 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800918 } elsif ($filename eq '-') {
919 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700920 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800921 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700923 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800924 if ($filename eq '-') {
925 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700926 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700927 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 } else {
929 $vname = $filename;
930 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800931 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932 chomp;
933 push(@rawlines, $_);
934 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800935 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700936
937 if ($#ARGV > 0 && $quiet == 0) {
938 print '-' x length($vname) . "\n";
939 print "$vname\n";
940 print '-' x length($vname) . "\n";
941 }
942
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700944 $exit = 1;
945 }
946 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800947 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700948 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700949 @fixed_inserted = ();
950 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700951 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700952 @modifierListFile = ();
953 @typeListFile = ();
954 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700955}
956
Joe Perchesd8469f12015-06-25 15:03:00 -0700957if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700958 hash_show_words(\%use_type, "Used");
959 hash_show_words(\%ignore_type, "Ignored");
960
Joe Perchesd8469f12015-06-25 15:03:00 -0700961 if ($^V lt 5.10.0) {
962 print << "EOM"
963
964NOTE: perl $^V is not modern enough to detect all possible issues.
965 An upgrade to at least perl v5.10.0 is suggested.
966EOM
967 }
968 if ($exit) {
969 print << "EOM"
970
971NOTE: If any of the errors are false positives, please report
972 them to the maintainer, see CHECKPATCH in MAINTAINERS.
973EOM
974 }
975}
976
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700977exit($exit);
978
979sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700980 my ($root) = @_;
981
982 my @tree_check = (
983 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
984 "README", "Documentation", "arch", "include", "drivers",
985 "fs", "init", "ipc", "kernel", "lib", "scripts",
986 );
987
988 foreach my $check (@tree_check) {
989 if (! -e $root . '/' . $check) {
990 return 0;
991 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700992 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700993 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700994}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700995
Joe Perches20112472011-07-25 17:13:23 -0700996sub parse_email {
997 my ($formatted_email) = @_;
998
999 my $name = "";
1000 my $address = "";
1001 my $comment = "";
1002
1003 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1004 $name = $1;
1005 $address = $2;
1006 $comment = $3 if defined $3;
1007 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1008 $address = $1;
1009 $comment = $2 if defined $2;
1010 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1011 $address = $1;
1012 $comment = $2 if defined $2;
1013 $formatted_email =~ s/$address.*$//;
1014 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001015 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001016 $name =~ s/^\"|\"$//g;
1017 # If there's a name left after stripping spaces and
1018 # leading quotes, and the address doesn't have both
1019 # leading and trailing angle brackets, the address
1020 # is invalid. ie:
1021 # "joe smith joe@smith.com" bad
1022 # "joe smith <joe@smith.com" bad
1023 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1024 $name = "";
1025 $address = "";
1026 $comment = "";
1027 }
1028 }
1029
Joe Perches3705ce52013-07-03 15:05:31 -07001030 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001031 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001032 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001033 $address =~ s/^\<|\>$//g;
1034
1035 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1036 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1037 $name = "\"$name\"";
1038 }
1039
1040 return ($name, $address, $comment);
1041}
1042
1043sub format_email {
1044 my ($name, $address) = @_;
1045
1046 my $formatted_email;
1047
Joe Perches3705ce52013-07-03 15:05:31 -07001048 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001049 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001050 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001051
1052 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1053 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1054 $name = "\"$name\"";
1055 }
1056
1057 if ("$name" eq "") {
1058 $formatted_email = "$address";
1059 } else {
1060 $formatted_email = "$name <$address>";
1061 }
1062
1063 return $formatted_email;
1064}
1065
Joe Perchesd311cd42014-08-06 16:10:57 -07001066sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001067 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001068
Joe Perchesbd474ca2014-08-06 16:11:10 -07001069 foreach my $path (split(/:/, $ENV{PATH})) {
1070 if (-e "$path/$bin") {
1071 return "$path/$bin";
1072 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001073 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001074
Joe Perchesbd474ca2014-08-06 16:11:10 -07001075 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001076}
1077
Joe Perches000d1cc12011-07-25 17:13:25 -07001078sub which_conf {
1079 my ($conf) = @_;
1080
1081 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1082 if (-e "$path/$conf") {
1083 return "$path/$conf";
1084 }
1085 }
1086
1087 return "";
1088}
1089
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001090sub expand_tabs {
1091 my ($str) = @_;
1092
1093 my $res = '';
1094 my $n = 0;
1095 for my $c (split(//, $str)) {
1096 if ($c eq "\t") {
1097 $res .= ' ';
1098 $n++;
1099 for (; ($n % 8) != 0; $n++) {
1100 $res .= ' ';
1101 }
1102 next;
1103 }
1104 $res .= $c;
1105 $n++;
1106 }
1107
1108 return $res;
1109}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001112 return $res;
1113}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001114
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001115sub line_stats {
1116 my ($line) = @_;
1117
1118 # Drop the diff line leader and expand tabs
1119 $line =~ s/^.//;
1120 $line = expand_tabs($line);
1121
1122 # Pick the indent from the front of the line.
1123 my ($white) = ($line =~ /^(\s*)/);
1124
1125 return (length($line), length($white));
1126}
1127
Andy Whitcroft773647a2008-03-28 14:15:58 -07001128my $sanitise_quote = '';
1129
1130sub sanitise_line_reset {
1131 my ($in_comment) = @_;
1132
1133 if ($in_comment) {
1134 $sanitise_quote = '*/';
1135 } else {
1136 $sanitise_quote = '';
1137 }
1138}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001139sub sanitise_line {
1140 my ($line) = @_;
1141
1142 my $res = '';
1143 my $l = '';
1144
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001145 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001146 my $off = 0;
1147 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001148
Andy Whitcroft773647a2008-03-28 14:15:58 -07001149 # Always copy over the diff marker.
1150 $res = substr($line, 0, 1);
1151
1152 for ($off = 1; $off < length($line); $off++) {
1153 $c = substr($line, $off, 1);
1154
1155 # Comments we are wacking completly including the begin
1156 # and end, all to $;.
1157 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1158 $sanitise_quote = '*/';
1159
1160 substr($res, $off, 2, "$;$;");
1161 $off++;
1162 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001163 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001164 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001165 $sanitise_quote = '';
1166 substr($res, $off, 2, "$;$;");
1167 $off++;
1168 next;
1169 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001170 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1171 $sanitise_quote = '//';
1172
1173 substr($res, $off, 2, $sanitise_quote);
1174 $off++;
1175 next;
1176 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177
1178 # A \ in a string means ignore the next character.
1179 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1180 $c eq "\\") {
1181 substr($res, $off, 2, 'XX');
1182 $off++;
1183 next;
1184 }
1185 # Regular quotes.
1186 if ($c eq "'" || $c eq '"') {
1187 if ($sanitise_quote eq '') {
1188 $sanitise_quote = $c;
1189
1190 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001191 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 } elsif ($sanitise_quote eq $c) {
1193 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001194 }
1195 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001197 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001198 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1199 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001200 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1201 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001202 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1203 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001204 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001205 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001206 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 }
1208
Daniel Walker113f04a2009-09-21 17:04:35 -07001209 if ($sanitise_quote eq '//') {
1210 $sanitise_quote = '';
1211 }
1212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001214 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 my $clean = 'X' x length($1);
1216 $res =~ s@\<.*\>@<$clean>@;
1217
1218 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001219 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001220 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001221 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001222 }
1223
Joe Perchesdadf6802016-08-02 14:04:33 -07001224 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1225 my $match = $1;
1226 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1227 }
1228
Andy Whitcroft00df3442007-06-08 13:47:06 -07001229 return $res;
1230}
1231
Joe Perchesa6962d72013-04-29 16:18:13 -07001232sub get_quoted_string {
1233 my ($line, $rawline) = @_;
1234
Joe Perches33acb542015-06-25 15:02:54 -07001235 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001236 return substr($rawline, $-[0], $+[0] - $-[0]);
1237}
1238
Andy Whitcroft8905a672007-11-28 16:21:06 -08001239sub ctx_statement_block {
1240 my ($linenr, $remain, $off) = @_;
1241 my $line = $linenr - 1;
1242 my $blk = '';
1243 my $soff = $off;
1244 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001246
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001247 my $loff = 0;
1248
Andy Whitcroft8905a672007-11-28 16:21:06 -08001249 my $type = '';
1250 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001251 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001252 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001253 my $c;
1254 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001255
1256 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001257 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001258 @stack = (['', 0]) if ($#stack == -1);
1259
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 # If we are about to drop off the end, pull in more
1262 # context.
1263 if ($off >= $len) {
1264 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001265 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001267 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001268 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001269 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001270 $len = length($blk);
1271 $line++;
1272 last;
1273 }
1274 # Bail if there is no further context.
1275 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001276 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001277 last;
1278 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001279 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1280 $level++;
1281 $type = '#';
1282 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001283 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001287
Andy Whitcroft773647a2008-03-28 14:15:58 -07001288 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001289
1290 # Handle nested #if/#else.
1291 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1292 push(@stack, [ $type, $level ]);
1293 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1294 ($type, $level) = @{$stack[$#stack - 1]};
1295 } elsif ($remainder =~ /^#\s*endif\b/) {
1296 ($type, $level) = @{pop(@stack)};
1297 }
1298
Andy Whitcroft8905a672007-11-28 16:21:06 -08001299 # Statement ends at the ';' or a close '}' at the
1300 # outermost level.
1301 if ($level == 0 && $c eq ';') {
1302 last;
1303 }
1304
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001305 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 if ($level == 0 && $coff_set == 0 &&
1307 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1308 $remainder =~ /^(else)(?:\s|{)/ &&
1309 $remainder !~ /^else\s+if\b/) {
1310 $coff = $off + length($1) - 1;
1311 $coff_set = 1;
1312 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1313 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001314 }
1315
Andy Whitcroft8905a672007-11-28 16:21:06 -08001316 if (($type eq '' || $type eq '(') && $c eq '(') {
1317 $level++;
1318 $type = '(';
1319 }
1320 if ($type eq '(' && $c eq ')') {
1321 $level--;
1322 $type = ($level != 0)? '(' : '';
1323
1324 if ($level == 0 && $coff < $soff) {
1325 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326 $coff_set = 1;
1327 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001328 }
1329 }
1330 if (($type eq '' || $type eq '{') && $c eq '{') {
1331 $level++;
1332 $type = '{';
1333 }
1334 if ($type eq '{' && $c eq '}') {
1335 $level--;
1336 $type = ($level != 0)? '{' : '';
1337
1338 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001339 if (substr($blk, $off + 1, 1) eq ';') {
1340 $off++;
1341 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001342 last;
1343 }
1344 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001345 # Preprocessor commands end at the newline unless escaped.
1346 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1347 $level--;
1348 $type = '';
1349 $off++;
1350 last;
1351 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 $off++;
1353 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001354 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001356 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001357 $line++;
1358 $remain--;
1359 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001360
1361 my $statement = substr($blk, $soff, $off - $soff + 1);
1362 my $condition = substr($blk, $soff, $coff - $soff + 1);
1363
1364 #warn "STATEMENT<$statement>\n";
1365 #warn "CONDITION<$condition>\n";
1366
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001368
1369 return ($statement, $condition,
1370 $line, $remain + 1, $off - $loff + 1, $level);
1371}
1372
Andy Whitcroftcf655042008-03-04 14:28:20 -08001373sub statement_lines {
1374 my ($stmt) = @_;
1375
1376 # Strip the diff line prefixes and rip blank lines at start and end.
1377 $stmt =~ s/(^|\n)./$1/g;
1378 $stmt =~ s/^\s*//;
1379 $stmt =~ s/\s*$//;
1380
1381 my @stmt_lines = ($stmt =~ /\n/g);
1382
1383 return $#stmt_lines + 2;
1384}
1385
1386sub statement_rawlines {
1387 my ($stmt) = @_;
1388
1389 my @stmt_lines = ($stmt =~ /\n/g);
1390
1391 return $#stmt_lines + 2;
1392}
1393
1394sub statement_block_size {
1395 my ($stmt) = @_;
1396
1397 $stmt =~ s/(^|\n)./$1/g;
1398 $stmt =~ s/^\s*{//;
1399 $stmt =~ s/}\s*$//;
1400 $stmt =~ s/^\s*//;
1401 $stmt =~ s/\s*$//;
1402
1403 my @stmt_lines = ($stmt =~ /\n/g);
1404 my @stmt_statements = ($stmt =~ /;/g);
1405
1406 my $stmt_lines = $#stmt_lines + 2;
1407 my $stmt_statements = $#stmt_statements + 1;
1408
1409 if ($stmt_lines > $stmt_statements) {
1410 return $stmt_lines;
1411 } else {
1412 return $stmt_statements;
1413 }
1414}
1415
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001416sub ctx_statement_full {
1417 my ($linenr, $remain, $off) = @_;
1418 my ($statement, $condition, $level);
1419
1420 my (@chunks);
1421
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001423 ($statement, $condition, $linenr, $remain, $off, $level) =
1424 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001426 push(@chunks, [ $condition, $statement ]);
1427 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1428 return ($level, $linenr, @chunks);
1429 }
1430
1431 # Pull in the following conditional/block pairs and see if they
1432 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001433 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001434 ($statement, $condition, $linenr, $remain, $off, $level) =
1435 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 #print "C: push\n";
1439 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 }
1441
1442 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443}
1444
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001445sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001446 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001447 my $line;
1448 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001449 my $blk = '';
1450 my @o;
1451 my @c;
1452 my @res = ();
1453
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001454 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001455 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001456 for ($line = $start; $remain > 0; $line++) {
1457 next if ($rawlines[$line] =~ /^-/);
1458 $remain--;
1459
1460 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001461
1462 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001463 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001464 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001465 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001466 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001467 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001468 $level = pop(@stack);
1469 }
1470
Andy Whitcroft01464f32010-10-26 14:23:19 -07001471 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001472 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1473 if ($off > 0) {
1474 $off--;
1475 next;
1476 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001478 if ($c eq $close && $level > 0) {
1479 $level--;
1480 last if ($level == 0);
1481 } elsif ($c eq $open) {
1482 $level++;
1483 }
1484 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001485
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001486 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001488 }
1489
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001490 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001491 }
1492
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001493 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494}
1495sub ctx_block_outer {
1496 my ($linenr, $remain) = @_;
1497
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001498 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1499 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001500}
1501sub ctx_block {
1502 my ($linenr, $remain) = @_;
1503
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001504 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1505 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001506}
1507sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001508 my ($linenr, $remain, $off) = @_;
1509
1510 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1511 return @r;
1512}
1513sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001514 my ($linenr, $remain) = @_;
1515
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001516 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001517}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001518sub ctx_statement_level {
1519 my ($linenr, $remain, $off) = @_;
1520
1521 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1522}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001523
1524sub ctx_locate_comment {
1525 my ($first_line, $end_line) = @_;
1526
1527 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001528 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001529 return $current_comment if (defined $current_comment);
1530
1531 # Look through the context and try and figure out if there is a
1532 # comment.
1533 my $in_comment = 0;
1534 $current_comment = '';
1535 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001536 my $line = $rawlines[$linenr - 1];
1537 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001538 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1539 $in_comment = 1;
1540 }
1541 if ($line =~ m@/\*@) {
1542 $in_comment = 1;
1543 }
1544 if (!$in_comment && $current_comment ne '') {
1545 $current_comment = '';
1546 }
1547 $current_comment .= $line . "\n" if ($in_comment);
1548 if ($line =~ m@\*/@) {
1549 $in_comment = 0;
1550 }
1551 }
1552
1553 chomp($current_comment);
1554 return($current_comment);
1555}
1556sub ctx_has_comment {
1557 my ($first_line, $end_line) = @_;
1558 my $cmt = ctx_locate_comment($first_line, $end_line);
1559
Andy Whitcroft00df3442007-06-08 13:47:06 -07001560 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001561 ##print "CMMT: $cmt\n";
1562
1563 return ($cmt ne '');
1564}
1565
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001566sub raw_line {
1567 my ($linenr, $cnt) = @_;
1568
1569 my $offset = $linenr - 1;
1570 $cnt++;
1571
1572 my $line;
1573 while ($cnt) {
1574 $line = $rawlines[$offset++];
1575 next if (defined($line) && $line =~ /^-/);
1576 $cnt--;
1577 }
1578
1579 return $line;
1580}
1581
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001582sub cat_vet {
1583 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001584 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001585
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001586 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001587 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1588 $res .= $1;
1589 if ($2 ne '') {
1590 $coded = sprintf("^%c", unpack('C', $2) + 64);
1591 $res .= $coded;
1592 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001593 }
1594 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001595
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001596 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001597}
1598
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001599my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001601my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001602my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001603
1604sub annotate_reset {
1605 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606 $av_pending = '_';
1607 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001608 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609}
1610
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001611sub annotate_values {
1612 my ($stream, $type) = @_;
1613
1614 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001615 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001616 my $cur = $stream;
1617
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001618 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001622 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001623 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001624 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001625 print "WS($1)\n" if ($dbg_values > 1);
1626 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001627 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629 }
1630
Florian Micklerc023e4732011-01-12 16:59:58 -08001631 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001632 print "CAST($1)\n" if ($dbg_values > 1);
1633 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001634 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001635
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001636 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001637 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001638 $type = 'T';
1639
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001640 } elsif ($cur =~ /^($Modifier)\s*/) {
1641 print "MODIFIER($1)\n" if ($dbg_values > 1);
1642 $type = 'T';
1643
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001644 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001645 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001646 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001647 push(@av_paren_type, $type);
1648 if ($2 ne '') {
1649 $av_pending = 'N';
1650 }
1651 $type = 'E';
1652
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001653 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 print "UNDEF($1)\n" if ($dbg_values > 1);
1655 $av_preprocessor = 1;
1656 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001657
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001658 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001659 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001661
1662 push(@av_paren_type, $type);
1663 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001664 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001666 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001667 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1668 $av_preprocessor = 1;
1669
1670 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1671
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001672 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001674 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001675 print "PRE_END($1)\n" if ($dbg_values > 1);
1676
1677 $av_preprocessor = 1;
1678
1679 # Assume all arms of the conditional end as this
1680 # one does, and continue as if the #endif was not here.
1681 pop(@av_paren_type);
1682 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001683 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001684
1685 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001686 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001687
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001688 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1689 print "ATTR($1)\n" if ($dbg_values > 1);
1690 $av_pending = $type;
1691 $type = 'N';
1692
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001693 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001695 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001696 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001697 }
1698 $type = 'N';
1699
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001700 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001701 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001702 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703 $type = 'N';
1704
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001705 } elsif ($cur =~/^(case)/o) {
1706 print "CASE($1)\n" if ($dbg_values > 1);
1707 $av_pend_colon = 'C';
1708 $type = 'N';
1709
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001710 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001711 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712 $type = 'N';
1713
1714 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001715 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001716 push(@av_paren_type, $av_pending);
1717 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 $type = 'N';
1719
1720 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001721 my $new_type = pop(@av_paren_type);
1722 if ($new_type ne '_') {
1723 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001724 print "PAREN('$1') -> $type\n"
1725 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
1729
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001730 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001731 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001732 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001733 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001735 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1736 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001737 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001738 } elsif ($type eq 'E') {
1739 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001740 }
1741 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1742 $type = 'V';
1743
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001744 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001746 $type = 'V';
1747
1748 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 $type = 'N';
1751
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001754 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001755 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001756
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001757 } elsif ($cur =~/^(,)/) {
1758 print "COMMA($1)\n" if ($dbg_values > 1);
1759 $type = 'C';
1760
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001761 } elsif ($cur =~ /^(\?)/o) {
1762 print "QUESTION($1)\n" if ($dbg_values > 1);
1763 $type = 'N';
1764
1765 } elsif ($cur =~ /^(:)/o) {
1766 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1767
1768 substr($var, length($res), 1, $av_pend_colon);
1769 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1770 $type = 'E';
1771 } else {
1772 $type = 'N';
1773 }
1774 $av_pend_colon = 'O';
1775
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001776 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001777 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001778 $type = 'N';
1779
Andy Whitcroft0d413862008-10-15 22:02:16 -07001780 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001781 my $variant;
1782
1783 print "OPV($1)\n" if ($dbg_values > 1);
1784 if ($type eq 'V') {
1785 $variant = 'B';
1786 } else {
1787 $variant = 'U';
1788 }
1789
1790 substr($var, length($res), 1, $variant);
1791 $type = 'N';
1792
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 if ($1 ne '++' && $1 ne '--') {
1796 $type = 'N';
1797 }
1798
1799 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 }
1802 if (defined $1) {
1803 $cur = substr($cur, length($1));
1804 $res .= $type x length($1);
1805 }
1806 }
1807
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001808 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809}
1810
Andy Whitcroft8905a672007-11-28 16:21:06 -08001811sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001812 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001813 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001814 ^(?:
1815 $Modifier|
1816 $Storage|
1817 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001818 DEFINE_\S+
1819 )$|
1820 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001821 goto|
1822 return|
1823 case|
1824 else|
1825 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001826 do|
1827 \#|
1828 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001829 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001830 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001831 )}x;
1832 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1833 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001834 # Check for modifiers.
1835 $possible =~ s/\s*$Storage\s*//g;
1836 $possible =~ s/\s*$Sparse\s*//g;
1837 if ($possible =~ /^\s*$/) {
1838
1839 } elsif ($possible =~ /\s/) {
1840 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001841 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001842 if ($modifier !~ $notPermitted) {
1843 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001844 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001845 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001846 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001847
1848 } else {
1849 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001850 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001851 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001852 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001853 } else {
1854 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001855 }
1856}
1857
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001858my $prefix = '';
1859
Joe Perches000d1cc12011-07-25 17:13:25 -07001860sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001861 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001862
Joe Perchescbec18a2014-04-03 14:49:19 -07001863 return defined $use_type{$type} if (scalar keys %use_type > 0);
1864
1865 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001866}
1867
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001868sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001869 my ($level, $type, $msg) = @_;
1870
1871 if (!show_type($type) ||
1872 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 return 0;
1874 }
Joe Perches57230292015-06-25 15:03:03 -07001875 my $output = '';
1876 if (-t STDOUT && $color) {
1877 if ($level eq 'ERROR') {
1878 $output .= RED;
1879 } elsif ($level eq 'WARNING') {
1880 $output .= YELLOW;
1881 } else {
1882 $output .= GREEN;
1883 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001884 }
Joe Perches57230292015-06-25 15:03:03 -07001885 $output .= $prefix . $level . ':';
1886 if ($show_types) {
1887 $output .= BLUE if (-t STDOUT && $color);
1888 $output .= "$type:";
1889 }
1890 $output .= RESET if (-t STDOUT && $color);
1891 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001892
1893 if ($showfile) {
1894 my @lines = split("\n", $output, -1);
1895 splice(@lines, 1, 1);
1896 $output = join("\n", @lines);
1897 }
Joe Perches57230292015-06-25 15:03:03 -07001898 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001899
Joe Perches57230292015-06-25 15:03:03 -07001900 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001901
1902 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001903}
Joe Perchescbec18a2014-04-03 14:49:19 -07001904
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001905sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001906 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001907}
Joe Perches000d1cc12011-07-25 17:13:25 -07001908
Joe Perchesd752fcc2014-08-06 16:11:05 -07001909sub fixup_current_range {
1910 my ($lineRef, $offset, $length) = @_;
1911
1912 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1913 my $o = $1;
1914 my $l = $2;
1915 my $no = $o + $offset;
1916 my $nl = $l + $length;
1917 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1918 }
1919}
1920
1921sub fix_inserted_deleted_lines {
1922 my ($linesRef, $insertedRef, $deletedRef) = @_;
1923
1924 my $range_last_linenr = 0;
1925 my $delta_offset = 0;
1926
1927 my $old_linenr = 0;
1928 my $new_linenr = 0;
1929
1930 my $next_insert = 0;
1931 my $next_delete = 0;
1932
1933 my @lines = ();
1934
1935 my $inserted = @{$insertedRef}[$next_insert++];
1936 my $deleted = @{$deletedRef}[$next_delete++];
1937
1938 foreach my $old_line (@{$linesRef}) {
1939 my $save_line = 1;
1940 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001941 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001942 $delta_offset = 0;
1943 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1944 $range_last_linenr = $new_linenr;
1945 fixup_current_range(\$line, $delta_offset, 0);
1946 }
1947
1948 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1949 $deleted = @{$deletedRef}[$next_delete++];
1950 $save_line = 0;
1951 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1952 }
1953
1954 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1955 push(@lines, ${$inserted}{'LINE'});
1956 $inserted = @{$insertedRef}[$next_insert++];
1957 $new_linenr++;
1958 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1959 }
1960
1961 if ($save_line) {
1962 push(@lines, $line);
1963 $new_linenr++;
1964 }
1965
1966 $old_linenr++;
1967 }
1968
1969 return @lines;
1970}
1971
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001972sub fix_insert_line {
1973 my ($linenr, $line) = @_;
1974
1975 my $inserted = {
1976 LINENR => $linenr,
1977 LINE => $line,
1978 };
1979 push(@fixed_inserted, $inserted);
1980}
1981
1982sub fix_delete_line {
1983 my ($linenr, $line) = @_;
1984
1985 my $deleted = {
1986 LINENR => $linenr,
1987 LINE => $line,
1988 };
1989
1990 push(@fixed_deleted, $deleted);
1991}
1992
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001993sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001994 my ($type, $msg) = @_;
1995
1996 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001997 our $clean = 0;
1998 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001999 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 }
Joe Perches3705ce52013-07-03 15:05:31 -07002001 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002002}
2003sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002004 my ($type, $msg) = @_;
2005
2006 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007 our $clean = 0;
2008 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002009 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002010 }
Joe Perches3705ce52013-07-03 15:05:31 -07002011 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002012}
2013sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002014 my ($type, $msg) = @_;
2015
2016 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017 our $clean = 0;
2018 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 }
Joe Perches3705ce52013-07-03 15:05:31 -07002021 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002022}
2023
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002024sub check_absolute_file {
2025 my ($absolute, $herecurr) = @_;
2026 my $file = $absolute;
2027
2028 ##print "absolute<$absolute>\n";
2029
2030 # See if any suffix of this path is a path within the tree.
2031 while ($file =~ s@^[^/]*/@@) {
2032 if (-f "$root/$file") {
2033 ##print "file<$file>\n";
2034 last;
2035 }
2036 }
2037 if (! -f _) {
2038 return 0;
2039 }
2040
2041 # It is, so see if the prefix is acceptable.
2042 my $prefix = $absolute;
2043 substr($prefix, -length($file)) = '';
2044
2045 ##print "prefix<$prefix>\n";
2046 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002047 WARN("USE_RELATIVE_PATH",
2048 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002049 }
2050}
2051
Joe Perches3705ce52013-07-03 15:05:31 -07002052sub trim {
2053 my ($string) = @_;
2054
Joe Perchesb34c6482013-09-11 14:24:01 -07002055 $string =~ s/^\s+|\s+$//g;
2056
2057 return $string;
2058}
2059
2060sub ltrim {
2061 my ($string) = @_;
2062
2063 $string =~ s/^\s+//;
2064
2065 return $string;
2066}
2067
2068sub rtrim {
2069 my ($string) = @_;
2070
2071 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002072
2073 return $string;
2074}
2075
Joe Perches52ea8502013-11-12 15:10:09 -08002076sub string_find_replace {
2077 my ($string, $find, $replace) = @_;
2078
2079 $string =~ s/$find/$replace/g;
2080
2081 return $string;
2082}
2083
Joe Perches3705ce52013-07-03 15:05:31 -07002084sub tabify {
2085 my ($leading) = @_;
2086
2087 my $source_indent = 8;
2088 my $max_spaces_before_tab = $source_indent - 1;
2089 my $spaces_to_tab = " " x $source_indent;
2090
2091 #convert leading spaces to tabs
2092 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2093 #Remove spaces before a tab
2094 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2095
2096 return "$leading";
2097}
2098
Gregory Beanf07523e2011-07-06 15:36:58 -07002099sub cleanup_continuation_headers {
2100 # Collapse any header-continuation lines into a single line so they
2101 # can be parsed meaningfully, as the parser only has one line
2102 # of context to work with.
2103 my $again;
2104 do {
2105 $again = 0;
2106 foreach my $n (0 .. scalar(@rawlines) - 2) {
2107 if ($rawlines[$n]=~/^\s*$/) {
2108 # A blank line means there's no more chance
2109 # of finding headers. Shortcut to done.
2110 return;
2111 }
2112 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2113 $rawlines[$n+1]=~/^\s+/) {
2114 # Continuation header. Collapse it.
2115 my $line = splice @rawlines, $n+1, 1;
2116 $line=~s/^\s+/ /;
2117 $rawlines[$n] .= $line;
2118 # We've 'destabilized' the list, so restart.
2119 $again = 1;
2120 last;
2121 }
2122 }
2123 } while ($again);
2124}
2125
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002126sub pos_last_openparen {
2127 my ($line) = @_;
2128
2129 my $pos = 0;
2130
2131 my $opens = $line =~ tr/\(/\(/;
2132 my $closes = $line =~ tr/\)/\)/;
2133
2134 my $last_openparen = 0;
2135
2136 if (($opens == 0) || ($closes >= $opens)) {
2137 return -1;
2138 }
2139
2140 my $len = length($line);
2141
2142 for ($pos = 0; $pos < $len; $pos++) {
2143 my $string = substr($line, $pos);
2144 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2145 $pos += length($1) - 1;
2146 } elsif (substr($line, $pos, 1) eq '(') {
2147 $last_openparen = $pos;
2148 } elsif (index($string, '(') == -1) {
2149 last;
2150 }
2151 }
2152
Joe Perches91cb5192014-04-03 14:49:32 -07002153 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002154}
2155
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002156sub process {
2157 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002158
2159 my $linenr=0;
2160 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002162 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002163 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002164 my $subjectline="";
2165 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002167 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002168 my $indent;
2169 my $previndent=0;
2170 my $stashindent=0;
2171
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002172 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002173 my $signoff = 0;
2174 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002175 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002176 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002177 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002178 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002179 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002180 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002181 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002182 my $non_utf8_charset = 0;
2183
Joe Perches365dd4e2014-08-06 16:10:42 -07002184 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002185 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002186
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002187 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 our $cnt_lines = 0;
2189 our $cnt_error = 0;
2190 our $cnt_warn = 0;
2191 our $cnt_chk = 0;
2192
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002193 # Trace the real file/line as we go.
2194 my $realfile = '';
2195 my $realline = 0;
2196 my $realcnt = 0;
2197 my $here = '';
2198 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002199 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002200 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002201 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002202
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002203 my $prev_values = 'E';
2204
2205 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002207 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002208 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002209 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002210
Joe Perches7e51f192013-09-11 14:23:57 -07002211 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002214 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002215 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002216 my @setup_docs = ();
2217 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218
Joe Perchesd8b07712013-11-12 15:10:06 -08002219 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002220 my $shorttext = BEFORE_SHORTTEXT;
2221 my $shorttext_exspc = 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002222 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002223
Andy Whitcroft773647a2008-03-28 14:15:58 -07002224 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002225 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002226 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002227
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002228 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 $linenr++;
2230 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002231
Joe Perches3705ce52013-07-03 15:05:31 -07002232 push(@fixed, $rawline) if ($fix);
2233
Andy Whitcroft773647a2008-03-28 14:15:58 -07002234 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002235 $setup_docs = 0;
2236 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2237 $setup_docs = 1;
2238 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002241 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2242 $realline=$1-1;
2243 if (defined $2) {
2244 $realcnt=$3+1;
2245 } else {
2246 $realcnt=1+1;
2247 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002248 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249
2250 # Guestimate if this is a continuing comment. Run
2251 # the context looking for a comment "edge". If this
2252 # edge is a close comment then we must be in a comment
2253 # at context start.
2254 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002255 my $cnt = $realcnt;
2256 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2257 next if (defined $rawlines[$ln - 1] &&
2258 $rawlines[$ln - 1] =~ /^-/);
2259 $cnt--;
2260 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002261 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002262 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2263 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2264 ($edge) = $1;
2265 last;
2266 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 }
2268 if (defined $edge && $edge eq '*/') {
2269 $in_comment = 1;
2270 }
2271
2272 # Guestimate if this is a continuing comment. If this
2273 # is the start of a diff block and this line starts
2274 # ' *' then it is very likely a comment.
2275 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002276 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 {
2278 $in_comment = 1;
2279 }
2280
2281 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2282 sanitise_line_reset($in_comment);
2283
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002286 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 $line = sanitise_line($rawline);
2288 }
2289 push(@lines, $line);
2290
2291 if ($realcnt > 1) {
2292 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2293 } else {
2294 $realcnt = 0;
2295 }
2296
2297 #print "==>$rawline\n";
2298 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299
2300 if ($setup_docs && $line =~ /^\+/) {
2301 push(@setup_docs, $line);
2302 }
2303 }
2304
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002305 $prefix = '';
2306
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 $realcnt = 0;
2308 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002309 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002310 foreach my $line (@lines) {
2311 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002312 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002313 my $sline = $line; #copy of $line
2314 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002316 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002317
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002319 if (!$in_commit_log &&
2320 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002321 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002322 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002323 $realline=$1-1;
2324 if (defined $2) {
2325 $realcnt=$3+1;
2326 } else {
2327 $realcnt=1+1;
2328 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002329 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002330 $prev_values = 'E';
2331
Andy Whitcroft773647a2008-03-28 14:15:58 -07002332 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002333 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002334 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002335 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002336 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002337
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002338# track the line number as we move through the hunk, note that
2339# new versions of GNU diff omit the leading space on completely
2340# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002343 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002344
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002345 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002346 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002347
2348 # Track the previous line.
2349 ($prevline, $stashline) = ($stashline, $line);
2350 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002351 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2352
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002354
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002355 } elsif ($realcnt == 1) {
2356 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002357 }
2358
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002359 my $hunk_line = ($realcnt != 0);
2360
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002361 $here = "#$linenr: " if (!$file);
2362 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002364 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002366 if ($line =~ /^diff --git.*?(\S+)$/) {
2367 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002368 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002369 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002370 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002371 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002372 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002373 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002374 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002375
2376 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002377 if (!$file && $tree && $p1_prefix ne '' &&
2378 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002379 WARN("PATCH_PREFIX",
2380 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002381 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002382
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002383 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002384 ERROR("MODIFIED_INCLUDE_ASM",
2385 "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 -07002386 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002387 $found_file = 1;
2388 }
2389
Joe Perches34d88152015-06-25 15:03:05 -07002390#make up the handle for any error we report on this line
2391 if ($showfile) {
2392 $prefix = "$realfile:$realline: "
2393 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002394 if ($file) {
2395 $prefix = "$filename:$realline: ";
2396 } else {
2397 $prefix = "$filename:$linenr: ";
2398 }
Joe Perches34d88152015-06-25 15:03:05 -07002399 }
2400
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002401 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002402 if (is_maintained_obsolete($realfile)) {
2403 WARN("OBSOLETE",
2404 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2405 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002406 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002407 $check = 1;
2408 } else {
2409 $check = $check_orig;
2410 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002411 next;
2412 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002413 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002414
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002415 my $hereline = "$here\n$rawline\n";
2416 my $herecurr = "$here\n$rawline\n";
2417 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002418
David Keitel72bf3e82016-03-23 20:52:44 -07002419 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002420 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2421 # the subject line was just processed,
2422 # a blank line must be next
2423 WARN("NONBLANK_AFTER_SUMMARY",
2424 "non-blank line after summary line\n" . $herecurr);
2425 $shorttext = IN_SHORTTEXT;
2426 # this non-blank line may or may not be commit text -
2427 # a warning has been generated so assume it is commit
2428 # text and move on
2429 $commit_text_present = 1;
2430 # fall through and treat this line as IN_SHORTTEXT
2431 }
David Keitel72bf3e82016-03-23 20:52:44 -07002432 if ($shorttext == IN_SHORTTEXT) {
2433 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002434 if ($commit_text_present == 0) {
2435 WARN("NO_COMMIT_TEXT",
2436 "please add commit text explaining " .
2437 "*why* the change is needed\n" .
2438 $herecurr);
2439 }
David Keitel72bf3e82016-03-23 20:52:44 -07002440 $shorttext = AFTER_SHORTTEXT;
Steve Muckled360fe42012-03-09 11:15:24 -08002441 } elsif ($line=~/^\s*change-id:/i ||
2442 $line=~/^\s*signed-off-by:/i ||
2443 $line=~/^\s*crs-fixed:/i ||
2444 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002445 # this is a tag, there must be commit
2446 # text by now
2447 if ($commit_text_present == 0) {
2448 WARN("NO_COMMIT_TEXT",
2449 "please add commit text explaining " .
2450 "*why* the change is needed\n" .
2451 $herecurr);
2452 # prevent duplicate warnings
2453 $commit_text_present = 1;
2454 }
2455 } elsif ($line=~/\S/) {
2456 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002457 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002458 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2459 # case of non-blank line in this state handled above
2460 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002461 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002462# The Subject line doesn't have to be the last header in the patch.
2463# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2464# Per RFC5322, continuation lines must be folded, so any left-justified
2465# text which looks like a header is definitely a header.
2466 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2467 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002468 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002469 if (length($line) != 0) {
2470 WARN("NONBLANK_AFTER_SUMMARY",
2471 "non-blank line after " .
2472 "summary line\n" .
2473 $sublinenr . $here .
2474 "\n" . $subjectline .
2475 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002476 # this non-blank line may or may not
2477 # be commit text - a warning has been
2478 # generated so assume it is commit
2479 # text and move on
2480 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002481 }
David Keitele288d232016-03-23 20:57:21 -07002482 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002483 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002484 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002485 # This is the subject line. Go to
2486 # CHECK_NEXT_SHORTTEXT to wait for the commit
2487 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002488 $shorttext = CHECK_NEXT_SHORTTEXT;
2489 $subjectline = $line;
2490 $sublinenr = "#$linenr & ";
2491# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002492 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002493 WARN("LONG_SUMMARY_LINE",
2494 "summary line over " .
2495 SHORTTEXT_LIMIT .
2496 " characters\n" . $herecurr);
2497 }
2498 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002499 # Indented format, this must be the summary
2500 # line (i.e. git show). There will be no more
2501 # headers so we are now in the shorttext.
2502 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002503 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002504 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002505 WARN("LONG_SUMMARY_LINE",
2506 "summary line over " .
2507 SHORTTEXT_LIMIT .
2508 " characters\n" . $herecurr);
2509 }
2510 }
2511 }
2512
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002513 $cnt_lines++ if ($realcnt != 0);
2514
Joe Perchese518e9a2015-06-25 15:03:27 -07002515# Check if the commit log has what seems like a diff which can confuse patch
2516 if ($in_commit_log && !$commit_log_has_diff &&
2517 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2518 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2519 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2520 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2521 ERROR("DIFF_IN_COMMIT_MSG",
2522 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2523 $commit_log_has_diff = 1;
2524 }
2525
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002526# Check for incorrect file permissions
2527 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2528 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002529 if ($realfile !~ m@scripts/@ &&
2530 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002531 ERROR("EXECUTE_PERMISSIONS",
2532 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002533 }
2534 }
2535
Joe Perches20112472011-07-25 17:13:23 -07002536# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002537 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002538 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002539 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002540 }
2541
Joe Perchese0d975b2014-12-10 15:51:49 -08002542# Check if MAINTAINERS is being updated. If so, there's probably no need to
2543# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2544 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2545 $reported_maintainer_file = 1;
2546 }
2547
Joe Perches20112472011-07-25 17:13:23 -07002548# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002549 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002550 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002551 my $space_before = $1;
2552 my $sign_off = $2;
2553 my $space_after = $3;
2554 my $email = $4;
2555 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2556
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002557 if ($sign_off !~ /$signature_tags/) {
2558 WARN("BAD_SIGN_OFF",
2559 "Non-standard signature: $sign_off\n" . $herecurr);
2560 }
Joe Perches20112472011-07-25 17:13:23 -07002561 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002562 if (WARN("BAD_SIGN_OFF",
2563 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2564 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002565 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002566 "$ucfirst_sign_off $email";
2567 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002568 }
Joe Perches20112472011-07-25 17:13:23 -07002569 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002570 if (WARN("BAD_SIGN_OFF",
2571 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002573 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002574 "$ucfirst_sign_off $email";
2575 }
2576
Joe Perches20112472011-07-25 17:13:23 -07002577 }
2578 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if (WARN("BAD_SIGN_OFF",
2580 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2581 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002582 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002583 "$ucfirst_sign_off $email";
2584 }
Joe Perches20112472011-07-25 17:13:23 -07002585 }
2586
2587 my ($email_name, $email_address, $comment) = parse_email($email);
2588 my $suggested_email = format_email(($email_name, $email_address));
2589 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002590 ERROR("BAD_SIGN_OFF",
2591 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002592 } else {
2593 my $dequoted = $suggested_email;
2594 $dequoted =~ s/^"//;
2595 $dequoted =~ s/" </ </;
2596 # Don't force email to have quotes
2597 # Allow just an angle bracketed address
2598 if ("$dequoted$comment" ne $email &&
2599 "<$email_address>$comment" ne $email &&
2600 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002601 WARN("BAD_SIGN_OFF",
2602 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002603 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002604 }
Maya Erezec77bd62017-02-26 09:14:43 +02002605 if ($chk_author) {
2606 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2607 $qca_sign_off = 1;
2608 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2609 $codeaurora_sign_off = 1;
2610 } elsif ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
2611 WARN("BAD_SIGN_OFF",
2612 "invalid Signed-off-by identity\n" . $line );
2613 }
2614 }
Joe Perches7e51f192013-09-11 14:23:57 -07002615
2616# Check for duplicate signatures
2617 my $sig_nospace = $line;
2618 $sig_nospace =~ s/\s//g;
2619 $sig_nospace = lc($sig_nospace);
2620 if (defined $signatures{$sig_nospace}) {
2621 WARN("BAD_SIGN_OFF",
2622 "Duplicate signature\n" . $herecurr);
2623 } else {
2624 $signatures{$sig_nospace} = 1;
2625 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002626 }
2627
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002628# Check email subject for common tools that don't need to be mentioned
2629 if ($in_header_lines &&
2630 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2631 WARN("EMAIL_SUBJECT",
2632 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2633 }
2634
Joe Perches9b3189e2014-06-04 16:12:10 -07002635# Check for old stable address
2636 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2637 ERROR("STABLE_ADDRESS",
2638 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2639 }
2640
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002641# Check for unwanted Gerrit info
2642 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2643 ERROR("GERRIT_CHANGE_ID",
2644 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2645 }
2646
Joe Perches369c8dd2015-11-06 16:31:34 -08002647# Check if the commit log is in a possible stack dump
2648 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2649 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2650 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2651 # timestamp
2652 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2653 # stack dump address
2654 $commit_log_possible_stack_dump = 1;
2655 }
2656
Joe Perches2a076f42015-04-16 12:44:28 -07002657# Check for line lengths > 75 in commit log, warn once
2658 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002659 length($line) > 75 &&
2660 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2661 # file delta changes
2662 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2663 # filename then :
2664 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2665 # A Fixes: or Link: line
2666 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002667 WARN("COMMIT_LOG_LONG_LINE",
2668 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2669 $commit_log_long_line = 1;
2670 }
2671
Joe Perchesbf4daf12015-09-09 15:37:50 -07002672# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002673 if ($in_commit_log && $commit_log_possible_stack_dump &&
2674 $line =~ /^\s*$/) {
2675 $commit_log_possible_stack_dump = 0;
2676 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002677
Joe Perches0d7835f2015-02-13 14:38:35 -08002678# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002679 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002680 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wang5b101522017-04-07 15:22:19 -07002681 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002682 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002683 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002684 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2685 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002686 my $init_char = "c";
2687 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002688 my $short = 1;
2689 my $long = 0;
2690 my $case = 1;
2691 my $space = 1;
2692 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002693 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002694 my $id = '0123456789ab';
2695 my $orig_desc = "commit description";
2696 my $description = "";
2697
Joe Perchesfe043ea2015-09-09 15:37:25 -07002698 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2699 $init_char = $1;
2700 $orig_commit = lc($2);
2701 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2702 $orig_commit = lc($1);
2703 }
2704
Joe Perches0d7835f2015-02-13 14:38:35 -08002705 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2706 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2707 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2708 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2709 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2710 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002711 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002712 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2713 defined $rawlines[$linenr] &&
2714 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2715 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002716 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002717 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2718 defined $rawlines[$linenr] &&
2719 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2720 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2721 $orig_desc = $1;
2722 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2723 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002724 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002725 }
2726
2727 ($id, $description) = git_commit_info($orig_commit,
2728 $id, $orig_desc);
2729
Joe Perches19c146a2015-02-13 14:39:00 -08002730 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002731 ERROR("GIT_COMMIT_ID",
2732 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2733 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002734 }
2735
Joe Perches13f19372014-08-06 16:10:59 -07002736# Check for added, moved or deleted files
2737 if (!$reported_maintainer_file && !$in_commit_log &&
2738 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2739 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2740 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2741 (defined($1) || defined($2))))) {
2742 $reported_maintainer_file = 1;
2743 WARN("FILE_PATH_CHANGES",
2744 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2745 }
2746
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002747#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002748 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
2749 $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002750 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2751 }
2752
Andy Whitcroft00df3442007-06-08 13:47:06 -07002753# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002754 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002755 ERROR("CORRUPTED_PATCH",
2756 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002757 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002758 }
2759
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002760# Check for absolute kernel paths.
2761 if ($tree) {
2762 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2763 my $file = $1;
2764
2765 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2766 check_absolute_file($1, $herecurr)) {
2767 #
2768 } else {
2769 check_absolute_file($file, $herecurr);
2770 }
2771 }
2772 }
2773
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002774# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2775 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002776 $rawline !~ m/^$UTF8*$/) {
2777 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2778
2779 my $blank = copy_spacing($rawline);
2780 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2781 my $hereptr = "$hereline$ptr\n";
2782
Joe Perches34d99212011-07-25 17:13:26 -07002783 CHK("INVALID_UTF8",
2784 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002785 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002786
Joe Perches15662b32011-10-31 17:13:12 -07002787# Check if it's the start of a commit log
2788# (not a header line and we haven't seen the patch filename)
2789 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002790 !($rawline =~ /^\s+\S/ ||
2791 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002792 $in_header_lines = 0;
2793 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002794 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002795 }
2796
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002797# Check if there is UTF-8 in a commit log when a mail header has explicitly
2798# declined it, i.e defined some charset where it is missing.
2799 if ($in_header_lines &&
2800 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2801 $1 !~ /utf-8/i) {
2802 $non_utf8_charset = 1;
2803 }
2804
2805 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002806 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002807 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002808 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2809 }
2810
Kees Cook66b47b42014-10-13 15:51:57 -07002811# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002812 if (defined($misspellings) &&
2813 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002814 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002815 my $typo = $1;
2816 my $typo_fix = $spelling_fix{lc($typo)};
2817 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2818 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2819 my $msg_type = \&WARN;
2820 $msg_type = \&CHK if ($file);
2821 if (&{$msg_type}("TYPO_SPELLING",
2822 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2823 $fix) {
2824 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2825 }
2826 }
2827 }
2828
Andy Whitcroft306708542008-10-15 22:02:28 -07002829# ignore non-hunk lines and lines being removed
2830 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002831
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002832#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002833 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002834 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002835 if (ERROR("DOS_LINE_ENDINGS",
2836 "DOS line endings\n" . $herevet) &&
2837 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002838 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002839 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002840 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2841 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002842 if (ERROR("TRAILING_WHITESPACE",
2843 "trailing whitespace\n" . $herevet) &&
2844 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002845 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002846 }
2847
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002848 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002849 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002850
Josh Triplett4783f892013-11-12 15:10:12 -08002851# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002852 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002853 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2854 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002855 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2856 my $msg_type = \&ERROR;
2857 $msg_type = \&CHK if ($file);
2858 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002859 "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 -08002860 }
2861
Andi Kleen33549572010-05-24 14:33:29 -07002862# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002863# Only applies when adding the entry originally, after that we do not have
2864# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002865 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002866 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002867 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002868 my $cnt = $realcnt;
2869 my $ln = $linenr + 1;
2870 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002871 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002872 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002873 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002874 $f = $lines[$ln - 1];
2875 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2876 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002877
2878 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002879 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002880
Joe Perches8d73e0e2014-08-06 16:10:46 -07002881 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002882 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002883 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002884 $length = -1;
2885 }
2886
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002887 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002888 $f =~ s/#.*//;
2889 $f =~ s/^\s+//;
2890 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002891 if ($f =~ /^\s*config\s/) {
2892 $is_end = 1;
2893 last;
2894 }
Andi Kleen33549572010-05-24 14:33:29 -07002895 $length++;
2896 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002897 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2898 WARN("CONFIG_DESCRIPTION",
2899 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2900 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002901 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002902 }
2903
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002904# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2905 if ($realfile =~ /Kconfig/ &&
2906 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2907 WARN("CONFIG_EXPERIMENTAL",
2908 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2909 }
2910
Christoph Jaeger327953e2015-02-13 14:38:29 -08002911# discourage the use of boolean for type definition attributes of Kconfig options
2912 if ($realfile =~ /Kconfig/ &&
2913 $line =~ /^\+\s*\bboolean\b/) {
2914 WARN("CONFIG_TYPE_BOOLEAN",
2915 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2916 }
2917
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002918 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2919 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2920 my $flag = $1;
2921 my $replacement = {
2922 'EXTRA_AFLAGS' => 'asflags-y',
2923 'EXTRA_CFLAGS' => 'ccflags-y',
2924 'EXTRA_CPPFLAGS' => 'cppflags-y',
2925 'EXTRA_LDFLAGS' => 'ldflags-y',
2926 };
2927
2928 WARN("DEPRECATED_VARIABLE",
2929 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2930 }
2931
Rob Herringbff5da42014-01-23 15:54:51 -08002932# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002933 if (defined $root &&
2934 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2935 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2936
Rob Herringbff5da42014-01-23 15:54:51 -08002937 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2938
Florian Vaussardcc933192014-04-03 14:49:27 -07002939 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2940 my $vp_file = $dt_path . "vendor-prefixes.txt";
2941
Rob Herringbff5da42014-01-23 15:54:51 -08002942 foreach my $compat (@compats) {
2943 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002944 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2945 my $compat3 = $compat;
2946 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2947 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002948 if ( $? >> 8 ) {
2949 WARN("UNDOCUMENTED_DT_STRING",
2950 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2951 }
2952
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002953 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2954 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002955 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002956 if ( $? >> 8 ) {
2957 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002958 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002959 }
2960 }
2961 }
2962
Andy Whitcroft5368df22008-10-15 22:02:27 -07002963# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002964 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002965
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002966# do DT checks:
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002967# * Property names should be lower-case
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002968# * Only newline after };
2969
2970 if ($realfile =~ /\.(dts|dtsi)$/ && $line =~ /^\+/) {
2971 if($line =~ /\};.+$/) { # check for any characters after };
2972 ERROR("DT_STYLE", "newline does not follow immediately after };\n" . $herecurr);
2973 }
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002974
2975 if($line =~ /[0-9a-zA-Z,\._\+\?#]+\s*=/ && $line !~ /[0-9a-z,\._\+\?#]+\s*=/) { # find property names with uppercase characters
2976 ERROR("DT_PROPERTY_NAME", "property name is not lowercase\n" . $herecurr);
2977 }
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002978 }
2979
Joe Perches47e0c882015-06-25 15:02:57 -07002980# line length limit (with some exclusions)
2981#
2982# There are a few types of lines that may extend beyond $max_line_length:
2983# logging functions like pr_info that end in a string
2984# lines with a single string
2985# #defines that are a single string
2986#
2987# There are 3 different line length message types:
2988# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2989# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2990# LONG_LINE all other lines longer than $max_line_length
2991#
2992# if LONG_LINE is ignored, the other 2 types are also ignored
2993#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002994 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002995 my $msg_type = "LONG_LINE";
2996
2997 # Check the allowed long line types first
2998
2999 # logging functions that end in a string that starts
3000 # before $max_line_length
3001 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3002 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3003 $msg_type = "";
3004
3005 # lines with only strings (w/ possible termination)
3006 # #defines with only strings
3007 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3008 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3009 $msg_type = "";
3010
Joe Perchesd560a5f2016-08-02 14:04:31 -07003011 # EFI_GUID is another special case
3012 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
3013 $msg_type = "";
3014
Joe Perches47e0c882015-06-25 15:02:57 -07003015 # Otherwise set the alternate message types
3016
3017 # a comment starts before $max_line_length
3018 } elsif ($line =~ /($;[\s$;]*)$/ &&
3019 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3020 $msg_type = "LONG_LINE_COMMENT"
3021
3022 # a quoted string starts before $max_line_length
3023 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3024 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3025 $msg_type = "LONG_LINE_STRING"
3026 }
3027
3028 if ($msg_type ne "" &&
3029 (show_type("LONG_LINE") || show_type($msg_type))) {
3030 WARN($msg_type,
3031 "line over $max_line_length characters\n" . $herecurr);
3032 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003033 }
3034
Andy Whitcroft8905a672007-11-28 16:21:06 -08003035# check for adding lines without a newline.
3036 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003037 WARN("MISSING_EOF_NEWLINE",
3038 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003039 }
3040
Mike Frysinger42e41c52009-09-21 17:04:40 -07003041# Blackfin: use hi/lo macros
3042 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3043 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3044 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003045 ERROR("LO_MACRO",
3046 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003047 }
3048 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3049 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003050 ERROR("HI_MACRO",
3051 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003052 }
3053 }
3054
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003055# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003056 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003057
3058# at the beginning of a line any tabs must come first and anything
3059# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003060 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3061 $rawline =~ /^\+\s* \s*/) {
3062 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003063 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003064 if (ERROR("CODE_INDENT",
3065 "code indent should use tabs where possible\n" . $herevet) &&
3066 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003067 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003068 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003069 }
3070
Alberto Panizzo08e44362010-03-05 13:43:54 -08003071# check for space before tabs.
3072 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3073 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003074 if (WARN("SPACE_BEFORE_TAB",
3075 "please, no space before tabs\n" . $herevet) &&
3076 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003077 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003078 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003079 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003080 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003081 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003082 }
3083
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003084# check for && or || at the start of a line
3085 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3086 CHK("LOGICAL_CONTINUATIONS",
3087 "Logical continuations should be on the previous line\n" . $hereprev);
3088 }
3089
Joe Perchesa91e8992016-05-20 17:04:05 -07003090# check indentation starts on a tab stop
3091 if ($^V && $^V ge 5.10.0 &&
3092 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3093 my $indent = length($1);
3094 if ($indent % 8) {
3095 if (WARN("TABSTOP",
3096 "Statements should start on a tabstop\n" . $herecurr) &&
3097 $fix) {
3098 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3099 }
3100 }
3101 }
3102
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003103# check multi-line statement indentation matches previous line
3104 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003105 $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 -07003106 $prevline =~ /^\+(\t*)(.*)$/;
3107 my $oldindent = $1;
3108 my $rest = $2;
3109
3110 my $pos = pos_last_openparen($rest);
3111 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003112 $line =~ /^(\+| )([ \t]*)/;
3113 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003114
3115 my $goodtabindent = $oldindent .
3116 "\t" x ($pos / 8) .
3117 " " x ($pos % 8);
3118 my $goodspaceindent = $oldindent . " " x $pos;
3119
3120 if ($newindent ne $goodtabindent &&
3121 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003122
3123 if (CHK("PARENTHESIS_ALIGNMENT",
3124 "Alignment should match open parenthesis\n" . $hereprev) &&
3125 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003126 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003127 s/^\+[ \t]*/\+$goodtabindent/;
3128 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003129 }
3130 }
3131 }
3132
Joe Perches6ab3a972015-04-16 12:44:05 -07003133# check for space after cast like "(int) foo" or "(struct foo) bar"
3134# avoid checking a few false positives:
3135# "sizeof(<type>)" or "__alignof__(<type>)"
3136# function pointer declarations like "(*foo)(int) = bar;"
3137# structure definitions like "(struct foo) { 0 };"
3138# multiline macros that define functions
3139# known attributes or the __attribute__ keyword
3140 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3141 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003142 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003143 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003145 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003146 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003147 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003148 }
3149
Joe Perches86406b12015-09-09 15:37:41 -07003150# Block comment styles
3151# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003152 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003153 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003154 $rawline =~ /^\+[ \t]*\*/ &&
3155 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003156 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3157 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3158 }
3159
Joe Perches86406b12015-09-09 15:37:41 -07003160# Block comments use * on subsequent lines
3161 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3162 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003163 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003164 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003165 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003166 WARN("BLOCK_COMMENT_STYLE",
3167 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003168 }
3169
Joe Perches86406b12015-09-09 15:37:41 -07003170# Block comments use */ on trailing lines
3171 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003172 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3173 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3174 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003175 WARN("BLOCK_COMMENT_STYLE",
3176 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003177 }
3178
Joe Perches08eb9b82016-10-11 13:51:50 -07003179# Block comment * alignment
3180 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003181 $line =~ /^\+[ \t]*$;/ && #leading comment
3182 $rawline =~ /^\+[ \t]*\*/ && #leading *
3183 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003184 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003185 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3186 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003187 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003188 if (defined($1)) {
3189 $oldindent = expand_tabs($1);
3190 } else {
3191 $prevrawline =~ m@^\+(.*/?)\*@;
3192 $oldindent = expand_tabs($1);
3193 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003194 $rawline =~ m@^\+([ \t]*)\*@;
3195 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003196 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003197 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003198 WARN("BLOCK_COMMENT_STYLE",
3199 "Block comments should align the * on each line\n" . $hereprev);
3200 }
3201 }
3202
Joe Perches7f619192014-08-06 16:10:39 -07003203# check for missing blank lines after struct/union declarations
3204# with exceptions for various attributes and macros
3205 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3206 $line =~ /^\+/ &&
3207 !($line =~ /^\+\s*$/ ||
3208 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3209 $line =~ /^\+\s*MODULE_/i ||
3210 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3211 $line =~ /^\+[a-z_]*init/ ||
3212 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3213 $line =~ /^\+\s*DECLARE/ ||
3214 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003215 if (CHK("LINE_SPACING",
3216 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3217 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003218 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003219 }
Joe Perches7f619192014-08-06 16:10:39 -07003220 }
3221
Joe Perches365dd4e2014-08-06 16:10:42 -07003222# check for multiple consecutive blank lines
3223 if ($prevline =~ /^[\+ ]\s*$/ &&
3224 $line =~ /^\+\s*$/ &&
3225 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003226 if (CHK("LINE_SPACING",
3227 "Please don't use multiple blank lines\n" . $hereprev) &&
3228 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003229 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003230 }
3231
Joe Perches365dd4e2014-08-06 16:10:42 -07003232 $last_blank_line = $linenr;
3233 }
3234
Joe Perches3b617e32014-04-03 14:49:28 -07003235# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003236 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3237 # actual declarations
3238 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003239 # function pointer declarations
3240 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003241 # foo bar; where foo is some local typedef or #define
3242 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3243 # known declaration macros
3244 $prevline =~ /^\+\s+$declaration_macros/) &&
3245 # for "else if" which can look like "$Ident $Ident"
3246 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3247 # other possible extensions of declaration lines
3248 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3249 # not starting a section or a macro "\" extended line
3250 $prevline =~ /(?:\{\s*|\\)$/) &&
3251 # looks like a declaration
3252 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003253 # function pointer declarations
3254 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003255 # foo bar; where foo is some local typedef or #define
3256 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3257 # known declaration macros
3258 $sline =~ /^\+\s+$declaration_macros/ ||
3259 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003260 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003261 # start or end of block or continuation of declaration
3262 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3263 # bitfield continuation
3264 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3265 # other possible extensions of declaration lines
3266 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3267 # indentation of previous and current line are the same
3268 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003269 if (WARN("LINE_SPACING",
3270 "Missing a blank line after declarations\n" . $hereprev) &&
3271 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003272 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003273 }
Joe Perches3b617e32014-04-03 14:49:28 -07003274 }
3275
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003276# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003277# Exceptions:
3278# 1) within comments
3279# 2) indented preprocessor commands
3280# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003281 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003282 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003283 if (WARN("LEADING_SPACE",
3284 "please, no spaces at the start of a line\n" . $herevet) &&
3285 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003286 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003287 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003288 }
3289
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003290# check we are in a valid C source file if not then ignore this hunk
3291 next if ($realfile !~ /\.(h|c)$/);
3292
Joe Perches032a4c02014-08-06 16:10:29 -07003293# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003294# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003295# if the previous line is a break or return and is indented 1 tab more...
3296 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3297 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003298 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3299 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3300 defined $lines[$linenr] &&
3301 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003302 WARN("UNNECESSARY_ELSE",
3303 "else is not generally useful after a break or return\n" . $hereprev);
3304 }
3305 }
3306
Joe Perchesc00df192014-08-06 16:11:01 -07003307# check indentation of a line with a break;
3308# if the previous line is a goto or return and is indented the same # of tabs
3309 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3310 my $tabs = $1;
3311 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3312 WARN("UNNECESSARY_BREAK",
3313 "break is not useful after a goto or return\n" . $hereprev);
3314 }
3315 }
3316
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003317# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3318 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3319 WARN("CONFIG_EXPERIMENTAL",
3320 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3321 }
3322
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003323# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003324 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003325 WARN("CVS_KEYWORD",
3326 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003327 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003328
Mike Frysinger42e41c52009-09-21 17:04:40 -07003329# Blackfin: don't use __builtin_bfin_[cs]sync
3330 if ($line =~ /__builtin_bfin_csync/) {
3331 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003332 ERROR("CSYNC",
3333 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003334 }
3335 if ($line =~ /__builtin_bfin_ssync/) {
3336 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003337 ERROR("SSYNC",
3338 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003339 }
3340
Joe Perches56e77d72013-02-21 16:44:14 -08003341# check for old HOTPLUG __dev<foo> section markings
3342 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3343 WARN("HOTPLUG_SECTION",
3344 "Using $1 is unnecessary\n" . $herecurr);
3345 }
3346
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003347# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003348 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3349 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003350#print "LINE<$line>\n";
3351 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003352 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003353 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003354 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003355 $stat =~ s/\n./\n /g;
3356 $cond =~ s/\n./\n /g;
3357
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003358#print "linenr<$linenr> <$stat>\n";
3359 # If this statement has no statement boundaries within
3360 # it there is no point in retrying a statement scan
3361 # until we hit end of it.
3362 my $frag = $stat; $frag =~ s/;+\s*$//;
3363 if ($frag !~ /(?:{|;)/) {
3364#print "skip<$line_nr_next>\n";
3365 $suppress_statement = $line_nr_next;
3366 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003367
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003368 # Find the real next line.
3369 $realline_next = $line_nr_next;
3370 if (defined $realline_next &&
3371 (!defined $lines[$realline_next - 1] ||
3372 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3373 $realline_next++;
3374 }
3375
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003376 my $s = $stat;
3377 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003378
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003379 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003380 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003381
3382 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003383 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003384
Andy Whitcroft463f2862009-09-21 17:04:34 -07003385 } elsif ($s =~ /^.\s*else\b/s) {
3386
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003387 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003388 } 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 -07003389 my $type = $1;
3390 $type =~ s/\s+/ /g;
3391 possible($type, "A:" . $s);
3392
Andy Whitcroft8905a672007-11-28 16:21:06 -08003393 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003394 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003395 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003396 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003397
3398 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003399 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003400 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003401 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402
3403 # Check for any sort of function declaration.
3404 # int foo(something bar, other baz);
3405 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003406 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 -08003407 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003408
Andy Whitcroftcf655042008-03-04 14:28:20 -08003409 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003410 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003411 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003412
Andy Whitcroft8905a672007-11-28 16:21:06 -08003413 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003414 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003415
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003416 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003417 }
3418 }
3419 }
3420
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003421 }
3422
Andy Whitcroft653d4872007-06-23 17:16:34 -07003423#
3424# Checks which may be anchored in the context.
3425#
3426
3427# Check for switch () and associated case and default
3428# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003429 if ($line=~/\bswitch\s*\(.*\)/) {
3430 my $err = '';
3431 my $sep = '';
3432 my @ctx = ctx_block_outer($linenr, $realcnt);
3433 shift(@ctx);
3434 for my $ctx (@ctx) {
3435 my ($clen, $cindent) = line_stats($ctx);
3436 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3437 $indent != $cindent) {
3438 $err .= "$sep$ctx\n";
3439 $sep = '';
3440 } else {
3441 $sep = "[...]\n";
3442 }
3443 }
3444 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003445 ERROR("SWITCH_CASE_INDENT_LEVEL",
3446 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003447 }
3448 }
3449
3450# if/while/etc brace do not go on next line, unless defining a do while loop,
3451# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003452 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 -07003453 my $pre_ctx = "$1$2";
3454
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003455 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003456
3457 if ($line =~ /^\+\t{6,}/) {
3458 WARN("DEEP_INDENTATION",
3459 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3460 }
3461
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003462 my $ctx_cnt = $realcnt - $#ctx - 1;
3463 my $ctx = join("\n", @ctx);
3464
Andy Whitcroft548596d2008-07-23 21:29:01 -07003465 my $ctx_ln = $linenr;
3466 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003467
Andy Whitcroft548596d2008-07-23 21:29:01 -07003468 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3469 defined $lines[$ctx_ln - 1] &&
3470 $lines[$ctx_ln - 1] =~ /^-/)) {
3471 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3472 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003473 $ctx_ln++;
3474 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003475
Andy Whitcroft53210162008-07-23 21:29:03 -07003476 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3477 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003478
Joe Perchesd752fcc2014-08-06 16:11:05 -07003479 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003480 ERROR("OPEN_BRACE",
3481 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003482 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003483 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003484 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3485 $ctx =~ /\)\s*\;\s*$/ &&
3486 defined $lines[$ctx_ln - 1])
3487 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003488 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3489 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003490 WARN("TRAILING_SEMICOLON",
3491 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003492 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003493 }
3494 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003495 }
3496
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003497# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003498 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 -08003499 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3500 ctx_statement_block($linenr, $realcnt, 0)
3501 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003502 my ($s, $c) = ($stat, $cond);
3503
3504 substr($s, 0, length($c), '');
3505
Joe Perches9f5af482015-09-09 15:37:30 -07003506 # remove inline comments
3507 $s =~ s/$;/ /g;
3508 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003509
3510 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003511 my @newlines = ($c =~ /\n/gs);
3512 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003513
Joe Perches9f5af482015-09-09 15:37:30 -07003514 # Make sure we remove the line prefixes as we have
3515 # none on the first line, and are going to readd them
3516 # where necessary.
3517 $s =~ s/\n./\n/gs;
3518 while ($s =~ /\n\s+\\\n/) {
3519 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3520 }
3521
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003522 # We want to check the first line inside the block
3523 # starting at the end of the conditional, so remove:
3524 # 1) any blank line termination
3525 # 2) any opening brace { on end of the line
3526 # 3) any do (...) {
3527 my $continuation = 0;
3528 my $check = 0;
3529 $s =~ s/^.*\bdo\b//;
3530 $s =~ s/^\s*{//;
3531 if ($s =~ s/^\s*\\//) {
3532 $continuation = 1;
3533 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003534 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003535 $check = 1;
3536 $cond_lines++;
3537 }
3538
3539 # Also ignore a loop construct at the end of a
3540 # preprocessor statement.
3541 if (($prevline =~ /^.\s*#\s*define\s/ ||
3542 $prevline =~ /\\\s*$/) && $continuation == 0) {
3543 $check = 0;
3544 }
3545
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003546 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003547 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003548 while ($cond_ptr != $cond_lines) {
3549 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003550
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003551 # If we see an #else/#elif then the code
3552 # is not linear.
3553 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3554 $check = 0;
3555 }
3556
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003557 # Ignore:
3558 # 1) blank lines, they should be at 0,
3559 # 2) preprocessor lines, and
3560 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003561 if ($continuation ||
3562 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003563 $s =~ /^\s*#\s*?/ ||
3564 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003565 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003566 if ($s =~ s/^.*?\n//) {
3567 $cond_lines++;
3568 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003569 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003570 }
3571
3572 my (undef, $sindent) = line_stats("+" . $s);
3573 my $stat_real = raw_line($linenr, $cond_lines);
3574
3575 # Check if either of these lines are modified, else
3576 # this is not this patch's fault.
3577 if (!defined($stat_real) ||
3578 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3579 $check = 0;
3580 }
3581 if (defined($stat_real) && $cond_lines > 1) {
3582 $stat_real = "[...]\n$stat_real";
3583 }
3584
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003585 #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 -07003586
Joe Perches9f5af482015-09-09 15:37:30 -07003587 if ($check && $s ne '' &&
3588 (($sindent % 8) != 0 ||
3589 ($sindent < $indent) ||
3590 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003591 WARN("SUSPECT_CODE_INDENT",
3592 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003593 }
3594 }
3595
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003596 # Track the 'values' across context and added lines.
3597 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003598 my ($curr_values, $curr_vars) =
3599 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003600 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003601 if ($dbg_values) {
3602 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003603 print "$linenr > .$outline\n";
3604 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003605 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003606 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003607 $prev_values = substr($curr_values, -1);
3608
Andy Whitcroft00df3442007-06-08 13:47:06 -07003609#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003610 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003611
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003612# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003613 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 -07003614 my $type = $1;
3615 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003616 $var = "" if (!defined $var);
3617 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003618 my $sign = $1;
3619 my $pointer = $2;
3620
3621 $pointer = "" if (!defined $pointer);
3622
3623 if (WARN("UNSPECIFIED_INT",
3624 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3625 $fix) {
3626 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003627 my $comp_pointer = $pointer;
3628 $comp_pointer =~ s/\s//g;
3629 $decl .= $comp_pointer;
3630 $decl = rtrim($decl) if ($var eq "");
3631 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003632 }
3633 }
3634 }
3635
Andy Whitcroft653d4872007-06-23 17:16:34 -07003636# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003637 if ($dbg_type) {
3638 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003639 ERROR("TEST_TYPE",
3640 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003641 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003642 ERROR("TEST_NOT_TYPE",
3643 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003644 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003645 next;
3646 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003647# TEST: allow direct testing of the attribute matcher.
3648 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003649 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003650 ERROR("TEST_ATTR",
3651 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003652 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003653 ERROR("TEST_NOT_ATTR",
3654 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003655 }
3656 next;
3657 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003658
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003659# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003660 if ($line =~ /^.\s*{/ &&
3661 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003662 if (ERROR("OPEN_BRACE",
3663 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003664 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3665 fix_delete_line($fixlinenr - 1, $prevrawline);
3666 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003667 my $fixedline = $prevrawline;
3668 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003669 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003670 $fixedline = $line;
Cyril Bur93eae952017-07-10 15:52:21 -07003671 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003672 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003673 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003674 }
3675
Andy Whitcroft653d4872007-06-23 17:16:34 -07003676#
3677# Checks which are anchored on the added line.
3678#
3679
3680# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003681 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003682 my $path = $1;
3683 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003684 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003685 "malformed #include filename\n" . $herecurr);
3686 }
3687 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3688 ERROR("UAPI_INCLUDE",
3689 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003690 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003691 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003692
3693# no C99 // comments
3694 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003695 if (ERROR("C99_COMMENTS",
3696 "do not use C99 // comments\n" . $herecurr) &&
3697 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003698 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003699 if ($line =~ /\/\/(.*)$/) {
3700 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003701 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003702 }
3703 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003704 }
3705 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003706 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003707 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003708
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003709# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3710# the whole statement.
3711#print "APW <$lines[$realline_next - 1]>\n";
3712 if (defined $realline_next &&
3713 exists $lines[$realline_next - 1] &&
3714 !defined $suppress_export{$realline_next} &&
3715 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3716 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003717 # Handle definitions which produce identifiers with
3718 # a prefix:
3719 # XXX(foo);
3720 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003721 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003722 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003723 $name =~ /^${Ident}_$2/) {
3724#print "FOO C name<$name>\n";
3725 $suppress_export{$realline_next} = 1;
3726
3727 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003728 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003729 ^.DEFINE_$Ident\(\Q$name\E\)|
3730 ^.DECLARE_$Ident\(\Q$name\E\)|
3731 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003732 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3733 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003734 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003735#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3736 $suppress_export{$realline_next} = 2;
3737 } else {
3738 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003739 }
3740 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003741 if (!defined $suppress_export{$linenr} &&
3742 $prevline =~ /^.\s*$/ &&
3743 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3744 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3745#print "FOO B <$lines[$linenr - 1]>\n";
3746 $suppress_export{$linenr} = 2;
3747 }
3748 if (defined $suppress_export{$linenr} &&
3749 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003750 WARN("EXPORT_SYMBOL",
3751 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003752 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003753
Joe Eloff5150bda2010-08-09 17:21:00 -07003754# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003755 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003756 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003757 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003758 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003759 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003760 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003761 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003762# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003763 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003764 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003765 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003766 $herecurr) &&
3767 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003768 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003769 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003770 }
3771
Joe Perches18130872014-08-06 16:11:22 -07003772# check for misordered declarations of char/short/int/long with signed/unsigned
3773 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3774 my $tmp = trim($1);
3775 WARN("MISORDERED_TYPE",
3776 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3777 }
3778
Joe Perchescb710ec2010-10-26 14:23:20 -07003779# check for static const char * arrays.
3780 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003781 WARN("STATIC_CONST_CHAR_ARRAY",
3782 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003783 $herecurr);
3784 }
3785
3786# check for static char foo[] = "bar" declarations.
3787 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003788 WARN("STATIC_CONST_CHAR_ARRAY",
3789 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003790 $herecurr);
3791 }
3792
Joe Perchesab7e23f2015-04-16 12:44:22 -07003793# check for const <foo> const where <foo> is not a pointer or array type
3794 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3795 my $found = $1;
3796 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3797 WARN("CONST_CONST",
3798 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3799 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3800 WARN("CONST_CONST",
3801 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3802 }
3803 }
3804
Joe Perches9b0fa602014-04-03 14:49:18 -07003805# check for non-global char *foo[] = {"bar", ...} declarations.
3806 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3807 WARN("STATIC_CONST_CHAR_ARRAY",
3808 "char * array declaration might be better as static const\n" .
3809 $herecurr);
3810 }
3811
Joe Perchesb598b672015-04-16 12:44:36 -07003812# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3813 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3814 my $array = $1;
3815 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3816 my $array_div = $1;
3817 if (WARN("ARRAY_SIZE",
3818 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3819 $fix) {
3820 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3821 }
3822 }
3823 }
3824
Joe Perchesb36190c2014-01-27 17:07:18 -08003825# check for function declarations without arguments like "int foo()"
3826 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3827 if (ERROR("FUNCTION_WITHOUT_ARGS",
3828 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003830 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003831 }
3832 }
3833
Andy Whitcroft653d4872007-06-23 17:16:34 -07003834# check for new typedefs, only function parameters and sparse annotations
3835# make sense.
3836 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003837 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003838 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003839 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003840 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003841 WARN("NEW_TYPEDEFS",
3842 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003843 }
3844
3845# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003846 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003847 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3848 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003849 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003850
Andy Whitcroft65863862009-01-06 14:41:21 -08003851 # Should start with a space.
3852 $to =~ s/^(\S)/ $1/;
3853 # Should not end with a space.
3854 $to =~ s/\s+$//;
3855 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003856 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003857 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003858
Joe Perches3705ce52013-07-03 15:05:31 -07003859## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003860 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003861 if (ERROR("POINTER_LOCATION",
3862 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3863 $fix) {
3864 my $sub_from = $ident;
3865 my $sub_to = $ident;
3866 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003867 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003868 s@\Q$sub_from\E@$sub_to@;
3869 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003870 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003871 }
3872 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3873 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003874 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003875
Andy Whitcroft65863862009-01-06 14:41:21 -08003876 # Should start with a space.
3877 $to =~ s/^(\S)/ $1/;
3878 # Should not end with a space.
3879 $to =~ s/\s+$//;
3880 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003881 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003882 }
3883 # Modifiers should have spaces.
3884 $to =~ s/(\b$Modifier$)/$1 /;
3885
Joe Perches3705ce52013-07-03 15:05:31 -07003886## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003887 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003888 if (ERROR("POINTER_LOCATION",
3889 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3890 $fix) {
3891
3892 my $sub_from = $match;
3893 my $sub_to = $match;
3894 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003895 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003896 s@\Q$sub_from\E@$sub_to@;
3897 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003898 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003899 }
3900
Joe Perches9d3e3c72015-09-09 15:37:27 -07003901# avoid BUG() or BUG_ON()
3902 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3903 my $msg_type = \&WARN;
3904 $msg_type = \&CHK if ($file);
3905 &{$msg_type}("AVOID_BUG",
3906 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3907 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003908
Joe Perches9d3e3c72015-09-09 15:37:27 -07003909# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003910 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003911 WARN("LINUX_VERSION_CODE",
3912 "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 -08003913 }
3914
Joe Perches17441222011-06-15 15:08:17 -07003915# check for uses of printk_ratelimit
3916 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003918 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003919 }
3920
Andy Whitcroft00df3442007-06-08 13:47:06 -07003921# printk should use KERN_* levels. Note that follow on printk's on the
3922# same line do not need a level, so we use the current block context
3923# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003924# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003925# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003926 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003927 my $ok = 0;
3928 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3929 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003930 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003931 # with "\n" ignore it, else it is to blame
3932 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3933 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3934 $ok = 1;
3935 }
3936 last;
3937 }
3938 }
3939 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003940 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3941 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003942 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003943 }
3944
Joe Perches243f3802012-05-31 16:26:09 -07003945 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3946 my $orig = $1;
3947 my $level = lc($orig);
3948 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003949 my $level2 = $level;
3950 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003951 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003952 "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 -07003953 }
3954
3955 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003956 if (WARN("PREFER_PR_LEVEL",
3957 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3958 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003959 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003960 s/\bpr_warning\b/pr_warn/;
3961 }
Joe Perches243f3802012-05-31 16:26:09 -07003962 }
3963
Joe Perchesdc139312013-02-21 16:44:13 -08003964 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3965 my $orig = $1;
3966 my $level = lc($orig);
3967 $level = "warn" if ($level eq "warning");
3968 $level = "dbg" if ($level eq "debug");
3969 WARN("PREFER_DEV_LEVEL",
3970 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3971 }
3972
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003973# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3974# number of false positives, but assembly files are not checked, so at
3975# least the arch entry code will not trigger this warning.
3976 if ($line =~ /\bENOSYS\b/) {
3977 WARN("ENOSYS",
3978 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3979 }
3980
Andy Whitcroft653d4872007-06-23 17:16:34 -07003981# function brace can't be on same line, except for #defines of do while,
3982# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003983 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003984 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003985 if (ERROR("OPEN_BRACE",
3986 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3987 $fix) {
3988 fix_delete_line($fixlinenr, $rawline);
3989 my $fixed_line = $rawline;
3990 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3991 my $line1 = $1;
3992 my $line2 = $2;
3993 fix_insert_line($fixlinenr, ltrim($line1));
3994 fix_insert_line($fixlinenr, "\+{");
3995 if ($line2 !~ /^\s*$/) {
3996 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3997 }
3998 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003999 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004000
Andy Whitcroft8905a672007-11-28 16:21:06 -08004001# open braces for enum, union and struct go on the same line.
4002 if ($line =~ /^.\s*{/ &&
4003 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004004 if (ERROR("OPEN_BRACE",
4005 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4006 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4007 fix_delete_line($fixlinenr - 1, $prevrawline);
4008 fix_delete_line($fixlinenr, $rawline);
4009 my $fixedline = rtrim($prevrawline) . " {";
4010 fix_insert_line($fixlinenr, $fixedline);
4011 $fixedline = $rawline;
Cyril Bur93eae952017-07-10 15:52:21 -07004012 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004013 if ($fixedline !~ /^\+\s*$/) {
4014 fix_insert_line($fixlinenr, $fixedline);
4015 }
4016 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004017 }
4018
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004019# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004020 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4021 if (WARN("SPACING",
4022 "missing space after $1 definition\n" . $herecurr) &&
4023 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004024 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004025 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4026 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004027 }
4028
Joe Perches31070b52014-01-23 15:54:49 -08004029# Function pointer declarations
4030# check spacing between type, funcptr, and args
4031# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004032 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004033 my $declare = $1;
4034 my $pre_pointer_space = $2;
4035 my $post_pointer_space = $3;
4036 my $funcname = $4;
4037 my $post_funcname_space = $5;
4038 my $pre_args_space = $6;
4039
Joe Perches91f72e92014-04-03 14:49:12 -07004040# the $Declare variable will capture all spaces after the type
4041# so check it for a missing trailing missing space but pointer return types
4042# don't need a space so don't warn for those.
4043 my $post_declare_space = "";
4044 if ($declare =~ /(\s+)$/) {
4045 $post_declare_space = $1;
4046 $declare = rtrim($declare);
4047 }
4048 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004049 WARN("SPACING",
4050 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004051 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004052 }
4053
4054# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004055# This test is not currently implemented because these declarations are
4056# equivalent to
4057# int foo(int bar, ...)
4058# and this is form shouldn't/doesn't generate a checkpatch warning.
4059#
4060# elsif ($declare =~ /\s{2,}$/) {
4061# WARN("SPACING",
4062# "Multiple spaces after return type\n" . $herecurr);
4063# }
Joe Perches31070b52014-01-23 15:54:49 -08004064
4065# unnecessary space "type ( *funcptr)(args...)"
4066 if (defined $pre_pointer_space &&
4067 $pre_pointer_space =~ /^\s/) {
4068 WARN("SPACING",
4069 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4070 }
4071
4072# unnecessary space "type (* funcptr)(args...)"
4073 if (defined $post_pointer_space &&
4074 $post_pointer_space =~ /^\s/) {
4075 WARN("SPACING",
4076 "Unnecessary space before function pointer name\n" . $herecurr);
4077 }
4078
4079# unnecessary space "type (*funcptr )(args...)"
4080 if (defined $post_funcname_space &&
4081 $post_funcname_space =~ /^\s/) {
4082 WARN("SPACING",
4083 "Unnecessary space after function pointer name\n" . $herecurr);
4084 }
4085
4086# unnecessary space "type (*funcptr) (args...)"
4087 if (defined $pre_args_space &&
4088 $pre_args_space =~ /^\s/) {
4089 WARN("SPACING",
4090 "Unnecessary space before function pointer arguments\n" . $herecurr);
4091 }
4092
4093 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004094 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004095 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004096 }
4097 }
4098
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004099# check for spacing round square brackets; allowed:
4100# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004101# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4102# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004103 while ($line =~ /(.*?\s)\[/g) {
4104 my ($where, $prefix) = ($-[1], $1);
4105 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004106 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004107 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004108 if (ERROR("BRACKET_SPACE",
4109 "space prohibited before open square bracket '['\n" . $herecurr) &&
4110 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004111 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004112 s/^(\+.*?)\s+\[/$1\[/;
4113 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004114 }
4115 }
4116
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004117# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004118 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004119 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004120 my $ctx_before = substr($line, 0, $-[1]);
4121 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004122
4123 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004124 if ($name =~ /^(?:
4125 if|for|while|switch|return|case|
4126 volatile|__volatile__|
4127 __attribute__|format|__extension__|
4128 asm|__asm__)$/x)
4129 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004130 # cpp #define statements have non-optional spaces, ie
4131 # if there is a space between the name and the open
4132 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004133 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004134
4135 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004136 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004137
4138 # If this whole things ends with a type its most
4139 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004140 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004141
4142 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004143 if (WARN("SPACING",
4144 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4145 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004146 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004147 s/\b$name\s+\(/$name\(/;
4148 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004149 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004150 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004151
Andy Whitcroft653d4872007-06-23 17:16:34 -07004152# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004153 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004154 my $fixed_line = "";
4155 my $line_fixed = 0;
4156
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004157 my $ops = qr{
4158 <<=|>>=|<=|>=|==|!=|
4159 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4160 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004161 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004162 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004163 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004164 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004165
4166## print("element count: <" . $#elements . ">\n");
4167## foreach my $el (@elements) {
4168## print("el: <$el>\n");
4169## }
4170
4171 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004172 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004173
Joe Perches3705ce52013-07-03 15:05:31 -07004174 foreach my $el (@elements) {
4175 push(@fix_elements, substr($rawline, $off, length($el)));
4176 $off += length($el);
4177 }
4178
4179 $off = 0;
4180
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004181 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004182 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004183
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004184 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004185
4186 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4187
4188## print("n: <$n> good: <$good>\n");
4189
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004190 $off += length($elements[$n]);
4191
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004192 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004193 my $ca = substr($opline, 0, $off);
4194 my $cc = '';
4195 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4196 $cc = substr($opline, $off + length($elements[$n + 1]));
4197 }
4198 my $cb = "$ca$;$cc";
4199
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004200 my $a = '';
4201 $a = 'V' if ($elements[$n] ne '');
4202 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004203 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004204 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4205 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004206 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004207
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004208 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004209
4210 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004211 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004212 $c = 'V' if ($elements[$n + 2] ne '');
4213 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004214 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004215 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4216 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004217 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004218 } else {
4219 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004220 }
4221
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004222 my $ctx = "${a}x${c}";
4223
4224 my $at = "(ctx:$ctx)";
4225
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004226 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004227 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004228
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004229 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004230 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004231
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004232 # Get the full operator variant.
4233 my $opv = $op . substr($curr_vars, $off, 1);
4234
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004235 # Ignore operators passed as parameters.
4236 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004237 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004238
Andy Whitcroftcf655042008-03-04 14:28:20 -08004239# # Ignore comments
4240# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004241
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004242 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004243 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004244 if ($ctx !~ /.x[WEBC]/ &&
4245 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004246 if (ERROR("SPACING",
4247 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004248 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004249 $line_fixed = 1;
4250 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004251 }
4252
4253 # // is a comment
4254 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004255
Joe Perchesb00e4812014-04-03 14:49:33 -07004256 # : when part of a bitfield
4257 } elsif ($opv eq ':B') {
4258 # skip the bitfield test for now
4259
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004260 # No spaces for:
4261 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004262 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004263 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004264 if (ERROR("SPACING",
4265 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004266 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004267 if (defined $fix_elements[$n + 2]) {
4268 $fix_elements[$n + 2] =~ s/^\s+//;
4269 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004270 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004271 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004272 }
4273
Joe Perches23810972014-12-10 15:51:32 -08004274 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004275 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004276 my $rtrim_before = 0;
4277 my $space_after = 0;
4278 if ($ctx =~ /Wx./) {
4279 if (ERROR("SPACING",
4280 "space prohibited before that '$op' $at\n" . $hereptr)) {
4281 $line_fixed = 1;
4282 $rtrim_before = 1;
4283 }
4284 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004285 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004286 if (ERROR("SPACING",
4287 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004288 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004289 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004290 $space_after = 1;
4291 }
4292 }
4293 if ($rtrim_before || $space_after) {
4294 if ($rtrim_before) {
4295 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4296 } else {
4297 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4298 }
4299 if ($space_after) {
4300 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004301 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004302 }
4303
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004304 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004305 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004306 #warn "'*' is part of type\n";
4307
4308 # unary operators should have a space before and
4309 # none after. May be left adjacent to another
4310 # unary operator, or a cast
4311 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004312 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004313 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004314 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004315 if (ERROR("SPACING",
4316 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004317 if ($n != $last_after + 2) {
4318 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4319 $line_fixed = 1;
4320 }
Joe Perches3705ce52013-07-03 15:05:31 -07004321 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004322 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004323 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004324 # A unary '*' may be const
4325
4326 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004327 if (ERROR("SPACING",
4328 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004329 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004330 if (defined $fix_elements[$n + 2]) {
4331 $fix_elements[$n + 2] =~ s/^\s+//;
4332 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004333 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004334 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004335 }
4336
4337 # unary ++ and unary -- are allowed no space on one side.
4338 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004340 if (ERROR("SPACING",
4341 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004342 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004343 $line_fixed = 1;
4344 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004345 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004346 if ($ctx =~ /Wx[BE]/ ||
4347 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004348 if (ERROR("SPACING",
4349 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004350 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004351 $line_fixed = 1;
4352 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004353 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004354 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004355 if (ERROR("SPACING",
4356 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004357 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004358 if (defined $fix_elements[$n + 2]) {
4359 $fix_elements[$n + 2] =~ s/^\s+//;
4360 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004361 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004362 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004363 }
4364
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004365 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004366 } elsif ($op eq '<<' or $op eq '>>' or
4367 $op eq '&' or $op eq '^' or $op eq '|' or
4368 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004369 $op eq '*' or $op eq '/' or
4370 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004371 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004372 if ($check) {
4373 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4374 if (CHK("SPACING",
4375 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4376 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4377 $fix_elements[$n + 2] =~ s/^\s+//;
4378 $line_fixed = 1;
4379 }
4380 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4381 if (CHK("SPACING",
4382 "space preferred before that '$op' $at\n" . $hereptr)) {
4383 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4384 $line_fixed = 1;
4385 }
4386 }
4387 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004388 if (ERROR("SPACING",
4389 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004390 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4391 if (defined $fix_elements[$n + 2]) {
4392 $fix_elements[$n + 2] =~ s/^\s+//;
4393 }
Joe Perches3705ce52013-07-03 15:05:31 -07004394 $line_fixed = 1;
4395 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004396 }
4397
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004398 # A colon needs no spaces before when it is
4399 # terminating a case value or a label.
4400 } elsif ($opv eq ':C' || $opv eq ':L') {
4401 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004402 if (ERROR("SPACING",
4403 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004404 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004405 $line_fixed = 1;
4406 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004407 }
4408
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004409 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004410 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004411 my $ok = 0;
4412
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004413 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004414 if (($op eq '<' &&
4415 $cc =~ /^\S+\@\S+>/) ||
4416 ($op eq '>' &&
4417 $ca =~ /<\S+\@\S+$/))
4418 {
4419 $ok = 1;
4420 }
4421
Joe Perchese0df7e12015-04-16 12:44:53 -07004422 # for asm volatile statements
4423 # ignore a colon with another
4424 # colon immediately before or after
4425 if (($op eq ':') &&
4426 ($ca =~ /:$/ || $cc =~ /^:/)) {
4427 $ok = 1;
4428 }
4429
Joe Perches84731622013-11-12 15:10:05 -08004430 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004431 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004432 my $msg_type = \&ERROR;
4433 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4434
4435 if (&{$msg_type}("SPACING",
4436 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004437 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4438 if (defined $fix_elements[$n + 2]) {
4439 $fix_elements[$n + 2] =~ s/^\s+//;
4440 }
Joe Perches3705ce52013-07-03 15:05:31 -07004441 $line_fixed = 1;
4442 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004443 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004444 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004445 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004446
4447## print("n: <$n> GOOD: <$good>\n");
4448
4449 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004450 }
Joe Perches3705ce52013-07-03 15:05:31 -07004451
4452 if (($#elements % 2) == 0) {
4453 $fixed_line = $fixed_line . $fix_elements[$#elements];
4454 }
4455
Joe Perches194f66f2014-08-06 16:11:03 -07004456 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4457 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004458 }
4459
4460
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004461 }
4462
Joe Perches786b6322013-07-03 15:05:32 -07004463# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004464 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004465 if (WARN("SPACING",
4466 "space prohibited before semicolon\n" . $herecurr) &&
4467 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004468 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004469 s/^(\+.*\S)\s+;/$1;/;
4470 }
4471 }
4472
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004473# check for multiple assignments
4474 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004475 CHK("MULTIPLE_ASSIGNMENTS",
4476 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004477 }
4478
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004479## # check for multiple declarations, allowing for a function declaration
4480## # continuation.
4481## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4482## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4483##
4484## # Remove any bracketed sections to ensure we do not
4485## # falsly report the parameters of functions.
4486## my $ln = $line;
4487## while ($ln =~ s/\([^\(\)]*\)//g) {
4488## }
4489## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004490## WARN("MULTIPLE_DECLARATION",
4491## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004492## }
4493## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004494
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004495#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004496 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004497 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004498 if (ERROR("SPACING",
4499 "space required before the open brace '{'\n" . $herecurr) &&
4500 $fix) {
Cyril Bur93eae952017-07-10 15:52:21 -07004501 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004502 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004503 }
4504
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004505## # check for blank lines before declarations
4506## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4507## $prevrawline =~ /^.\s*$/) {
4508## WARN("SPACING",
4509## "No blank lines before declarations\n" . $hereprev);
4510## }
4511##
4512
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004513# closing brace should have a space following it when it has anything
4514# on the line
4515 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004516 if (ERROR("SPACING",
4517 "space required after that close brace '}'\n" . $herecurr) &&
4518 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004519 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004520 s/}((?!(?:,|;|\)))\S)/} $1/;
4521 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004522 }
4523
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004524# check spacing on square brackets
4525 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004526 if (ERROR("SPACING",
4527 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4528 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004529 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004530 s/\[\s+/\[/;
4531 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004532 }
4533 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004534 if (ERROR("SPACING",
4535 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4536 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004537 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004538 s/\s+\]/\]/;
4539 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004540 }
4541
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004542# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004543 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004544 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004545 if (ERROR("SPACING",
4546 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004549 s/\(\s+/\(/;
4550 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004551 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004552 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004553 $line !~ /for\s*\(.*;\s+\)/ &&
4554 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004555 if (ERROR("SPACING",
4556 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4557 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004558 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004559 s/\s+\)/\)/;
4560 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004561 }
4562
Joe Perchese2826fd2014-08-06 16:10:48 -07004563# check unnecessary parentheses around addressof/dereference single $Lvals
4564# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4565
4566 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004567 my $var = $1;
4568 if (CHK("UNNECESSARY_PARENTHESES",
4569 "Unnecessary parentheses around $var\n" . $herecurr) &&
4570 $fix) {
4571 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4572 }
4573 }
4574
4575# check for unnecessary parentheses around function pointer uses
4576# ie: (foo->bar)(); should be foo->bar();
4577# but not "if (foo->bar) (" to avoid some false positives
4578 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4579 my $var = $2;
4580 if (CHK("UNNECESSARY_PARENTHESES",
4581 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4582 $fix) {
4583 my $var2 = deparenthesize($var);
4584 $var2 =~ s/\s//g;
4585 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4586 }
4587 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004588
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004589#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004590 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004591 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004592 if (WARN("INDENTED_LABEL",
4593 "labels should not be indented\n" . $herecurr) &&
4594 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004595 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004596 s/^(.)\s+/$1/;
4597 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004598 }
4599
Joe Perches5b9553a2014-04-03 14:49:21 -07004600# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004601 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004602 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004603 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004604 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4605 my $value = $1;
4606 $value = deparenthesize($value);
4607 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4608 ERROR("RETURN_PARENTHESES",
4609 "return is not a function, parentheses are not required\n" . $herecurr);
4610 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004611 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004612 ERROR("SPACING",
4613 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004614 }
4615 }
Joe Perches507e5142013-11-12 15:10:13 -08004616
Joe Perchesb43ae212014-06-23 13:22:07 -07004617# unnecessary return in a void function
4618# at end-of-function, with the previous line a single leading tab, then return;
4619# and the line before that not a goto label target like "out:"
4620 if ($sline =~ /^[ \+]}\s*$/ &&
4621 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4622 $linenr >= 3 &&
4623 $lines[$linenr - 3] =~ /^[ +]/ &&
4624 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004625 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004626 "void function return statements are not generally useful\n" . $hereprev);
4627 }
Joe Perches9819cf22014-06-04 16:12:09 -07004628
Joe Perches189248d2014-01-23 15:54:47 -08004629# if statements using unnecessary parentheses - ie: if ((foo == bar))
4630 if ($^V && $^V ge 5.10.0 &&
4631 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4632 my $openparens = $1;
4633 my $count = $openparens =~ tr@\(@\(@;
4634 my $msg = "";
4635 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4636 my $comp = $4; #Not $1 because of $LvalOrFunc
4637 $msg = " - maybe == should be = ?" if ($comp eq "==");
4638 WARN("UNNECESSARY_PARENTHESES",
4639 "Unnecessary parentheses$msg\n" . $herecurr);
4640 }
4641 }
4642
Joe Perchesc5595fa2015-09-09 15:37:58 -07004643# comparisons with a constant or upper case identifier on the left
4644# avoid cases like "foo + BAR < baz"
4645# only fix matches surrounded by parentheses to avoid incorrect
4646# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4647 if ($^V && $^V ge 5.10.0 &&
4648 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4649 my $lead = $1;
4650 my $const = $2;
4651 my $comp = $3;
4652 my $to = $4;
4653 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004654 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004655 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4656 WARN("CONSTANT_COMPARISON",
4657 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4658 $fix) {
4659 if ($comp eq "<") {
4660 $newcomp = ">";
4661 } elsif ($comp eq "<=") {
4662 $newcomp = ">=";
4663 } elsif ($comp eq ">") {
4664 $newcomp = "<";
4665 } elsif ($comp eq ">=") {
4666 $newcomp = "<=";
4667 }
4668 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4669 }
4670 }
4671
Joe Perchesf34e4a42015-04-16 12:44:19 -07004672# Return of what appears to be an errno should normally be negative
4673 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004674 my $name = $1;
4675 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004676 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004677 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004678 }
4679 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004680
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004681# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004682 if ($line =~ /\b(if|while|for|switch)\(/) {
4683 if (ERROR("SPACING",
4684 "space required before the open parenthesis '('\n" . $herecurr) &&
4685 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004686 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004687 s/\b(if|while|for|switch)\(/$1 \(/;
4688 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004689 }
4690
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004691# Check for illegal assignment in if conditional -- and check for trailing
4692# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004693 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004694 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4695 ctx_statement_block($linenr, $realcnt, 0)
4696 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004697 my ($stat_next) = ctx_statement_block($line_nr_next,
4698 $remain_next, $off_next);
4699 $stat_next =~ s/\n./\n /g;
4700 ##print "stat<$stat> stat_next<$stat_next>\n";
4701
4702 if ($stat_next =~ /^\s*while\b/) {
4703 # If the statement carries leading newlines,
4704 # then count those as offsets.
4705 my ($whitespace) =
4706 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4707 my $offset =
4708 statement_rawlines($whitespace) - 1;
4709
4710 $suppress_whiletrailers{$line_nr_next +
4711 $offset} = 1;
4712 }
4713 }
4714 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004715 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004716 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004717 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004718
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004719 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004720 ERROR("ASSIGN_IN_IF",
4721 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004722 }
4723
4724 # Find out what is on the end of the line after the
4725 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004726 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004727 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004728 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004729 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4730 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004731 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004732 # Find out how long the conditional actually is.
4733 my @newlines = ($c =~ /\n/gs);
4734 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004735 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004736
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004737 $stat_real = raw_line($linenr, $cond_lines)
4738 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004739 if (defined($stat_real) && $cond_lines > 1) {
4740 $stat_real = "[...]\n$stat_real";
4741 }
4742
Joe Perches000d1cc12011-07-25 17:13:25 -07004743 ERROR("TRAILING_STATEMENTS",
4744 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004745 }
4746 }
4747
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004748# Check for bitwise tests written as boolean
4749 if ($line =~ /
4750 (?:
4751 (?:\[|\(|\&\&|\|\|)
4752 \s*0[xX][0-9]+\s*
4753 (?:\&\&|\|\|)
4754 |
4755 (?:\&\&|\|\|)
4756 \s*0[xX][0-9]+\s*
4757 (?:\&\&|\|\||\)|\])
4758 )/x)
4759 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004760 WARN("HEXADECIMAL_BOOLEAN_TEST",
4761 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004762 }
4763
Andy Whitcroft8905a672007-11-28 16:21:06 -08004764# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004765 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4766 my $s = $1;
4767 $s =~ s/$;//g; # Remove any comments
4768 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769 ERROR("TRAILING_STATEMENTS",
4770 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004771 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004772 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004773# if should not continue a brace
4774 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004775 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004776 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004777 $herecurr);
4778 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004779# case and default should not have general statements after them
4780 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4781 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004782 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004783 \s*return\s+
4784 )/xg)
4785 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004786 ERROR("TRAILING_STATEMENTS",
4787 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004788 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004789
4790 # Check for }<nl>else {, these must be at the same
4791 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004792 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4793 $previndent == $indent) {
4794 if (ERROR("ELSE_AFTER_BRACE",
4795 "else should follow close brace '}'\n" . $hereprev) &&
4796 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4797 fix_delete_line($fixlinenr - 1, $prevrawline);
4798 fix_delete_line($fixlinenr, $rawline);
4799 my $fixedline = $prevrawline;
4800 $fixedline =~ s/}\s*$//;
4801 if ($fixedline !~ /^\+\s*$/) {
4802 fix_insert_line($fixlinenr, $fixedline);
4803 }
4804 $fixedline = $rawline;
4805 $fixedline =~ s/^(.\s*)else/$1} else/;
4806 fix_insert_line($fixlinenr, $fixedline);
4807 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004808 }
4809
Joe Perches8b8856f2014-08-06 16:11:14 -07004810 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4811 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004812 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4813
4814 # Find out what is on the end of the line after the
4815 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004816 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004817 $s =~ s/\n.*//g;
4818
4819 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004820 if (ERROR("WHILE_AFTER_BRACE",
4821 "while should follow close brace '}'\n" . $hereprev) &&
4822 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4823 fix_delete_line($fixlinenr - 1, $prevrawline);
4824 fix_delete_line($fixlinenr, $rawline);
4825 my $fixedline = $prevrawline;
4826 my $trailing = $rawline;
4827 $trailing =~ s/^\+//;
4828 $trailing = trim($trailing);
4829 $fixedline =~ s/}\s*$/} $trailing/;
4830 fix_insert_line($fixlinenr, $fixedline);
4831 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004832 }
4833 }
4834
Joe Perches95e2c602013-07-03 15:05:20 -07004835#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004836 while ($line =~ m{($Constant|$Lval)}g) {
4837 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004838
4839#gcc binary extension
4840 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004841 if (WARN("GCC_BINARY_CONSTANT",
4842 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4843 $fix) {
4844 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004845 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004846 s/\b$var\b/$hexval/;
4847 }
Joe Perches95e2c602013-07-03 15:05:20 -07004848 }
4849
4850#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004851 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004852 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004853#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004854 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004855#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004856 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4857#Ignore some three character SI units explicitly, like MiB and KHz
4858 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004859 while ($var =~ m{($Ident)}g) {
4860 my $word = $1;
4861 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004862 if ($check) {
4863 seed_camelcase_includes();
4864 if (!$file && !$camelcase_file_seeded) {
4865 seed_camelcase_file($realfile);
4866 $camelcase_file_seeded = 1;
4867 }
4868 }
Joe Perches7e781f62013-09-11 14:23:55 -07004869 if (!defined $camelcase{$word}) {
4870 $camelcase{$word} = 1;
4871 CHK("CAMELCASE",
4872 "Avoid CamelCase: <$word>\n" . $herecurr);
4873 }
Joe Perches34456862013-07-03 15:05:34 -07004874 }
Joe Perches323c1262012-12-17 16:02:07 -08004875 }
4876 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004877
4878#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004879 if ($line =~ /\#\s*define.*\\\s+$/) {
4880 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4881 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4882 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004883 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004884 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004885 }
4886
Fabian Frederick0e212e02015-04-16 12:44:25 -07004887# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4888# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004889 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004890 my $file = "$1.h";
4891 my $checkfile = "include/linux/$file";
4892 if (-f "$root/$checkfile" &&
4893 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004894 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004895 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004896 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4897 if ($asminclude > 0) {
4898 if ($realfile =~ m{^arch/}) {
4899 CHK("ARCH_INCLUDE_LINUX",
4900 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4901 } else {
4902 WARN("INCLUDE_LINUX",
4903 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4904 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004905 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004906 }
4907 }
4908
Andy Whitcroft653d4872007-06-23 17:16:34 -07004909# multi-statement macros should be enclosed in a do while loop, grab the
4910# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004911# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004912 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4913 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004914 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004915 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004916 my ($off, $dstat, $dcond, $rest);
4917 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004918 my $has_flow_statement = 0;
4919 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004920 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004921 ctx_statement_block($linenr, $realcnt, 0);
4922 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004923 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004924 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004925
Joe Perches08a28432014-10-13 15:51:55 -07004926 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004927 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004928
Joe Perchesf59b64b2016-10-11 13:52:08 -07004929 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4930 my $define_args = $1;
4931 my $define_stmt = $dstat;
4932 my @def_args = ();
4933
4934 if (defined $define_args && $define_args ne "") {
4935 $define_args = substr($define_args, 1, length($define_args) - 2);
4936 $define_args =~ s/\s*//g;
4937 @def_args = split(",", $define_args);
4938 }
4939
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004940 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004941 $dstat =~ s/\\\n.//g;
4942 $dstat =~ s/^\s*//s;
4943 $dstat =~ s/\s*$//s;
4944
4945 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004946 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4947 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004948 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004949 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004950 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004951
Gregory Bean02870be2011-05-11 09:11:12 -07004952 # Extremely long macros may fall off the end of the
4953 # available context without closing. Give a dangling
4954 # backslash the benefit of the doubt and allow it
4955 # to gobble any hanging open-parens.
4956 $dstat =~ s/\(.+\\$/1/;
4957
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004958 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004959 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4960 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004961 {
4962 }
4963
Joe Perches42e15292016-03-15 14:58:01 -07004964 # Make asm volatile uses seem like a generic function
4965 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4966
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004967 my $exceptions = qr{
4968 $Declare|
4969 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004970 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004971 DECLARE_PER_CPU|
4972 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004973 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004974 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004975 union|
4976 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004977 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004978 ^\"|\"$|
4979 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004980 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004981 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004982
4983 $ctx =~ s/\n*$//;
4984 my $herectx = $here . "\n";
4985 my $stmt_cnt = statement_rawlines($ctx);
4986
4987 for (my $n = 0; $n < $stmt_cnt; $n++) {
4988 $herectx .= raw_line($linenr, $n) . "\n";
4989 }
4990
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004991 if ($dstat ne '' &&
4992 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4993 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004994 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004995 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004996 $dstat !~ /$exceptions/ &&
4997 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004998 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004999 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005000 $dstat !~ /^for\s*$Constant$/ && # for (...)
5001 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5002 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005003 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005004 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005005 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005006
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005007 if ($dstat =~ /;/) {
5008 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5009 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5010 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005011 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005012 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005013 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005014
5015 }
Joe Perches52076492016-10-11 13:52:14 -07005016
5017 # Make $define_stmt single line, comment-free, etc
5018 my @stmt_array = split('\n', $define_stmt);
5019 my $first = 1;
5020 $define_stmt = "";
5021 foreach my $l (@stmt_array) {
5022 $l =~ s/\\$//;
5023 if ($first) {
5024 $define_stmt = $l;
5025 $first = 0;
5026 } elsif ($l =~ /^[\+ ]/) {
5027 $define_stmt .= substr($l, 1);
5028 }
5029 }
5030 $define_stmt =~ s/$;//g;
5031 $define_stmt =~ s/\s+/ /g;
5032 $define_stmt = trim($define_stmt);
5033
Joe Perchesf59b64b2016-10-11 13:52:08 -07005034# check if any macro arguments are reused (ignore '...' and 'type')
5035 foreach my $arg (@def_args) {
5036 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005037 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005038 my $tmp = $define_stmt;
5039 $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 -07005040 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005041 $tmp =~ s/\b$arg\s*\#\#//g;
5042 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5043 if ($use_cnt > 1) {
5044 CHK("MACRO_ARG_REUSE",
5045 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005046 }
5047# check if any macro arguments may have other precedence issues
5048 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5049 ((defined($1) && $1 ne ',') ||
5050 (defined($2) && $2 ne ','))) {
5051 CHK("MACRO_ARG_PRECEDENCE",
5052 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005053 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005054 }
Joe Perches5023d342012-12-17 16:01:47 -08005055
Joe Perches08a28432014-10-13 15:51:55 -07005056# check for macros with flow control, but without ## concatenation
5057# ## concatenation is commonly a macro that defines a function so ignore those
5058 if ($has_flow_statement && !$has_arg_concat) {
5059 my $herectx = $here . "\n";
5060 my $cnt = statement_rawlines($ctx);
5061
5062 for (my $n = 0; $n < $cnt; $n++) {
5063 $herectx .= raw_line($linenr, $n) . "\n";
5064 }
5065 WARN("MACRO_WITH_FLOW_CONTROL",
5066 "Macros with flow control statements should be avoided\n" . "$herectx");
5067 }
5068
Joe Perches481eb482012-12-17 16:01:56 -08005069# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005070
5071 } else {
5072 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005073 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5074 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005075 $line =~ /^\+.*\\$/) {
5076 WARN("LINE_CONTINUATIONS",
5077 "Avoid unnecessary line continuations\n" . $herecurr);
5078 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005079 }
5080
Joe Perchesb13edf72012-07-30 14:41:24 -07005081# do {} while (0) macro tests:
5082# single-statement macros do not need to be enclosed in do while (0) loop,
5083# macro should not end with a semicolon
5084 if ($^V && $^V ge 5.10.0 &&
5085 $realfile !~ m@/vmlinux.lds.h$@ &&
5086 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5087 my $ln = $linenr;
5088 my $cnt = $realcnt;
5089 my ($off, $dstat, $dcond, $rest);
5090 my $ctx = '';
5091 ($dstat, $dcond, $ln, $cnt, $off) =
5092 ctx_statement_block($linenr, $realcnt, 0);
5093 $ctx = $dstat;
5094
5095 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005096 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005097
5098 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5099 my $stmts = $2;
5100 my $semis = $3;
5101
5102 $ctx =~ s/\n*$//;
5103 my $cnt = statement_rawlines($ctx);
5104 my $herectx = $here . "\n";
5105
5106 for (my $n = 0; $n < $cnt; $n++) {
5107 $herectx .= raw_line($linenr, $n) . "\n";
5108 }
5109
Joe Perchesac8e97f2012-08-21 16:15:53 -07005110 if (($stmts =~ tr/;/;/) == 1 &&
5111 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005112 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5113 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5114 }
5115 if (defined $semis && $semis ne "") {
5116 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5117 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5118 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005119 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5120 $ctx =~ s/\n*$//;
5121 my $cnt = statement_rawlines($ctx);
5122 my $herectx = $here . "\n";
5123
5124 for (my $n = 0; $n < $cnt; $n++) {
5125 $herectx .= raw_line($linenr, $n) . "\n";
5126 }
5127
5128 WARN("TRAILING_SEMICOLON",
5129 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005130 }
5131 }
5132
Mike Frysinger080ba922009-01-06 14:41:25 -08005133# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5134# all assignments may have only one of the following with an assignment:
5135# .
5136# ALIGN(...)
5137# VMLINUX_SYMBOL(...)
5138 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005139 WARN("MISSING_VMLINUX_SYMBOL",
5140 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005141 }
5142
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005143# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005144 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5145 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005146 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005147 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005148 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5149 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005150 my @allowed = ();
5151 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005152 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005153 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005154 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005155 for my $chunk (@chunks) {
5156 my ($cond, $block) = @{$chunk};
5157
Andy Whitcroft773647a2008-03-28 14:15:58 -07005158 # If the condition carries leading newlines, then count those as offsets.
5159 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5160 my $offset = statement_rawlines($whitespace) - 1;
5161
Joe Perchesaad4f612012-03-23 15:02:19 -07005162 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005163 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5164
5165 # We have looked at and allowed this specific line.
5166 $suppress_ifbraces{$ln + $offset} = 1;
5167
5168 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005169 $ln += statement_rawlines($block) - 1;
5170
Andy Whitcroft773647a2008-03-28 14:15:58 -07005171 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005172
5173 $seen++ if ($block =~ /^\s*{/);
5174
Joe Perchesaad4f612012-03-23 15:02:19 -07005175 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005176 if (statement_lines($cond) > 1) {
5177 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005178 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005179 }
5180 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005181 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005182 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005183 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005184 if (statement_block_size($block) > 1) {
5185 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005186 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005187 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005188 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005189 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005190 if ($seen) {
5191 my $sum_allowed = 0;
5192 foreach (@allowed) {
5193 $sum_allowed += $_;
5194 }
5195 if ($sum_allowed == 0) {
5196 WARN("BRACES",
5197 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5198 } elsif ($sum_allowed != $allow &&
5199 $seen != $allow) {
5200 CHK("BRACES",
5201 "braces {} should be used on all arms of this statement\n" . $herectx);
5202 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005203 }
5204 }
5205 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005206 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005207 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005208 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005209
Andy Whitcroftcf655042008-03-04 14:28:20 -08005210 # Check the pre-context.
5211 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5212 #print "APW: ALLOWED: pre<$1>\n";
5213 $allowed = 1;
5214 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005215
5216 my ($level, $endln, @chunks) =
5217 ctx_statement_full($linenr, $realcnt, $-[0]);
5218
Andy Whitcroftcf655042008-03-04 14:28:20 -08005219 # Check the condition.
5220 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005221 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005222 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005223 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005224 }
5225 if (statement_lines($cond) > 1) {
5226 #print "APW: ALLOWED: cond<$cond>\n";
5227 $allowed = 1;
5228 }
5229 if ($block =~/\b(?:if|for|while)\b/) {
5230 #print "APW: ALLOWED: block<$block>\n";
5231 $allowed = 1;
5232 }
5233 if (statement_block_size($block) > 1) {
5234 #print "APW: ALLOWED: lines block<$block>\n";
5235 $allowed = 1;
5236 }
5237 # Check the post-context.
5238 if (defined $chunks[1]) {
5239 my ($cond, $block) = @{$chunks[1]};
5240 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005241 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005242 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005243 if ($block =~ /^\s*\{/) {
5244 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5245 $allowed = 1;
5246 }
5247 }
5248 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005249 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005250 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005251
Andy Whitcroftf0556632008-10-15 22:02:23 -07005252 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005253 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005254 }
5255
Joe Perches000d1cc12011-07-25 17:13:25 -07005256 WARN("BRACES",
5257 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005258 }
5259 }
5260
Joe Perches0979ae62012-12-17 16:01:59 -08005261# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005262 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005263 if (CHK("BRACES",
5264 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5265 $fix && $prevrawline =~ /^\+/) {
5266 fix_delete_line($fixlinenr - 1, $prevrawline);
5267 }
Joe Perches0979ae62012-12-17 16:01:59 -08005268 }
Joe Perches77b9a532013-07-03 15:05:29 -07005269 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005270 if (CHK("BRACES",
5271 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5272 $fix) {
5273 fix_delete_line($fixlinenr, $rawline);
5274 }
Joe Perches0979ae62012-12-17 16:01:59 -08005275 }
5276
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005277# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005278 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5279 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005280 WARN("VOLATILE",
5281 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005282 }
5283
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005284# Check for user-visible strings broken across lines, which breaks the ability
5285# to grep for the string. Make exceptions when the previous string ends in a
5286# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5287# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005288 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005289 $prevline =~ /"\s*$/ &&
5290 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5291 if (WARN("SPLIT_STRING",
5292 "quoted string split across lines\n" . $hereprev) &&
5293 $fix &&
5294 $prevrawline =~ /^\+.*"\s*$/ &&
5295 $last_coalesced_string_linenr != $linenr - 1) {
5296 my $extracted_string = get_quoted_string($line, $rawline);
5297 my $comma_close = "";
5298 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5299 $comma_close = $1;
5300 }
5301
5302 fix_delete_line($fixlinenr - 1, $prevrawline);
5303 fix_delete_line($fixlinenr, $rawline);
5304 my $fixedline = $prevrawline;
5305 $fixedline =~ s/"\s*$//;
5306 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5307 fix_insert_line($fixlinenr - 1, $fixedline);
5308 $fixedline = $rawline;
5309 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5310 if ($fixedline !~ /\+\s*$/) {
5311 fix_insert_line($fixlinenr, $fixedline);
5312 }
5313 $last_coalesced_string_linenr = $linenr;
5314 }
5315 }
5316
5317# check for missing a space in a string concatenation
5318 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5319 WARN('MISSING_SPACE',
5320 "break quoted strings at a space character\n" . $hereprev);
5321 }
5322
5323# check for spaces before a quoted newline
5324 if ($rawline =~ /^.*\".*\s\\n/) {
5325 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5326 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5327 $fix) {
5328 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5329 }
5330
5331 }
5332
Joe Perchesf17dba42014-10-13 15:51:51 -07005333# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005334 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005335 CHK("CONCATENATED_STRING",
5336 "Concatenated strings should use spaces between elements\n" . $herecurr);
5337 }
5338
Joe Perches90ad30e2014-12-10 15:51:59 -08005339# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005340 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005341 WARN("STRING_FRAGMENTS",
5342 "Consecutive strings are generally better as a single string\n" . $herecurr);
5343 }
5344
Joe Perches6e300752015-09-09 15:37:47 -07005345# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005346 my $string;
5347 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5348 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5349 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005350 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005351 WARN("PRINTF_L",
5352 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5353 last;
5354 }
Joe Perches6e300752015-09-09 15:37:47 -07005355 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5356 ERROR("PRINTF_0xDECIMAL",
5357 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5358 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005359 }
5360
5361# check for line continuations in quoted strings with odd counts of "
5362 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5363 WARN("LINE_CONTINUATIONS",
5364 "Avoid line continuations in quoted strings\n" . $herecurr);
5365 }
5366
Gregory Bean6c9c6842011-03-17 14:18:20 -07005367# sys_open/read/write/close are not allowed in the kernel
5368 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5369 ERROR("FILE_OPS",
5370 "$1 is inappropriate in kernel code.\n" .
5371 $herecurr);
5372 }
5373
Gregory Beana7cce0a2011-06-07 08:06:45 -07005374# filp_open is a backdoor for sys_open
5375 if ($line =~ /\b(filp_open)\b/) {
5376 ERROR("FILE_OPS",
5377 "$1 is inappropriate in kernel code.\n" .
5378 $herecurr);
5379 }
5380
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005381# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5382 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5383 ERROR("NON_RELAXED_IO",
5384 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5385 "with appropriate memory barriers instead.\n" .
5386 $herecurr);
5387 }
5388
5389# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5390 if ($line =~ /\b((in|out)([bwl]))\b/) {
5391 my ($all, $pref, $suf) = ($1, $2, $3);
5392 $pref =~ s/in/read/;
5393 $pref =~ s/out/write/;
5394 ERROR("NON_RELAXED_IO",
5395 "Use of $all is deprecated: use " .
5396 "__raw_$pref$suf\n\t" .
5397 "with appropriate memory barriers instead.\n" .
5398 $herecurr);
5399 }
5400
Gregory Bean438bf5f2011-06-15 09:32:38 -07005401# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005402 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005403 WARN("ARM_BARRIER",
5404 "Use of dsb is discouranged: prefer mb.\n" .
5405 $herecurr);
5406 }
5407
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005408# unbounded string functions are overflow risks
5409 my %str_fns = (
5410 "sprintf" => "snprintf",
5411 "strcpy" => "strlcpy",
5412 "strncpy" => "strlcpy",
5413 "strcat" => "strlcat",
5414 "strncat" => "strlcat",
5415 "vsprintf" => "vsnprintf",
5416 "strchr" => "strnchr",
5417 "strstr" => "strnstr",
5418 );
5419 foreach my $k (keys %str_fns) {
5420 if ($line =~ /\b$k\b/) {
5421 ERROR("UNBOUNDED_STRING_FNS",
5422 "Use of $k is deprecated: " .
5423 "use $str_fns{$k} instead.\n" .
5424 $herecurr);
5425 }
5426 }
5427
Andy Whitcroft00df3442007-06-08 13:47:06 -07005428# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005429 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005430 WARN("IF_0",
5431 "if this code is redundant consider removing it\n"
5432 . $herecurr);
5433 }
5434
5435# warn about #if 1
5436 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5437 WARN("IF_1",
5438 "if this code is required consider removing"
5439 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005440 }
5441
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005442# check for needless "if (<foo>) fn(<foo>)" uses
5443 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005444 my $tested = quotemeta($1);
5445 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5446 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5447 my $func = $1;
5448 if (WARN('NEEDLESS_IF',
5449 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5450 $fix) {
5451 my $do_fix = 1;
5452 my $leading_tabs = "";
5453 my $new_leading_tabs = "";
5454 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5455 $leading_tabs = $1;
5456 } else {
5457 $do_fix = 0;
5458 }
5459 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5460 $new_leading_tabs = $1;
5461 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5462 $do_fix = 0;
5463 }
5464 } else {
5465 $do_fix = 0;
5466 }
5467 if ($do_fix) {
5468 fix_delete_line($fixlinenr - 1, $prevrawline);
5469 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5470 }
5471 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005472 }
5473 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005474
Joe Perchesebfdc402014-08-06 16:10:27 -07005475# check for unnecessary "Out of Memory" messages
5476 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5477 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5478 (defined $1 || defined $3) &&
5479 $linenr > 3) {
5480 my $testval = $2;
5481 my $testline = $lines[$linenr - 3];
5482
5483 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5484# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5485
5486 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5487 WARN("OOM_MESSAGE",
5488 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5489 }
5490 }
5491
Joe Perchesf78d98f2014-10-13 15:52:01 -07005492# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005493 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005494 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5495 my $level = $1;
5496 if (WARN("UNNECESSARY_KERN_LEVEL",
5497 "Possible unnecessary $level\n" . $herecurr) &&
5498 $fix) {
5499 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5500 }
5501 }
5502
Joe Perchesabb08a52014-12-10 15:51:46 -08005503# check for mask then right shift without a parentheses
5504 if ($^V && $^V ge 5.10.0 &&
5505 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5506 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5507 WARN("MASK_THEN_SHIFT",
5508 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5509 }
5510
Joe Perchesb75ac612014-12-10 15:52:02 -08005511# check for pointer comparisons to NULL
5512 if ($^V && $^V ge 5.10.0) {
5513 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5514 my $val = $1;
5515 my $equal = "!";
5516 $equal = "" if ($4 eq "!=");
5517 if (CHK("COMPARISON_TO_NULL",
5518 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5519 $fix) {
5520 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5521 }
5522 }
5523 }
5524
Joe Perches8716de32013-09-11 14:24:05 -07005525# check for bad placement of section $InitAttribute (e.g.: __initdata)
5526 if ($line =~ /(\b$InitAttribute\b)/) {
5527 my $attr = $1;
5528 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5529 my $ptr = $1;
5530 my $var = $2;
5531 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5532 ERROR("MISPLACED_INIT",
5533 "$attr should be placed after $var\n" . $herecurr)) ||
5534 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5535 WARN("MISPLACED_INIT",
5536 "$attr should be placed after $var\n" . $herecurr))) &&
5537 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005538 $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 -07005539 }
5540 }
5541 }
5542
Joe Perchese970b882013-11-12 15:10:10 -08005543# check for $InitAttributeData (ie: __initdata) with const
5544 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5545 my $attr = $1;
5546 $attr =~ /($InitAttributePrefix)(.*)/;
5547 my $attr_prefix = $1;
5548 my $attr_type = $2;
5549 if (ERROR("INIT_ATTRIBUTE",
5550 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5551 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005552 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005553 s/$InitAttributeData/${attr_prefix}initconst/;
5554 }
5555 }
5556
5557# check for $InitAttributeConst (ie: __initconst) without const
5558 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5559 my $attr = $1;
5560 if (ERROR("INIT_ATTRIBUTE",
5561 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5562 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005563 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005564 /(^\+\s*(?:static\s+))/;
5565 $lead = rtrim($1);
5566 $lead = "$lead " if ($lead !~ /^\+$/);
5567 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005568 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005569 }
5570 }
5571
Joe Perchesc17893c2015-04-16 12:44:42 -07005572# check for __read_mostly with const non-pointer (should just be const)
5573 if ($line =~ /\b__read_mostly\b/ &&
5574 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5575 if (ERROR("CONST_READ_MOSTLY",
5576 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5577 $fix) {
5578 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5579 }
5580 }
5581
Joe Perchesfbdb8132014-04-03 14:49:14 -07005582# don't use __constant_<foo> functions outside of include/uapi/
5583 if ($realfile !~ m@^include/uapi/@ &&
5584 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5585 my $constant_func = $1;
5586 my $func = $constant_func;
5587 $func =~ s/^__constant_//;
5588 if (WARN("CONSTANT_CONVERSION",
5589 "$constant_func should be $func\n" . $herecurr) &&
5590 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005591 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005592 }
5593 }
5594
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005595# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005596 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005597 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005598 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005599 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005600 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005601 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5602 }
5603 if ($delay > 2000) {
5604 WARN("LONG_UDELAY",
5605 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005606 }
5607 }
5608
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005609# warn about unexpectedly long msleep's
5610 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5611 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005612 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005613 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005614 }
5615 }
5616
Joe Perches36ec1932013-07-03 15:05:25 -07005617# check for comparisons of jiffies
5618 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5619 WARN("JIFFIES_COMPARISON",
5620 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5621 }
5622
Joe Perches9d7a34a2013-07-03 15:05:26 -07005623# check for comparisons of get_jiffies_64()
5624 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5625 WARN("JIFFIES_COMPARISON",
5626 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5627 }
5628
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005629# check the patch for use of mdelay
5630 if ($line =~ /\bmdelay\s*\(/) {
5631 WARN("MDELAY",
5632 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5633 }
5634
Andy Whitcroft00df3442007-06-08 13:47:06 -07005635# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005636# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005637# print "#ifdef in C files should be avoided\n";
5638# print "$herecurr";
5639# $clean = 0;
5640# }
5641
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005642# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005643 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005644 if (ERROR("SPACING",
5645 "exactly one space required after that #$1\n" . $herecurr) &&
5646 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005647 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005648 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5649 }
5650
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005651 }
5652
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005653# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005654 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5655 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005656 my $which = $1;
5657 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005658 CHK("UNCOMMENTED_DEFINITION",
5659 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005660 }
5661 }
5662# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005663
5664 my $barriers = qr{
5665 mb|
5666 rmb|
5667 wmb|
5668 read_barrier_depends
5669 }x;
5670 my $barrier_stems = qr{
5671 mb__before_atomic|
5672 mb__after_atomic|
5673 store_release|
5674 load_acquire|
5675 store_mb|
5676 (?:$barriers)
5677 }x;
5678 my $all_barriers = qr{
5679 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005680 smp_(?:$barrier_stems)|
5681 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005682 }x;
5683
5684 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005685 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005686 WARN("MEMORY_BARRIER",
5687 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005688 }
5689 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005690
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005691 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5692
5693 if ($realfile !~ m@^include/asm-generic/@ &&
5694 $realfile !~ m@/barrier\.h$@ &&
5695 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5696 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5697 WARN("MEMORY_BARRIER",
5698 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5699 }
5700
Joe Perchescb426e92015-06-25 15:02:46 -07005701# check for waitqueue_active without a comment.
5702 if ($line =~ /\bwaitqueue_active\s*\(/) {
5703 if (!ctx_has_comment($first_line, $linenr)) {
5704 WARN("WAITQUEUE_ACTIVE",
5705 "waitqueue_active without comment\n" . $herecurr);
5706 }
5707 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005708
5709# Check for expedited grace periods that interrupt non-idle non-nohz
5710# online CPUs. These expedited can therefore degrade real-time response
5711# if used carelessly, and should be avoided where not absolutely
5712# needed. It is always OK to use synchronize_rcu_expedited() and
5713# synchronize_sched_expedited() at boot time (before real-time applications
5714# start) and in error situations where real-time response is compromised in
5715# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5716# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5717# Of course, nothing comes for free, and srcu_read_lock() and
5718# srcu_read_unlock() do contain full memory barriers in payment for
5719# synchronize_srcu_expedited() non-interruption properties.
5720 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5721 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5722 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5723
5724 }
5725
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005726# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005727 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005728 CHK("ARCH_DEFINES",
5729 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005730 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005731
Tobias Klauserd4977c72010-05-24 14:33:30 -07005732# Check that the storage class is at the beginning of a declaration
5733 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005734 WARN("STORAGE_CLASS",
5735 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005736 }
5737
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005738# check the location of the inline attribute, that it is between
5739# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005740 if ($line =~ /\b$Type\s+$Inline\b/ ||
5741 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005742 ERROR("INLINE_LOCATION",
5743 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005744 }
5745
Andy Whitcroft8905a672007-11-28 16:21:06 -08005746# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005747 if ($realfile !~ m@\binclude/uapi/@ &&
5748 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005749 if (WARN("INLINE",
5750 "plain inline is preferred over $1\n" . $herecurr) &&
5751 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005752 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005753
5754 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005755 }
5756
Joe Perches3d130fd2011-01-12 17:00:00 -08005757# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005758 if ($realfile !~ m@\binclude/uapi/@ &&
5759 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005760 WARN("PREFER_PACKED",
5761 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005762 }
5763
Joe Perches39b7e282011-07-25 17:13:24 -07005764# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005765 if ($realfile !~ m@\binclude/uapi/@ &&
5766 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005767 WARN("PREFER_ALIGNED",
5768 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005769 }
5770
Joe Perches5f14d3b2012-01-10 15:09:52 -08005771# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005772 if ($realfile !~ m@\binclude/uapi/@ &&
5773 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005774 if (WARN("PREFER_PRINTF",
5775 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5776 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005777 $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 -07005778
5779 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005780 }
5781
Joe Perches6061d942012-03-23 15:02:16 -07005782# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005783 if ($realfile !~ m@\binclude/uapi/@ &&
5784 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005785 if (WARN("PREFER_SCANF",
5786 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5787 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005788 $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 -07005789 }
Joe Perches6061d942012-03-23 15:02:16 -07005790 }
5791
Joe Perches619a9082014-12-10 15:51:35 -08005792# Check for __attribute__ weak, or __weak declarations (may have link issues)
5793 if ($^V && $^V ge 5.10.0 &&
5794 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5795 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5796 $line =~ /\b__weak\b/)) {
5797 ERROR("WEAK_DECLARATION",
5798 "Using weak declarations can have unintended link defects\n" . $herecurr);
5799 }
5800
Joe Perchese6176fa2015-06-25 15:02:49 -07005801# check for c99 types like uint8_t used outside of uapi/
5802 if ($realfile !~ m@\binclude/uapi/@ &&
5803 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5804 my $type = $1;
5805 if ($type =~ /\b($typeC99Typedefs)\b/) {
5806 $type = $1;
5807 my $kernel_type = 'u';
5808 $kernel_type = 's' if ($type =~ /^_*[si]/);
5809 $type =~ /(\d+)/;
5810 $kernel_type .= $1;
5811 if (CHK("PREFER_KERNEL_TYPES",
5812 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5813 $fix) {
5814 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5815 }
5816 }
5817 }
5818
Joe Perches938224b2016-01-20 14:59:15 -08005819# check for cast of C90 native int or longer types constants
5820 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5821 my $cast = $1;
5822 my $const = $2;
5823 if (WARN("TYPECAST_INT_CONSTANT",
5824 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5825 $fix) {
5826 my $suffix = "";
5827 my $newconst = $const;
5828 $newconst =~ s/${Int_type}$//;
5829 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5830 if ($cast =~ /\blong\s+long\b/) {
5831 $suffix .= 'LL';
5832 } elsif ($cast =~ /\blong\b/) {
5833 $suffix .= 'L';
5834 }
5835 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5836 }
5837 }
5838
Joe Perches8f53a9b2010-03-05 13:43:48 -08005839# check for sizeof(&)
5840 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005841 WARN("SIZEOF_ADDRESS",
5842 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005843 }
5844
Joe Perches66c80b62012-07-30 14:41:22 -07005845# check for sizeof without parenthesis
5846 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005847 if (WARN("SIZEOF_PARENTHESIS",
5848 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5849 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005850 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005851 }
Joe Perches66c80b62012-07-30 14:41:22 -07005852 }
5853
Joe Perches88982fe2012-12-17 16:02:00 -08005854# check for struct spinlock declarations
5855 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5856 WARN("USE_SPINLOCK_T",
5857 "struct spinlock should be spinlock_t\n" . $herecurr);
5858 }
5859
Joe Perchesa6962d72013-04-29 16:18:13 -07005860# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005861 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005862 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005863 $fmt =~ s/%%//g;
5864 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005865 if (WARN("PREFER_SEQ_PUTS",
5866 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005868 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005869 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005870 }
5871 }
5872
Andy Whitcroft554e1652012-01-10 15:09:57 -08005873# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005874 if ($^V && $^V ge 5.10.0 &&
5875 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005876 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005877
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005878 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005879 my $ms_val = $7;
5880 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005881
Andy Whitcroft554e1652012-01-10 15:09:57 -08005882 if ($ms_size =~ /^(0x|)0$/i) {
5883 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005884 "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 -08005885 } elsif ($ms_size =~ /^(0x|)1$/i) {
5886 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005887 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5888 }
5889 }
5890
Joe Perches98a9bba2014-01-23 15:54:52 -08005891# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005892# if ($^V && $^V ge 5.10.0 &&
5893# defined $stat &&
5894# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5895# if (WARN("PREFER_ETHER_ADDR_COPY",
5896# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5897# $fix) {
5898# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5899# }
5900# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005901
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005902# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005903# if ($^V && $^V ge 5.10.0 &&
5904# defined $stat &&
5905# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5906# WARN("PREFER_ETHER_ADDR_EQUAL",
5907# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5908# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005909
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005910# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5911# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005912# if ($^V && $^V ge 5.10.0 &&
5913# defined $stat &&
5914# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5915#
5916# my $ms_val = $7;
5917#
5918# if ($ms_val =~ /^(?:0x|)0+$/i) {
5919# if (WARN("PREFER_ETH_ZERO_ADDR",
5920# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5921# $fix) {
5922# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5923# }
5924# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5925# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5926# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5927# $fix) {
5928# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5929# }
5930# }
5931# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005932
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005933# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005934 if ($^V && $^V ge 5.10.0 &&
5935 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005936 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005937 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005938 my $call = $1;
5939 my $cast1 = deparenthesize($2);
5940 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005941 my $cast2 = deparenthesize($7);
5942 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005943 my $cast;
5944
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005945 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005946 $cast = "$cast1 or $cast2";
5947 } elsif ($cast1 ne "") {
5948 $cast = $cast1;
5949 } else {
5950 $cast = $cast2;
5951 }
5952 WARN("MINMAX",
5953 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005954 }
5955 }
5956
Joe Perches4a273192012-07-30 14:41:20 -07005957# check usleep_range arguments
5958 if ($^V && $^V ge 5.10.0 &&
5959 defined $stat &&
5960 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5961 my $min = $1;
5962 my $max = $7;
5963 if ($min eq $max) {
5964 WARN("USLEEP_RANGE",
5965 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5966 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5967 $min > $max) {
5968 WARN("USLEEP_RANGE",
5969 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5970 }
5971 }
5972
Joe Perches823b7942013-11-12 15:10:15 -08005973# check for naked sscanf
5974 if ($^V && $^V ge 5.10.0 &&
5975 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005976 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005977 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5978 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5979 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5980 my $lc = $stat =~ tr@\n@@;
5981 $lc = $lc + $linenr;
5982 my $stat_real = raw_line($linenr, 0);
5983 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5984 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5985 }
5986 WARN("NAKED_SSCANF",
5987 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5988 }
5989
Joe Perchesafc819a2014-06-04 16:12:08 -07005990# check for simple sscanf that should be kstrto<foo>
5991 if ($^V && $^V ge 5.10.0 &&
5992 defined $stat &&
5993 $line =~ /\bsscanf\b/) {
5994 my $lc = $stat =~ tr@\n@@;
5995 $lc = $lc + $linenr;
5996 my $stat_real = raw_line($linenr, 0);
5997 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5998 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5999 }
6000 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6001 my $format = $6;
6002 my $count = $format =~ tr@%@%@;
6003 if ($count == 1 &&
6004 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6005 WARN("SSCANF_TO_KSTRTO",
6006 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6007 }
6008 }
6009 }
6010
Joe Perches70dc8a42013-09-11 14:23:58 -07006011# check for new externs in .h files.
6012 if ($realfile =~ /\.h$/ &&
6013 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006014 if (CHK("AVOID_EXTERNS",
6015 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006016 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006017 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006018 }
6019 }
6020
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006021# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006022 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006023 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006024 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006025 my $function_name = $1;
6026 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006027
6028 my $s = $stat;
6029 if (defined $cond) {
6030 substr($s, 0, length($cond), '');
6031 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006032 if ($s =~ /^\s*;/ &&
6033 $function_name ne 'uninitialized_var')
6034 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006035 WARN("AVOID_EXTERNS",
6036 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006037 }
6038
6039 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006040 WARN("FUNCTION_ARGUMENTS",
6041 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006042 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006043
6044 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6045 $stat =~ /^.\s*extern\s+/)
6046 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006047 WARN("AVOID_EXTERNS",
6048 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006049 }
6050
Joe Perchesca0d8922016-10-11 13:52:16 -07006051 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6052 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6053 $1 ne "void") {
6054 my $args = trim($1);
6055 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6056 my $arg = trim($1);
6057 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6058 WARN("FUNCTION_ARGUMENTS",
6059 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6060 }
6061 }
6062 }
6063
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006064# checks for new __setup's
6065 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6066 my $name = $1;
6067
6068 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006069 CHK("UNDOCUMENTED_SETUP",
6070 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006071 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006072 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006073
6074# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006075 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006076 WARN("UNNECESSARY_CASTS",
6077 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006078 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006079
Joe Perchesa640d252013-07-03 15:05:21 -07006080# alloc style
6081# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6082 if ($^V && $^V ge 5.10.0 &&
6083 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6084 CHK("ALLOC_SIZEOF_STRUCT",
6085 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6086 }
6087
Joe Perches60a55362014-06-04 16:12:07 -07006088# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6089 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006090 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07006091 my $oldfunc = $3;
6092 my $a1 = $4;
6093 my $a2 = $10;
6094 my $newfunc = "kmalloc_array";
6095 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006096 my $r1 = $a1;
6097 my $r2 = $a2;
6098 if ($a1 =~ /^sizeof\s*\S/) {
6099 $r1 = $a2;
6100 $r2 = $a1;
6101 }
6102 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6103 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006104 if (WARN("ALLOC_WITH_MULTIPLY",
6105 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6106 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006107 $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 -07006108
6109 }
6110 }
6111 }
6112
Joe Perches972fdea2013-04-29 16:18:12 -07006113# check for krealloc arg reuse
6114 if ($^V && $^V ge 5.10.0 &&
6115 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6116 WARN("KREALLOC_ARG_REUSE",
6117 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6118 }
6119
Joe Perches5ce59ae2013-02-21 16:44:18 -08006120# check for alloc argument mismatch
6121 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6122 WARN("ALLOC_ARRAY_ARGS",
6123 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6124 }
6125
Joe Perchescaf2a542011-01-12 16:59:56 -08006126# check for multiple semicolons
6127 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006128 if (WARN("ONE_SEMICOLON",
6129 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6130 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006131 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006132 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006133 }
6134
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006135# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6136 if ($realfile !~ m@^include/uapi/@ &&
6137 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006138 my $ull = "";
6139 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6140 if (CHK("BIT_MACRO",
6141 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6142 $fix) {
6143 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6144 }
6145 }
6146
Joe Perches2d632742016-05-20 17:04:00 -07006147# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6148 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6149 my $config = $1;
6150 if (WARN("PREFER_IS_ENABLED",
6151 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6152 $fix) {
6153 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6154 }
6155 }
6156
Joe Perchese81f2392014-08-06 16:11:25 -07006157# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006158 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6159 my $has_break = 0;
6160 my $has_statement = 0;
6161 my $count = 0;
6162 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006163 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006164 $prevline--;
6165 my $rline = $rawlines[$prevline - 1];
6166 my $fline = $lines[$prevline - 1];
6167 last if ($fline =~ /^\@\@/);
6168 next if ($fline =~ /^\-/);
6169 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6170 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6171 next if ($fline =~ /^.[\s$;]*$/);
6172 $has_statement = 1;
6173 $count++;
6174 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6175 }
6176 if (!$has_break && $has_statement) {
6177 WARN("MISSING_BREAK",
6178 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6179 }
6180 }
6181
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006182# check for switch/default statements without a break;
6183 if ($^V && $^V ge 5.10.0 &&
6184 defined $stat &&
6185 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6186 my $ctx = '';
6187 my $herectx = $here . "\n";
6188 my $cnt = statement_rawlines($stat);
6189 for (my $n = 0; $n < $cnt; $n++) {
6190 $herectx .= raw_line($linenr, $n) . "\n";
6191 }
6192 WARN("DEFAULT_NO_BREAK",
6193 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006194 }
6195
Patrick Pannutof370e252010-07-23 13:34:18 -07006196# check for return codes on error paths
6197 if ($line =~ /\breturn\s+-\d+/) {
6198 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006199 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006200 }
6201
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006202# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006203 if ($line =~ /\b__FUNCTION__\b/) {
6204 if (WARN("USE_FUNC",
6205 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6206 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006207 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006208 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006209 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006210
Joe Perches62ec8182015-02-13 14:38:18 -08006211# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6212 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6213 ERROR("DATE_TIME",
6214 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6215 }
6216
Joe Perches2c924882012-03-23 15:02:20 -07006217# check for use of yield()
6218 if ($line =~ /\byield\s*\(\s*\)/) {
6219 WARN("YIELD",
6220 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6221 }
6222
Joe Perches179f8f42013-07-03 15:05:30 -07006223# check for comparisons against true and false
6224 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6225 my $lead = $1;
6226 my $arg = $2;
6227 my $test = $3;
6228 my $otype = $4;
6229 my $trail = $5;
6230 my $op = "!";
6231
6232 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6233
6234 my $type = lc($otype);
6235 if ($type =~ /^(?:true|false)$/) {
6236 if (("$test" eq "==" && "$type" eq "true") ||
6237 ("$test" eq "!=" && "$type" eq "false")) {
6238 $op = "";
6239 }
6240
6241 CHK("BOOL_COMPARISON",
6242 "Using comparison to $otype is error prone\n" . $herecurr);
6243
6244## maybe suggesting a correct construct would better
6245## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6246
6247 }
6248 }
6249
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006250# check for semaphores initialized locked
6251 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006252 WARN("CONSIDER_COMPLETION",
6253 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006254 }
Joe Perches6712d852012-03-23 15:02:20 -07006255
Joe Perches67d0a072011-10-31 17:13:10 -07006256# recommend kstrto* over simple_strto* and strict_strto*
6257 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006258 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006259 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006260 }
Joe Perches6712d852012-03-23 15:02:20 -07006261
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006262# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006263 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006264 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006265 "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 -07006266 }
Joe Perches6712d852012-03-23 15:02:20 -07006267
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006268# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006269 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006270 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006271 WARN("CONST_STRUCT",
6272 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006273 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006274 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006275
6276# use of NR_CPUS is usually wrong
6277# ignore definitions of NR_CPUS and usage to define arrays as likely right
6278 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006279 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6280 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006281 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6282 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6283 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006284 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006285 WARN("NR_CPUS",
6286 "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 -07006287 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006288
Joe Perches52ea8502013-11-12 15:10:09 -08006289# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6290 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6291 ERROR("DEFINE_ARCH_HAS",
6292 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6293 }
6294
Joe Perchesacd93622015-02-13 14:38:38 -08006295# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6296 if ($^V && $^V ge 5.10.0 &&
6297 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6298 WARN("LIKELY_MISUSE",
6299 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6300 }
6301
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006302# whine mightly about in_atomic
6303 if ($line =~ /\bin_atomic\s*\(/) {
6304 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006305 ERROR("IN_ATOMIC",
6306 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006307 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006308 WARN("IN_ATOMIC",
6309 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006310 }
6311 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006312
Joe Perches481aea52016-05-20 17:04:08 -07006313# whine about ACCESS_ONCE
6314 if ($^V && $^V ge 5.10.0 &&
6315 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6316 my $par = $1;
6317 my $eq = $2;
6318 my $fun = $3;
6319 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6320 if (defined($eq)) {
6321 if (WARN("PREFER_WRITE_ONCE",
6322 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6323 $fix) {
6324 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6325 }
6326 } else {
6327 if (WARN("PREFER_READ_ONCE",
6328 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6329 $fix) {
6330 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6331 }
6332 }
6333 }
6334
Peter Zijlstra1704f472010-03-19 01:37:42 +01006335# check for lockdep_set_novalidate_class
6336 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6337 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6338 if ($realfile !~ m@^kernel/lockdep@ &&
6339 $realfile !~ m@^include/linux/lockdep@ &&
6340 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006341 ERROR("LOCKDEP",
6342 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006343 }
6344 }
Dave Jones88f88312011-01-12 16:59:59 -08006345
Joe Perchesb392c642015-04-16 12:44:16 -07006346 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6347 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006348 WARN("EXPORTED_WORLD_WRITABLE",
6349 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006350 }
Joe Perches24358802014-04-03 14:49:13 -07006351
Joe Perches515a2352014-04-03 14:49:24 -07006352# Mode permission misuses where it seems decimal should be octal
6353# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6354 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006355 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006356 $line =~ /$mode_perms_search/) {
6357 foreach my $entry (@mode_permission_funcs) {
6358 my $func = $entry->[0];
6359 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006360
Joe Perches459cf0a2016-10-11 13:52:19 -07006361 my $lc = $stat =~ tr@\n@@;
6362 $lc = $lc + $linenr;
6363 my $stat_real = raw_line($linenr, 0);
6364 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6365 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6366 }
6367
Joe Perches515a2352014-04-03 14:49:24 -07006368 my $skip_args = "";
6369 if ($arg_pos > 1) {
6370 $arg_pos--;
6371 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6372 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006373 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006374 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006375 my $val = $1;
6376 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006377 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6378 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006379 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006380 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006381 }
6382 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006383 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006384 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006385 }
Joe Perches24358802014-04-03 14:49:13 -07006386 }
6387 }
6388 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006389
Joe Perches459cf0a2016-10-11 13:52:19 -07006390# check for uses of S_<PERMS> that could be octal for readability
6391 if ($line =~ /\b$mode_perms_string_search\b/) {
6392 my $val = "";
6393 my $oval = "";
6394 my $to = 0;
6395 my $curpos = 0;
6396 my $lastpos = 0;
6397 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6398 $curpos = pos($line);
6399 my $match = $2;
6400 my $omatch = $1;
6401 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6402 $lastpos = $curpos;
6403 $to |= $mode_permission_string_types{$match};
6404 $val .= '\s*\|\s*' if ($val ne "");
6405 $val .= $match;
6406 $oval .= $omatch;
6407 }
6408 $oval =~ s/^\s*\|\s*//;
6409 $oval =~ s/\s*\|\s*$//;
6410 my $octal = sprintf("%04o", $to);
6411 if (WARN("SYMBOLIC_PERMS",
6412 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6413 $fix) {
6414 $fixed[$fixlinenr] =~ s/$val/$octal/;
6415 }
6416 }
6417
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006418# validate content of MODULE_LICENSE against list from include/linux/module.h
6419 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6420 my $extracted_string = get_quoted_string($line, $rawline);
6421 my $valid_licenses = qr{
6422 GPL|
6423 GPL\ v2|
6424 GPL\ and\ additional\ rights|
6425 Dual\ BSD/GPL|
6426 Dual\ MIT/GPL|
6427 Dual\ MPL/GPL|
6428 Proprietary
6429 }x;
6430 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6431 WARN("MODULE_LICENSE",
6432 "unknown module license " . $extracted_string . "\n" . $herecurr);
6433 }
6434 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006435 }
6436
Maya Erezec77bd62017-02-26 09:14:43 +02006437 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6438 WARN("BAD_SIGN_OFF",
6439 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6440 }
6441
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006442 # If we have no input at all, then there is nothing to report on
6443 # so just keep quiet.
6444 if ($#rawlines == -1) {
6445 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006446 }
6447
Andy Whitcroft8905a672007-11-28 16:21:06 -08006448 # In mailback mode only produce a report in the negative, for
6449 # things that appear to be patches.
6450 if ($mailback && ($clean == 1 || !$is_patch)) {
6451 exit(0);
6452 }
6453
6454 # This is not a patch, and we are are in 'no-patch' mode so
6455 # just keep quiet.
6456 if (!$chk_patch && !$is_patch) {
6457 exit(0);
6458 }
6459
Joe Perches06330fc2015-06-25 15:03:11 -07006460 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006461 ERROR("NOT_UNIFIED_DIFF",
6462 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006463 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006464 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006465 ERROR("MISSING_SIGN_OFF",
6466 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006467 }
6468
Andy Whitcroft8905a672007-11-28 16:21:06 -08006469 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006470 if ($summary && !($clean == 1 && $quiet == 1)) {
6471 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006472 print "total: $cnt_error errors, $cnt_warn warnings, " .
6473 (($check)? "$cnt_chk checks, " : "") .
6474 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006475 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006476
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006477 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006478 # If there were any defects found and not already fixing them
6479 if (!$clean and !$fix) {
6480 print << "EOM"
6481
6482NOTE: For some of the reported defects, checkpatch may be able to
6483 mechanically convert to the typical style using --fix or --fix-inplace.
6484EOM
6485 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006486 # If there were whitespace errors which cleanpatch can fix
6487 # then suggest that.
6488 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006489 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006490 print << "EOM"
6491
6492NOTE: Whitespace errors detected.
6493 You may wish to use scripts/cleanpatch or scripts/cleanfile
6494EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006495 }
6496 }
6497
Joe Perchesd752fcc2014-08-06 16:11:05 -07006498 if ($clean == 0 && $fix &&
6499 ("@rawlines" ne "@fixed" ||
6500 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006501 my $newfile = $filename;
6502 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006503 my $linecount = 0;
6504 my $f;
6505
Joe Perchesd752fcc2014-08-06 16:11:05 -07006506 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6507
Joe Perches3705ce52013-07-03 15:05:31 -07006508 open($f, '>', $newfile)
6509 or die "$P: Can't open $newfile for write\n";
6510 foreach my $fixed_line (@fixed) {
6511 $linecount++;
6512 if ($file) {
6513 if ($linecount > 3) {
6514 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006515 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006516 }
6517 } else {
6518 print $f $fixed_line . "\n";
6519 }
6520 }
6521 close($f);
6522
6523 if (!$quiet) {
6524 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006525
Joe Perches3705ce52013-07-03 15:05:31 -07006526Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6527
6528Do _NOT_ trust the results written to this file.
6529Do _NOT_ submit these changes without inspecting them for correctness.
6530
6531This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6532No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006533EOM
6534 }
6535 }
6536
Joe Perchesd8469f12015-06-25 15:03:00 -07006537 if ($quiet == 0) {
6538 print "\n";
6539 if ($clean == 1) {
6540 print "$vname has no obvious style problems and is ready for submission.\n";
6541 } else {
6542 print "$vname has style problems, please review.\n";
6543 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006544 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006545 return $clean;
6546}