blob: c5b281e8c8fd9e01a70d6a91962daa32adeb1e2f [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;
2441 } elsif (length($line) > (SHORTTEXT_LIMIT +
2442 $shorttext_exspc)
2443 && $line !~ /^:([0-7]{6}\s){2}
2444 ([[:xdigit:]]+\.*
2445 \s){2}\w+\s\w+/xms) {
2446 WARN("LONG_COMMIT_TEXT",
2447 "commit text line over " .
2448 SHORTTEXT_LIMIT .
2449 " characters\n" . $herecurr);
Steve Muckled360fe42012-03-09 11:15:24 -08002450 } elsif ($line=~/^\s*change-id:/i ||
2451 $line=~/^\s*signed-off-by:/i ||
2452 $line=~/^\s*crs-fixed:/i ||
2453 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002454 # this is a tag, there must be commit
2455 # text by now
2456 if ($commit_text_present == 0) {
2457 WARN("NO_COMMIT_TEXT",
2458 "please add commit text explaining " .
2459 "*why* the change is needed\n" .
2460 $herecurr);
2461 # prevent duplicate warnings
2462 $commit_text_present = 1;
2463 }
2464 } elsif ($line=~/\S/) {
2465 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002466 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002467 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2468 # case of non-blank line in this state handled above
2469 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002470 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002471# The Subject line doesn't have to be the last header in the patch.
2472# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2473# Per RFC5322, continuation lines must be folded, so any left-justified
2474# text which looks like a header is definitely a header.
2475 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2476 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002477 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002478 if (length($line) != 0) {
2479 WARN("NONBLANK_AFTER_SUMMARY",
2480 "non-blank line after " .
2481 "summary line\n" .
2482 $sublinenr . $here .
2483 "\n" . $subjectline .
2484 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002485 # this non-blank line may or may not
2486 # be commit text - a warning has been
2487 # generated so assume it is commit
2488 # text and move on
2489 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002490 }
David Keitele288d232016-03-23 20:57:21 -07002491 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002492 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002493 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002494 # This is the subject line. Go to
2495 # CHECK_NEXT_SHORTTEXT to wait for the commit
2496 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002497 $shorttext = CHECK_NEXT_SHORTTEXT;
2498 $subjectline = $line;
2499 $sublinenr = "#$linenr & ";
2500# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002501 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002502 WARN("LONG_SUMMARY_LINE",
2503 "summary line over " .
2504 SHORTTEXT_LIMIT .
2505 " characters\n" . $herecurr);
2506 }
2507 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002508 # Indented format, this must be the summary
2509 # line (i.e. git show). There will be no more
2510 # headers so we are now in the shorttext.
2511 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002512 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002513 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002514 WARN("LONG_SUMMARY_LINE",
2515 "summary line over " .
2516 SHORTTEXT_LIMIT .
2517 " characters\n" . $herecurr);
2518 }
2519 }
2520 }
2521
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002522 $cnt_lines++ if ($realcnt != 0);
2523
Joe Perchese518e9a2015-06-25 15:03:27 -07002524# Check if the commit log has what seems like a diff which can confuse patch
2525 if ($in_commit_log && !$commit_log_has_diff &&
2526 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2527 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2528 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2529 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2530 ERROR("DIFF_IN_COMMIT_MSG",
2531 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2532 $commit_log_has_diff = 1;
2533 }
2534
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002535# Check for incorrect file permissions
2536 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2537 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002538 if ($realfile !~ m@scripts/@ &&
2539 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002540 ERROR("EXECUTE_PERMISSIONS",
2541 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002542 }
2543 }
2544
Joe Perches20112472011-07-25 17:13:23 -07002545# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002546 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002547 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002548 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002549 }
2550
Joe Perchese0d975b2014-12-10 15:51:49 -08002551# Check if MAINTAINERS is being updated. If so, there's probably no need to
2552# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2553 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2554 $reported_maintainer_file = 1;
2555 }
2556
Joe Perches20112472011-07-25 17:13:23 -07002557# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002558 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002559 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002560 my $space_before = $1;
2561 my $sign_off = $2;
2562 my $space_after = $3;
2563 my $email = $4;
2564 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2565
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002566 if ($sign_off !~ /$signature_tags/) {
2567 WARN("BAD_SIGN_OFF",
2568 "Non-standard signature: $sign_off\n" . $herecurr);
2569 }
Joe Perches20112472011-07-25 17:13:23 -07002570 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002571 if (WARN("BAD_SIGN_OFF",
2572 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2573 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002574 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002575 "$ucfirst_sign_off $email";
2576 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002577 }
Joe Perches20112472011-07-25 17:13:23 -07002578 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if (WARN("BAD_SIGN_OFF",
2580 "'$ucfirst_sign_off' is the preferred signature form\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 }
2585
Joe Perches20112472011-07-25 17:13:23 -07002586 }
2587 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002588 if (WARN("BAD_SIGN_OFF",
2589 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2590 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002591 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002592 "$ucfirst_sign_off $email";
2593 }
Joe Perches20112472011-07-25 17:13:23 -07002594 }
2595
2596 my ($email_name, $email_address, $comment) = parse_email($email);
2597 my $suggested_email = format_email(($email_name, $email_address));
2598 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002599 ERROR("BAD_SIGN_OFF",
2600 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002601 } else {
2602 my $dequoted = $suggested_email;
2603 $dequoted =~ s/^"//;
2604 $dequoted =~ s/" </ </;
2605 # Don't force email to have quotes
2606 # Allow just an angle bracketed address
2607 if ("$dequoted$comment" ne $email &&
2608 "<$email_address>$comment" ne $email &&
2609 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002610 WARN("BAD_SIGN_OFF",
2611 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002612 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002613 }
Maya Erezec77bd62017-02-26 09:14:43 +02002614 if ($chk_author) {
2615 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2616 $qca_sign_off = 1;
2617 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2618 $codeaurora_sign_off = 1;
2619 } elsif ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
2620 WARN("BAD_SIGN_OFF",
2621 "invalid Signed-off-by identity\n" . $line );
2622 }
2623 }
Joe Perches7e51f192013-09-11 14:23:57 -07002624
2625# Check for duplicate signatures
2626 my $sig_nospace = $line;
2627 $sig_nospace =~ s/\s//g;
2628 $sig_nospace = lc($sig_nospace);
2629 if (defined $signatures{$sig_nospace}) {
2630 WARN("BAD_SIGN_OFF",
2631 "Duplicate signature\n" . $herecurr);
2632 } else {
2633 $signatures{$sig_nospace} = 1;
2634 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002635 }
2636
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002637# Check email subject for common tools that don't need to be mentioned
2638 if ($in_header_lines &&
2639 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2640 WARN("EMAIL_SUBJECT",
2641 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2642 }
2643
Joe Perches9b3189e2014-06-04 16:12:10 -07002644# Check for old stable address
2645 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2646 ERROR("STABLE_ADDRESS",
2647 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2648 }
2649
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002650# Check for unwanted Gerrit info
2651 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2652 ERROR("GERRIT_CHANGE_ID",
2653 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2654 }
2655
Joe Perches369c8dd2015-11-06 16:31:34 -08002656# Check if the commit log is in a possible stack dump
2657 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2658 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2659 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2660 # timestamp
2661 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2662 # stack dump address
2663 $commit_log_possible_stack_dump = 1;
2664 }
2665
Joe Perches2a076f42015-04-16 12:44:28 -07002666# Check for line lengths > 75 in commit log, warn once
2667 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002668 length($line) > 75 &&
2669 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2670 # file delta changes
2671 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2672 # filename then :
2673 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2674 # A Fixes: or Link: line
2675 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002676 WARN("COMMIT_LOG_LONG_LINE",
2677 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2678 $commit_log_long_line = 1;
2679 }
2680
Joe Perchesbf4daf12015-09-09 15:37:50 -07002681# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002682 if ($in_commit_log && $commit_log_possible_stack_dump &&
2683 $line =~ /^\s*$/) {
2684 $commit_log_possible_stack_dump = 0;
2685 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002686
Joe Perches0d7835f2015-02-13 14:38:35 -08002687# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002688 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002689 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002690 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002691 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002692 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2693 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002694 my $init_char = "c";
2695 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002696 my $short = 1;
2697 my $long = 0;
2698 my $case = 1;
2699 my $space = 1;
2700 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002701 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002702 my $id = '0123456789ab';
2703 my $orig_desc = "commit description";
2704 my $description = "";
2705
Joe Perchesfe043ea2015-09-09 15:37:25 -07002706 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2707 $init_char = $1;
2708 $orig_commit = lc($2);
2709 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2710 $orig_commit = lc($1);
2711 }
2712
Joe Perches0d7835f2015-02-13 14:38:35 -08002713 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2714 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2715 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2716 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2717 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2718 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002719 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002720 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2721 defined $rawlines[$linenr] &&
2722 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2723 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002724 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002725 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2726 defined $rawlines[$linenr] &&
2727 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2728 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2729 $orig_desc = $1;
2730 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2731 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002732 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002733 }
2734
2735 ($id, $description) = git_commit_info($orig_commit,
2736 $id, $orig_desc);
2737
Joe Perches19c146a2015-02-13 14:39:00 -08002738 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002739 ERROR("GIT_COMMIT_ID",
2740 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2741 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002742 }
2743
Joe Perches13f19372014-08-06 16:10:59 -07002744# Check for added, moved or deleted files
2745 if (!$reported_maintainer_file && !$in_commit_log &&
2746 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2747 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2748 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2749 (defined($1) || defined($2))))) {
2750 $reported_maintainer_file = 1;
2751 WARN("FILE_PATH_CHANGES",
2752 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2753 }
2754
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002755#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002756 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
2757 $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002758 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2759 }
2760
Andy Whitcroft00df3442007-06-08 13:47:06 -07002761# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002762 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002763 ERROR("CORRUPTED_PATCH",
2764 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002765 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002766 }
2767
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002768# Check for absolute kernel paths.
2769 if ($tree) {
2770 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2771 my $file = $1;
2772
2773 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2774 check_absolute_file($1, $herecurr)) {
2775 #
2776 } else {
2777 check_absolute_file($file, $herecurr);
2778 }
2779 }
2780 }
2781
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002782# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2783 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002784 $rawline !~ m/^$UTF8*$/) {
2785 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2786
2787 my $blank = copy_spacing($rawline);
2788 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2789 my $hereptr = "$hereline$ptr\n";
2790
Joe Perches34d99212011-07-25 17:13:26 -07002791 CHK("INVALID_UTF8",
2792 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002793 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002794
Joe Perches15662b32011-10-31 17:13:12 -07002795# Check if it's the start of a commit log
2796# (not a header line and we haven't seen the patch filename)
2797 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002798 !($rawline =~ /^\s+\S/ ||
2799 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002800 $in_header_lines = 0;
2801 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002802 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002803 }
2804
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002805# Check if there is UTF-8 in a commit log when a mail header has explicitly
2806# declined it, i.e defined some charset where it is missing.
2807 if ($in_header_lines &&
2808 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2809 $1 !~ /utf-8/i) {
2810 $non_utf8_charset = 1;
2811 }
2812
2813 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002814 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002815 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002816 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2817 }
2818
Kees Cook66b47b42014-10-13 15:51:57 -07002819# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002820 if (defined($misspellings) &&
2821 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002822 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002823 my $typo = $1;
2824 my $typo_fix = $spelling_fix{lc($typo)};
2825 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2826 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2827 my $msg_type = \&WARN;
2828 $msg_type = \&CHK if ($file);
2829 if (&{$msg_type}("TYPO_SPELLING",
2830 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2831 $fix) {
2832 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2833 }
2834 }
2835 }
2836
Andy Whitcroft306708542008-10-15 22:02:28 -07002837# ignore non-hunk lines and lines being removed
2838 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002839
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002840#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002841 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002842 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002843 if (ERROR("DOS_LINE_ENDINGS",
2844 "DOS line endings\n" . $herevet) &&
2845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002846 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002847 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002848 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2849 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002850 if (ERROR("TRAILING_WHITESPACE",
2851 "trailing whitespace\n" . $herevet) &&
2852 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002853 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002854 }
2855
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002856 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002857 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002858
Josh Triplett4783f892013-11-12 15:10:12 -08002859# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002860 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002861 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2862 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002863 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2864 my $msg_type = \&ERROR;
2865 $msg_type = \&CHK if ($file);
2866 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002867 "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 -08002868 }
2869
Andi Kleen33549572010-05-24 14:33:29 -07002870# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002871# Only applies when adding the entry originally, after that we do not have
2872# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002873 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002874 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002875 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002876 my $cnt = $realcnt;
2877 my $ln = $linenr + 1;
2878 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002879 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002880 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002881 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002882 $f = $lines[$ln - 1];
2883 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2884 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002885
2886 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002887 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002888
Joe Perches8d73e0e2014-08-06 16:10:46 -07002889 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002890 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002891 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002892 $length = -1;
2893 }
2894
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002895 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002896 $f =~ s/#.*//;
2897 $f =~ s/^\s+//;
2898 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002899 if ($f =~ /^\s*config\s/) {
2900 $is_end = 1;
2901 last;
2902 }
Andi Kleen33549572010-05-24 14:33:29 -07002903 $length++;
2904 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002905 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2906 WARN("CONFIG_DESCRIPTION",
2907 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2908 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002909 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002910 }
2911
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002912# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2913 if ($realfile =~ /Kconfig/ &&
2914 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2915 WARN("CONFIG_EXPERIMENTAL",
2916 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2917 }
2918
Christoph Jaeger327953e2015-02-13 14:38:29 -08002919# discourage the use of boolean for type definition attributes of Kconfig options
2920 if ($realfile =~ /Kconfig/ &&
2921 $line =~ /^\+\s*\bboolean\b/) {
2922 WARN("CONFIG_TYPE_BOOLEAN",
2923 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2924 }
2925
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002926 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2927 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2928 my $flag = $1;
2929 my $replacement = {
2930 'EXTRA_AFLAGS' => 'asflags-y',
2931 'EXTRA_CFLAGS' => 'ccflags-y',
2932 'EXTRA_CPPFLAGS' => 'cppflags-y',
2933 'EXTRA_LDFLAGS' => 'ldflags-y',
2934 };
2935
2936 WARN("DEPRECATED_VARIABLE",
2937 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2938 }
2939
Rob Herringbff5da42014-01-23 15:54:51 -08002940# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002941 if (defined $root &&
2942 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2943 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2944
Rob Herringbff5da42014-01-23 15:54:51 -08002945 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2946
Florian Vaussardcc933192014-04-03 14:49:27 -07002947 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2948 my $vp_file = $dt_path . "vendor-prefixes.txt";
2949
Rob Herringbff5da42014-01-23 15:54:51 -08002950 foreach my $compat (@compats) {
2951 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002952 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2953 my $compat3 = $compat;
2954 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2955 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002956 if ( $? >> 8 ) {
2957 WARN("UNDOCUMENTED_DT_STRING",
2958 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2959 }
2960
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002961 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2962 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002963 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002964 if ( $? >> 8 ) {
2965 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002966 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002967 }
2968 }
2969 }
2970
Andy Whitcroft5368df22008-10-15 22:02:27 -07002971# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002972 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002973
Joe Perches47e0c882015-06-25 15:02:57 -07002974# line length limit (with some exclusions)
2975#
2976# There are a few types of lines that may extend beyond $max_line_length:
2977# logging functions like pr_info that end in a string
2978# lines with a single string
2979# #defines that are a single string
2980#
2981# There are 3 different line length message types:
2982# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2983# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2984# LONG_LINE all other lines longer than $max_line_length
2985#
2986# if LONG_LINE is ignored, the other 2 types are also ignored
2987#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002988 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002989 my $msg_type = "LONG_LINE";
2990
2991 # Check the allowed long line types first
2992
2993 # logging functions that end in a string that starts
2994 # before $max_line_length
2995 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2996 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2997 $msg_type = "";
2998
2999 # lines with only strings (w/ possible termination)
3000 # #defines with only strings
3001 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3002 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3003 $msg_type = "";
3004
Joe Perchesd560a5f2016-08-02 14:04:31 -07003005 # EFI_GUID is another special case
3006 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
3007 $msg_type = "";
3008
Joe Perches47e0c882015-06-25 15:02:57 -07003009 # Otherwise set the alternate message types
3010
3011 # a comment starts before $max_line_length
3012 } elsif ($line =~ /($;[\s$;]*)$/ &&
3013 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3014 $msg_type = "LONG_LINE_COMMENT"
3015
3016 # a quoted string starts before $max_line_length
3017 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3018 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3019 $msg_type = "LONG_LINE_STRING"
3020 }
3021
3022 if ($msg_type ne "" &&
3023 (show_type("LONG_LINE") || show_type($msg_type))) {
3024 WARN($msg_type,
3025 "line over $max_line_length characters\n" . $herecurr);
3026 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003027 }
3028
Andy Whitcroft8905a672007-11-28 16:21:06 -08003029# check for adding lines without a newline.
3030 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003031 WARN("MISSING_EOF_NEWLINE",
3032 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003033 }
3034
Mike Frysinger42e41c52009-09-21 17:04:40 -07003035# Blackfin: use hi/lo macros
3036 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3037 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3038 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003039 ERROR("LO_MACRO",
3040 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003041 }
3042 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3043 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003044 ERROR("HI_MACRO",
3045 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003046 }
3047 }
3048
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003049# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003050 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003051
3052# at the beginning of a line any tabs must come first and anything
3053# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003054 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3055 $rawline =~ /^\+\s* \s*/) {
3056 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003057 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003058 if (ERROR("CODE_INDENT",
3059 "code indent should use tabs where possible\n" . $herevet) &&
3060 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003061 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003062 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003063 }
3064
Alberto Panizzo08e44362010-03-05 13:43:54 -08003065# check for space before tabs.
3066 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3067 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003068 if (WARN("SPACE_BEFORE_TAB",
3069 "please, no space before tabs\n" . $herevet) &&
3070 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003071 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003072 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003073 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003074 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003075 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003076 }
3077
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003078# check for && or || at the start of a line
3079 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3080 CHK("LOGICAL_CONTINUATIONS",
3081 "Logical continuations should be on the previous line\n" . $hereprev);
3082 }
3083
Joe Perchesa91e8992016-05-20 17:04:05 -07003084# check indentation starts on a tab stop
3085 if ($^V && $^V ge 5.10.0 &&
3086 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3087 my $indent = length($1);
3088 if ($indent % 8) {
3089 if (WARN("TABSTOP",
3090 "Statements should start on a tabstop\n" . $herecurr) &&
3091 $fix) {
3092 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3093 }
3094 }
3095 }
3096
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003097# check multi-line statement indentation matches previous line
3098 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003099 $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 -07003100 $prevline =~ /^\+(\t*)(.*)$/;
3101 my $oldindent = $1;
3102 my $rest = $2;
3103
3104 my $pos = pos_last_openparen($rest);
3105 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003106 $line =~ /^(\+| )([ \t]*)/;
3107 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003108
3109 my $goodtabindent = $oldindent .
3110 "\t" x ($pos / 8) .
3111 " " x ($pos % 8);
3112 my $goodspaceindent = $oldindent . " " x $pos;
3113
3114 if ($newindent ne $goodtabindent &&
3115 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003116
3117 if (CHK("PARENTHESIS_ALIGNMENT",
3118 "Alignment should match open parenthesis\n" . $hereprev) &&
3119 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003120 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003121 s/^\+[ \t]*/\+$goodtabindent/;
3122 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003123 }
3124 }
3125 }
3126
Joe Perches6ab3a972015-04-16 12:44:05 -07003127# check for space after cast like "(int) foo" or "(struct foo) bar"
3128# avoid checking a few false positives:
3129# "sizeof(<type>)" or "__alignof__(<type>)"
3130# function pointer declarations like "(*foo)(int) = bar;"
3131# structure definitions like "(struct foo) { 0 };"
3132# multiline macros that define functions
3133# known attributes or the __attribute__ keyword
3134 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3135 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003136 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003137 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003138 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003139 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003140 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003141 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003142 }
3143
Joe Perches86406b12015-09-09 15:37:41 -07003144# Block comment styles
3145# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003146 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003147 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003148 $rawline =~ /^\+[ \t]*\*/ &&
3149 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003150 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3151 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3152 }
3153
Joe Perches86406b12015-09-09 15:37:41 -07003154# Block comments use * on subsequent lines
3155 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3156 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003157 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003158 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003159 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003160 WARN("BLOCK_COMMENT_STYLE",
3161 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003162 }
3163
Joe Perches86406b12015-09-09 15:37:41 -07003164# Block comments use */ on trailing lines
3165 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003166 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3167 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3168 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003169 WARN("BLOCK_COMMENT_STYLE",
3170 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003171 }
3172
Joe Perches08eb9b82016-10-11 13:51:50 -07003173# Block comment * alignment
3174 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003175 $line =~ /^\+[ \t]*$;/ && #leading comment
3176 $rawline =~ /^\+[ \t]*\*/ && #leading *
3177 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003178 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003179 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3180 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003181 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003182 if (defined($1)) {
3183 $oldindent = expand_tabs($1);
3184 } else {
3185 $prevrawline =~ m@^\+(.*/?)\*@;
3186 $oldindent = expand_tabs($1);
3187 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003188 $rawline =~ m@^\+([ \t]*)\*@;
3189 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003190 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003191 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003192 WARN("BLOCK_COMMENT_STYLE",
3193 "Block comments should align the * on each line\n" . $hereprev);
3194 }
3195 }
3196
Joe Perches7f619192014-08-06 16:10:39 -07003197# check for missing blank lines after struct/union declarations
3198# with exceptions for various attributes and macros
3199 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3200 $line =~ /^\+/ &&
3201 !($line =~ /^\+\s*$/ ||
3202 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3203 $line =~ /^\+\s*MODULE_/i ||
3204 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3205 $line =~ /^\+[a-z_]*init/ ||
3206 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3207 $line =~ /^\+\s*DECLARE/ ||
3208 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003209 if (CHK("LINE_SPACING",
3210 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3211 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003212 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003213 }
Joe Perches7f619192014-08-06 16:10:39 -07003214 }
3215
Joe Perches365dd4e2014-08-06 16:10:42 -07003216# check for multiple consecutive blank lines
3217 if ($prevline =~ /^[\+ ]\s*$/ &&
3218 $line =~ /^\+\s*$/ &&
3219 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003220 if (CHK("LINE_SPACING",
3221 "Please don't use multiple blank lines\n" . $hereprev) &&
3222 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003223 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003224 }
3225
Joe Perches365dd4e2014-08-06 16:10:42 -07003226 $last_blank_line = $linenr;
3227 }
3228
Joe Perches3b617e32014-04-03 14:49:28 -07003229# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003230 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3231 # actual declarations
3232 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003233 # function pointer declarations
3234 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003235 # foo bar; where foo is some local typedef or #define
3236 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3237 # known declaration macros
3238 $prevline =~ /^\+\s+$declaration_macros/) &&
3239 # for "else if" which can look like "$Ident $Ident"
3240 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3241 # other possible extensions of declaration lines
3242 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3243 # not starting a section or a macro "\" extended line
3244 $prevline =~ /(?:\{\s*|\\)$/) &&
3245 # looks like a declaration
3246 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003247 # function pointer declarations
3248 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003249 # foo bar; where foo is some local typedef or #define
3250 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3251 # known declaration macros
3252 $sline =~ /^\+\s+$declaration_macros/ ||
3253 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003254 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003255 # start or end of block or continuation of declaration
3256 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3257 # bitfield continuation
3258 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3259 # other possible extensions of declaration lines
3260 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3261 # indentation of previous and current line are the same
3262 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003263 if (WARN("LINE_SPACING",
3264 "Missing a blank line after declarations\n" . $hereprev) &&
3265 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003266 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003267 }
Joe Perches3b617e32014-04-03 14:49:28 -07003268 }
3269
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003270# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003271# Exceptions:
3272# 1) within comments
3273# 2) indented preprocessor commands
3274# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003275 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003276 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003277 if (WARN("LEADING_SPACE",
3278 "please, no spaces at the start of a line\n" . $herevet) &&
3279 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003280 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003281 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003282 }
3283
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003284# check we are in a valid C source file if not then ignore this hunk
3285 next if ($realfile !~ /\.(h|c)$/);
3286
Joe Perches032a4c02014-08-06 16:10:29 -07003287# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003288# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003289# if the previous line is a break or return and is indented 1 tab more...
3290 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3291 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003292 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3293 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3294 defined $lines[$linenr] &&
3295 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003296 WARN("UNNECESSARY_ELSE",
3297 "else is not generally useful after a break or return\n" . $hereprev);
3298 }
3299 }
3300
Joe Perchesc00df192014-08-06 16:11:01 -07003301# check indentation of a line with a break;
3302# if the previous line is a goto or return and is indented the same # of tabs
3303 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3304 my $tabs = $1;
3305 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3306 WARN("UNNECESSARY_BREAK",
3307 "break is not useful after a goto or return\n" . $hereprev);
3308 }
3309 }
3310
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003311# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3312 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3313 WARN("CONFIG_EXPERIMENTAL",
3314 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3315 }
3316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003317# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003318 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003319 WARN("CVS_KEYWORD",
3320 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003321 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003322
Mike Frysinger42e41c52009-09-21 17:04:40 -07003323# Blackfin: don't use __builtin_bfin_[cs]sync
3324 if ($line =~ /__builtin_bfin_csync/) {
3325 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003326 ERROR("CSYNC",
3327 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003328 }
3329 if ($line =~ /__builtin_bfin_ssync/) {
3330 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003331 ERROR("SSYNC",
3332 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003333 }
3334
Joe Perches56e77d72013-02-21 16:44:14 -08003335# check for old HOTPLUG __dev<foo> section markings
3336 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3337 WARN("HOTPLUG_SECTION",
3338 "Using $1 is unnecessary\n" . $herecurr);
3339 }
3340
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003341# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003342 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3343 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003344#print "LINE<$line>\n";
3345 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003346 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003347 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003348 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003349 $stat =~ s/\n./\n /g;
3350 $cond =~ s/\n./\n /g;
3351
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003352#print "linenr<$linenr> <$stat>\n";
3353 # If this statement has no statement boundaries within
3354 # it there is no point in retrying a statement scan
3355 # until we hit end of it.
3356 my $frag = $stat; $frag =~ s/;+\s*$//;
3357 if ($frag !~ /(?:{|;)/) {
3358#print "skip<$line_nr_next>\n";
3359 $suppress_statement = $line_nr_next;
3360 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003361
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003362 # Find the real next line.
3363 $realline_next = $line_nr_next;
3364 if (defined $realline_next &&
3365 (!defined $lines[$realline_next - 1] ||
3366 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3367 $realline_next++;
3368 }
3369
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003370 my $s = $stat;
3371 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003372
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003373 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003374 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003375
3376 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003377 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003378
Andy Whitcroft463f2862009-09-21 17:04:34 -07003379 } elsif ($s =~ /^.\s*else\b/s) {
3380
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003381 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003382 } 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 -07003383 my $type = $1;
3384 $type =~ s/\s+/ /g;
3385 possible($type, "A:" . $s);
3386
Andy Whitcroft8905a672007-11-28 16:21:06 -08003387 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003388 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003389 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003390 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003391
3392 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003393 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003394 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003395 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003396
3397 # Check for any sort of function declaration.
3398 # int foo(something bar, other baz);
3399 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003400 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 -08003401 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402
Andy Whitcroftcf655042008-03-04 14:28:20 -08003403 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003404 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003405 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003406
Andy Whitcroft8905a672007-11-28 16:21:06 -08003407 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003408 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003409
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003410 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003411 }
3412 }
3413 }
3414
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003415 }
3416
Andy Whitcroft653d4872007-06-23 17:16:34 -07003417#
3418# Checks which may be anchored in the context.
3419#
3420
3421# Check for switch () and associated case and default
3422# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003423 if ($line=~/\bswitch\s*\(.*\)/) {
3424 my $err = '';
3425 my $sep = '';
3426 my @ctx = ctx_block_outer($linenr, $realcnt);
3427 shift(@ctx);
3428 for my $ctx (@ctx) {
3429 my ($clen, $cindent) = line_stats($ctx);
3430 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3431 $indent != $cindent) {
3432 $err .= "$sep$ctx\n";
3433 $sep = '';
3434 } else {
3435 $sep = "[...]\n";
3436 }
3437 }
3438 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003439 ERROR("SWITCH_CASE_INDENT_LEVEL",
3440 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003441 }
3442 }
3443
3444# if/while/etc brace do not go on next line, unless defining a do while loop,
3445# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003446 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 -07003447 my $pre_ctx = "$1$2";
3448
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003449 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003450
3451 if ($line =~ /^\+\t{6,}/) {
3452 WARN("DEEP_INDENTATION",
3453 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3454 }
3455
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003456 my $ctx_cnt = $realcnt - $#ctx - 1;
3457 my $ctx = join("\n", @ctx);
3458
Andy Whitcroft548596d2008-07-23 21:29:01 -07003459 my $ctx_ln = $linenr;
3460 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003461
Andy Whitcroft548596d2008-07-23 21:29:01 -07003462 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3463 defined $lines[$ctx_ln - 1] &&
3464 $lines[$ctx_ln - 1] =~ /^-/)) {
3465 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3466 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003467 $ctx_ln++;
3468 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003469
Andy Whitcroft53210162008-07-23 21:29:03 -07003470 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3471 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003472
Joe Perchesd752fcc2014-08-06 16:11:05 -07003473 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003474 ERROR("OPEN_BRACE",
3475 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003476 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003477 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003478 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3479 $ctx =~ /\)\s*\;\s*$/ &&
3480 defined $lines[$ctx_ln - 1])
3481 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003482 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3483 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003484 WARN("TRAILING_SEMICOLON",
3485 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003486 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003487 }
3488 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003489 }
3490
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003491# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003492 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 -08003493 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3494 ctx_statement_block($linenr, $realcnt, 0)
3495 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003496 my ($s, $c) = ($stat, $cond);
3497
3498 substr($s, 0, length($c), '');
3499
Joe Perches9f5af482015-09-09 15:37:30 -07003500 # remove inline comments
3501 $s =~ s/$;/ /g;
3502 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003503
3504 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003505 my @newlines = ($c =~ /\n/gs);
3506 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003507
Joe Perches9f5af482015-09-09 15:37:30 -07003508 # Make sure we remove the line prefixes as we have
3509 # none on the first line, and are going to readd them
3510 # where necessary.
3511 $s =~ s/\n./\n/gs;
3512 while ($s =~ /\n\s+\\\n/) {
3513 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3514 }
3515
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003516 # We want to check the first line inside the block
3517 # starting at the end of the conditional, so remove:
3518 # 1) any blank line termination
3519 # 2) any opening brace { on end of the line
3520 # 3) any do (...) {
3521 my $continuation = 0;
3522 my $check = 0;
3523 $s =~ s/^.*\bdo\b//;
3524 $s =~ s/^\s*{//;
3525 if ($s =~ s/^\s*\\//) {
3526 $continuation = 1;
3527 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003528 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003529 $check = 1;
3530 $cond_lines++;
3531 }
3532
3533 # Also ignore a loop construct at the end of a
3534 # preprocessor statement.
3535 if (($prevline =~ /^.\s*#\s*define\s/ ||
3536 $prevline =~ /\\\s*$/) && $continuation == 0) {
3537 $check = 0;
3538 }
3539
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003540 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003541 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003542 while ($cond_ptr != $cond_lines) {
3543 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003544
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003545 # If we see an #else/#elif then the code
3546 # is not linear.
3547 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3548 $check = 0;
3549 }
3550
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003551 # Ignore:
3552 # 1) blank lines, they should be at 0,
3553 # 2) preprocessor lines, and
3554 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003555 if ($continuation ||
3556 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003557 $s =~ /^\s*#\s*?/ ||
3558 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003559 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003560 if ($s =~ s/^.*?\n//) {
3561 $cond_lines++;
3562 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003563 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003564 }
3565
3566 my (undef, $sindent) = line_stats("+" . $s);
3567 my $stat_real = raw_line($linenr, $cond_lines);
3568
3569 # Check if either of these lines are modified, else
3570 # this is not this patch's fault.
3571 if (!defined($stat_real) ||
3572 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3573 $check = 0;
3574 }
3575 if (defined($stat_real) && $cond_lines > 1) {
3576 $stat_real = "[...]\n$stat_real";
3577 }
3578
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003579 #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 -07003580
Joe Perches9f5af482015-09-09 15:37:30 -07003581 if ($check && $s ne '' &&
3582 (($sindent % 8) != 0 ||
3583 ($sindent < $indent) ||
3584 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003585 WARN("SUSPECT_CODE_INDENT",
3586 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003587 }
3588 }
3589
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003590 # Track the 'values' across context and added lines.
3591 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003592 my ($curr_values, $curr_vars) =
3593 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003594 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003595 if ($dbg_values) {
3596 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003597 print "$linenr > .$outline\n";
3598 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003599 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003600 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003601 $prev_values = substr($curr_values, -1);
3602
Andy Whitcroft00df3442007-06-08 13:47:06 -07003603#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003604 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003605
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003606# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003607 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 -07003608 my $type = $1;
3609 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003610 $var = "" if (!defined $var);
3611 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003612 my $sign = $1;
3613 my $pointer = $2;
3614
3615 $pointer = "" if (!defined $pointer);
3616
3617 if (WARN("UNSPECIFIED_INT",
3618 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3619 $fix) {
3620 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003621 my $comp_pointer = $pointer;
3622 $comp_pointer =~ s/\s//g;
3623 $decl .= $comp_pointer;
3624 $decl = rtrim($decl) if ($var eq "");
3625 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003626 }
3627 }
3628 }
3629
Andy Whitcroft653d4872007-06-23 17:16:34 -07003630# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003631 if ($dbg_type) {
3632 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003633 ERROR("TEST_TYPE",
3634 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003635 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003636 ERROR("TEST_NOT_TYPE",
3637 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003638 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003639 next;
3640 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003641# TEST: allow direct testing of the attribute matcher.
3642 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003643 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003644 ERROR("TEST_ATTR",
3645 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003646 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003647 ERROR("TEST_NOT_ATTR",
3648 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003649 }
3650 next;
3651 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003652
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003653# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003654 if ($line =~ /^.\s*{/ &&
3655 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003656 if (ERROR("OPEN_BRACE",
3657 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003658 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3659 fix_delete_line($fixlinenr - 1, $prevrawline);
3660 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003661 my $fixedline = $prevrawline;
3662 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003663 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003664 $fixedline = $line;
3665 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003666 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003667 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003668 }
3669
Andy Whitcroft653d4872007-06-23 17:16:34 -07003670#
3671# Checks which are anchored on the added line.
3672#
3673
3674# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003675 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003676 my $path = $1;
3677 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003678 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003679 "malformed #include filename\n" . $herecurr);
3680 }
3681 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3682 ERROR("UAPI_INCLUDE",
3683 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003684 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003685 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003686
3687# no C99 // comments
3688 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003689 if (ERROR("C99_COMMENTS",
3690 "do not use C99 // comments\n" . $herecurr) &&
3691 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003692 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003693 if ($line =~ /\/\/(.*)$/) {
3694 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003695 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003696 }
3697 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003698 }
3699 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003700 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003701 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003702
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003703# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3704# the whole statement.
3705#print "APW <$lines[$realline_next - 1]>\n";
3706 if (defined $realline_next &&
3707 exists $lines[$realline_next - 1] &&
3708 !defined $suppress_export{$realline_next} &&
3709 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3710 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003711 # Handle definitions which produce identifiers with
3712 # a prefix:
3713 # XXX(foo);
3714 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003715 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003716 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003717 $name =~ /^${Ident}_$2/) {
3718#print "FOO C name<$name>\n";
3719 $suppress_export{$realline_next} = 1;
3720
3721 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003722 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003723 ^.DEFINE_$Ident\(\Q$name\E\)|
3724 ^.DECLARE_$Ident\(\Q$name\E\)|
3725 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003726 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3727 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003728 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003729#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3730 $suppress_export{$realline_next} = 2;
3731 } else {
3732 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003733 }
3734 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003735 if (!defined $suppress_export{$linenr} &&
3736 $prevline =~ /^.\s*$/ &&
3737 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3738 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3739#print "FOO B <$lines[$linenr - 1]>\n";
3740 $suppress_export{$linenr} = 2;
3741 }
3742 if (defined $suppress_export{$linenr} &&
3743 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003744 WARN("EXPORT_SYMBOL",
3745 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003746 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003747
Joe Eloff5150bda2010-08-09 17:21:00 -07003748# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003749 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003750 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003751 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003752 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003753 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003754 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003755 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003756# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003757 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003758 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003759 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003760 $herecurr) &&
3761 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003762 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003763 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003764 }
3765
Joe Perches18130872014-08-06 16:11:22 -07003766# check for misordered declarations of char/short/int/long with signed/unsigned
3767 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3768 my $tmp = trim($1);
3769 WARN("MISORDERED_TYPE",
3770 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3771 }
3772
Joe Perchescb710ec2010-10-26 14:23:20 -07003773# check for static const char * arrays.
3774 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003775 WARN("STATIC_CONST_CHAR_ARRAY",
3776 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003777 $herecurr);
3778 }
3779
3780# check for static char foo[] = "bar" declarations.
3781 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003782 WARN("STATIC_CONST_CHAR_ARRAY",
3783 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003784 $herecurr);
3785 }
3786
Joe Perchesab7e23f2015-04-16 12:44:22 -07003787# check for const <foo> const where <foo> is not a pointer or array type
3788 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3789 my $found = $1;
3790 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3791 WARN("CONST_CONST",
3792 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3793 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3794 WARN("CONST_CONST",
3795 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3796 }
3797 }
3798
Joe Perches9b0fa602014-04-03 14:49:18 -07003799# check for non-global char *foo[] = {"bar", ...} declarations.
3800 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3801 WARN("STATIC_CONST_CHAR_ARRAY",
3802 "char * array declaration might be better as static const\n" .
3803 $herecurr);
3804 }
3805
Joe Perchesb598b672015-04-16 12:44:36 -07003806# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3807 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3808 my $array = $1;
3809 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3810 my $array_div = $1;
3811 if (WARN("ARRAY_SIZE",
3812 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3813 $fix) {
3814 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3815 }
3816 }
3817 }
3818
Joe Perchesb36190c2014-01-27 17:07:18 -08003819# check for function declarations without arguments like "int foo()"
3820 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3821 if (ERROR("FUNCTION_WITHOUT_ARGS",
3822 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3823 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003824 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003825 }
3826 }
3827
Andy Whitcroft653d4872007-06-23 17:16:34 -07003828# check for new typedefs, only function parameters and sparse annotations
3829# make sense.
3830 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003831 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003832 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003833 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003834 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003835 WARN("NEW_TYPEDEFS",
3836 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003837 }
3838
3839# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003840 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003841 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3842 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003843 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003844
Andy Whitcroft65863862009-01-06 14:41:21 -08003845 # Should start with a space.
3846 $to =~ s/^(\S)/ $1/;
3847 # Should not end with a space.
3848 $to =~ s/\s+$//;
3849 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003850 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003851 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003852
Joe Perches3705ce52013-07-03 15:05:31 -07003853## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003854 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003855 if (ERROR("POINTER_LOCATION",
3856 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3857 $fix) {
3858 my $sub_from = $ident;
3859 my $sub_to = $ident;
3860 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003861 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003862 s@\Q$sub_from\E@$sub_to@;
3863 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003864 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003865 }
3866 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3867 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003868 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003869
Andy Whitcroft65863862009-01-06 14:41:21 -08003870 # Should start with a space.
3871 $to =~ s/^(\S)/ $1/;
3872 # Should not end with a space.
3873 $to =~ s/\s+$//;
3874 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003875 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003876 }
3877 # Modifiers should have spaces.
3878 $to =~ s/(\b$Modifier$)/$1 /;
3879
Joe Perches3705ce52013-07-03 15:05:31 -07003880## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003881 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003882 if (ERROR("POINTER_LOCATION",
3883 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3884 $fix) {
3885
3886 my $sub_from = $match;
3887 my $sub_to = $match;
3888 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003889 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003890 s@\Q$sub_from\E@$sub_to@;
3891 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003892 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003893 }
3894
Joe Perches9d3e3c72015-09-09 15:37:27 -07003895# avoid BUG() or BUG_ON()
3896 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3897 my $msg_type = \&WARN;
3898 $msg_type = \&CHK if ($file);
3899 &{$msg_type}("AVOID_BUG",
3900 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3901 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003902
Joe Perches9d3e3c72015-09-09 15:37:27 -07003903# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003904 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003905 WARN("LINUX_VERSION_CODE",
3906 "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 -08003907 }
3908
Joe Perches17441222011-06-15 15:08:17 -07003909# check for uses of printk_ratelimit
3910 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003911 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003912 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003913 }
3914
Andy Whitcroft00df3442007-06-08 13:47:06 -07003915# printk should use KERN_* levels. Note that follow on printk's on the
3916# same line do not need a level, so we use the current block context
3917# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003918# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003919# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003920 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003921 my $ok = 0;
3922 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3923 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003924 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003925 # with "\n" ignore it, else it is to blame
3926 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3927 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3928 $ok = 1;
3929 }
3930 last;
3931 }
3932 }
3933 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003934 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3935 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003936 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003937 }
3938
Joe Perches243f3802012-05-31 16:26:09 -07003939 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3940 my $orig = $1;
3941 my $level = lc($orig);
3942 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003943 my $level2 = $level;
3944 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003945 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003946 "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 -07003947 }
3948
3949 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003950 if (WARN("PREFER_PR_LEVEL",
3951 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3952 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003953 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003954 s/\bpr_warning\b/pr_warn/;
3955 }
Joe Perches243f3802012-05-31 16:26:09 -07003956 }
3957
Joe Perchesdc139312013-02-21 16:44:13 -08003958 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3959 my $orig = $1;
3960 my $level = lc($orig);
3961 $level = "warn" if ($level eq "warning");
3962 $level = "dbg" if ($level eq "debug");
3963 WARN("PREFER_DEV_LEVEL",
3964 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3965 }
3966
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003967# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3968# number of false positives, but assembly files are not checked, so at
3969# least the arch entry code will not trigger this warning.
3970 if ($line =~ /\bENOSYS\b/) {
3971 WARN("ENOSYS",
3972 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3973 }
3974
Andy Whitcroft653d4872007-06-23 17:16:34 -07003975# function brace can't be on same line, except for #defines of do while,
3976# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003977 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003978 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003979 if (ERROR("OPEN_BRACE",
3980 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3981 $fix) {
3982 fix_delete_line($fixlinenr, $rawline);
3983 my $fixed_line = $rawline;
3984 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3985 my $line1 = $1;
3986 my $line2 = $2;
3987 fix_insert_line($fixlinenr, ltrim($line1));
3988 fix_insert_line($fixlinenr, "\+{");
3989 if ($line2 !~ /^\s*$/) {
3990 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3991 }
3992 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003993 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003994
Andy Whitcroft8905a672007-11-28 16:21:06 -08003995# open braces for enum, union and struct go on the same line.
3996 if ($line =~ /^.\s*{/ &&
3997 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003998 if (ERROR("OPEN_BRACE",
3999 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4000 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4001 fix_delete_line($fixlinenr - 1, $prevrawline);
4002 fix_delete_line($fixlinenr, $rawline);
4003 my $fixedline = rtrim($prevrawline) . " {";
4004 fix_insert_line($fixlinenr, $fixedline);
4005 $fixedline = $rawline;
4006 $fixedline =~ s/^(.\s*){\s*/$1\t/;
4007 if ($fixedline !~ /^\+\s*$/) {
4008 fix_insert_line($fixlinenr, $fixedline);
4009 }
4010 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004011 }
4012
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004013# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004014 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4015 if (WARN("SPACING",
4016 "missing space after $1 definition\n" . $herecurr) &&
4017 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004018 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004019 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4020 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004021 }
4022
Joe Perches31070b52014-01-23 15:54:49 -08004023# Function pointer declarations
4024# check spacing between type, funcptr, and args
4025# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004026 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004027 my $declare = $1;
4028 my $pre_pointer_space = $2;
4029 my $post_pointer_space = $3;
4030 my $funcname = $4;
4031 my $post_funcname_space = $5;
4032 my $pre_args_space = $6;
4033
Joe Perches91f72e92014-04-03 14:49:12 -07004034# the $Declare variable will capture all spaces after the type
4035# so check it for a missing trailing missing space but pointer return types
4036# don't need a space so don't warn for those.
4037 my $post_declare_space = "";
4038 if ($declare =~ /(\s+)$/) {
4039 $post_declare_space = $1;
4040 $declare = rtrim($declare);
4041 }
4042 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004043 WARN("SPACING",
4044 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004045 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004046 }
4047
4048# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004049# This test is not currently implemented because these declarations are
4050# equivalent to
4051# int foo(int bar, ...)
4052# and this is form shouldn't/doesn't generate a checkpatch warning.
4053#
4054# elsif ($declare =~ /\s{2,}$/) {
4055# WARN("SPACING",
4056# "Multiple spaces after return type\n" . $herecurr);
4057# }
Joe Perches31070b52014-01-23 15:54:49 -08004058
4059# unnecessary space "type ( *funcptr)(args...)"
4060 if (defined $pre_pointer_space &&
4061 $pre_pointer_space =~ /^\s/) {
4062 WARN("SPACING",
4063 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4064 }
4065
4066# unnecessary space "type (* funcptr)(args...)"
4067 if (defined $post_pointer_space &&
4068 $post_pointer_space =~ /^\s/) {
4069 WARN("SPACING",
4070 "Unnecessary space before function pointer name\n" . $herecurr);
4071 }
4072
4073# unnecessary space "type (*funcptr )(args...)"
4074 if (defined $post_funcname_space &&
4075 $post_funcname_space =~ /^\s/) {
4076 WARN("SPACING",
4077 "Unnecessary space after function pointer name\n" . $herecurr);
4078 }
4079
4080# unnecessary space "type (*funcptr) (args...)"
4081 if (defined $pre_args_space &&
4082 $pre_args_space =~ /^\s/) {
4083 WARN("SPACING",
4084 "Unnecessary space before function pointer arguments\n" . $herecurr);
4085 }
4086
4087 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004088 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004089 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004090 }
4091 }
4092
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004093# check for spacing round square brackets; allowed:
4094# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004095# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4096# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004097 while ($line =~ /(.*?\s)\[/g) {
4098 my ($where, $prefix) = ($-[1], $1);
4099 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004100 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004101 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004102 if (ERROR("BRACKET_SPACE",
4103 "space prohibited before open square bracket '['\n" . $herecurr) &&
4104 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004105 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004106 s/^(\+.*?)\s+\[/$1\[/;
4107 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004108 }
4109 }
4110
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004111# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004112 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004113 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004114 my $ctx_before = substr($line, 0, $-[1]);
4115 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004116
4117 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004118 if ($name =~ /^(?:
4119 if|for|while|switch|return|case|
4120 volatile|__volatile__|
4121 __attribute__|format|__extension__|
4122 asm|__asm__)$/x)
4123 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004124 # cpp #define statements have non-optional spaces, ie
4125 # if there is a space between the name and the open
4126 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004127 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004128
4129 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004130 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004131
4132 # If this whole things ends with a type its most
4133 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004134 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004135
4136 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004137 if (WARN("SPACING",
4138 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4139 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004140 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004141 s/\b$name\s+\(/$name\(/;
4142 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004143 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004144 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004145
Andy Whitcroft653d4872007-06-23 17:16:34 -07004146# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004147 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004148 my $fixed_line = "";
4149 my $line_fixed = 0;
4150
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004151 my $ops = qr{
4152 <<=|>>=|<=|>=|==|!=|
4153 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4154 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004155 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004156 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004157 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004158 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004159
4160## print("element count: <" . $#elements . ">\n");
4161## foreach my $el (@elements) {
4162## print("el: <$el>\n");
4163## }
4164
4165 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004166 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004167
Joe Perches3705ce52013-07-03 15:05:31 -07004168 foreach my $el (@elements) {
4169 push(@fix_elements, substr($rawline, $off, length($el)));
4170 $off += length($el);
4171 }
4172
4173 $off = 0;
4174
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004175 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004176 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004177
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004178 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004179
4180 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4181
4182## print("n: <$n> good: <$good>\n");
4183
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004184 $off += length($elements[$n]);
4185
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004186 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004187 my $ca = substr($opline, 0, $off);
4188 my $cc = '';
4189 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4190 $cc = substr($opline, $off + length($elements[$n + 1]));
4191 }
4192 my $cb = "$ca$;$cc";
4193
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004194 my $a = '';
4195 $a = 'V' if ($elements[$n] ne '');
4196 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004197 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004198 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4199 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004200 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004201
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004202 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004203
4204 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004205 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004206 $c = 'V' if ($elements[$n + 2] ne '');
4207 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004208 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004209 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4210 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004211 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004212 } else {
4213 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004214 }
4215
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004216 my $ctx = "${a}x${c}";
4217
4218 my $at = "(ctx:$ctx)";
4219
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004220 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004221 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004222
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004223 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004224 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004225
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004226 # Get the full operator variant.
4227 my $opv = $op . substr($curr_vars, $off, 1);
4228
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004229 # Ignore operators passed as parameters.
4230 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004231 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004232
Andy Whitcroftcf655042008-03-04 14:28:20 -08004233# # Ignore comments
4234# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004235
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004236 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004237 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004238 if ($ctx !~ /.x[WEBC]/ &&
4239 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004240 if (ERROR("SPACING",
4241 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004242 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004243 $line_fixed = 1;
4244 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004245 }
4246
4247 # // is a comment
4248 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004249
Joe Perchesb00e4812014-04-03 14:49:33 -07004250 # : when part of a bitfield
4251 } elsif ($opv eq ':B') {
4252 # skip the bitfield test for now
4253
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004254 # No spaces for:
4255 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004256 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004257 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004258 if (ERROR("SPACING",
4259 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004260 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004261 if (defined $fix_elements[$n + 2]) {
4262 $fix_elements[$n + 2] =~ s/^\s+//;
4263 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004264 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004265 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004266 }
4267
Joe Perches23810972014-12-10 15:51:32 -08004268 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004269 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004270 my $rtrim_before = 0;
4271 my $space_after = 0;
4272 if ($ctx =~ /Wx./) {
4273 if (ERROR("SPACING",
4274 "space prohibited before that '$op' $at\n" . $hereptr)) {
4275 $line_fixed = 1;
4276 $rtrim_before = 1;
4277 }
4278 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004279 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004280 if (ERROR("SPACING",
4281 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004282 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004283 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004284 $space_after = 1;
4285 }
4286 }
4287 if ($rtrim_before || $space_after) {
4288 if ($rtrim_before) {
4289 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4290 } else {
4291 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4292 }
4293 if ($space_after) {
4294 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004295 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004296 }
4297
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004298 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004299 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004300 #warn "'*' is part of type\n";
4301
4302 # unary operators should have a space before and
4303 # none after. May be left adjacent to another
4304 # unary operator, or a cast
4305 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004306 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004307 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004308 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004309 if (ERROR("SPACING",
4310 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004311 if ($n != $last_after + 2) {
4312 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4313 $line_fixed = 1;
4314 }
Joe Perches3705ce52013-07-03 15:05:31 -07004315 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004316 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004317 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004318 # A unary '*' may be const
4319
4320 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004321 if (ERROR("SPACING",
4322 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004323 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004324 if (defined $fix_elements[$n + 2]) {
4325 $fix_elements[$n + 2] =~ s/^\s+//;
4326 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004327 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004328 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004329 }
4330
4331 # unary ++ and unary -- are allowed no space on one side.
4332 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004333 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004334 if (ERROR("SPACING",
4335 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004336 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004337 $line_fixed = 1;
4338 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004339 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004340 if ($ctx =~ /Wx[BE]/ ||
4341 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004342 if (ERROR("SPACING",
4343 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004344 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004345 $line_fixed = 1;
4346 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004347 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004348 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004349 if (ERROR("SPACING",
4350 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004351 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004352 if (defined $fix_elements[$n + 2]) {
4353 $fix_elements[$n + 2] =~ s/^\s+//;
4354 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004355 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004356 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004357 }
4358
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004359 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004360 } elsif ($op eq '<<' or $op eq '>>' or
4361 $op eq '&' or $op eq '^' or $op eq '|' or
4362 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004363 $op eq '*' or $op eq '/' or
4364 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004365 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004366 if ($check) {
4367 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4368 if (CHK("SPACING",
4369 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4370 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4371 $fix_elements[$n + 2] =~ s/^\s+//;
4372 $line_fixed = 1;
4373 }
4374 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4375 if (CHK("SPACING",
4376 "space preferred before that '$op' $at\n" . $hereptr)) {
4377 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4378 $line_fixed = 1;
4379 }
4380 }
4381 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004382 if (ERROR("SPACING",
4383 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004384 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4385 if (defined $fix_elements[$n + 2]) {
4386 $fix_elements[$n + 2] =~ s/^\s+//;
4387 }
Joe Perches3705ce52013-07-03 15:05:31 -07004388 $line_fixed = 1;
4389 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004390 }
4391
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004392 # A colon needs no spaces before when it is
4393 # terminating a case value or a label.
4394 } elsif ($opv eq ':C' || $opv eq ':L') {
4395 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004396 if (ERROR("SPACING",
4397 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004398 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004399 $line_fixed = 1;
4400 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004401 }
4402
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004403 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004404 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004405 my $ok = 0;
4406
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004407 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004408 if (($op eq '<' &&
4409 $cc =~ /^\S+\@\S+>/) ||
4410 ($op eq '>' &&
4411 $ca =~ /<\S+\@\S+$/))
4412 {
4413 $ok = 1;
4414 }
4415
Joe Perchese0df7e12015-04-16 12:44:53 -07004416 # for asm volatile statements
4417 # ignore a colon with another
4418 # colon immediately before or after
4419 if (($op eq ':') &&
4420 ($ca =~ /:$/ || $cc =~ /^:/)) {
4421 $ok = 1;
4422 }
4423
Joe Perches84731622013-11-12 15:10:05 -08004424 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004425 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004426 my $msg_type = \&ERROR;
4427 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4428
4429 if (&{$msg_type}("SPACING",
4430 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004431 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4432 if (defined $fix_elements[$n + 2]) {
4433 $fix_elements[$n + 2] =~ s/^\s+//;
4434 }
Joe Perches3705ce52013-07-03 15:05:31 -07004435 $line_fixed = 1;
4436 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004437 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004438 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004439 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004440
4441## print("n: <$n> GOOD: <$good>\n");
4442
4443 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004444 }
Joe Perches3705ce52013-07-03 15:05:31 -07004445
4446 if (($#elements % 2) == 0) {
4447 $fixed_line = $fixed_line . $fix_elements[$#elements];
4448 }
4449
Joe Perches194f66f2014-08-06 16:11:03 -07004450 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4451 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004452 }
4453
4454
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004455 }
4456
Joe Perches786b6322013-07-03 15:05:32 -07004457# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004458 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004459 if (WARN("SPACING",
4460 "space prohibited before semicolon\n" . $herecurr) &&
4461 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004462 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004463 s/^(\+.*\S)\s+;/$1;/;
4464 }
4465 }
4466
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004467# check for multiple assignments
4468 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004469 CHK("MULTIPLE_ASSIGNMENTS",
4470 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004471 }
4472
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004473## # check for multiple declarations, allowing for a function declaration
4474## # continuation.
4475## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4476## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4477##
4478## # Remove any bracketed sections to ensure we do not
4479## # falsly report the parameters of functions.
4480## my $ln = $line;
4481## while ($ln =~ s/\([^\(\)]*\)//g) {
4482## }
4483## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004484## WARN("MULTIPLE_DECLARATION",
4485## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004486## }
4487## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004488
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004489#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004490 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004491 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004492 if (ERROR("SPACING",
4493 "space required before the open brace '{'\n" . $herecurr) &&
4494 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004495 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004496 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004497 }
4498
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004499## # check for blank lines before declarations
4500## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4501## $prevrawline =~ /^.\s*$/) {
4502## WARN("SPACING",
4503## "No blank lines before declarations\n" . $hereprev);
4504## }
4505##
4506
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004507# closing brace should have a space following it when it has anything
4508# on the line
4509 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004510 if (ERROR("SPACING",
4511 "space required after that close brace '}'\n" . $herecurr) &&
4512 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004513 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004514 s/}((?!(?:,|;|\)))\S)/} $1/;
4515 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004516 }
4517
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004518# check spacing on square brackets
4519 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004520 if (ERROR("SPACING",
4521 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004523 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004524 s/\[\s+/\[/;
4525 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004526 }
4527 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004528 if (ERROR("SPACING",
4529 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4530 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004531 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004532 s/\s+\]/\]/;
4533 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004534 }
4535
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004536# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004537 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004538 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004539 if (ERROR("SPACING",
4540 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4541 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004542 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004543 s/\(\s+/\(/;
4544 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004545 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004546 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004547 $line !~ /for\s*\(.*;\s+\)/ &&
4548 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004549 if (ERROR("SPACING",
4550 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4551 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004552 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004553 s/\s+\)/\)/;
4554 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004555 }
4556
Joe Perchese2826fd2014-08-06 16:10:48 -07004557# check unnecessary parentheses around addressof/dereference single $Lvals
4558# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4559
4560 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004561 my $var = $1;
4562 if (CHK("UNNECESSARY_PARENTHESES",
4563 "Unnecessary parentheses around $var\n" . $herecurr) &&
4564 $fix) {
4565 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4566 }
4567 }
4568
4569# check for unnecessary parentheses around function pointer uses
4570# ie: (foo->bar)(); should be foo->bar();
4571# but not "if (foo->bar) (" to avoid some false positives
4572 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4573 my $var = $2;
4574 if (CHK("UNNECESSARY_PARENTHESES",
4575 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4576 $fix) {
4577 my $var2 = deparenthesize($var);
4578 $var2 =~ s/\s//g;
4579 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4580 }
4581 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004582
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004583#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004584 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004585 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004586 if (WARN("INDENTED_LABEL",
4587 "labels should not be indented\n" . $herecurr) &&
4588 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004589 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004590 s/^(.)\s+/$1/;
4591 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004592 }
4593
Joe Perches5b9553a2014-04-03 14:49:21 -07004594# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004595 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004596 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004597 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004598 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4599 my $value = $1;
4600 $value = deparenthesize($value);
4601 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4602 ERROR("RETURN_PARENTHESES",
4603 "return is not a function, parentheses are not required\n" . $herecurr);
4604 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004605 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004606 ERROR("SPACING",
4607 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004608 }
4609 }
Joe Perches507e5142013-11-12 15:10:13 -08004610
Joe Perchesb43ae212014-06-23 13:22:07 -07004611# unnecessary return in a void function
4612# at end-of-function, with the previous line a single leading tab, then return;
4613# and the line before that not a goto label target like "out:"
4614 if ($sline =~ /^[ \+]}\s*$/ &&
4615 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4616 $linenr >= 3 &&
4617 $lines[$linenr - 3] =~ /^[ +]/ &&
4618 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004619 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004620 "void function return statements are not generally useful\n" . $hereprev);
4621 }
Joe Perches9819cf22014-06-04 16:12:09 -07004622
Joe Perches189248d2014-01-23 15:54:47 -08004623# if statements using unnecessary parentheses - ie: if ((foo == bar))
4624 if ($^V && $^V ge 5.10.0 &&
4625 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4626 my $openparens = $1;
4627 my $count = $openparens =~ tr@\(@\(@;
4628 my $msg = "";
4629 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4630 my $comp = $4; #Not $1 because of $LvalOrFunc
4631 $msg = " - maybe == should be = ?" if ($comp eq "==");
4632 WARN("UNNECESSARY_PARENTHESES",
4633 "Unnecessary parentheses$msg\n" . $herecurr);
4634 }
4635 }
4636
Joe Perchesc5595fa2015-09-09 15:37:58 -07004637# comparisons with a constant or upper case identifier on the left
4638# avoid cases like "foo + BAR < baz"
4639# only fix matches surrounded by parentheses to avoid incorrect
4640# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4641 if ($^V && $^V ge 5.10.0 &&
4642 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4643 my $lead = $1;
4644 my $const = $2;
4645 my $comp = $3;
4646 my $to = $4;
4647 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004648 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004649 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4650 WARN("CONSTANT_COMPARISON",
4651 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4652 $fix) {
4653 if ($comp eq "<") {
4654 $newcomp = ">";
4655 } elsif ($comp eq "<=") {
4656 $newcomp = ">=";
4657 } elsif ($comp eq ">") {
4658 $newcomp = "<";
4659 } elsif ($comp eq ">=") {
4660 $newcomp = "<=";
4661 }
4662 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4663 }
4664 }
4665
Joe Perchesf34e4a42015-04-16 12:44:19 -07004666# Return of what appears to be an errno should normally be negative
4667 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004668 my $name = $1;
4669 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004670 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004671 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004672 }
4673 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004674
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004675# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004676 if ($line =~ /\b(if|while|for|switch)\(/) {
4677 if (ERROR("SPACING",
4678 "space required before the open parenthesis '('\n" . $herecurr) &&
4679 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004680 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004681 s/\b(if|while|for|switch)\(/$1 \(/;
4682 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004683 }
4684
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004685# Check for illegal assignment in if conditional -- and check for trailing
4686# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004687 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004688 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4689 ctx_statement_block($linenr, $realcnt, 0)
4690 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004691 my ($stat_next) = ctx_statement_block($line_nr_next,
4692 $remain_next, $off_next);
4693 $stat_next =~ s/\n./\n /g;
4694 ##print "stat<$stat> stat_next<$stat_next>\n";
4695
4696 if ($stat_next =~ /^\s*while\b/) {
4697 # If the statement carries leading newlines,
4698 # then count those as offsets.
4699 my ($whitespace) =
4700 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4701 my $offset =
4702 statement_rawlines($whitespace) - 1;
4703
4704 $suppress_whiletrailers{$line_nr_next +
4705 $offset} = 1;
4706 }
4707 }
4708 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004709 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004710 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004711 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004712
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004713 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004714 ERROR("ASSIGN_IN_IF",
4715 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004716 }
4717
4718 # Find out what is on the end of the line after the
4719 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004720 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004721 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004722 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004723 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4724 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004725 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004726 # Find out how long the conditional actually is.
4727 my @newlines = ($c =~ /\n/gs);
4728 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004729 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004730
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004731 $stat_real = raw_line($linenr, $cond_lines)
4732 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004733 if (defined($stat_real) && $cond_lines > 1) {
4734 $stat_real = "[...]\n$stat_real";
4735 }
4736
Joe Perches000d1cc12011-07-25 17:13:25 -07004737 ERROR("TRAILING_STATEMENTS",
4738 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004739 }
4740 }
4741
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004742# Check for bitwise tests written as boolean
4743 if ($line =~ /
4744 (?:
4745 (?:\[|\(|\&\&|\|\|)
4746 \s*0[xX][0-9]+\s*
4747 (?:\&\&|\|\|)
4748 |
4749 (?:\&\&|\|\|)
4750 \s*0[xX][0-9]+\s*
4751 (?:\&\&|\|\||\)|\])
4752 )/x)
4753 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004754 WARN("HEXADECIMAL_BOOLEAN_TEST",
4755 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004756 }
4757
Andy Whitcroft8905a672007-11-28 16:21:06 -08004758# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004759 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4760 my $s = $1;
4761 $s =~ s/$;//g; # Remove any comments
4762 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004763 ERROR("TRAILING_STATEMENTS",
4764 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004765 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004766 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004767# if should not continue a brace
4768 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004770 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004771 $herecurr);
4772 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004773# case and default should not have general statements after them
4774 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4775 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004776 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004777 \s*return\s+
4778 )/xg)
4779 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004780 ERROR("TRAILING_STATEMENTS",
4781 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004782 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004783
4784 # Check for }<nl>else {, these must be at the same
4785 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004786 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4787 $previndent == $indent) {
4788 if (ERROR("ELSE_AFTER_BRACE",
4789 "else should follow close brace '}'\n" . $hereprev) &&
4790 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4791 fix_delete_line($fixlinenr - 1, $prevrawline);
4792 fix_delete_line($fixlinenr, $rawline);
4793 my $fixedline = $prevrawline;
4794 $fixedline =~ s/}\s*$//;
4795 if ($fixedline !~ /^\+\s*$/) {
4796 fix_insert_line($fixlinenr, $fixedline);
4797 }
4798 $fixedline = $rawline;
4799 $fixedline =~ s/^(.\s*)else/$1} else/;
4800 fix_insert_line($fixlinenr, $fixedline);
4801 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004802 }
4803
Joe Perches8b8856f2014-08-06 16:11:14 -07004804 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4805 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004806 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4807
4808 # Find out what is on the end of the line after the
4809 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004810 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004811 $s =~ s/\n.*//g;
4812
4813 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004814 if (ERROR("WHILE_AFTER_BRACE",
4815 "while should follow close brace '}'\n" . $hereprev) &&
4816 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4817 fix_delete_line($fixlinenr - 1, $prevrawline);
4818 fix_delete_line($fixlinenr, $rawline);
4819 my $fixedline = $prevrawline;
4820 my $trailing = $rawline;
4821 $trailing =~ s/^\+//;
4822 $trailing = trim($trailing);
4823 $fixedline =~ s/}\s*$/} $trailing/;
4824 fix_insert_line($fixlinenr, $fixedline);
4825 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004826 }
4827 }
4828
Joe Perches95e2c602013-07-03 15:05:20 -07004829#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004830 while ($line =~ m{($Constant|$Lval)}g) {
4831 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004832
4833#gcc binary extension
4834 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004835 if (WARN("GCC_BINARY_CONSTANT",
4836 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4837 $fix) {
4838 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004839 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004840 s/\b$var\b/$hexval/;
4841 }
Joe Perches95e2c602013-07-03 15:05:20 -07004842 }
4843
4844#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004845 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004846 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004847#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004848 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004849#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004850 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4851#Ignore some three character SI units explicitly, like MiB and KHz
4852 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004853 while ($var =~ m{($Ident)}g) {
4854 my $word = $1;
4855 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004856 if ($check) {
4857 seed_camelcase_includes();
4858 if (!$file && !$camelcase_file_seeded) {
4859 seed_camelcase_file($realfile);
4860 $camelcase_file_seeded = 1;
4861 }
4862 }
Joe Perches7e781f62013-09-11 14:23:55 -07004863 if (!defined $camelcase{$word}) {
4864 $camelcase{$word} = 1;
4865 CHK("CAMELCASE",
4866 "Avoid CamelCase: <$word>\n" . $herecurr);
4867 }
Joe Perches34456862013-07-03 15:05:34 -07004868 }
Joe Perches323c1262012-12-17 16:02:07 -08004869 }
4870 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004871
4872#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004873 if ($line =~ /\#\s*define.*\\\s+$/) {
4874 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4875 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4876 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004877 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004878 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004879 }
4880
Fabian Frederick0e212e02015-04-16 12:44:25 -07004881# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4882# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004883 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004884 my $file = "$1.h";
4885 my $checkfile = "include/linux/$file";
4886 if (-f "$root/$checkfile" &&
4887 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004888 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004889 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004890 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4891 if ($asminclude > 0) {
4892 if ($realfile =~ m{^arch/}) {
4893 CHK("ARCH_INCLUDE_LINUX",
4894 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4895 } else {
4896 WARN("INCLUDE_LINUX",
4897 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4898 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004899 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004900 }
4901 }
4902
Andy Whitcroft653d4872007-06-23 17:16:34 -07004903# multi-statement macros should be enclosed in a do while loop, grab the
4904# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004905# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004906 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4907 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004908 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004909 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004910 my ($off, $dstat, $dcond, $rest);
4911 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004912 my $has_flow_statement = 0;
4913 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004914 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004915 ctx_statement_block($linenr, $realcnt, 0);
4916 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004917 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004918 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004919
Joe Perches08a28432014-10-13 15:51:55 -07004920 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004921 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004922
Joe Perchesf59b64b2016-10-11 13:52:08 -07004923 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4924 my $define_args = $1;
4925 my $define_stmt = $dstat;
4926 my @def_args = ();
4927
4928 if (defined $define_args && $define_args ne "") {
4929 $define_args = substr($define_args, 1, length($define_args) - 2);
4930 $define_args =~ s/\s*//g;
4931 @def_args = split(",", $define_args);
4932 }
4933
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004934 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004935 $dstat =~ s/\\\n.//g;
4936 $dstat =~ s/^\s*//s;
4937 $dstat =~ s/\s*$//s;
4938
4939 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004940 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4941 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004942 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004943 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004944 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004945
Gregory Bean02870be2011-05-11 09:11:12 -07004946 # Extremely long macros may fall off the end of the
4947 # available context without closing. Give a dangling
4948 # backslash the benefit of the doubt and allow it
4949 # to gobble any hanging open-parens.
4950 $dstat =~ s/\(.+\\$/1/;
4951
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004952 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004953 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4954 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004955 {
4956 }
4957
Joe Perches42e15292016-03-15 14:58:01 -07004958 # Make asm volatile uses seem like a generic function
4959 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4960
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004961 my $exceptions = qr{
4962 $Declare|
4963 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004964 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004965 DECLARE_PER_CPU|
4966 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004967 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004968 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004969 union|
4970 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004971 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004972 ^\"|\"$|
4973 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004974 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004975 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004976
4977 $ctx =~ s/\n*$//;
4978 my $herectx = $here . "\n";
4979 my $stmt_cnt = statement_rawlines($ctx);
4980
4981 for (my $n = 0; $n < $stmt_cnt; $n++) {
4982 $herectx .= raw_line($linenr, $n) . "\n";
4983 }
4984
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004985 if ($dstat ne '' &&
4986 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4987 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004988 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004989 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004990 $dstat !~ /$exceptions/ &&
4991 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004992 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004993 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004994 $dstat !~ /^for\s*$Constant$/ && # for (...)
4995 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4996 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004997 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004998 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004999 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005000
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005001 if ($dstat =~ /;/) {
5002 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5003 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5004 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005005 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005006 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005007 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005008
5009 }
Joe Perches52076492016-10-11 13:52:14 -07005010
5011 # Make $define_stmt single line, comment-free, etc
5012 my @stmt_array = split('\n', $define_stmt);
5013 my $first = 1;
5014 $define_stmt = "";
5015 foreach my $l (@stmt_array) {
5016 $l =~ s/\\$//;
5017 if ($first) {
5018 $define_stmt = $l;
5019 $first = 0;
5020 } elsif ($l =~ /^[\+ ]/) {
5021 $define_stmt .= substr($l, 1);
5022 }
5023 }
5024 $define_stmt =~ s/$;//g;
5025 $define_stmt =~ s/\s+/ /g;
5026 $define_stmt = trim($define_stmt);
5027
Joe Perchesf59b64b2016-10-11 13:52:08 -07005028# check if any macro arguments are reused (ignore '...' and 'type')
5029 foreach my $arg (@def_args) {
5030 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005031 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005032 my $tmp = $define_stmt;
5033 $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 -07005034 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005035 $tmp =~ s/\b$arg\s*\#\#//g;
5036 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5037 if ($use_cnt > 1) {
5038 CHK("MACRO_ARG_REUSE",
5039 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005040 }
5041# check if any macro arguments may have other precedence issues
5042 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5043 ((defined($1) && $1 ne ',') ||
5044 (defined($2) && $2 ne ','))) {
5045 CHK("MACRO_ARG_PRECEDENCE",
5046 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005047 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005048 }
Joe Perches5023d342012-12-17 16:01:47 -08005049
Joe Perches08a28432014-10-13 15:51:55 -07005050# check for macros with flow control, but without ## concatenation
5051# ## concatenation is commonly a macro that defines a function so ignore those
5052 if ($has_flow_statement && !$has_arg_concat) {
5053 my $herectx = $here . "\n";
5054 my $cnt = statement_rawlines($ctx);
5055
5056 for (my $n = 0; $n < $cnt; $n++) {
5057 $herectx .= raw_line($linenr, $n) . "\n";
5058 }
5059 WARN("MACRO_WITH_FLOW_CONTROL",
5060 "Macros with flow control statements should be avoided\n" . "$herectx");
5061 }
5062
Joe Perches481eb482012-12-17 16:01:56 -08005063# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005064
5065 } else {
5066 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005067 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5068 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005069 $line =~ /^\+.*\\$/) {
5070 WARN("LINE_CONTINUATIONS",
5071 "Avoid unnecessary line continuations\n" . $herecurr);
5072 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005073 }
5074
Joe Perchesb13edf72012-07-30 14:41:24 -07005075# do {} while (0) macro tests:
5076# single-statement macros do not need to be enclosed in do while (0) loop,
5077# macro should not end with a semicolon
5078 if ($^V && $^V ge 5.10.0 &&
5079 $realfile !~ m@/vmlinux.lds.h$@ &&
5080 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5081 my $ln = $linenr;
5082 my $cnt = $realcnt;
5083 my ($off, $dstat, $dcond, $rest);
5084 my $ctx = '';
5085 ($dstat, $dcond, $ln, $cnt, $off) =
5086 ctx_statement_block($linenr, $realcnt, 0);
5087 $ctx = $dstat;
5088
5089 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005090 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005091
5092 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5093 my $stmts = $2;
5094 my $semis = $3;
5095
5096 $ctx =~ s/\n*$//;
5097 my $cnt = statement_rawlines($ctx);
5098 my $herectx = $here . "\n";
5099
5100 for (my $n = 0; $n < $cnt; $n++) {
5101 $herectx .= raw_line($linenr, $n) . "\n";
5102 }
5103
Joe Perchesac8e97f2012-08-21 16:15:53 -07005104 if (($stmts =~ tr/;/;/) == 1 &&
5105 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005106 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5107 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5108 }
5109 if (defined $semis && $semis ne "") {
5110 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5111 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5112 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005113 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5114 $ctx =~ s/\n*$//;
5115 my $cnt = statement_rawlines($ctx);
5116 my $herectx = $here . "\n";
5117
5118 for (my $n = 0; $n < $cnt; $n++) {
5119 $herectx .= raw_line($linenr, $n) . "\n";
5120 }
5121
5122 WARN("TRAILING_SEMICOLON",
5123 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005124 }
5125 }
5126
Mike Frysinger080ba922009-01-06 14:41:25 -08005127# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5128# all assignments may have only one of the following with an assignment:
5129# .
5130# ALIGN(...)
5131# VMLINUX_SYMBOL(...)
5132 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005133 WARN("MISSING_VMLINUX_SYMBOL",
5134 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005135 }
5136
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005137# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005138 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5139 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005140 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005141 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005142 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5143 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005144 my @allowed = ();
5145 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005146 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005147 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005148 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005149 for my $chunk (@chunks) {
5150 my ($cond, $block) = @{$chunk};
5151
Andy Whitcroft773647a2008-03-28 14:15:58 -07005152 # If the condition carries leading newlines, then count those as offsets.
5153 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5154 my $offset = statement_rawlines($whitespace) - 1;
5155
Joe Perchesaad4f612012-03-23 15:02:19 -07005156 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005157 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5158
5159 # We have looked at and allowed this specific line.
5160 $suppress_ifbraces{$ln + $offset} = 1;
5161
5162 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005163 $ln += statement_rawlines($block) - 1;
5164
Andy Whitcroft773647a2008-03-28 14:15:58 -07005165 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005166
5167 $seen++ if ($block =~ /^\s*{/);
5168
Joe Perchesaad4f612012-03-23 15:02:19 -07005169 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005170 if (statement_lines($cond) > 1) {
5171 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005172 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005173 }
5174 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005175 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005176 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005177 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005178 if (statement_block_size($block) > 1) {
5179 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005180 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005181 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005182 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005183 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005184 if ($seen) {
5185 my $sum_allowed = 0;
5186 foreach (@allowed) {
5187 $sum_allowed += $_;
5188 }
5189 if ($sum_allowed == 0) {
5190 WARN("BRACES",
5191 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5192 } elsif ($sum_allowed != $allow &&
5193 $seen != $allow) {
5194 CHK("BRACES",
5195 "braces {} should be used on all arms of this statement\n" . $herectx);
5196 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005197 }
5198 }
5199 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005200 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005201 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005202 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005203
Andy Whitcroftcf655042008-03-04 14:28:20 -08005204 # Check the pre-context.
5205 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5206 #print "APW: ALLOWED: pre<$1>\n";
5207 $allowed = 1;
5208 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005209
5210 my ($level, $endln, @chunks) =
5211 ctx_statement_full($linenr, $realcnt, $-[0]);
5212
Andy Whitcroftcf655042008-03-04 14:28:20 -08005213 # Check the condition.
5214 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005215 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005216 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005217 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005218 }
5219 if (statement_lines($cond) > 1) {
5220 #print "APW: ALLOWED: cond<$cond>\n";
5221 $allowed = 1;
5222 }
5223 if ($block =~/\b(?:if|for|while)\b/) {
5224 #print "APW: ALLOWED: block<$block>\n";
5225 $allowed = 1;
5226 }
5227 if (statement_block_size($block) > 1) {
5228 #print "APW: ALLOWED: lines block<$block>\n";
5229 $allowed = 1;
5230 }
5231 # Check the post-context.
5232 if (defined $chunks[1]) {
5233 my ($cond, $block) = @{$chunks[1]};
5234 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005235 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005236 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005237 if ($block =~ /^\s*\{/) {
5238 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5239 $allowed = 1;
5240 }
5241 }
5242 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005243 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005244 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005245
Andy Whitcroftf0556632008-10-15 22:02:23 -07005246 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005247 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005248 }
5249
Joe Perches000d1cc12011-07-25 17:13:25 -07005250 WARN("BRACES",
5251 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005252 }
5253 }
5254
Joe Perches0979ae62012-12-17 16:01:59 -08005255# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005256 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005257 if (CHK("BRACES",
5258 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5259 $fix && $prevrawline =~ /^\+/) {
5260 fix_delete_line($fixlinenr - 1, $prevrawline);
5261 }
Joe Perches0979ae62012-12-17 16:01:59 -08005262 }
Joe Perches77b9a532013-07-03 15:05:29 -07005263 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005264 if (CHK("BRACES",
5265 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5266 $fix) {
5267 fix_delete_line($fixlinenr, $rawline);
5268 }
Joe Perches0979ae62012-12-17 16:01:59 -08005269 }
5270
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005271# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005272 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5273 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005274 WARN("VOLATILE",
5275 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005276 }
5277
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005278# Check for user-visible strings broken across lines, which breaks the ability
5279# to grep for the string. Make exceptions when the previous string ends in a
5280# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5281# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005282 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005283 $prevline =~ /"\s*$/ &&
5284 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5285 if (WARN("SPLIT_STRING",
5286 "quoted string split across lines\n" . $hereprev) &&
5287 $fix &&
5288 $prevrawline =~ /^\+.*"\s*$/ &&
5289 $last_coalesced_string_linenr != $linenr - 1) {
5290 my $extracted_string = get_quoted_string($line, $rawline);
5291 my $comma_close = "";
5292 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5293 $comma_close = $1;
5294 }
5295
5296 fix_delete_line($fixlinenr - 1, $prevrawline);
5297 fix_delete_line($fixlinenr, $rawline);
5298 my $fixedline = $prevrawline;
5299 $fixedline =~ s/"\s*$//;
5300 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5301 fix_insert_line($fixlinenr - 1, $fixedline);
5302 $fixedline = $rawline;
5303 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5304 if ($fixedline !~ /\+\s*$/) {
5305 fix_insert_line($fixlinenr, $fixedline);
5306 }
5307 $last_coalesced_string_linenr = $linenr;
5308 }
5309 }
5310
5311# check for missing a space in a string concatenation
5312 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5313 WARN('MISSING_SPACE',
5314 "break quoted strings at a space character\n" . $hereprev);
5315 }
5316
5317# check for spaces before a quoted newline
5318 if ($rawline =~ /^.*\".*\s\\n/) {
5319 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5320 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5321 $fix) {
5322 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5323 }
5324
5325 }
5326
Joe Perchesf17dba42014-10-13 15:51:51 -07005327# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005328 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005329 CHK("CONCATENATED_STRING",
5330 "Concatenated strings should use spaces between elements\n" . $herecurr);
5331 }
5332
Joe Perches90ad30e2014-12-10 15:51:59 -08005333# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005334 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005335 WARN("STRING_FRAGMENTS",
5336 "Consecutive strings are generally better as a single string\n" . $herecurr);
5337 }
5338
Joe Perches6e300752015-09-09 15:37:47 -07005339# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005340 my $string;
5341 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5342 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5343 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005344 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005345 WARN("PRINTF_L",
5346 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5347 last;
5348 }
Joe Perches6e300752015-09-09 15:37:47 -07005349 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5350 ERROR("PRINTF_0xDECIMAL",
5351 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5352 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005353 }
5354
5355# check for line continuations in quoted strings with odd counts of "
5356 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5357 WARN("LINE_CONTINUATIONS",
5358 "Avoid line continuations in quoted strings\n" . $herecurr);
5359 }
5360
Gregory Bean6c9c6842011-03-17 14:18:20 -07005361# sys_open/read/write/close are not allowed in the kernel
5362 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5363 ERROR("FILE_OPS",
5364 "$1 is inappropriate in kernel code.\n" .
5365 $herecurr);
5366 }
5367
Gregory Beana7cce0a2011-06-07 08:06:45 -07005368# filp_open is a backdoor for sys_open
5369 if ($line =~ /\b(filp_open)\b/) {
5370 ERROR("FILE_OPS",
5371 "$1 is inappropriate in kernel code.\n" .
5372 $herecurr);
5373 }
5374
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005375# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5376 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5377 ERROR("NON_RELAXED_IO",
5378 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5379 "with appropriate memory barriers instead.\n" .
5380 $herecurr);
5381 }
5382
5383# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5384 if ($line =~ /\b((in|out)([bwl]))\b/) {
5385 my ($all, $pref, $suf) = ($1, $2, $3);
5386 $pref =~ s/in/read/;
5387 $pref =~ s/out/write/;
5388 ERROR("NON_RELAXED_IO",
5389 "Use of $all is deprecated: use " .
5390 "__raw_$pref$suf\n\t" .
5391 "with appropriate memory barriers instead.\n" .
5392 $herecurr);
5393 }
5394
Gregory Bean438bf5f2011-06-15 09:32:38 -07005395# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005396 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005397 WARN("ARM_BARRIER",
5398 "Use of dsb is discouranged: prefer mb.\n" .
5399 $herecurr);
5400 }
5401
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005402# unbounded string functions are overflow risks
5403 my %str_fns = (
5404 "sprintf" => "snprintf",
5405 "strcpy" => "strlcpy",
5406 "strncpy" => "strlcpy",
5407 "strcat" => "strlcat",
5408 "strncat" => "strlcat",
5409 "vsprintf" => "vsnprintf",
5410 "strchr" => "strnchr",
5411 "strstr" => "strnstr",
5412 );
5413 foreach my $k (keys %str_fns) {
5414 if ($line =~ /\b$k\b/) {
5415 ERROR("UNBOUNDED_STRING_FNS",
5416 "Use of $k is deprecated: " .
5417 "use $str_fns{$k} instead.\n" .
5418 $herecurr);
5419 }
5420 }
5421
Andy Whitcroft00df3442007-06-08 13:47:06 -07005422# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005423 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005424 WARN("IF_0",
5425 "if this code is redundant consider removing it\n"
5426 . $herecurr);
5427 }
5428
5429# warn about #if 1
5430 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5431 WARN("IF_1",
5432 "if this code is required consider removing"
5433 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005434 }
5435
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005436# check for needless "if (<foo>) fn(<foo>)" uses
5437 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005438 my $tested = quotemeta($1);
5439 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5440 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5441 my $func = $1;
5442 if (WARN('NEEDLESS_IF',
5443 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5444 $fix) {
5445 my $do_fix = 1;
5446 my $leading_tabs = "";
5447 my $new_leading_tabs = "";
5448 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5449 $leading_tabs = $1;
5450 } else {
5451 $do_fix = 0;
5452 }
5453 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5454 $new_leading_tabs = $1;
5455 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5456 $do_fix = 0;
5457 }
5458 } else {
5459 $do_fix = 0;
5460 }
5461 if ($do_fix) {
5462 fix_delete_line($fixlinenr - 1, $prevrawline);
5463 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5464 }
5465 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005466 }
5467 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005468
Joe Perchesebfdc402014-08-06 16:10:27 -07005469# check for unnecessary "Out of Memory" messages
5470 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5471 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5472 (defined $1 || defined $3) &&
5473 $linenr > 3) {
5474 my $testval = $2;
5475 my $testline = $lines[$linenr - 3];
5476
5477 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5478# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5479
5480 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5481 WARN("OOM_MESSAGE",
5482 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5483 }
5484 }
5485
Joe Perchesf78d98f2014-10-13 15:52:01 -07005486# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005487 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005488 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5489 my $level = $1;
5490 if (WARN("UNNECESSARY_KERN_LEVEL",
5491 "Possible unnecessary $level\n" . $herecurr) &&
5492 $fix) {
5493 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5494 }
5495 }
5496
Joe Perchesabb08a52014-12-10 15:51:46 -08005497# check for mask then right shift without a parentheses
5498 if ($^V && $^V ge 5.10.0 &&
5499 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5500 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5501 WARN("MASK_THEN_SHIFT",
5502 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5503 }
5504
Joe Perchesb75ac612014-12-10 15:52:02 -08005505# check for pointer comparisons to NULL
5506 if ($^V && $^V ge 5.10.0) {
5507 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5508 my $val = $1;
5509 my $equal = "!";
5510 $equal = "" if ($4 eq "!=");
5511 if (CHK("COMPARISON_TO_NULL",
5512 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5513 $fix) {
5514 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5515 }
5516 }
5517 }
5518
Joe Perches8716de32013-09-11 14:24:05 -07005519# check for bad placement of section $InitAttribute (e.g.: __initdata)
5520 if ($line =~ /(\b$InitAttribute\b)/) {
5521 my $attr = $1;
5522 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5523 my $ptr = $1;
5524 my $var = $2;
5525 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5526 ERROR("MISPLACED_INIT",
5527 "$attr should be placed after $var\n" . $herecurr)) ||
5528 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5529 WARN("MISPLACED_INIT",
5530 "$attr should be placed after $var\n" . $herecurr))) &&
5531 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005532 $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 -07005533 }
5534 }
5535 }
5536
Joe Perchese970b882013-11-12 15:10:10 -08005537# check for $InitAttributeData (ie: __initdata) with const
5538 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5539 my $attr = $1;
5540 $attr =~ /($InitAttributePrefix)(.*)/;
5541 my $attr_prefix = $1;
5542 my $attr_type = $2;
5543 if (ERROR("INIT_ATTRIBUTE",
5544 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5545 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005546 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005547 s/$InitAttributeData/${attr_prefix}initconst/;
5548 }
5549 }
5550
5551# check for $InitAttributeConst (ie: __initconst) without const
5552 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5553 my $attr = $1;
5554 if (ERROR("INIT_ATTRIBUTE",
5555 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5556 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005557 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005558 /(^\+\s*(?:static\s+))/;
5559 $lead = rtrim($1);
5560 $lead = "$lead " if ($lead !~ /^\+$/);
5561 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005562 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005563 }
5564 }
5565
Joe Perchesc17893c2015-04-16 12:44:42 -07005566# check for __read_mostly with const non-pointer (should just be const)
5567 if ($line =~ /\b__read_mostly\b/ &&
5568 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5569 if (ERROR("CONST_READ_MOSTLY",
5570 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5571 $fix) {
5572 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5573 }
5574 }
5575
Joe Perchesfbdb8132014-04-03 14:49:14 -07005576# don't use __constant_<foo> functions outside of include/uapi/
5577 if ($realfile !~ m@^include/uapi/@ &&
5578 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5579 my $constant_func = $1;
5580 my $func = $constant_func;
5581 $func =~ s/^__constant_//;
5582 if (WARN("CONSTANT_CONVERSION",
5583 "$constant_func should be $func\n" . $herecurr) &&
5584 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005585 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005586 }
5587 }
5588
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005589# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005590 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005591 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005592 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005593 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005594 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005595 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5596 }
5597 if ($delay > 2000) {
5598 WARN("LONG_UDELAY",
5599 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005600 }
5601 }
5602
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005603# warn about unexpectedly long msleep's
5604 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5605 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005606 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005607 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005608 }
5609 }
5610
Joe Perches36ec1932013-07-03 15:05:25 -07005611# check for comparisons of jiffies
5612 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5613 WARN("JIFFIES_COMPARISON",
5614 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5615 }
5616
Joe Perches9d7a34a2013-07-03 15:05:26 -07005617# check for comparisons of get_jiffies_64()
5618 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5619 WARN("JIFFIES_COMPARISON",
5620 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5621 }
5622
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005623# check the patch for use of mdelay
5624 if ($line =~ /\bmdelay\s*\(/) {
5625 WARN("MDELAY",
5626 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5627 }
5628
Andy Whitcroft00df3442007-06-08 13:47:06 -07005629# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005630# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005631# print "#ifdef in C files should be avoided\n";
5632# print "$herecurr";
5633# $clean = 0;
5634# }
5635
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005636# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005637 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005638 if (ERROR("SPACING",
5639 "exactly one space required after that #$1\n" . $herecurr) &&
5640 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005641 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005642 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5643 }
5644
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005645 }
5646
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005647# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005648 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5649 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005650 my $which = $1;
5651 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005652 CHK("UNCOMMENTED_DEFINITION",
5653 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005654 }
5655 }
5656# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005657
5658 my $barriers = qr{
5659 mb|
5660 rmb|
5661 wmb|
5662 read_barrier_depends
5663 }x;
5664 my $barrier_stems = qr{
5665 mb__before_atomic|
5666 mb__after_atomic|
5667 store_release|
5668 load_acquire|
5669 store_mb|
5670 (?:$barriers)
5671 }x;
5672 my $all_barriers = qr{
5673 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005674 smp_(?:$barrier_stems)|
5675 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005676 }x;
5677
5678 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005679 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005680 WARN("MEMORY_BARRIER",
5681 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005682 }
5683 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005684
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005685 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5686
5687 if ($realfile !~ m@^include/asm-generic/@ &&
5688 $realfile !~ m@/barrier\.h$@ &&
5689 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5690 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5691 WARN("MEMORY_BARRIER",
5692 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5693 }
5694
Joe Perchescb426e92015-06-25 15:02:46 -07005695# check for waitqueue_active without a comment.
5696 if ($line =~ /\bwaitqueue_active\s*\(/) {
5697 if (!ctx_has_comment($first_line, $linenr)) {
5698 WARN("WAITQUEUE_ACTIVE",
5699 "waitqueue_active without comment\n" . $herecurr);
5700 }
5701 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005702
5703# Check for expedited grace periods that interrupt non-idle non-nohz
5704# online CPUs. These expedited can therefore degrade real-time response
5705# if used carelessly, and should be avoided where not absolutely
5706# needed. It is always OK to use synchronize_rcu_expedited() and
5707# synchronize_sched_expedited() at boot time (before real-time applications
5708# start) and in error situations where real-time response is compromised in
5709# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5710# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5711# Of course, nothing comes for free, and srcu_read_lock() and
5712# srcu_read_unlock() do contain full memory barriers in payment for
5713# synchronize_srcu_expedited() non-interruption properties.
5714 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5715 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5716 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5717
5718 }
5719
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005720# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005721 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005722 CHK("ARCH_DEFINES",
5723 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005724 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005725
Tobias Klauserd4977c72010-05-24 14:33:30 -07005726# Check that the storage class is at the beginning of a declaration
5727 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005728 WARN("STORAGE_CLASS",
5729 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005730 }
5731
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005732# check the location of the inline attribute, that it is between
5733# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005734 if ($line =~ /\b$Type\s+$Inline\b/ ||
5735 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005736 ERROR("INLINE_LOCATION",
5737 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005738 }
5739
Andy Whitcroft8905a672007-11-28 16:21:06 -08005740# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005741 if ($realfile !~ m@\binclude/uapi/@ &&
5742 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005743 if (WARN("INLINE",
5744 "plain inline is preferred over $1\n" . $herecurr) &&
5745 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005746 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005747
5748 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005749 }
5750
Joe Perches3d130fd2011-01-12 17:00:00 -08005751# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005752 if ($realfile !~ m@\binclude/uapi/@ &&
5753 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005754 WARN("PREFER_PACKED",
5755 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005756 }
5757
Joe Perches39b7e282011-07-25 17:13:24 -07005758# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005759 if ($realfile !~ m@\binclude/uapi/@ &&
5760 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005761 WARN("PREFER_ALIGNED",
5762 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005763 }
5764
Joe Perches5f14d3b2012-01-10 15:09:52 -08005765# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005766 if ($realfile !~ m@\binclude/uapi/@ &&
5767 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005768 if (WARN("PREFER_PRINTF",
5769 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5770 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005771 $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 -07005772
5773 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005774 }
5775
Joe Perches6061d942012-03-23 15:02:16 -07005776# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005777 if ($realfile !~ m@\binclude/uapi/@ &&
5778 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005779 if (WARN("PREFER_SCANF",
5780 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5781 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005782 $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 -07005783 }
Joe Perches6061d942012-03-23 15:02:16 -07005784 }
5785
Joe Perches619a9082014-12-10 15:51:35 -08005786# Check for __attribute__ weak, or __weak declarations (may have link issues)
5787 if ($^V && $^V ge 5.10.0 &&
5788 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5789 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5790 $line =~ /\b__weak\b/)) {
5791 ERROR("WEAK_DECLARATION",
5792 "Using weak declarations can have unintended link defects\n" . $herecurr);
5793 }
5794
Joe Perchese6176fa2015-06-25 15:02:49 -07005795# check for c99 types like uint8_t used outside of uapi/
5796 if ($realfile !~ m@\binclude/uapi/@ &&
5797 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5798 my $type = $1;
5799 if ($type =~ /\b($typeC99Typedefs)\b/) {
5800 $type = $1;
5801 my $kernel_type = 'u';
5802 $kernel_type = 's' if ($type =~ /^_*[si]/);
5803 $type =~ /(\d+)/;
5804 $kernel_type .= $1;
5805 if (CHK("PREFER_KERNEL_TYPES",
5806 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5807 $fix) {
5808 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5809 }
5810 }
5811 }
5812
Joe Perches938224b2016-01-20 14:59:15 -08005813# check for cast of C90 native int or longer types constants
5814 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5815 my $cast = $1;
5816 my $const = $2;
5817 if (WARN("TYPECAST_INT_CONSTANT",
5818 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5819 $fix) {
5820 my $suffix = "";
5821 my $newconst = $const;
5822 $newconst =~ s/${Int_type}$//;
5823 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5824 if ($cast =~ /\blong\s+long\b/) {
5825 $suffix .= 'LL';
5826 } elsif ($cast =~ /\blong\b/) {
5827 $suffix .= 'L';
5828 }
5829 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5830 }
5831 }
5832
Joe Perches8f53a9b2010-03-05 13:43:48 -08005833# check for sizeof(&)
5834 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005835 WARN("SIZEOF_ADDRESS",
5836 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005837 }
5838
Joe Perches66c80b62012-07-30 14:41:22 -07005839# check for sizeof without parenthesis
5840 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005841 if (WARN("SIZEOF_PARENTHESIS",
5842 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5843 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005844 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005845 }
Joe Perches66c80b62012-07-30 14:41:22 -07005846 }
5847
Joe Perches88982fe2012-12-17 16:02:00 -08005848# check for struct spinlock declarations
5849 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5850 WARN("USE_SPINLOCK_T",
5851 "struct spinlock should be spinlock_t\n" . $herecurr);
5852 }
5853
Joe Perchesa6962d72013-04-29 16:18:13 -07005854# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005855 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005856 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005857 $fmt =~ s/%%//g;
5858 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005859 if (WARN("PREFER_SEQ_PUTS",
5860 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5861 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005862 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005863 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005864 }
5865 }
5866
Andy Whitcroft554e1652012-01-10 15:09:57 -08005867# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005868 if ($^V && $^V ge 5.10.0 &&
5869 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005870 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005871
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005872 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005873 my $ms_val = $7;
5874 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005875
Andy Whitcroft554e1652012-01-10 15:09:57 -08005876 if ($ms_size =~ /^(0x|)0$/i) {
5877 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005878 "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 -08005879 } elsif ($ms_size =~ /^(0x|)1$/i) {
5880 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005881 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5882 }
5883 }
5884
Joe Perches98a9bba2014-01-23 15:54:52 -08005885# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005886# if ($^V && $^V ge 5.10.0 &&
5887# defined $stat &&
5888# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5889# if (WARN("PREFER_ETHER_ADDR_COPY",
5890# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5891# $fix) {
5892# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5893# }
5894# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005895
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005896# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005897# if ($^V && $^V ge 5.10.0 &&
5898# defined $stat &&
5899# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5900# WARN("PREFER_ETHER_ADDR_EQUAL",
5901# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5902# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005903
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005904# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5905# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005906# if ($^V && $^V ge 5.10.0 &&
5907# defined $stat &&
5908# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5909#
5910# my $ms_val = $7;
5911#
5912# if ($ms_val =~ /^(?:0x|)0+$/i) {
5913# if (WARN("PREFER_ETH_ZERO_ADDR",
5914# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5915# $fix) {
5916# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5917# }
5918# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5919# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5920# "Prefer eth_broadcast_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_broadcast_addr($2)/;
5923# }
5924# }
5925# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005926
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005927# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005928 if ($^V && $^V ge 5.10.0 &&
5929 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005930 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005931 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005932 my $call = $1;
5933 my $cast1 = deparenthesize($2);
5934 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005935 my $cast2 = deparenthesize($7);
5936 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005937 my $cast;
5938
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005939 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005940 $cast = "$cast1 or $cast2";
5941 } elsif ($cast1 ne "") {
5942 $cast = $cast1;
5943 } else {
5944 $cast = $cast2;
5945 }
5946 WARN("MINMAX",
5947 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005948 }
5949 }
5950
Joe Perches4a273192012-07-30 14:41:20 -07005951# check usleep_range arguments
5952 if ($^V && $^V ge 5.10.0 &&
5953 defined $stat &&
5954 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5955 my $min = $1;
5956 my $max = $7;
5957 if ($min eq $max) {
5958 WARN("USLEEP_RANGE",
5959 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5960 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5961 $min > $max) {
5962 WARN("USLEEP_RANGE",
5963 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5964 }
5965 }
5966
Joe Perches823b7942013-11-12 15:10:15 -08005967# check for naked sscanf
5968 if ($^V && $^V ge 5.10.0 &&
5969 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005970 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005971 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5972 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5973 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5974 my $lc = $stat =~ tr@\n@@;
5975 $lc = $lc + $linenr;
5976 my $stat_real = raw_line($linenr, 0);
5977 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5978 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5979 }
5980 WARN("NAKED_SSCANF",
5981 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5982 }
5983
Joe Perchesafc819a2014-06-04 16:12:08 -07005984# check for simple sscanf that should be kstrto<foo>
5985 if ($^V && $^V ge 5.10.0 &&
5986 defined $stat &&
5987 $line =~ /\bsscanf\b/) {
5988 my $lc = $stat =~ tr@\n@@;
5989 $lc = $lc + $linenr;
5990 my $stat_real = raw_line($linenr, 0);
5991 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5992 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5993 }
5994 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5995 my $format = $6;
5996 my $count = $format =~ tr@%@%@;
5997 if ($count == 1 &&
5998 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5999 WARN("SSCANF_TO_KSTRTO",
6000 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6001 }
6002 }
6003 }
6004
Joe Perches70dc8a42013-09-11 14:23:58 -07006005# check for new externs in .h files.
6006 if ($realfile =~ /\.h$/ &&
6007 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006008 if (CHK("AVOID_EXTERNS",
6009 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006010 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006011 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006012 }
6013 }
6014
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006015# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006016 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006017 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006018 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006019 my $function_name = $1;
6020 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006021
6022 my $s = $stat;
6023 if (defined $cond) {
6024 substr($s, 0, length($cond), '');
6025 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006026 if ($s =~ /^\s*;/ &&
6027 $function_name ne 'uninitialized_var')
6028 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006029 WARN("AVOID_EXTERNS",
6030 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006031 }
6032
6033 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006034 WARN("FUNCTION_ARGUMENTS",
6035 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006036 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006037
6038 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6039 $stat =~ /^.\s*extern\s+/)
6040 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006041 WARN("AVOID_EXTERNS",
6042 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006043 }
6044
Joe Perchesca0d8922016-10-11 13:52:16 -07006045 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6046 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6047 $1 ne "void") {
6048 my $args = trim($1);
6049 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6050 my $arg = trim($1);
6051 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6052 WARN("FUNCTION_ARGUMENTS",
6053 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6054 }
6055 }
6056 }
6057
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006058# checks for new __setup's
6059 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6060 my $name = $1;
6061
6062 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006063 CHK("UNDOCUMENTED_SETUP",
6064 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006065 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006066 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006067
6068# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006069 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006070 WARN("UNNECESSARY_CASTS",
6071 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006072 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006073
Joe Perchesa640d252013-07-03 15:05:21 -07006074# alloc style
6075# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6076 if ($^V && $^V ge 5.10.0 &&
6077 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6078 CHK("ALLOC_SIZEOF_STRUCT",
6079 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6080 }
6081
Joe Perches60a55362014-06-04 16:12:07 -07006082# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6083 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006084 $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 -07006085 my $oldfunc = $3;
6086 my $a1 = $4;
6087 my $a2 = $10;
6088 my $newfunc = "kmalloc_array";
6089 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006090 my $r1 = $a1;
6091 my $r2 = $a2;
6092 if ($a1 =~ /^sizeof\s*\S/) {
6093 $r1 = $a2;
6094 $r2 = $a1;
6095 }
6096 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6097 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006098 if (WARN("ALLOC_WITH_MULTIPLY",
6099 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6100 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006101 $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 -07006102
6103 }
6104 }
6105 }
6106
Joe Perches972fdea2013-04-29 16:18:12 -07006107# check for krealloc arg reuse
6108 if ($^V && $^V ge 5.10.0 &&
6109 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6110 WARN("KREALLOC_ARG_REUSE",
6111 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6112 }
6113
Joe Perches5ce59ae2013-02-21 16:44:18 -08006114# check for alloc argument mismatch
6115 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6116 WARN("ALLOC_ARRAY_ARGS",
6117 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6118 }
6119
Joe Perchescaf2a542011-01-12 16:59:56 -08006120# check for multiple semicolons
6121 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006122 if (WARN("ONE_SEMICOLON",
6123 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6124 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006125 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006126 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006127 }
6128
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006129# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6130 if ($realfile !~ m@^include/uapi/@ &&
6131 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006132 my $ull = "";
6133 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6134 if (CHK("BIT_MACRO",
6135 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6136 $fix) {
6137 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6138 }
6139 }
6140
Joe Perches2d632742016-05-20 17:04:00 -07006141# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6142 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6143 my $config = $1;
6144 if (WARN("PREFER_IS_ENABLED",
6145 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6146 $fix) {
6147 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6148 }
6149 }
6150
Joe Perchese81f2392014-08-06 16:11:25 -07006151# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006152 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6153 my $has_break = 0;
6154 my $has_statement = 0;
6155 my $count = 0;
6156 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006157 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006158 $prevline--;
6159 my $rline = $rawlines[$prevline - 1];
6160 my $fline = $lines[$prevline - 1];
6161 last if ($fline =~ /^\@\@/);
6162 next if ($fline =~ /^\-/);
6163 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6164 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6165 next if ($fline =~ /^.[\s$;]*$/);
6166 $has_statement = 1;
6167 $count++;
6168 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6169 }
6170 if (!$has_break && $has_statement) {
6171 WARN("MISSING_BREAK",
6172 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6173 }
6174 }
6175
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006176# check for switch/default statements without a break;
6177 if ($^V && $^V ge 5.10.0 &&
6178 defined $stat &&
6179 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6180 my $ctx = '';
6181 my $herectx = $here . "\n";
6182 my $cnt = statement_rawlines($stat);
6183 for (my $n = 0; $n < $cnt; $n++) {
6184 $herectx .= raw_line($linenr, $n) . "\n";
6185 }
6186 WARN("DEFAULT_NO_BREAK",
6187 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006188 }
6189
Patrick Pannutof370e252010-07-23 13:34:18 -07006190# check for return codes on error paths
6191 if ($line =~ /\breturn\s+-\d+/) {
6192 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006193 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006194 }
6195
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006196# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006197 if ($line =~ /\b__FUNCTION__\b/) {
6198 if (WARN("USE_FUNC",
6199 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6200 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006201 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006202 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006203 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006204
Joe Perches62ec8182015-02-13 14:38:18 -08006205# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6206 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6207 ERROR("DATE_TIME",
6208 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6209 }
6210
Joe Perches2c924882012-03-23 15:02:20 -07006211# check for use of yield()
6212 if ($line =~ /\byield\s*\(\s*\)/) {
6213 WARN("YIELD",
6214 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6215 }
6216
Joe Perches179f8f42013-07-03 15:05:30 -07006217# check for comparisons against true and false
6218 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6219 my $lead = $1;
6220 my $arg = $2;
6221 my $test = $3;
6222 my $otype = $4;
6223 my $trail = $5;
6224 my $op = "!";
6225
6226 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6227
6228 my $type = lc($otype);
6229 if ($type =~ /^(?:true|false)$/) {
6230 if (("$test" eq "==" && "$type" eq "true") ||
6231 ("$test" eq "!=" && "$type" eq "false")) {
6232 $op = "";
6233 }
6234
6235 CHK("BOOL_COMPARISON",
6236 "Using comparison to $otype is error prone\n" . $herecurr);
6237
6238## maybe suggesting a correct construct would better
6239## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6240
6241 }
6242 }
6243
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006244# check for semaphores initialized locked
6245 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006246 WARN("CONSIDER_COMPLETION",
6247 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006248 }
Joe Perches6712d852012-03-23 15:02:20 -07006249
Joe Perches67d0a072011-10-31 17:13:10 -07006250# recommend kstrto* over simple_strto* and strict_strto*
6251 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006252 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006253 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006254 }
Joe Perches6712d852012-03-23 15:02:20 -07006255
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006256# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006257 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006258 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006259 "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 -07006260 }
Joe Perches6712d852012-03-23 15:02:20 -07006261
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006262# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006263 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006264 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006265 WARN("CONST_STRUCT",
6266 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006267 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006268 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006269
6270# use of NR_CPUS is usually wrong
6271# ignore definitions of NR_CPUS and usage to define arrays as likely right
6272 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006273 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6274 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006275 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6276 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6277 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006278 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006279 WARN("NR_CPUS",
6280 "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 -07006281 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006282
Joe Perches52ea8502013-11-12 15:10:09 -08006283# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6284 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6285 ERROR("DEFINE_ARCH_HAS",
6286 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6287 }
6288
Joe Perchesacd93622015-02-13 14:38:38 -08006289# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6290 if ($^V && $^V ge 5.10.0 &&
6291 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6292 WARN("LIKELY_MISUSE",
6293 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6294 }
6295
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006296# whine mightly about in_atomic
6297 if ($line =~ /\bin_atomic\s*\(/) {
6298 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006299 ERROR("IN_ATOMIC",
6300 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006301 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006302 WARN("IN_ATOMIC",
6303 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006304 }
6305 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006306
Joe Perches481aea52016-05-20 17:04:08 -07006307# whine about ACCESS_ONCE
6308 if ($^V && $^V ge 5.10.0 &&
6309 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6310 my $par = $1;
6311 my $eq = $2;
6312 my $fun = $3;
6313 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6314 if (defined($eq)) {
6315 if (WARN("PREFER_WRITE_ONCE",
6316 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6317 $fix) {
6318 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6319 }
6320 } else {
6321 if (WARN("PREFER_READ_ONCE",
6322 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6323 $fix) {
6324 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6325 }
6326 }
6327 }
6328
Peter Zijlstra1704f472010-03-19 01:37:42 +01006329# check for lockdep_set_novalidate_class
6330 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6331 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6332 if ($realfile !~ m@^kernel/lockdep@ &&
6333 $realfile !~ m@^include/linux/lockdep@ &&
6334 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006335 ERROR("LOCKDEP",
6336 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006337 }
6338 }
Dave Jones88f88312011-01-12 16:59:59 -08006339
Joe Perchesb392c642015-04-16 12:44:16 -07006340 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6341 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006342 WARN("EXPORTED_WORLD_WRITABLE",
6343 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006344 }
Joe Perches24358802014-04-03 14:49:13 -07006345
Joe Perches515a2352014-04-03 14:49:24 -07006346# Mode permission misuses where it seems decimal should be octal
6347# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6348 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006349 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006350 $line =~ /$mode_perms_search/) {
6351 foreach my $entry (@mode_permission_funcs) {
6352 my $func = $entry->[0];
6353 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006354
Joe Perches459cf0a2016-10-11 13:52:19 -07006355 my $lc = $stat =~ tr@\n@@;
6356 $lc = $lc + $linenr;
6357 my $stat_real = raw_line($linenr, 0);
6358 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6359 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6360 }
6361
Joe Perches515a2352014-04-03 14:49:24 -07006362 my $skip_args = "";
6363 if ($arg_pos > 1) {
6364 $arg_pos--;
6365 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6366 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006367 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006368 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006369 my $val = $1;
6370 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006371 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6372 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006373 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006374 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006375 }
6376 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006377 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006378 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006379 }
Joe Perches24358802014-04-03 14:49:13 -07006380 }
6381 }
6382 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006383
Joe Perches459cf0a2016-10-11 13:52:19 -07006384# check for uses of S_<PERMS> that could be octal for readability
6385 if ($line =~ /\b$mode_perms_string_search\b/) {
6386 my $val = "";
6387 my $oval = "";
6388 my $to = 0;
6389 my $curpos = 0;
6390 my $lastpos = 0;
6391 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6392 $curpos = pos($line);
6393 my $match = $2;
6394 my $omatch = $1;
6395 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6396 $lastpos = $curpos;
6397 $to |= $mode_permission_string_types{$match};
6398 $val .= '\s*\|\s*' if ($val ne "");
6399 $val .= $match;
6400 $oval .= $omatch;
6401 }
6402 $oval =~ s/^\s*\|\s*//;
6403 $oval =~ s/\s*\|\s*$//;
6404 my $octal = sprintf("%04o", $to);
6405 if (WARN("SYMBOLIC_PERMS",
6406 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6407 $fix) {
6408 $fixed[$fixlinenr] =~ s/$val/$octal/;
6409 }
6410 }
6411
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006412# validate content of MODULE_LICENSE against list from include/linux/module.h
6413 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6414 my $extracted_string = get_quoted_string($line, $rawline);
6415 my $valid_licenses = qr{
6416 GPL|
6417 GPL\ v2|
6418 GPL\ and\ additional\ rights|
6419 Dual\ BSD/GPL|
6420 Dual\ MIT/GPL|
6421 Dual\ MPL/GPL|
6422 Proprietary
6423 }x;
6424 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6425 WARN("MODULE_LICENSE",
6426 "unknown module license " . $extracted_string . "\n" . $herecurr);
6427 }
6428 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006429 }
6430
Maya Erezec77bd62017-02-26 09:14:43 +02006431 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6432 WARN("BAD_SIGN_OFF",
6433 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6434 }
6435
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006436 # If we have no input at all, then there is nothing to report on
6437 # so just keep quiet.
6438 if ($#rawlines == -1) {
6439 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006440 }
6441
Andy Whitcroft8905a672007-11-28 16:21:06 -08006442 # In mailback mode only produce a report in the negative, for
6443 # things that appear to be patches.
6444 if ($mailback && ($clean == 1 || !$is_patch)) {
6445 exit(0);
6446 }
6447
6448 # This is not a patch, and we are are in 'no-patch' mode so
6449 # just keep quiet.
6450 if (!$chk_patch && !$is_patch) {
6451 exit(0);
6452 }
6453
Joe Perches06330fc2015-06-25 15:03:11 -07006454 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006455 ERROR("NOT_UNIFIED_DIFF",
6456 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006457 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006458 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006459 ERROR("MISSING_SIGN_OFF",
6460 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006461 }
6462
Andy Whitcroft8905a672007-11-28 16:21:06 -08006463 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006464 if ($summary && !($clean == 1 && $quiet == 1)) {
6465 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006466 print "total: $cnt_error errors, $cnt_warn warnings, " .
6467 (($check)? "$cnt_chk checks, " : "") .
6468 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006469 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006470
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006471 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006472 # If there were any defects found and not already fixing them
6473 if (!$clean and !$fix) {
6474 print << "EOM"
6475
6476NOTE: For some of the reported defects, checkpatch may be able to
6477 mechanically convert to the typical style using --fix or --fix-inplace.
6478EOM
6479 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006480 # If there were whitespace errors which cleanpatch can fix
6481 # then suggest that.
6482 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006483 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006484 print << "EOM"
6485
6486NOTE: Whitespace errors detected.
6487 You may wish to use scripts/cleanpatch or scripts/cleanfile
6488EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006489 }
6490 }
6491
Joe Perchesd752fcc2014-08-06 16:11:05 -07006492 if ($clean == 0 && $fix &&
6493 ("@rawlines" ne "@fixed" ||
6494 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006495 my $newfile = $filename;
6496 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006497 my $linecount = 0;
6498 my $f;
6499
Joe Perchesd752fcc2014-08-06 16:11:05 -07006500 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6501
Joe Perches3705ce52013-07-03 15:05:31 -07006502 open($f, '>', $newfile)
6503 or die "$P: Can't open $newfile for write\n";
6504 foreach my $fixed_line (@fixed) {
6505 $linecount++;
6506 if ($file) {
6507 if ($linecount > 3) {
6508 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006509 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006510 }
6511 } else {
6512 print $f $fixed_line . "\n";
6513 }
6514 }
6515 close($f);
6516
6517 if (!$quiet) {
6518 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006519
Joe Perches3705ce52013-07-03 15:05:31 -07006520Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6521
6522Do _NOT_ trust the results written to this file.
6523Do _NOT_ submit these changes without inspecting them for correctness.
6524
6525This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6526No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006527EOM
6528 }
6529 }
6530
Joe Perchesd8469f12015-06-25 15:03:00 -07006531 if ($quiet == 0) {
6532 print "\n";
6533 if ($clean == 1) {
6534 print "$vname has no obvious style problems and is ready for submission.\n";
6535 } else {
6536 print "$vname has style problems, please review.\n";
6537 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006538 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006539 return $clean;
6540}