blob: d93b6ce0fdd713f97d7b98acabeea2b468581bec [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;
15use constant IN_SHORTTEXT => 1;
16use constant AFTER_SHORTTEXT => 2;
17use constant SHORTTEXT_LIMIT => 75;
18
Andy Whitcroft0a920b52007-06-01 00:46:48 -070019my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080020my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021
Joe Perches000d1cc12011-07-25 17:13:25 -070022my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070023
24use Getopt::Long qw(:config no_auto_abbrev);
25
26my $quiet = 0;
27my $tree = 1;
28my $chk_signoff = 1;
29my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080030my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070031my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080033my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070034my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070036my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070037my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070038my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070039my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080040my $summary = 1;
41my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080042my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070043my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070044my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070045my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080046my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070047my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080048my %debug;
Joe Perches34456862013-07-03 15:05:34 -070049my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070050my %use_type = ();
51my @use = ();
52my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070053my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070054my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070055my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080056my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070057my $ignore_perl_version = 0;
58my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070059my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070060my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070061my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070062my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perches57230292015-06-25 15:03:03 -070063my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070064my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070065
66sub help {
67 my ($exitcode) = @_;
68
69 print << "EOM";
70Usage: $P [OPTION]... [FILE]...
71Version: $V
72
73Options:
74 -q, --quiet quiet
75 --no-tree run without a kernel tree
76 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080077 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070078 --patch treat FILE as patchfile (default)
79 --emacs emacs compile window format
80 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070081 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070082 -g, --git treat FILE as a single commit or git revision range
83 single git commit with:
84 <rev>
85 <rev>^
86 <rev>~n
87 multiple git commits with:
88 <rev1>..<rev2>
89 <rev1>...<rev2>
90 <rev>-<count>
91 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070092 -f, --file treat FILE as regular source file
93 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070094 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070095 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070096 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070097 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080098 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070099 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700100 --root=PATH PATH to the kernel tree root
101 --no-summary suppress the per-file summary
102 --mailback only produce a report in case of warnings/errors
103 --summary-file include the filename in summary
104 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
105 'values', 'possible', 'type', and 'attr' (default
106 is all off)
107 --test-only=WORD report only warnings/errors containing WORD
108 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700109 --fix EXPERIMENTAL - may create horrible results
110 If correctable single-line errors exist, create
111 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
112 with potential errors corrected to the preferred
113 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800114 --fix-inplace EXPERIMENTAL - may create horrible results
115 Is the same as --fix, but overwrites the input
116 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700117 --ignore-perl-version override checking of perl version. expect
118 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700119 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700120 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700121 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700122 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700123 -h, --help, --version display this help and exit
124
125When FILE is - read standard input.
126EOM
127
128 exit($exitcode);
129}
130
Joe Perches3beb42e2016-05-20 17:04:14 -0700131sub uniq {
132 my %seen;
133 return grep { !$seen{$_}++ } @_;
134}
135
136sub list_types {
137 my ($exitcode) = @_;
138
139 my $count = 0;
140
141 local $/ = undef;
142
143 open(my $script, '<', abs_path($P)) or
144 die "$P: Can't read '$P' $!\n";
145
146 my $text = <$script>;
147 close($script);
148
149 my @types = ();
150 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
151 push (@types, $_);
152 }
153 @types = sort(uniq(@types));
154 print("#\tMessage type\n\n");
155 foreach my $type (@types) {
156 print(++$count . "\t" . $type . "\n");
157 }
158
159 exit($exitcode);
160}
161
Joe Perches000d1cc12011-07-25 17:13:25 -0700162my $conf = which_conf($configuration_file);
163if (-f $conf) {
164 my @conf_args;
165 open(my $conffile, '<', "$conf")
166 or warn "$P: Can't find a readable $configuration_file file $!\n";
167
168 while (<$conffile>) {
169 my $line = $_;
170
171 $line =~ s/\s*\n?$//g;
172 $line =~ s/^\s*//g;
173 $line =~ s/\s+/ /g;
174
175 next if ($line =~ m/^\s*#/);
176 next if ($line =~ m/^\s*$/);
177
178 my @words = split(" ", $line);
179 foreach my $word (@words) {
180 last if ($word =~ m/^#/);
181 push (@conf_args, $word);
182 }
183 }
184 close($conffile);
185 unshift(@ARGV, @conf_args) if @conf_args;
186}
187
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700188GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700190 'tree!' => \$tree,
191 'signoff!' => \$chk_signoff,
192 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800193 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800195 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700196 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700197 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700198 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'subjective!' => \$check,
200 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700201 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700202 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700203 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700204 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800205 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700206 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800208 'summary!' => \$summary,
209 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800210 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700211 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800212 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700213 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800214 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700215 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700216 'codespell!' => \$codespell,
217 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700218 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700219 'h|help' => \$help,
220 'version' => \$help
221) or help(1);
222
223help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700224
Joe Perches3beb42e2016-05-20 17:04:14 -0700225list_types(0) if ($list_types);
226
Joe Perches9624b8d2014-01-23 15:54:44 -0800227$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700228$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800229
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700230my $exit = 0;
231
Dave Hansend62a2012013-09-11 14:23:56 -0700232if ($^V && $^V lt $minimum_perl_version) {
233 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
234 if (!$ignore_perl_version) {
235 exit(1);
236 }
237}
238
Allen Hubbe45107ff2016-08-02 14:04:47 -0700239#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700240if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700241 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700242}
243
Joe Perches91bfe482013-09-11 14:23:59 -0700244sub hash_save_array_words {
245 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700246
Joe Perches91bfe482013-09-11 14:23:59 -0700247 my @array = split(/,/, join(',', @$arrayRef));
248 foreach my $word (@array) {
249 $word =~ s/\s*\n?$//g;
250 $word =~ s/^\s*//g;
251 $word =~ s/\s+/ /g;
252 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700253
Joe Perches91bfe482013-09-11 14:23:59 -0700254 next if ($word =~ m/^\s*#/);
255 next if ($word =~ m/^\s*$/);
256
257 $hashRef->{$word}++;
258 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700259}
260
Joe Perches91bfe482013-09-11 14:23:59 -0700261sub hash_show_words {
262 my ($hashRef, $prefix) = @_;
263
Joe Perches3c816e42015-06-25 15:03:29 -0700264 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700265 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700266 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700267 print " $word";
268 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700269 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700270 }
271}
272
273hash_save_array_words(\%ignore_type, \@ignore);
274hash_save_array_words(\%use_type, \@use);
275
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800276my $dbg_values = 0;
277my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700278my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700279my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800280for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800281 ## no critic
282 eval "\${dbg_$key} = '$debug{$key}';";
283 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800284}
285
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700286my $rpt_cleaners = 0;
287
Andy Whitcroft8905a672007-11-28 16:21:06 -0800288if ($terse) {
289 $emacs = 1;
290 $quiet++;
291}
292
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700293if ($tree) {
294 if (defined $root) {
295 if (!top_of_kernel_tree($root)) {
296 die "$P: $root: --root does not point at a valid tree\n";
297 }
298 } else {
299 if (top_of_kernel_tree('.')) {
300 $root = '.';
301 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
302 top_of_kernel_tree($1)) {
303 $root = $1;
304 }
305 }
306
307 if (!defined $root) {
308 print "Must be run from the top-level dir. of a kernel tree\n";
309 exit(2);
310 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700311}
312
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313my $emitted_corrupt = 0;
314
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700315our $Ident = qr{
316 [A-Za-z_][A-Za-z\d_]*
317 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
318 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319our $Storage = qr{extern|static|asmlinkage};
320our $Sparse = qr{
321 __user|
322 __kernel|
323 __force|
324 __iomem|
325 __must_check|
326 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800327 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700328 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800329 __rcu|
330 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700331 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800332our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
333our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
334our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
335our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
336our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700337
Wolfram Sang52131292010-03-05 13:43:51 -0800338# Notes to $Attribute:
339# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700340our $Attribute = qr{
341 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700342 __percpu|
343 __nocast|
344 __safe|
345 __bitwise__|
346 __packed__|
347 __packed2__|
348 __naked|
349 __maybe_unused|
350 __always_unused|
351 __noreturn|
352 __used|
353 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800354 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700355 __noclone|
356 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700357 __read_mostly|
358 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700359 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700360 ____cacheline_aligned|
361 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800362 ____cacheline_internodealigned_in_smp|
363 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700365our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700366our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
368our $Lval = qr{$Ident(?:$Member)*};
369
Joe Perches95e2c602013-07-03 15:05:20 -0700370our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
371our $Binary = qr{(?i)0b[01]+$Int_type?};
372our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
373our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700374our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800375our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800376our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
377our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
378our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800379our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700380our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800381our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700382our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700383our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384our $Operators = qr{
385 <=|>=|==|!=|
386 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700387 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388 }x;
389
Joe Perches91cb5192014-04-03 14:49:32 -0700390our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
391
Joe Perchesab7e23f2015-04-16 12:44:22 -0700392our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800393our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700394our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700395our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700397our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700399our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800400
Joe Perches15662b32011-10-31 17:13:12 -0700401our $NON_ASCII_UTF8 = qr{
402 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700403 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
404 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
405 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
406 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
407 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
408 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
409}x;
410
Joe Perches15662b32011-10-31 17:13:12 -0700411our $UTF8 = qr{
412 [\x09\x0A\x0D\x20-\x7E] # ASCII
413 | $NON_ASCII_UTF8
414}x;
415
Joe Perchese6176fa2015-06-25 15:02:49 -0700416our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800417our $typeOtherOSTypedefs = qr{(?x:
418 u_(?:char|short|int|long) | # bsd
419 u(?:nchar|short|int|long) # sysv
420)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700421our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700422 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700423 atomic_t
424)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700425our $typeTypedefs = qr{(?x:
426 $typeC99Typedefs\b|
427 $typeOtherOSTypedefs\b|
428 $typeKernelTypedefs\b
429)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700430
Joe Perches6d32f7a2015-11-06 16:31:37 -0800431our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
432
Joe Perches691e6692010-03-05 13:43:51 -0800433our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700434 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700435 (?:[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 -0700436 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700437 panic|
Joe Perches06668722013-11-12 15:10:07 -0800438 MODULE_[A-Z_]+|
439 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800440)};
441
Joe Perches20112472011-07-25 17:13:23 -0700442our $signature_tags = qr{(?xi:
443 Signed-off-by:|
444 Acked-by:|
445 Tested-by:|
446 Reviewed-by:|
447 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700448 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700449 To:|
450 Cc:
451)};
452
Joe Perches18130872014-08-06 16:11:22 -0700453our @typeListMisordered = (
454 qr{char\s+(?:un)?signed},
455 qr{int\s+(?:(?:un)?signed\s+)?short\s},
456 qr{int\s+short(?:\s+(?:un)?signed)},
457 qr{short\s+int(?:\s+(?:un)?signed)},
458 qr{(?:un)?signed\s+int\s+short},
459 qr{short\s+(?:un)?signed},
460 qr{long\s+int\s+(?:un)?signed},
461 qr{int\s+long\s+(?:un)?signed},
462 qr{long\s+(?:un)?signed\s+int},
463 qr{int\s+(?:un)?signed\s+long},
464 qr{int\s+(?:un)?signed},
465 qr{int\s+long\s+long\s+(?:un)?signed},
466 qr{long\s+long\s+int\s+(?:un)?signed},
467 qr{long\s+long\s+(?:un)?signed\s+int},
468 qr{long\s+long\s+(?:un)?signed},
469 qr{long\s+(?:un)?signed},
470);
471
Andy Whitcroft8905a672007-11-28 16:21:06 -0800472our @typeList = (
473 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700474 qr{(?:(?:un)?signed\s+)?char},
475 qr{(?:(?:un)?signed\s+)?short\s+int},
476 qr{(?:(?:un)?signed\s+)?short},
477 qr{(?:(?:un)?signed\s+)?int},
478 qr{(?:(?:un)?signed\s+)?long\s+int},
479 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
480 qr{(?:(?:un)?signed\s+)?long\s+long},
481 qr{(?:(?:un)?signed\s+)?long},
482 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800483 qr{float},
484 qr{double},
485 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800486 qr{struct\s+$Ident},
487 qr{union\s+$Ident},
488 qr{enum\s+$Ident},
489 qr{${Ident}_t},
490 qr{${Ident}_handler},
491 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700492 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800493);
Joe Perches938224b2016-01-20 14:59:15 -0800494
495our $C90_int_types = qr{(?x:
496 long\s+long\s+int\s+(?:un)?signed|
497 long\s+long\s+(?:un)?signed\s+int|
498 long\s+long\s+(?:un)?signed|
499 (?:(?:un)?signed\s+)?long\s+long\s+int|
500 (?:(?:un)?signed\s+)?long\s+long|
501 int\s+long\s+long\s+(?:un)?signed|
502 int\s+(?:(?:un)?signed\s+)?long\s+long|
503
504 long\s+int\s+(?:un)?signed|
505 long\s+(?:un)?signed\s+int|
506 long\s+(?:un)?signed|
507 (?:(?:un)?signed\s+)?long\s+int|
508 (?:(?:un)?signed\s+)?long|
509 int\s+long\s+(?:un)?signed|
510 int\s+(?:(?:un)?signed\s+)?long|
511
512 int\s+(?:un)?signed|
513 (?:(?:un)?signed\s+)?int
514)};
515
Alex Dowad485ff232015-06-25 15:02:52 -0700516our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700517our @typeListWithAttr = (
518 @typeList,
519 qr{struct\s+$InitAttribute\s+$Ident},
520 qr{union\s+$InitAttribute\s+$Ident},
521);
522
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700523our @modifierList = (
524 qr{fastcall},
525);
Alex Dowad485ff232015-06-25 15:02:52 -0700526our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800527
Joe Perches24358802014-04-03 14:49:13 -0700528our @mode_permission_funcs = (
529 ["module_param", 3],
530 ["module_param_(?:array|named|string)", 4],
531 ["module_param_array_named", 5],
532 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
533 ["proc_create(?:_data|)", 2],
534 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
535);
536
Joe Perches515a2352014-04-03 14:49:24 -0700537#Create a search pattern for all these functions to speed up a loop below
538our $mode_perms_search = "";
539foreach my $entry (@mode_permission_funcs) {
540 $mode_perms_search .= '|' if ($mode_perms_search ne "");
541 $mode_perms_search .= $entry->[0];
542}
543
Joe Perchesb392c642015-04-16 12:44:16 -0700544our $mode_perms_world_writable = qr{
545 S_IWUGO |
546 S_IWOTH |
547 S_IRWXUGO |
548 S_IALLUGO |
549 0[0-7][0-7][2367]
550}x;
551
Wolfram Sang7840a942010-08-09 17:20:57 -0700552our $allowed_asm_includes = qr{(?x:
553 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700554 memory|
555 time|
556 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700557)};
558# memory.h: ARM has a custom one
559
Kees Cook66b47b42014-10-13 15:51:57 -0700560# Load common spelling mistakes and build regular expression list.
561my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700562my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700563
Joe Perches36061e32014-12-10 15:51:43 -0800564if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800565 while (<$spelling>) {
566 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700567
Joe Perches36061e32014-12-10 15:51:43 -0800568 $line =~ s/\s*\n?$//g;
569 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700570
Joe Perches36061e32014-12-10 15:51:43 -0800571 next if ($line =~ m/^\s*#/);
572 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700573
Joe Perches36061e32014-12-10 15:51:43 -0800574 my ($suspect, $fix) = split(/\|\|/, $line);
575
Joe Perches36061e32014-12-10 15:51:43 -0800576 $spelling_fix{$suspect} = $fix;
577 }
578 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800579} else {
580 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700581}
Kees Cook66b47b42014-10-13 15:51:57 -0700582
Joe Perchesebfd7d62015-04-16 12:44:14 -0700583if ($codespell) {
584 if (open(my $spelling, '<', $codespellfile)) {
585 while (<$spelling>) {
586 my $line = $_;
587
588 $line =~ s/\s*\n?$//g;
589 $line =~ s/^\s*//g;
590
591 next if ($line =~ m/^\s*#/);
592 next if ($line =~ m/^\s*$/);
593 next if ($line =~ m/, disabled/i);
594
595 $line =~ s/,.*$//;
596
597 my ($suspect, $fix) = split(/->/, $line);
598
599 $spelling_fix{$suspect} = $fix;
600 }
601 close($spelling);
602 } else {
603 warn "No codespell typos will be found - file '$codespellfile': $!\n";
604 }
605}
606
607$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
608
Andy Whitcroft8905a672007-11-28 16:21:06 -0800609sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700610 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
611 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700612 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700613 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700614 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700615 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700616 (?:$typeTypedefs\b)|
617 (?:${all}\b)
618 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800619 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700620 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800621 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800622 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700623 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700624 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800625 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700626 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800627 }x;
Joe Perches18130872014-08-06 16:11:22 -0700628 $NonptrTypeMisordered = qr{
629 (?:$Modifier\s+|const\s+)*
630 (?:
631 (?:${Misordered}\b)
632 )
633 (?:\s+$Modifier|\s+const)*
634 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700635 $NonptrTypeWithAttr = qr{
636 (?:$Modifier\s+|const\s+)*
637 (?:
638 (?:typeof|__typeof__)\s*\([^\)]*\)|
639 (?:$typeTypedefs\b)|
640 (?:${allWithAttr}\b)
641 )
642 (?:\s+$Modifier|\s+const)*
643 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800644 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700645 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700646 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700647 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800648 }x;
Joe Perches18130872014-08-06 16:11:22 -0700649 $TypeMisordered = qr{
650 $NonptrTypeMisordered
651 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
652 (?:\s+$Inline|\s+$Modifier)*
653 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700654 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700655 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800656}
657build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700658
Joe Perches7d2367a2011-07-25 17:13:22 -0700659our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700660
661# Using $balanced_parens, $LvalOrFunc, or $FuncArg
662# requires at least perl version v5.10.0
663# Any use must be runtime checked with $^V
664
665our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700666our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800667our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700668
Joe Perchesf8422302014-08-06 16:11:31 -0700669our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700670 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700671 (?:$Storage\s+)?LIST_HEAD\s*\(|
672 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
673)};
674
Joe Perches7d2367a2011-07-25 17:13:22 -0700675sub deparenthesize {
676 my ($string) = @_;
677 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700678
679 while ($string =~ /^\s*\(.*\)\s*$/) {
680 $string =~ s@^\s*\(\s*@@;
681 $string =~ s@\s*\)\s*$@@;
682 }
683
Joe Perches7d2367a2011-07-25 17:13:22 -0700684 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700685
Joe Perches7d2367a2011-07-25 17:13:22 -0700686 return $string;
687}
688
Joe Perches34456862013-07-03 15:05:34 -0700689sub seed_camelcase_file {
690 my ($file) = @_;
691
692 return if (!(-f $file));
693
694 local $/;
695
696 open(my $include_file, '<', "$file")
697 or warn "$P: Can't read '$file' $!\n";
698 my $text = <$include_file>;
699 close($include_file);
700
701 my @lines = split('\n', $text);
702
703 foreach my $line (@lines) {
704 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
705 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
706 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800707 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
708 $camelcase{$1} = 1;
709 } 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 -0700710 $camelcase{$1} = 1;
711 }
712 }
713}
714
715my $camelcase_seeded = 0;
716sub seed_camelcase_includes {
717 return if ($camelcase_seeded);
718
719 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700720 my $camelcase_cache = "";
721 my @include_files = ();
722
723 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700724
Richard Genoud3645e322014-02-10 14:25:32 -0800725 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700726 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
727 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700728 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700729 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700730 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700731 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700732 @include_files = split('\n', $files);
733 foreach my $file (@include_files) {
734 my $date = POSIX::strftime("%Y%m%d%H%M",
735 localtime((stat $file)[9]));
736 $last_mod_date = $date if ($last_mod_date < $date);
737 }
738 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700739 }
Joe Perchesc707a812013-07-08 16:00:43 -0700740
741 if ($camelcase_cache ne "" && -f $camelcase_cache) {
742 open(my $camelcase_file, '<', "$camelcase_cache")
743 or warn "$P: Can't read '$camelcase_cache' $!\n";
744 while (<$camelcase_file>) {
745 chomp;
746 $camelcase{$_} = 1;
747 }
748 close($camelcase_file);
749
750 return;
751 }
752
Richard Genoud3645e322014-02-10 14:25:32 -0800753 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700754 $files = `git ls-files "include/*.h"`;
755 @include_files = split('\n', $files);
756 }
757
Joe Perches34456862013-07-03 15:05:34 -0700758 foreach my $file (@include_files) {
759 seed_camelcase_file($file);
760 }
Joe Perches351b2a12013-07-03 15:05:36 -0700761
Joe Perchesc707a812013-07-08 16:00:43 -0700762 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700763 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700764 open(my $camelcase_file, '>', "$camelcase_cache")
765 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700766 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
767 print $camelcase_file ("$_\n");
768 }
769 close($camelcase_file);
770 }
Joe Perches34456862013-07-03 15:05:34 -0700771}
772
Joe Perchesd311cd42014-08-06 16:10:57 -0700773sub git_commit_info {
774 my ($commit, $id, $desc) = @_;
775
776 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
777
778 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
779 $output =~ s/^\s*//gm;
780 my @lines = split("\n", $output);
781
Joe Perches0d7835f2015-02-13 14:38:35 -0800782 return ($id, $desc) if ($#lines < 0);
783
Joe Perchesd311cd42014-08-06 16:10:57 -0700784 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
785# Maybe one day convert this block of bash into something that returns
786# all matching commit ids, but it's very slow...
787#
788# echo "checking commits $1..."
789# git rev-list --remotes | grep -i "^$1" |
790# while read line ; do
791# git log --format='%H %s' -1 $line |
792# echo "commit $(cut -c 1-12,41-)"
793# done
794 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
795 } else {
796 $id = substr($lines[0], 0, 12);
797 $desc = substr($lines[0], 41);
798 }
799
800 return ($id, $desc);
801}
802
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700803$chk_signoff = 0 if ($file);
804
Andy Whitcroft00df3442007-06-08 13:47:06 -0700805my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800806my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700807my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700808my @fixed_inserted = ();
809my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700810my $fixlinenr = -1;
811
Du, Changbin4a593c32016-05-20 17:04:16 -0700812# If input is git commits, extract all commits from the commit expressions.
813# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
814die "$P: No git repository found\n" if ($git && !-e ".git");
815
816if ($git) {
817 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700818 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700819 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700820 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
821 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700822 } elsif ($commit_expr =~ m/\.\./) {
823 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700824 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700825 $git_range = "-1 $commit_expr";
826 }
827 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
828 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700829 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
830 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700831 my $sha1 = $1;
832 my $subject = $2;
833 unshift(@commits, $sha1);
834 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700835 }
836 }
837 die "$P: no git commits after extraction!\n" if (@commits == 0);
838 @ARGV = @commits;
839}
840
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800841my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700842for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800843 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700844 if ($git) {
845 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
846 die "$P: $filename: git format-patch failed - $!\n";
847 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800848 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700849 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800850 } elsif ($filename eq '-') {
851 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700852 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800853 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700854 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700855 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800856 if ($filename eq '-') {
857 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700858 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700859 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800860 } else {
861 $vname = $filename;
862 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800863 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700864 chomp;
865 push(@rawlines, $_);
866 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800867 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700868
869 if ($#ARGV > 0 && $quiet == 0) {
870 print '-' x length($vname) . "\n";
871 print "$vname\n";
872 print '-' x length($vname) . "\n";
873 }
874
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800875 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700876 $exit = 1;
877 }
878 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800879 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700880 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700881 @fixed_inserted = ();
882 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700883 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700884 @modifierListFile = ();
885 @typeListFile = ();
886 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700887}
888
Joe Perchesd8469f12015-06-25 15:03:00 -0700889if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700890 hash_show_words(\%use_type, "Used");
891 hash_show_words(\%ignore_type, "Ignored");
892
Joe Perchesd8469f12015-06-25 15:03:00 -0700893 if ($^V lt 5.10.0) {
894 print << "EOM"
895
896NOTE: perl $^V is not modern enough to detect all possible issues.
897 An upgrade to at least perl v5.10.0 is suggested.
898EOM
899 }
900 if ($exit) {
901 print << "EOM"
902
903NOTE: If any of the errors are false positives, please report
904 them to the maintainer, see CHECKPATCH in MAINTAINERS.
905EOM
906 }
907}
908
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700909exit($exit);
910
911sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700912 my ($root) = @_;
913
914 my @tree_check = (
915 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
916 "README", "Documentation", "arch", "include", "drivers",
917 "fs", "init", "ipc", "kernel", "lib", "scripts",
918 );
919
920 foreach my $check (@tree_check) {
921 if (! -e $root . '/' . $check) {
922 return 0;
923 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700924 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700925 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700926}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700927
Joe Perches20112472011-07-25 17:13:23 -0700928sub parse_email {
929 my ($formatted_email) = @_;
930
931 my $name = "";
932 my $address = "";
933 my $comment = "";
934
935 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
936 $name = $1;
937 $address = $2;
938 $comment = $3 if defined $3;
939 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
940 $address = $1;
941 $comment = $2 if defined $2;
942 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
943 $address = $1;
944 $comment = $2 if defined $2;
945 $formatted_email =~ s/$address.*$//;
946 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700947 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700948 $name =~ s/^\"|\"$//g;
949 # If there's a name left after stripping spaces and
950 # leading quotes, and the address doesn't have both
951 # leading and trailing angle brackets, the address
952 # is invalid. ie:
953 # "joe smith joe@smith.com" bad
954 # "joe smith <joe@smith.com" bad
955 if ($name ne "" && $address !~ /^<[^>]+>$/) {
956 $name = "";
957 $address = "";
958 $comment = "";
959 }
960 }
961
Joe Perches3705ce52013-07-03 15:05:31 -0700962 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700963 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700964 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700965 $address =~ s/^\<|\>$//g;
966
967 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
968 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
969 $name = "\"$name\"";
970 }
971
972 return ($name, $address, $comment);
973}
974
975sub format_email {
976 my ($name, $address) = @_;
977
978 my $formatted_email;
979
Joe Perches3705ce52013-07-03 15:05:31 -0700980 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700981 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700982 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700983
984 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
985 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
986 $name = "\"$name\"";
987 }
988
989 if ("$name" eq "") {
990 $formatted_email = "$address";
991 } else {
992 $formatted_email = "$name <$address>";
993 }
994
995 return $formatted_email;
996}
997
Joe Perchesd311cd42014-08-06 16:10:57 -0700998sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700999 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001000
Joe Perchesbd474ca2014-08-06 16:11:10 -07001001 foreach my $path (split(/:/, $ENV{PATH})) {
1002 if (-e "$path/$bin") {
1003 return "$path/$bin";
1004 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001005 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001006
Joe Perchesbd474ca2014-08-06 16:11:10 -07001007 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001008}
1009
Joe Perches000d1cc12011-07-25 17:13:25 -07001010sub which_conf {
1011 my ($conf) = @_;
1012
1013 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1014 if (-e "$path/$conf") {
1015 return "$path/$conf";
1016 }
1017 }
1018
1019 return "";
1020}
1021
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001022sub expand_tabs {
1023 my ($str) = @_;
1024
1025 my $res = '';
1026 my $n = 0;
1027 for my $c (split(//, $str)) {
1028 if ($c eq "\t") {
1029 $res .= ' ';
1030 $n++;
1031 for (; ($n % 8) != 0; $n++) {
1032 $res .= ' ';
1033 }
1034 next;
1035 }
1036 $res .= $c;
1037 $n++;
1038 }
1039
1040 return $res;
1041}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001043 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001044 return $res;
1045}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001046
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001047sub line_stats {
1048 my ($line) = @_;
1049
1050 # Drop the diff line leader and expand tabs
1051 $line =~ s/^.//;
1052 $line = expand_tabs($line);
1053
1054 # Pick the indent from the front of the line.
1055 my ($white) = ($line =~ /^(\s*)/);
1056
1057 return (length($line), length($white));
1058}
1059
Andy Whitcroft773647a2008-03-28 14:15:58 -07001060my $sanitise_quote = '';
1061
1062sub sanitise_line_reset {
1063 my ($in_comment) = @_;
1064
1065 if ($in_comment) {
1066 $sanitise_quote = '*/';
1067 } else {
1068 $sanitise_quote = '';
1069 }
1070}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001071sub sanitise_line {
1072 my ($line) = @_;
1073
1074 my $res = '';
1075 my $l = '';
1076
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001077 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001078 my $off = 0;
1079 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001080
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 # Always copy over the diff marker.
1082 $res = substr($line, 0, 1);
1083
1084 for ($off = 1; $off < length($line); $off++) {
1085 $c = substr($line, $off, 1);
1086
1087 # Comments we are wacking completly including the begin
1088 # and end, all to $;.
1089 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1090 $sanitise_quote = '*/';
1091
1092 substr($res, $off, 2, "$;$;");
1093 $off++;
1094 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001095 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001096 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001097 $sanitise_quote = '';
1098 substr($res, $off, 2, "$;$;");
1099 $off++;
1100 next;
1101 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001102 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1103 $sanitise_quote = '//';
1104
1105 substr($res, $off, 2, $sanitise_quote);
1106 $off++;
1107 next;
1108 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001109
1110 # A \ in a string means ignore the next character.
1111 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1112 $c eq "\\") {
1113 substr($res, $off, 2, 'XX');
1114 $off++;
1115 next;
1116 }
1117 # Regular quotes.
1118 if ($c eq "'" || $c eq '"') {
1119 if ($sanitise_quote eq '') {
1120 $sanitise_quote = $c;
1121
1122 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001123 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001124 } elsif ($sanitise_quote eq $c) {
1125 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001126 }
1127 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001128
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001129 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001130 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1131 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001132 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1133 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001134 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1135 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001136 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001137 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001138 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001139 }
1140
Daniel Walker113f04a2009-09-21 17:04:35 -07001141 if ($sanitise_quote eq '//') {
1142 $sanitise_quote = '';
1143 }
1144
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001145 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001146 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001147 my $clean = 'X' x length($1);
1148 $res =~ s@\<.*\>@<$clean>@;
1149
1150 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001151 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001152 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001153 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001154 }
1155
Joe Perchesdadf6802016-08-02 14:04:33 -07001156 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1157 my $match = $1;
1158 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1159 }
1160
Andy Whitcroft00df3442007-06-08 13:47:06 -07001161 return $res;
1162}
1163
Joe Perchesa6962d72013-04-29 16:18:13 -07001164sub get_quoted_string {
1165 my ($line, $rawline) = @_;
1166
Joe Perches33acb542015-06-25 15:02:54 -07001167 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001168 return substr($rawline, $-[0], $+[0] - $-[0]);
1169}
1170
Andy Whitcroft8905a672007-11-28 16:21:06 -08001171sub ctx_statement_block {
1172 my ($linenr, $remain, $off) = @_;
1173 my $line = $linenr - 1;
1174 my $blk = '';
1175 my $soff = $off;
1176 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001178
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001179 my $loff = 0;
1180
Andy Whitcroft8905a672007-11-28 16:21:06 -08001181 my $type = '';
1182 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001183 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001184 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001185 my $c;
1186 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001187
1188 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001189 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001190 @stack = (['', 0]) if ($#stack == -1);
1191
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001193 # If we are about to drop off the end, pull in more
1194 # context.
1195 if ($off >= $len) {
1196 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001197 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001198 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001199 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001200 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001201 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001202 $len = length($blk);
1203 $line++;
1204 last;
1205 }
1206 # Bail if there is no further context.
1207 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001208 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001209 last;
1210 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001211 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1212 $level++;
1213 $type = '#';
1214 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001215 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001216 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001217 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001218 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001219
Andy Whitcroft773647a2008-03-28 14:15:58 -07001220 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001221
1222 # Handle nested #if/#else.
1223 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1224 push(@stack, [ $type, $level ]);
1225 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1226 ($type, $level) = @{$stack[$#stack - 1]};
1227 } elsif ($remainder =~ /^#\s*endif\b/) {
1228 ($type, $level) = @{pop(@stack)};
1229 }
1230
Andy Whitcroft8905a672007-11-28 16:21:06 -08001231 # Statement ends at the ';' or a close '}' at the
1232 # outermost level.
1233 if ($level == 0 && $c eq ';') {
1234 last;
1235 }
1236
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001237 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001238 if ($level == 0 && $coff_set == 0 &&
1239 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1240 $remainder =~ /^(else)(?:\s|{)/ &&
1241 $remainder !~ /^else\s+if\b/) {
1242 $coff = $off + length($1) - 1;
1243 $coff_set = 1;
1244 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1245 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001246 }
1247
Andy Whitcroft8905a672007-11-28 16:21:06 -08001248 if (($type eq '' || $type eq '(') && $c eq '(') {
1249 $level++;
1250 $type = '(';
1251 }
1252 if ($type eq '(' && $c eq ')') {
1253 $level--;
1254 $type = ($level != 0)? '(' : '';
1255
1256 if ($level == 0 && $coff < $soff) {
1257 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001258 $coff_set = 1;
1259 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001260 }
1261 }
1262 if (($type eq '' || $type eq '{') && $c eq '{') {
1263 $level++;
1264 $type = '{';
1265 }
1266 if ($type eq '{' && $c eq '}') {
1267 $level--;
1268 $type = ($level != 0)? '{' : '';
1269
1270 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001271 if (substr($blk, $off + 1, 1) eq ';') {
1272 $off++;
1273 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001274 last;
1275 }
1276 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001277 # Preprocessor commands end at the newline unless escaped.
1278 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1279 $level--;
1280 $type = '';
1281 $off++;
1282 last;
1283 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001284 $off++;
1285 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001286 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001287 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001288 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001289 $line++;
1290 $remain--;
1291 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001292
1293 my $statement = substr($blk, $soff, $off - $soff + 1);
1294 my $condition = substr($blk, $soff, $coff - $soff + 1);
1295
1296 #warn "STATEMENT<$statement>\n";
1297 #warn "CONDITION<$condition>\n";
1298
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001300
1301 return ($statement, $condition,
1302 $line, $remain + 1, $off - $loff + 1, $level);
1303}
1304
Andy Whitcroftcf655042008-03-04 14:28:20 -08001305sub statement_lines {
1306 my ($stmt) = @_;
1307
1308 # Strip the diff line prefixes and rip blank lines at start and end.
1309 $stmt =~ s/(^|\n)./$1/g;
1310 $stmt =~ s/^\s*//;
1311 $stmt =~ s/\s*$//;
1312
1313 my @stmt_lines = ($stmt =~ /\n/g);
1314
1315 return $#stmt_lines + 2;
1316}
1317
1318sub statement_rawlines {
1319 my ($stmt) = @_;
1320
1321 my @stmt_lines = ($stmt =~ /\n/g);
1322
1323 return $#stmt_lines + 2;
1324}
1325
1326sub statement_block_size {
1327 my ($stmt) = @_;
1328
1329 $stmt =~ s/(^|\n)./$1/g;
1330 $stmt =~ s/^\s*{//;
1331 $stmt =~ s/}\s*$//;
1332 $stmt =~ s/^\s*//;
1333 $stmt =~ s/\s*$//;
1334
1335 my @stmt_lines = ($stmt =~ /\n/g);
1336 my @stmt_statements = ($stmt =~ /;/g);
1337
1338 my $stmt_lines = $#stmt_lines + 2;
1339 my $stmt_statements = $#stmt_statements + 1;
1340
1341 if ($stmt_lines > $stmt_statements) {
1342 return $stmt_lines;
1343 } else {
1344 return $stmt_statements;
1345 }
1346}
1347
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001348sub ctx_statement_full {
1349 my ($linenr, $remain, $off) = @_;
1350 my ($statement, $condition, $level);
1351
1352 my (@chunks);
1353
Andy Whitcroftcf655042008-03-04 14:28:20 -08001354 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 ($statement, $condition, $linenr, $remain, $off, $level) =
1356 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001357 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001358 push(@chunks, [ $condition, $statement ]);
1359 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1360 return ($level, $linenr, @chunks);
1361 }
1362
1363 # Pull in the following conditional/block pairs and see if they
1364 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001365 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001366 ($statement, $condition, $linenr, $remain, $off, $level) =
1367 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001368 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001369 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001370 #print "C: push\n";
1371 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001372 }
1373
1374 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001375}
1376
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001377sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001378 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001379 my $line;
1380 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001381 my $blk = '';
1382 my @o;
1383 my @c;
1384 my @res = ();
1385
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001386 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001387 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001388 for ($line = $start; $remain > 0; $line++) {
1389 next if ($rawlines[$line] =~ /^-/);
1390 $remain--;
1391
1392 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001393
1394 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001395 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001396 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001397 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001398 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001399 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001400 $level = pop(@stack);
1401 }
1402
Andy Whitcroft01464f32010-10-26 14:23:19 -07001403 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001404 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1405 if ($off > 0) {
1406 $off--;
1407 next;
1408 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001409
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001410 if ($c eq $close && $level > 0) {
1411 $level--;
1412 last if ($level == 0);
1413 } elsif ($c eq $open) {
1414 $level++;
1415 }
1416 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001417
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001418 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001419 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001420 }
1421
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001422 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001423 }
1424
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001425 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001426}
1427sub ctx_block_outer {
1428 my ($linenr, $remain) = @_;
1429
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001430 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1431 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001432}
1433sub ctx_block {
1434 my ($linenr, $remain) = @_;
1435
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001436 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1437 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001438}
1439sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001440 my ($linenr, $remain, $off) = @_;
1441
1442 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1443 return @r;
1444}
1445sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001446 my ($linenr, $remain) = @_;
1447
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001448 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001449}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001450sub ctx_statement_level {
1451 my ($linenr, $remain, $off) = @_;
1452
1453 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1454}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001455
1456sub ctx_locate_comment {
1457 my ($first_line, $end_line) = @_;
1458
1459 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001460 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001461 return $current_comment if (defined $current_comment);
1462
1463 # Look through the context and try and figure out if there is a
1464 # comment.
1465 my $in_comment = 0;
1466 $current_comment = '';
1467 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001468 my $line = $rawlines[$linenr - 1];
1469 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001470 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1471 $in_comment = 1;
1472 }
1473 if ($line =~ m@/\*@) {
1474 $in_comment = 1;
1475 }
1476 if (!$in_comment && $current_comment ne '') {
1477 $current_comment = '';
1478 }
1479 $current_comment .= $line . "\n" if ($in_comment);
1480 if ($line =~ m@\*/@) {
1481 $in_comment = 0;
1482 }
1483 }
1484
1485 chomp($current_comment);
1486 return($current_comment);
1487}
1488sub ctx_has_comment {
1489 my ($first_line, $end_line) = @_;
1490 my $cmt = ctx_locate_comment($first_line, $end_line);
1491
Andy Whitcroft00df3442007-06-08 13:47:06 -07001492 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001493 ##print "CMMT: $cmt\n";
1494
1495 return ($cmt ne '');
1496}
1497
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001498sub raw_line {
1499 my ($linenr, $cnt) = @_;
1500
1501 my $offset = $linenr - 1;
1502 $cnt++;
1503
1504 my $line;
1505 while ($cnt) {
1506 $line = $rawlines[$offset++];
1507 next if (defined($line) && $line =~ /^-/);
1508 $cnt--;
1509 }
1510
1511 return $line;
1512}
1513
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001514sub cat_vet {
1515 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001516 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001517
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001518 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001519 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1520 $res .= $1;
1521 if ($2 ne '') {
1522 $coded = sprintf("^%c", unpack('C', $2) + 64);
1523 $res .= $coded;
1524 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001525 }
1526 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001527
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001528 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001529}
1530
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001531my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001532my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001533my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001534my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001535
1536sub annotate_reset {
1537 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001538 $av_pending = '_';
1539 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001540 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001541}
1542
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001543sub annotate_values {
1544 my ($stream, $type) = @_;
1545
1546 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001547 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548 my $cur = $stream;
1549
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001550 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001551
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001552 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001553 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001554 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001555 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001556 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001557 print "WS($1)\n" if ($dbg_values > 1);
1558 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001559 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001560 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001561 }
1562
Florian Micklerc023e4732011-01-12 16:59:58 -08001563 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001564 print "CAST($1)\n" if ($dbg_values > 1);
1565 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001566 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001567
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001568 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001569 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001570 $type = 'T';
1571
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001572 } elsif ($cur =~ /^($Modifier)\s*/) {
1573 print "MODIFIER($1)\n" if ($dbg_values > 1);
1574 $type = 'T';
1575
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001576 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001577 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001578 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001579 push(@av_paren_type, $type);
1580 if ($2 ne '') {
1581 $av_pending = 'N';
1582 }
1583 $type = 'E';
1584
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001585 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001586 print "UNDEF($1)\n" if ($dbg_values > 1);
1587 $av_preprocessor = 1;
1588 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001589
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001590 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001591 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001592 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001593
1594 push(@av_paren_type, $type);
1595 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001596 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001597
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001598 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001599 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1600 $av_preprocessor = 1;
1601
1602 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1603
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001604 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001605
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001606 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001607 print "PRE_END($1)\n" if ($dbg_values > 1);
1608
1609 $av_preprocessor = 1;
1610
1611 # Assume all arms of the conditional end as this
1612 # one does, and continue as if the #endif was not here.
1613 pop(@av_paren_type);
1614 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001615 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001616
1617 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001618 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001619
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001620 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1621 print "ATTR($1)\n" if ($dbg_values > 1);
1622 $av_pending = $type;
1623 $type = 'N';
1624
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001625 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001626 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001627 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001628 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629 }
1630 $type = 'N';
1631
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001632 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001633 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001634 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001635 $type = 'N';
1636
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001637 } elsif ($cur =~/^(case)/o) {
1638 print "CASE($1)\n" if ($dbg_values > 1);
1639 $av_pend_colon = 'C';
1640 $type = 'N';
1641
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001642 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001643 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001644 $type = 'N';
1645
1646 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001647 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001648 push(@av_paren_type, $av_pending);
1649 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001650 $type = 'N';
1651
1652 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001653 my $new_type = pop(@av_paren_type);
1654 if ($new_type ne '_') {
1655 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001656 print "PAREN('$1') -> $type\n"
1657 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001658 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001659 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001660 }
1661
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001662 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001663 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001664 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001666
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001667 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1668 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001669 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001670 } elsif ($type eq 'E') {
1671 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001672 }
1673 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1674 $type = 'V';
1675
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001676 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001677 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001678 $type = 'V';
1679
1680 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001681 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001682 $type = 'N';
1683
Andy Whitcroftcf655042008-03-04 14:28:20 -08001684 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001685 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001686 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001687 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001688
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001689 } elsif ($cur =~/^(,)/) {
1690 print "COMMA($1)\n" if ($dbg_values > 1);
1691 $type = 'C';
1692
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001693 } elsif ($cur =~ /^(\?)/o) {
1694 print "QUESTION($1)\n" if ($dbg_values > 1);
1695 $type = 'N';
1696
1697 } elsif ($cur =~ /^(:)/o) {
1698 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1699
1700 substr($var, length($res), 1, $av_pend_colon);
1701 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1702 $type = 'E';
1703 } else {
1704 $type = 'N';
1705 }
1706 $av_pend_colon = 'O';
1707
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001708 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001709 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001710 $type = 'N';
1711
Andy Whitcroft0d413862008-10-15 22:02:16 -07001712 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001713 my $variant;
1714
1715 print "OPV($1)\n" if ($dbg_values > 1);
1716 if ($type eq 'V') {
1717 $variant = 'B';
1718 } else {
1719 $variant = 'U';
1720 }
1721
1722 substr($var, length($res), 1, $variant);
1723 $type = 'N';
1724
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001725 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001726 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001727 if ($1 ne '++' && $1 ne '--') {
1728 $type = 'N';
1729 }
1730
1731 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001732 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001733 }
1734 if (defined $1) {
1735 $cur = substr($cur, length($1));
1736 $res .= $type x length($1);
1737 }
1738 }
1739
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001740 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001741}
1742
Andy Whitcroft8905a672007-11-28 16:21:06 -08001743sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001744 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001745 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001746 ^(?:
1747 $Modifier|
1748 $Storage|
1749 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001750 DEFINE_\S+
1751 )$|
1752 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001753 goto|
1754 return|
1755 case|
1756 else|
1757 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001758 do|
1759 \#|
1760 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001761 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001762 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001763 )}x;
1764 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1765 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001766 # Check for modifiers.
1767 $possible =~ s/\s*$Storage\s*//g;
1768 $possible =~ s/\s*$Sparse\s*//g;
1769 if ($possible =~ /^\s*$/) {
1770
1771 } elsif ($possible =~ /\s/) {
1772 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001773 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001774 if ($modifier !~ $notPermitted) {
1775 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001776 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001777 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001778 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001779
1780 } else {
1781 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001782 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001783 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001784 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001785 } else {
1786 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001787 }
1788}
1789
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790my $prefix = '';
1791
Joe Perches000d1cc12011-07-25 17:13:25 -07001792sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001793 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001794
Joe Perchescbec18a2014-04-03 14:49:19 -07001795 return defined $use_type{$type} if (scalar keys %use_type > 0);
1796
1797 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001798}
1799
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001800sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001801 my ($level, $type, $msg) = @_;
1802
1803 if (!show_type($type) ||
1804 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001805 return 0;
1806 }
Joe Perches57230292015-06-25 15:03:03 -07001807 my $output = '';
1808 if (-t STDOUT && $color) {
1809 if ($level eq 'ERROR') {
1810 $output .= RED;
1811 } elsif ($level eq 'WARNING') {
1812 $output .= YELLOW;
1813 } else {
1814 $output .= GREEN;
1815 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001816 }
Joe Perches57230292015-06-25 15:03:03 -07001817 $output .= $prefix . $level . ':';
1818 if ($show_types) {
1819 $output .= BLUE if (-t STDOUT && $color);
1820 $output .= "$type:";
1821 }
1822 $output .= RESET if (-t STDOUT && $color);
1823 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001824
1825 if ($showfile) {
1826 my @lines = split("\n", $output, -1);
1827 splice(@lines, 1, 1);
1828 $output = join("\n", @lines);
1829 }
Joe Perches57230292015-06-25 15:03:03 -07001830 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001831
Joe Perches57230292015-06-25 15:03:03 -07001832 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001833
1834 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001835}
Joe Perchescbec18a2014-04-03 14:49:19 -07001836
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001837sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001838 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001839}
Joe Perches000d1cc12011-07-25 17:13:25 -07001840
Joe Perchesd752fcc2014-08-06 16:11:05 -07001841sub fixup_current_range {
1842 my ($lineRef, $offset, $length) = @_;
1843
1844 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1845 my $o = $1;
1846 my $l = $2;
1847 my $no = $o + $offset;
1848 my $nl = $l + $length;
1849 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1850 }
1851}
1852
1853sub fix_inserted_deleted_lines {
1854 my ($linesRef, $insertedRef, $deletedRef) = @_;
1855
1856 my $range_last_linenr = 0;
1857 my $delta_offset = 0;
1858
1859 my $old_linenr = 0;
1860 my $new_linenr = 0;
1861
1862 my $next_insert = 0;
1863 my $next_delete = 0;
1864
1865 my @lines = ();
1866
1867 my $inserted = @{$insertedRef}[$next_insert++];
1868 my $deleted = @{$deletedRef}[$next_delete++];
1869
1870 foreach my $old_line (@{$linesRef}) {
1871 my $save_line = 1;
1872 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001873 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001874 $delta_offset = 0;
1875 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1876 $range_last_linenr = $new_linenr;
1877 fixup_current_range(\$line, $delta_offset, 0);
1878 }
1879
1880 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1881 $deleted = @{$deletedRef}[$next_delete++];
1882 $save_line = 0;
1883 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1884 }
1885
1886 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1887 push(@lines, ${$inserted}{'LINE'});
1888 $inserted = @{$insertedRef}[$next_insert++];
1889 $new_linenr++;
1890 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1891 }
1892
1893 if ($save_line) {
1894 push(@lines, $line);
1895 $new_linenr++;
1896 }
1897
1898 $old_linenr++;
1899 }
1900
1901 return @lines;
1902}
1903
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001904sub fix_insert_line {
1905 my ($linenr, $line) = @_;
1906
1907 my $inserted = {
1908 LINENR => $linenr,
1909 LINE => $line,
1910 };
1911 push(@fixed_inserted, $inserted);
1912}
1913
1914sub fix_delete_line {
1915 my ($linenr, $line) = @_;
1916
1917 my $deleted = {
1918 LINENR => $linenr,
1919 LINE => $line,
1920 };
1921
1922 push(@fixed_deleted, $deleted);
1923}
1924
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001925sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001926 my ($type, $msg) = @_;
1927
1928 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001929 our $clean = 0;
1930 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001931 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001932 }
Joe Perches3705ce52013-07-03 15:05:31 -07001933 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001934}
1935sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001936 my ($type, $msg) = @_;
1937
1938 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001939 our $clean = 0;
1940 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001941 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001942 }
Joe Perches3705ce52013-07-03 15:05:31 -07001943 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001944}
1945sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001946 my ($type, $msg) = @_;
1947
1948 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001949 our $clean = 0;
1950 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001951 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001952 }
Joe Perches3705ce52013-07-03 15:05:31 -07001953 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001954}
1955
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001956sub check_absolute_file {
1957 my ($absolute, $herecurr) = @_;
1958 my $file = $absolute;
1959
1960 ##print "absolute<$absolute>\n";
1961
1962 # See if any suffix of this path is a path within the tree.
1963 while ($file =~ s@^[^/]*/@@) {
1964 if (-f "$root/$file") {
1965 ##print "file<$file>\n";
1966 last;
1967 }
1968 }
1969 if (! -f _) {
1970 return 0;
1971 }
1972
1973 # It is, so see if the prefix is acceptable.
1974 my $prefix = $absolute;
1975 substr($prefix, -length($file)) = '';
1976
1977 ##print "prefix<$prefix>\n";
1978 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001979 WARN("USE_RELATIVE_PATH",
1980 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001981 }
1982}
1983
Joe Perches3705ce52013-07-03 15:05:31 -07001984sub trim {
1985 my ($string) = @_;
1986
Joe Perchesb34c6482013-09-11 14:24:01 -07001987 $string =~ s/^\s+|\s+$//g;
1988
1989 return $string;
1990}
1991
1992sub ltrim {
1993 my ($string) = @_;
1994
1995 $string =~ s/^\s+//;
1996
1997 return $string;
1998}
1999
2000sub rtrim {
2001 my ($string) = @_;
2002
2003 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002004
2005 return $string;
2006}
2007
Joe Perches52ea8502013-11-12 15:10:09 -08002008sub string_find_replace {
2009 my ($string, $find, $replace) = @_;
2010
2011 $string =~ s/$find/$replace/g;
2012
2013 return $string;
2014}
2015
Joe Perches3705ce52013-07-03 15:05:31 -07002016sub tabify {
2017 my ($leading) = @_;
2018
2019 my $source_indent = 8;
2020 my $max_spaces_before_tab = $source_indent - 1;
2021 my $spaces_to_tab = " " x $source_indent;
2022
2023 #convert leading spaces to tabs
2024 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2025 #Remove spaces before a tab
2026 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2027
2028 return "$leading";
2029}
2030
Gregory Beanf07523e2011-07-06 15:36:58 -07002031sub cleanup_continuation_headers {
2032 # Collapse any header-continuation lines into a single line so they
2033 # can be parsed meaningfully, as the parser only has one line
2034 # of context to work with.
2035 my $again;
2036 do {
2037 $again = 0;
2038 foreach my $n (0 .. scalar(@rawlines) - 2) {
2039 if ($rawlines[$n]=~/^\s*$/) {
2040 # A blank line means there's no more chance
2041 # of finding headers. Shortcut to done.
2042 return;
2043 }
2044 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2045 $rawlines[$n+1]=~/^\s+/) {
2046 # Continuation header. Collapse it.
2047 my $line = splice @rawlines, $n+1, 1;
2048 $line=~s/^\s+/ /;
2049 $rawlines[$n] .= $line;
2050 # We've 'destabilized' the list, so restart.
2051 $again = 1;
2052 last;
2053 }
2054 }
2055 } while ($again);
2056}
2057
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002058sub pos_last_openparen {
2059 my ($line) = @_;
2060
2061 my $pos = 0;
2062
2063 my $opens = $line =~ tr/\(/\(/;
2064 my $closes = $line =~ tr/\)/\)/;
2065
2066 my $last_openparen = 0;
2067
2068 if (($opens == 0) || ($closes >= $opens)) {
2069 return -1;
2070 }
2071
2072 my $len = length($line);
2073
2074 for ($pos = 0; $pos < $len; $pos++) {
2075 my $string = substr($line, $pos);
2076 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2077 $pos += length($1) - 1;
2078 } elsif (substr($line, $pos, 1) eq '(') {
2079 $last_openparen = $pos;
2080 } elsif (index($string, '(') == -1) {
2081 last;
2082 }
2083 }
2084
Joe Perches91cb5192014-04-03 14:49:32 -07002085 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002086}
2087
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002088sub process {
2089 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002090
2091 my $linenr=0;
2092 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002093 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002094 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002095 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002096
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002097 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002098 my $indent;
2099 my $previndent=0;
2100 my $stashindent=0;
2101
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002102 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002103 my $signoff = 0;
2104 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002105 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002106 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002107 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002108 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002109 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002110 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002111 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002112 my $non_utf8_charset = 0;
2113
Joe Perches365dd4e2014-08-06 16:10:42 -07002114 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002115 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002116
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002117 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002118 our $cnt_lines = 0;
2119 our $cnt_error = 0;
2120 our $cnt_warn = 0;
2121 our $cnt_chk = 0;
2122
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002123 # Trace the real file/line as we go.
2124 my $realfile = '';
2125 my $realline = 0;
2126 my $realcnt = 0;
2127 my $here = '';
2128 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002129 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002130 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002131 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002132
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002133 my $prev_values = 'E';
2134
2135 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002136 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002137 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002138 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002139 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002140
Joe Perches7e51f192013-09-11 14:23:57 -07002141 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002142
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002143 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002144 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002145 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002146 my @setup_docs = ();
2147 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002148
Joe Perchesd8b07712013-11-12 15:10:06 -08002149 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002150 my $shorttext = BEFORE_SHORTTEXT;
2151 my $shorttext_exspc = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002152
Andy Whitcroft773647a2008-03-28 14:15:58 -07002153 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002154 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002155 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002156
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002157 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002158 $linenr++;
2159 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002160
Joe Perches3705ce52013-07-03 15:05:31 -07002161 push(@fixed, $rawline) if ($fix);
2162
Andy Whitcroft773647a2008-03-28 14:15:58 -07002163 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002164 $setup_docs = 0;
2165 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2166 $setup_docs = 1;
2167 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002168 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002169 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002170 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2171 $realline=$1-1;
2172 if (defined $2) {
2173 $realcnt=$3+1;
2174 } else {
2175 $realcnt=1+1;
2176 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002177 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002178
2179 # Guestimate if this is a continuing comment. Run
2180 # the context looking for a comment "edge". If this
2181 # edge is a close comment then we must be in a comment
2182 # at context start.
2183 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002184 my $cnt = $realcnt;
2185 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2186 next if (defined $rawlines[$ln - 1] &&
2187 $rawlines[$ln - 1] =~ /^-/);
2188 $cnt--;
2189 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002190 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002191 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2192 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2193 ($edge) = $1;
2194 last;
2195 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002196 }
2197 if (defined $edge && $edge eq '*/') {
2198 $in_comment = 1;
2199 }
2200
2201 # Guestimate if this is a continuing comment. If this
2202 # is the start of a diff block and this line starts
2203 # ' *' then it is very likely a comment.
2204 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002205 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 {
2207 $in_comment = 1;
2208 }
2209
2210 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2211 sanitise_line_reset($in_comment);
2212
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002213 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002214 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002215 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002216 $line = sanitise_line($rawline);
2217 }
2218 push(@lines, $line);
2219
2220 if ($realcnt > 1) {
2221 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2222 } else {
2223 $realcnt = 0;
2224 }
2225
2226 #print "==>$rawline\n";
2227 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002228
2229 if ($setup_docs && $line =~ /^\+/) {
2230 push(@setup_docs, $line);
2231 }
2232 }
2233
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002234 $prefix = '';
2235
Andy Whitcroft773647a2008-03-28 14:15:58 -07002236 $realcnt = 0;
2237 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002238 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002239 foreach my $line (@lines) {
2240 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002241 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002242 my $sline = $line; #copy of $line
2243 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002244
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002245 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002246
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002247#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002248 if (!$in_commit_log &&
2249 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002250 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002251 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002252 $realline=$1-1;
2253 if (defined $2) {
2254 $realcnt=$3+1;
2255 } else {
2256 $realcnt=1+1;
2257 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002258 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002259 $prev_values = 'E';
2260
Andy Whitcroft773647a2008-03-28 14:15:58 -07002261 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002262 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002263 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002264 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002265 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002266
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002267# track the line number as we move through the hunk, note that
2268# new versions of GNU diff omit the leading space on completely
2269# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002270 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002271 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002272 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002273
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002274 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002275 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002276
2277 # Track the previous line.
2278 ($prevline, $stashline) = ($stashline, $line);
2279 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002280 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2281
Andy Whitcroft773647a2008-03-28 14:15:58 -07002282 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002283
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002284 } elsif ($realcnt == 1) {
2285 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002286 }
2287
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002288 my $hunk_line = ($realcnt != 0);
2289
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002290 $here = "#$linenr: " if (!$file);
2291 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002292
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002293 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002294 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002295 if ($line =~ /^diff --git.*?(\S+)$/) {
2296 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002297 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002298 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002299 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002300 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002301 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002302 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002303 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002304
2305 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002306 if (!$file && $tree && $p1_prefix ne '' &&
2307 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002308 WARN("PATCH_PREFIX",
2309 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002310 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002311
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002312 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002313 ERROR("MODIFIED_INCLUDE_ASM",
2314 "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 -07002315 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002316 $found_file = 1;
2317 }
2318
Joe Perches34d88152015-06-25 15:03:05 -07002319#make up the handle for any error we report on this line
2320 if ($showfile) {
2321 $prefix = "$realfile:$realline: "
2322 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002323 if ($file) {
2324 $prefix = "$filename:$realline: ";
2325 } else {
2326 $prefix = "$filename:$linenr: ";
2327 }
Joe Perches34d88152015-06-25 15:03:05 -07002328 }
2329
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002330 if ($found_file) {
Joe Perches7bd7e482015-09-09 15:37:44 -07002331 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002332 $check = 1;
2333 } else {
2334 $check = $check_orig;
2335 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002336 next;
2337 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002338 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002339
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002340 my $hereline = "$here\n$rawline\n";
2341 my $herecurr = "$here\n$rawline\n";
2342 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002343
David Keitel72bf3e82016-03-23 20:52:44 -07002344 if ($shorttext != AFTER_SHORTTEXT) {
2345 if ($shorttext == IN_SHORTTEXT) {
2346 if ($line=~/^---/ || $line=~/^diff.*/) {
2347 $shorttext = AFTER_SHORTTEXT;
2348 } elsif (length($line) > (SHORTTEXT_LIMIT +
2349 $shorttext_exspc)
2350 && $line !~ /^:([0-7]{6}\s){2}
2351 ([[:xdigit:]]+\.*
2352 \s){2}\w+\s\w+/xms) {
2353 WARN("LONG_COMMIT_TEXT",
2354 "commit text line over " .
2355 SHORTTEXT_LIMIT .
2356 " characters\n" . $herecurr);
2357 }
2358 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
2359 $shorttext = IN_SHORTTEXT;
2360 if (length($1) > SHORTTEXT_LIMIT) {
2361 WARN("LONG_SUMMARY_LINE",
2362 "summary line over " .
2363 SHORTTEXT_LIMIT .
2364 " characters\n" . $herecurr);
2365 }
2366 } elsif ($line=~/^ (.*)/) {
2367 $shorttext = IN_SHORTTEXT;
2368 $shorttext_exspc = 4;
2369 if (length($1) > SHORTTEXT_LIMIT) {
2370 WARN("LONG_SUMMARY_LINE",
2371 "summary line over " .
2372 SHORTTEXT_LIMIT .
2373 " characters\n" . $herecurr);
2374 }
2375 }
2376 }
2377
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002378 $cnt_lines++ if ($realcnt != 0);
2379
Joe Perchese518e9a2015-06-25 15:03:27 -07002380# Check if the commit log has what seems like a diff which can confuse patch
2381 if ($in_commit_log && !$commit_log_has_diff &&
2382 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2383 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2384 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2385 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2386 ERROR("DIFF_IN_COMMIT_MSG",
2387 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2388 $commit_log_has_diff = 1;
2389 }
2390
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002391# Check for incorrect file permissions
2392 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2393 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002394 if ($realfile !~ m@scripts/@ &&
2395 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002396 ERROR("EXECUTE_PERMISSIONS",
2397 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002398 }
2399 }
2400
Joe Perches20112472011-07-25 17:13:23 -07002401# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002402 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002403 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002404 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002405 }
2406
Joe Perchese0d975b2014-12-10 15:51:49 -08002407# Check if MAINTAINERS is being updated. If so, there's probably no need to
2408# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2409 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2410 $reported_maintainer_file = 1;
2411 }
2412
Joe Perches20112472011-07-25 17:13:23 -07002413# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002414 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002415 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002416 my $space_before = $1;
2417 my $sign_off = $2;
2418 my $space_after = $3;
2419 my $email = $4;
2420 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2421
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002422 if ($sign_off !~ /$signature_tags/) {
2423 WARN("BAD_SIGN_OFF",
2424 "Non-standard signature: $sign_off\n" . $herecurr);
2425 }
Joe Perches20112472011-07-25 17:13:23 -07002426 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002427 if (WARN("BAD_SIGN_OFF",
2428 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2429 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002430 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002431 "$ucfirst_sign_off $email";
2432 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002433 }
Joe Perches20112472011-07-25 17:13:23 -07002434 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002435 if (WARN("BAD_SIGN_OFF",
2436 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2437 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002438 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002439 "$ucfirst_sign_off $email";
2440 }
2441
Joe Perches20112472011-07-25 17:13:23 -07002442 }
2443 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002444 if (WARN("BAD_SIGN_OFF",
2445 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2446 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002447 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002448 "$ucfirst_sign_off $email";
2449 }
Joe Perches20112472011-07-25 17:13:23 -07002450 }
2451
2452 my ($email_name, $email_address, $comment) = parse_email($email);
2453 my $suggested_email = format_email(($email_name, $email_address));
2454 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002455 ERROR("BAD_SIGN_OFF",
2456 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002457 } else {
2458 my $dequoted = $suggested_email;
2459 $dequoted =~ s/^"//;
2460 $dequoted =~ s/" </ </;
2461 # Don't force email to have quotes
2462 # Allow just an angle bracketed address
2463 if ("$dequoted$comment" ne $email &&
2464 "<$email_address>$comment" ne $email &&
2465 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002466 WARN("BAD_SIGN_OFF",
2467 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002468 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002469 }
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002470 if ($chk_author && $line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002471 WARN("BAD_SIGN_OFF",
2472 "invalid Signed-off-by identity\n" . $line );
2473 }
Joe Perches7e51f192013-09-11 14:23:57 -07002474
2475# Check for duplicate signatures
2476 my $sig_nospace = $line;
2477 $sig_nospace =~ s/\s//g;
2478 $sig_nospace = lc($sig_nospace);
2479 if (defined $signatures{$sig_nospace}) {
2480 WARN("BAD_SIGN_OFF",
2481 "Duplicate signature\n" . $herecurr);
2482 } else {
2483 $signatures{$sig_nospace} = 1;
2484 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002485 }
2486
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002487# Check email subject for common tools that don't need to be mentioned
2488 if ($in_header_lines &&
2489 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2490 WARN("EMAIL_SUBJECT",
2491 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2492 }
2493
Joe Perches9b3189e2014-06-04 16:12:10 -07002494# Check for old stable address
2495 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2496 ERROR("STABLE_ADDRESS",
2497 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2498 }
2499
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002500# Check for unwanted Gerrit info
2501 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2502 ERROR("GERRIT_CHANGE_ID",
2503 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2504 }
2505
Joe Perches369c8dd2015-11-06 16:31:34 -08002506# Check if the commit log is in a possible stack dump
2507 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2508 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2509 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2510 # timestamp
2511 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2512 # stack dump address
2513 $commit_log_possible_stack_dump = 1;
2514 }
2515
Joe Perches2a076f42015-04-16 12:44:28 -07002516# Check for line lengths > 75 in commit log, warn once
2517 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002518 length($line) > 75 &&
2519 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2520 # file delta changes
2521 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2522 # filename then :
2523 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2524 # A Fixes: or Link: line
2525 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002526 WARN("COMMIT_LOG_LONG_LINE",
2527 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2528 $commit_log_long_line = 1;
2529 }
2530
Joe Perchesbf4daf12015-09-09 15:37:50 -07002531# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002532 if ($in_commit_log && $commit_log_possible_stack_dump &&
2533 $line =~ /^\s*$/) {
2534 $commit_log_possible_stack_dump = 0;
2535 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002536
Joe Perches0d7835f2015-02-13 14:38:35 -08002537# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002538 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002539 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002540 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002541 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002542 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2543 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002544 my $init_char = "c";
2545 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002546 my $short = 1;
2547 my $long = 0;
2548 my $case = 1;
2549 my $space = 1;
2550 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002551 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002552 my $id = '0123456789ab';
2553 my $orig_desc = "commit description";
2554 my $description = "";
2555
Joe Perchesfe043ea2015-09-09 15:37:25 -07002556 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2557 $init_char = $1;
2558 $orig_commit = lc($2);
2559 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2560 $orig_commit = lc($1);
2561 }
2562
Joe Perches0d7835f2015-02-13 14:38:35 -08002563 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2564 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2565 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2566 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2567 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2568 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002569 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002570 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2571 defined $rawlines[$linenr] &&
2572 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2573 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002574 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002575 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2576 defined $rawlines[$linenr] &&
2577 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2578 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2579 $orig_desc = $1;
2580 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2581 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002582 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002583 }
2584
2585 ($id, $description) = git_commit_info($orig_commit,
2586 $id, $orig_desc);
2587
Joe Perches19c146a2015-02-13 14:39:00 -08002588 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002589 ERROR("GIT_COMMIT_ID",
2590 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2591 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002592 }
2593
Joe Perches13f19372014-08-06 16:10:59 -07002594# Check for added, moved or deleted files
2595 if (!$reported_maintainer_file && !$in_commit_log &&
2596 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2597 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2598 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2599 (defined($1) || defined($2))))) {
2600 $reported_maintainer_file = 1;
2601 WARN("FILE_PATH_CHANGES",
2602 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2603 }
2604
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002605#check the patch for invalid author credentials
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002606 if ($chk_author && $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002607 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2608 }
2609
Andy Whitcroft00df3442007-06-08 13:47:06 -07002610# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002611 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002612 ERROR("CORRUPTED_PATCH",
2613 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002614 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002615 }
2616
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002617# Check for absolute kernel paths.
2618 if ($tree) {
2619 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2620 my $file = $1;
2621
2622 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2623 check_absolute_file($1, $herecurr)) {
2624 #
2625 } else {
2626 check_absolute_file($file, $herecurr);
2627 }
2628 }
2629 }
2630
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002631# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2632 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002633 $rawline !~ m/^$UTF8*$/) {
2634 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2635
2636 my $blank = copy_spacing($rawline);
2637 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2638 my $hereptr = "$hereline$ptr\n";
2639
Joe Perches34d99212011-07-25 17:13:26 -07002640 CHK("INVALID_UTF8",
2641 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002642 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002643
Joe Perches15662b32011-10-31 17:13:12 -07002644# Check if it's the start of a commit log
2645# (not a header line and we haven't seen the patch filename)
2646 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002647 !($rawline =~ /^\s+\S/ ||
2648 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002649 $in_header_lines = 0;
2650 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002651 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002652 }
2653
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002654# Check if there is UTF-8 in a commit log when a mail header has explicitly
2655# declined it, i.e defined some charset where it is missing.
2656 if ($in_header_lines &&
2657 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2658 $1 !~ /utf-8/i) {
2659 $non_utf8_charset = 1;
2660 }
2661
2662 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002663 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002664 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002665 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2666 }
2667
Kees Cook66b47b42014-10-13 15:51:57 -07002668# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002669 if (defined($misspellings) &&
2670 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002671 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002672 my $typo = $1;
2673 my $typo_fix = $spelling_fix{lc($typo)};
2674 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2675 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2676 my $msg_type = \&WARN;
2677 $msg_type = \&CHK if ($file);
2678 if (&{$msg_type}("TYPO_SPELLING",
2679 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2680 $fix) {
2681 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2682 }
2683 }
2684 }
2685
Andy Whitcroft306708542008-10-15 22:02:28 -07002686# ignore non-hunk lines and lines being removed
2687 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002688
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002689#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002690 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002691 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002692 if (ERROR("DOS_LINE_ENDINGS",
2693 "DOS line endings\n" . $herevet) &&
2694 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002695 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002696 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002697 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2698 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002699 if (ERROR("TRAILING_WHITESPACE",
2700 "trailing whitespace\n" . $herevet) &&
2701 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002702 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002703 }
2704
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002705 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002706 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002707
Josh Triplett4783f892013-11-12 15:10:12 -08002708# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002709 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002710 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2711 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002712 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2713 my $msg_type = \&ERROR;
2714 $msg_type = \&CHK if ($file);
2715 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002716 "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 -08002717 }
2718
Andi Kleen33549572010-05-24 14:33:29 -07002719# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002720# Only applies when adding the entry originally, after that we do not have
2721# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002722 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002723 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002724 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002725 my $cnt = $realcnt;
2726 my $ln = $linenr + 1;
2727 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002728 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002729 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002730 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002731 $f = $lines[$ln - 1];
2732 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2733 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002734
2735 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002736 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002737
Joe Perches8d73e0e2014-08-06 16:10:46 -07002738 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002739 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002740 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002741 $length = -1;
2742 }
2743
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002744 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002745 $f =~ s/#.*//;
2746 $f =~ s/^\s+//;
2747 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002748 if ($f =~ /^\s*config\s/) {
2749 $is_end = 1;
2750 last;
2751 }
Andi Kleen33549572010-05-24 14:33:29 -07002752 $length++;
2753 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002754 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2755 WARN("CONFIG_DESCRIPTION",
2756 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2757 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002758 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002759 }
2760
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002761# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2762 if ($realfile =~ /Kconfig/ &&
2763 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2764 WARN("CONFIG_EXPERIMENTAL",
2765 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2766 }
2767
Christoph Jaeger327953e2015-02-13 14:38:29 -08002768# discourage the use of boolean for type definition attributes of Kconfig options
2769 if ($realfile =~ /Kconfig/ &&
2770 $line =~ /^\+\s*\bboolean\b/) {
2771 WARN("CONFIG_TYPE_BOOLEAN",
2772 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2773 }
2774
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002775 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2776 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2777 my $flag = $1;
2778 my $replacement = {
2779 'EXTRA_AFLAGS' => 'asflags-y',
2780 'EXTRA_CFLAGS' => 'ccflags-y',
2781 'EXTRA_CPPFLAGS' => 'cppflags-y',
2782 'EXTRA_LDFLAGS' => 'ldflags-y',
2783 };
2784
2785 WARN("DEPRECATED_VARIABLE",
2786 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2787 }
2788
Rob Herringbff5da42014-01-23 15:54:51 -08002789# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002790 if (defined $root &&
2791 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2792 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2793
Rob Herringbff5da42014-01-23 15:54:51 -08002794 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2795
Florian Vaussardcc933192014-04-03 14:49:27 -07002796 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2797 my $vp_file = $dt_path . "vendor-prefixes.txt";
2798
Rob Herringbff5da42014-01-23 15:54:51 -08002799 foreach my $compat (@compats) {
2800 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002801 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2802 my $compat3 = $compat;
2803 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2804 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002805 if ( $? >> 8 ) {
2806 WARN("UNDOCUMENTED_DT_STRING",
2807 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2808 }
2809
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002810 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2811 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002812 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002813 if ( $? >> 8 ) {
2814 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002815 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002816 }
2817 }
2818 }
2819
Andy Whitcroft5368df22008-10-15 22:02:27 -07002820# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002821 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002822
Joe Perches47e0c882015-06-25 15:02:57 -07002823# line length limit (with some exclusions)
2824#
2825# There are a few types of lines that may extend beyond $max_line_length:
2826# logging functions like pr_info that end in a string
2827# lines with a single string
2828# #defines that are a single string
2829#
2830# There are 3 different line length message types:
2831# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2832# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2833# LONG_LINE all other lines longer than $max_line_length
2834#
2835# if LONG_LINE is ignored, the other 2 types are also ignored
2836#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002837 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002838 my $msg_type = "LONG_LINE";
2839
2840 # Check the allowed long line types first
2841
2842 # logging functions that end in a string that starts
2843 # before $max_line_length
2844 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2845 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2846 $msg_type = "";
2847
2848 # lines with only strings (w/ possible termination)
2849 # #defines with only strings
2850 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2851 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2852 $msg_type = "";
2853
Joe Perchesd560a5f2016-08-02 14:04:31 -07002854 # EFI_GUID is another special case
2855 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2856 $msg_type = "";
2857
Joe Perches47e0c882015-06-25 15:02:57 -07002858 # Otherwise set the alternate message types
2859
2860 # a comment starts before $max_line_length
2861 } elsif ($line =~ /($;[\s$;]*)$/ &&
2862 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2863 $msg_type = "LONG_LINE_COMMENT"
2864
2865 # a quoted string starts before $max_line_length
2866 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2867 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2868 $msg_type = "LONG_LINE_STRING"
2869 }
2870
2871 if ($msg_type ne "" &&
2872 (show_type("LONG_LINE") || show_type($msg_type))) {
2873 WARN($msg_type,
2874 "line over $max_line_length characters\n" . $herecurr);
2875 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002876 }
2877
Andy Whitcroft8905a672007-11-28 16:21:06 -08002878# check for adding lines without a newline.
2879 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002880 WARN("MISSING_EOF_NEWLINE",
2881 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002882 }
2883
Mike Frysinger42e41c52009-09-21 17:04:40 -07002884# Blackfin: use hi/lo macros
2885 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2886 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2887 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002888 ERROR("LO_MACRO",
2889 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002890 }
2891 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2892 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002893 ERROR("HI_MACRO",
2894 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002895 }
2896 }
2897
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002898# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002899 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002900
2901# at the beginning of a line any tabs must come first and anything
2902# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002903 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2904 $rawline =~ /^\+\s* \s*/) {
2905 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002906 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002907 if (ERROR("CODE_INDENT",
2908 "code indent should use tabs where possible\n" . $herevet) &&
2909 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002910 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002911 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002912 }
2913
Alberto Panizzo08e44362010-03-05 13:43:54 -08002914# check for space before tabs.
2915 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2916 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002917 if (WARN("SPACE_BEFORE_TAB",
2918 "please, no space before tabs\n" . $herevet) &&
2919 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002920 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002921 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002922 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002923 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002924 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002925 }
2926
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002927# check for && or || at the start of a line
2928 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2929 CHK("LOGICAL_CONTINUATIONS",
2930 "Logical continuations should be on the previous line\n" . $hereprev);
2931 }
2932
Joe Perchesa91e8992016-05-20 17:04:05 -07002933# check indentation starts on a tab stop
2934 if ($^V && $^V ge 5.10.0 &&
2935 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2936 my $indent = length($1);
2937 if ($indent % 8) {
2938 if (WARN("TABSTOP",
2939 "Statements should start on a tabstop\n" . $herecurr) &&
2940 $fix) {
2941 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2942 }
2943 }
2944 }
2945
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002946# check multi-line statement indentation matches previous line
2947 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002948 $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 -07002949 $prevline =~ /^\+(\t*)(.*)$/;
2950 my $oldindent = $1;
2951 my $rest = $2;
2952
2953 my $pos = pos_last_openparen($rest);
2954 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002955 $line =~ /^(\+| )([ \t]*)/;
2956 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002957
2958 my $goodtabindent = $oldindent .
2959 "\t" x ($pos / 8) .
2960 " " x ($pos % 8);
2961 my $goodspaceindent = $oldindent . " " x $pos;
2962
2963 if ($newindent ne $goodtabindent &&
2964 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002965
2966 if (CHK("PARENTHESIS_ALIGNMENT",
2967 "Alignment should match open parenthesis\n" . $hereprev) &&
2968 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002969 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002970 s/^\+[ \t]*/\+$goodtabindent/;
2971 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002972 }
2973 }
2974 }
2975
Joe Perches6ab3a972015-04-16 12:44:05 -07002976# check for space after cast like "(int) foo" or "(struct foo) bar"
2977# avoid checking a few false positives:
2978# "sizeof(<type>)" or "__alignof__(<type>)"
2979# function pointer declarations like "(*foo)(int) = bar;"
2980# structure definitions like "(struct foo) { 0 };"
2981# multiline macros that define functions
2982# known attributes or the __attribute__ keyword
2983 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2984 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002985 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002986 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002987 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002988 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002989 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002990 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002991 }
2992
Joe Perches86406b12015-09-09 15:37:41 -07002993# Block comment styles
2994# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07002995 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002996 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002997 $rawline =~ /^\+[ \t]*\*/ &&
2998 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002999 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3000 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3001 }
3002
Joe Perches86406b12015-09-09 15:37:41 -07003003# Block comments use * on subsequent lines
3004 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3005 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003006 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003007 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003008 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003009 WARN("BLOCK_COMMENT_STYLE",
3010 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003011 }
3012
Joe Perches86406b12015-09-09 15:37:41 -07003013# Block comments use */ on trailing lines
3014 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003015 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3016 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3017 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003018 WARN("BLOCK_COMMENT_STYLE",
3019 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003020 }
3021
Joe Perches7f619192014-08-06 16:10:39 -07003022# check for missing blank lines after struct/union declarations
3023# with exceptions for various attributes and macros
3024 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3025 $line =~ /^\+/ &&
3026 !($line =~ /^\+\s*$/ ||
3027 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3028 $line =~ /^\+\s*MODULE_/i ||
3029 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3030 $line =~ /^\+[a-z_]*init/ ||
3031 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3032 $line =~ /^\+\s*DECLARE/ ||
3033 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003034 if (CHK("LINE_SPACING",
3035 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3036 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003037 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003038 }
Joe Perches7f619192014-08-06 16:10:39 -07003039 }
3040
Joe Perches365dd4e2014-08-06 16:10:42 -07003041# check for multiple consecutive blank lines
3042 if ($prevline =~ /^[\+ ]\s*$/ &&
3043 $line =~ /^\+\s*$/ &&
3044 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003045 if (CHK("LINE_SPACING",
3046 "Please don't use multiple blank lines\n" . $hereprev) &&
3047 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003048 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003049 }
3050
Joe Perches365dd4e2014-08-06 16:10:42 -07003051 $last_blank_line = $linenr;
3052 }
3053
Joe Perches3b617e32014-04-03 14:49:28 -07003054# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003055 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3056 # actual declarations
3057 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003058 # function pointer declarations
3059 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003060 # foo bar; where foo is some local typedef or #define
3061 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3062 # known declaration macros
3063 $prevline =~ /^\+\s+$declaration_macros/) &&
3064 # for "else if" which can look like "$Ident $Ident"
3065 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3066 # other possible extensions of declaration lines
3067 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3068 # not starting a section or a macro "\" extended line
3069 $prevline =~ /(?:\{\s*|\\)$/) &&
3070 # looks like a declaration
3071 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003072 # function pointer declarations
3073 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003074 # foo bar; where foo is some local typedef or #define
3075 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3076 # known declaration macros
3077 $sline =~ /^\+\s+$declaration_macros/ ||
3078 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003079 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003080 # start or end of block or continuation of declaration
3081 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3082 # bitfield continuation
3083 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3084 # other possible extensions of declaration lines
3085 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3086 # indentation of previous and current line are the same
3087 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003088 if (WARN("LINE_SPACING",
3089 "Missing a blank line after declarations\n" . $hereprev) &&
3090 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003091 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003092 }
Joe Perches3b617e32014-04-03 14:49:28 -07003093 }
3094
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003095# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003096# Exceptions:
3097# 1) within comments
3098# 2) indented preprocessor commands
3099# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003100 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003101 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003102 if (WARN("LEADING_SPACE",
3103 "please, no spaces at the start of a line\n" . $herevet) &&
3104 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003105 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003106 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003107 }
3108
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003109# check we are in a valid C source file if not then ignore this hunk
3110 next if ($realfile !~ /\.(h|c)$/);
3111
Joe Perches032a4c02014-08-06 16:10:29 -07003112# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003113# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003114# if the previous line is a break or return and is indented 1 tab more...
3115 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3116 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003117 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3118 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3119 defined $lines[$linenr] &&
3120 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003121 WARN("UNNECESSARY_ELSE",
3122 "else is not generally useful after a break or return\n" . $hereprev);
3123 }
3124 }
3125
Joe Perchesc00df192014-08-06 16:11:01 -07003126# check indentation of a line with a break;
3127# if the previous line is a goto or return and is indented the same # of tabs
3128 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3129 my $tabs = $1;
3130 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3131 WARN("UNNECESSARY_BREAK",
3132 "break is not useful after a goto or return\n" . $hereprev);
3133 }
3134 }
3135
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003136# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3137 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3138 WARN("CONFIG_EXPERIMENTAL",
3139 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3140 }
3141
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003142# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003143 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003144 WARN("CVS_KEYWORD",
3145 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003146 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003147
Mike Frysinger42e41c52009-09-21 17:04:40 -07003148# Blackfin: don't use __builtin_bfin_[cs]sync
3149 if ($line =~ /__builtin_bfin_csync/) {
3150 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003151 ERROR("CSYNC",
3152 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003153 }
3154 if ($line =~ /__builtin_bfin_ssync/) {
3155 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003156 ERROR("SSYNC",
3157 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003158 }
3159
Joe Perches56e77d72013-02-21 16:44:14 -08003160# check for old HOTPLUG __dev<foo> section markings
3161 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3162 WARN("HOTPLUG_SECTION",
3163 "Using $1 is unnecessary\n" . $herecurr);
3164 }
3165
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003166# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003167 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3168 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003169#print "LINE<$line>\n";
3170 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003171 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003172 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003173 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003174 $stat =~ s/\n./\n /g;
3175 $cond =~ s/\n./\n /g;
3176
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003177#print "linenr<$linenr> <$stat>\n";
3178 # If this statement has no statement boundaries within
3179 # it there is no point in retrying a statement scan
3180 # until we hit end of it.
3181 my $frag = $stat; $frag =~ s/;+\s*$//;
3182 if ($frag !~ /(?:{|;)/) {
3183#print "skip<$line_nr_next>\n";
3184 $suppress_statement = $line_nr_next;
3185 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003186
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003187 # Find the real next line.
3188 $realline_next = $line_nr_next;
3189 if (defined $realline_next &&
3190 (!defined $lines[$realline_next - 1] ||
3191 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3192 $realline_next++;
3193 }
3194
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003195 my $s = $stat;
3196 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003197
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003198 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003199 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003200
3201 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003202 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003203
Andy Whitcroft463f2862009-09-21 17:04:34 -07003204 } elsif ($s =~ /^.\s*else\b/s) {
3205
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003206 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003207 } 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 -07003208 my $type = $1;
3209 $type =~ s/\s+/ /g;
3210 possible($type, "A:" . $s);
3211
Andy Whitcroft8905a672007-11-28 16:21:06 -08003212 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003213 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003214 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003215 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003216
3217 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003218 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003219 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003220 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003221
3222 # Check for any sort of function declaration.
3223 # int foo(something bar, other baz);
3224 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003225 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 -08003226 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003227
Andy Whitcroftcf655042008-03-04 14:28:20 -08003228 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003229 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003230 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003231
Andy Whitcroft8905a672007-11-28 16:21:06 -08003232 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003233 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003234
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003235 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003236 }
3237 }
3238 }
3239
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003240 }
3241
Andy Whitcroft653d4872007-06-23 17:16:34 -07003242#
3243# Checks which may be anchored in the context.
3244#
3245
3246# Check for switch () and associated case and default
3247# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003248 if ($line=~/\bswitch\s*\(.*\)/) {
3249 my $err = '';
3250 my $sep = '';
3251 my @ctx = ctx_block_outer($linenr, $realcnt);
3252 shift(@ctx);
3253 for my $ctx (@ctx) {
3254 my ($clen, $cindent) = line_stats($ctx);
3255 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3256 $indent != $cindent) {
3257 $err .= "$sep$ctx\n";
3258 $sep = '';
3259 } else {
3260 $sep = "[...]\n";
3261 }
3262 }
3263 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003264 ERROR("SWITCH_CASE_INDENT_LEVEL",
3265 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003266 }
3267 }
3268
3269# if/while/etc brace do not go on next line, unless defining a do while loop,
3270# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003271 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 -07003272 my $pre_ctx = "$1$2";
3273
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003274 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003275
3276 if ($line =~ /^\+\t{6,}/) {
3277 WARN("DEEP_INDENTATION",
3278 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3279 }
3280
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003281 my $ctx_cnt = $realcnt - $#ctx - 1;
3282 my $ctx = join("\n", @ctx);
3283
Andy Whitcroft548596d2008-07-23 21:29:01 -07003284 my $ctx_ln = $linenr;
3285 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003286
Andy Whitcroft548596d2008-07-23 21:29:01 -07003287 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3288 defined $lines[$ctx_ln - 1] &&
3289 $lines[$ctx_ln - 1] =~ /^-/)) {
3290 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3291 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003292 $ctx_ln++;
3293 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003294
Andy Whitcroft53210162008-07-23 21:29:03 -07003295 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3296 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003297
Joe Perchesd752fcc2014-08-06 16:11:05 -07003298 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003299 ERROR("OPEN_BRACE",
3300 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003301 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003302 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003303 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3304 $ctx =~ /\)\s*\;\s*$/ &&
3305 defined $lines[$ctx_ln - 1])
3306 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003307 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3308 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003309 WARN("TRAILING_SEMICOLON",
3310 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003311 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003312 }
3313 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003314 }
3315
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003316# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003317 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 -08003318 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3319 ctx_statement_block($linenr, $realcnt, 0)
3320 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003321 my ($s, $c) = ($stat, $cond);
3322
3323 substr($s, 0, length($c), '');
3324
Joe Perches9f5af482015-09-09 15:37:30 -07003325 # remove inline comments
3326 $s =~ s/$;/ /g;
3327 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003328
3329 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003330 my @newlines = ($c =~ /\n/gs);
3331 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003332
Joe Perches9f5af482015-09-09 15:37:30 -07003333 # Make sure we remove the line prefixes as we have
3334 # none on the first line, and are going to readd them
3335 # where necessary.
3336 $s =~ s/\n./\n/gs;
3337 while ($s =~ /\n\s+\\\n/) {
3338 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3339 }
3340
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003341 # We want to check the first line inside the block
3342 # starting at the end of the conditional, so remove:
3343 # 1) any blank line termination
3344 # 2) any opening brace { on end of the line
3345 # 3) any do (...) {
3346 my $continuation = 0;
3347 my $check = 0;
3348 $s =~ s/^.*\bdo\b//;
3349 $s =~ s/^\s*{//;
3350 if ($s =~ s/^\s*\\//) {
3351 $continuation = 1;
3352 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003353 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003354 $check = 1;
3355 $cond_lines++;
3356 }
3357
3358 # Also ignore a loop construct at the end of a
3359 # preprocessor statement.
3360 if (($prevline =~ /^.\s*#\s*define\s/ ||
3361 $prevline =~ /\\\s*$/) && $continuation == 0) {
3362 $check = 0;
3363 }
3364
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003365 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003366 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003367 while ($cond_ptr != $cond_lines) {
3368 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003369
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003370 # If we see an #else/#elif then the code
3371 # is not linear.
3372 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3373 $check = 0;
3374 }
3375
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003376 # Ignore:
3377 # 1) blank lines, they should be at 0,
3378 # 2) preprocessor lines, and
3379 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003380 if ($continuation ||
3381 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003382 $s =~ /^\s*#\s*?/ ||
3383 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003384 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003385 if ($s =~ s/^.*?\n//) {
3386 $cond_lines++;
3387 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003388 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003389 }
3390
3391 my (undef, $sindent) = line_stats("+" . $s);
3392 my $stat_real = raw_line($linenr, $cond_lines);
3393
3394 # Check if either of these lines are modified, else
3395 # this is not this patch's fault.
3396 if (!defined($stat_real) ||
3397 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3398 $check = 0;
3399 }
3400 if (defined($stat_real) && $cond_lines > 1) {
3401 $stat_real = "[...]\n$stat_real";
3402 }
3403
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003404 #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 -07003405
Joe Perches9f5af482015-09-09 15:37:30 -07003406 if ($check && $s ne '' &&
3407 (($sindent % 8) != 0 ||
3408 ($sindent < $indent) ||
3409 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003410 WARN("SUSPECT_CODE_INDENT",
3411 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003412 }
3413 }
3414
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003415 # Track the 'values' across context and added lines.
3416 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003417 my ($curr_values, $curr_vars) =
3418 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003419 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003420 if ($dbg_values) {
3421 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003422 print "$linenr > .$outline\n";
3423 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003424 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003425 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003426 $prev_values = substr($curr_values, -1);
3427
Andy Whitcroft00df3442007-06-08 13:47:06 -07003428#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003429 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003430
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003431# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003432 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 -07003433 my $type = $1;
3434 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003435 $var = "" if (!defined $var);
3436 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003437 my $sign = $1;
3438 my $pointer = $2;
3439
3440 $pointer = "" if (!defined $pointer);
3441
3442 if (WARN("UNSPECIFIED_INT",
3443 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3444 $fix) {
3445 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003446 my $comp_pointer = $pointer;
3447 $comp_pointer =~ s/\s//g;
3448 $decl .= $comp_pointer;
3449 $decl = rtrim($decl) if ($var eq "");
3450 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003451 }
3452 }
3453 }
3454
Andy Whitcroft653d4872007-06-23 17:16:34 -07003455# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003456 if ($dbg_type) {
3457 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003458 ERROR("TEST_TYPE",
3459 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003460 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003461 ERROR("TEST_NOT_TYPE",
3462 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003463 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003464 next;
3465 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003466# TEST: allow direct testing of the attribute matcher.
3467 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003468 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003469 ERROR("TEST_ATTR",
3470 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003471 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003472 ERROR("TEST_NOT_ATTR",
3473 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003474 }
3475 next;
3476 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003478# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003479 if ($line =~ /^.\s*{/ &&
3480 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003481 if (ERROR("OPEN_BRACE",
3482 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003483 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3484 fix_delete_line($fixlinenr - 1, $prevrawline);
3485 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003486 my $fixedline = $prevrawline;
3487 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003488 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003489 $fixedline = $line;
3490 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003491 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003492 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003493 }
3494
Andy Whitcroft653d4872007-06-23 17:16:34 -07003495#
3496# Checks which are anchored on the added line.
3497#
3498
3499# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003500 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003501 my $path = $1;
3502 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003503 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003504 "malformed #include filename\n" . $herecurr);
3505 }
3506 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3507 ERROR("UAPI_INCLUDE",
3508 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003509 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003510 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003511
3512# no C99 // comments
3513 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003514 if (ERROR("C99_COMMENTS",
3515 "do not use C99 // comments\n" . $herecurr) &&
3516 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003517 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003518 if ($line =~ /\/\/(.*)$/) {
3519 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003520 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003521 }
3522 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003523 }
3524 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003525 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003526 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003527
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003528# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3529# the whole statement.
3530#print "APW <$lines[$realline_next - 1]>\n";
3531 if (defined $realline_next &&
3532 exists $lines[$realline_next - 1] &&
3533 !defined $suppress_export{$realline_next} &&
3534 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3535 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003536 # Handle definitions which produce identifiers with
3537 # a prefix:
3538 # XXX(foo);
3539 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003540 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003541 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003542 $name =~ /^${Ident}_$2/) {
3543#print "FOO C name<$name>\n";
3544 $suppress_export{$realline_next} = 1;
3545
3546 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003547 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003548 ^.DEFINE_$Ident\(\Q$name\E\)|
3549 ^.DECLARE_$Ident\(\Q$name\E\)|
3550 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003551 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3552 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003553 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003554#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3555 $suppress_export{$realline_next} = 2;
3556 } else {
3557 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003558 }
3559 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003560 if (!defined $suppress_export{$linenr} &&
3561 $prevline =~ /^.\s*$/ &&
3562 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3563 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3564#print "FOO B <$lines[$linenr - 1]>\n";
3565 $suppress_export{$linenr} = 2;
3566 }
3567 if (defined $suppress_export{$linenr} &&
3568 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003569 WARN("EXPORT_SYMBOL",
3570 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003571 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003572
Joe Eloff5150bda2010-08-09 17:21:00 -07003573# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003574 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003575 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003576 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003577 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003578 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003579 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003580 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003581# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003582 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003583 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003584 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003585 $herecurr) &&
3586 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003587 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003588 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003589 }
3590
Joe Perches18130872014-08-06 16:11:22 -07003591# check for misordered declarations of char/short/int/long with signed/unsigned
3592 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3593 my $tmp = trim($1);
3594 WARN("MISORDERED_TYPE",
3595 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3596 }
3597
Joe Perchescb710ec2010-10-26 14:23:20 -07003598# check for static const char * arrays.
3599 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003600 WARN("STATIC_CONST_CHAR_ARRAY",
3601 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003602 $herecurr);
3603 }
3604
3605# check for static char foo[] = "bar" declarations.
3606 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003607 WARN("STATIC_CONST_CHAR_ARRAY",
3608 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003609 $herecurr);
3610 }
3611
Joe Perchesab7e23f2015-04-16 12:44:22 -07003612# check for const <foo> const where <foo> is not a pointer or array type
3613 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3614 my $found = $1;
3615 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3616 WARN("CONST_CONST",
3617 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3618 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3619 WARN("CONST_CONST",
3620 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3621 }
3622 }
3623
Joe Perches9b0fa602014-04-03 14:49:18 -07003624# check for non-global char *foo[] = {"bar", ...} declarations.
3625 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3626 WARN("STATIC_CONST_CHAR_ARRAY",
3627 "char * array declaration might be better as static const\n" .
3628 $herecurr);
3629 }
3630
Joe Perchesb598b672015-04-16 12:44:36 -07003631# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3632 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3633 my $array = $1;
3634 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3635 my $array_div = $1;
3636 if (WARN("ARRAY_SIZE",
3637 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3638 $fix) {
3639 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3640 }
3641 }
3642 }
3643
Joe Perchesb36190c2014-01-27 17:07:18 -08003644# check for function declarations without arguments like "int foo()"
3645 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3646 if (ERROR("FUNCTION_WITHOUT_ARGS",
3647 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3648 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003649 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003650 }
3651 }
3652
Joe Perches92e112f2013-12-13 11:36:22 -07003653# check for uses of DEFINE_PCI_DEVICE_TABLE
3654 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3655 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3656 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3657 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003658 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
Joe Perches92e112f2013-12-13 11:36:22 -07003659 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003660 }
3661
Andy Whitcroft653d4872007-06-23 17:16:34 -07003662# check for new typedefs, only function parameters and sparse annotations
3663# make sense.
3664 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003665 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003666 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003667 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003668 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003669 WARN("NEW_TYPEDEFS",
3670 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003671 }
3672
3673# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003674 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003675 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3676 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003677 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003678
Andy Whitcroft65863862009-01-06 14:41:21 -08003679 # Should start with a space.
3680 $to =~ s/^(\S)/ $1/;
3681 # Should not end with a space.
3682 $to =~ s/\s+$//;
3683 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003684 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003685 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003686
Joe Perches3705ce52013-07-03 15:05:31 -07003687## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003688 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003689 if (ERROR("POINTER_LOCATION",
3690 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3691 $fix) {
3692 my $sub_from = $ident;
3693 my $sub_to = $ident;
3694 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003695 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003696 s@\Q$sub_from\E@$sub_to@;
3697 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003698 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003699 }
3700 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3701 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003702 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003703
Andy Whitcroft65863862009-01-06 14:41:21 -08003704 # Should start with a space.
3705 $to =~ s/^(\S)/ $1/;
3706 # Should not end with a space.
3707 $to =~ s/\s+$//;
3708 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003709 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003710 }
3711 # Modifiers should have spaces.
3712 $to =~ s/(\b$Modifier$)/$1 /;
3713
Joe Perches3705ce52013-07-03 15:05:31 -07003714## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003715 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003716 if (ERROR("POINTER_LOCATION",
3717 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3718 $fix) {
3719
3720 my $sub_from = $match;
3721 my $sub_to = $match;
3722 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003723 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003724 s@\Q$sub_from\E@$sub_to@;
3725 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003726 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003727 }
3728
Joe Perches9d3e3c72015-09-09 15:37:27 -07003729# avoid BUG() or BUG_ON()
3730 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3731 my $msg_type = \&WARN;
3732 $msg_type = \&CHK if ($file);
3733 &{$msg_type}("AVOID_BUG",
3734 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3735 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003736
Joe Perches9d3e3c72015-09-09 15:37:27 -07003737# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003738 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003739 WARN("LINUX_VERSION_CODE",
3740 "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 -08003741 }
3742
Joe Perches17441222011-06-15 15:08:17 -07003743# check for uses of printk_ratelimit
3744 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003745 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003746 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003747 }
3748
Andy Whitcroft00df3442007-06-08 13:47:06 -07003749# printk should use KERN_* levels. Note that follow on printk's on the
3750# same line do not need a level, so we use the current block context
3751# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003752# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003753# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003754 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003755 my $ok = 0;
3756 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3757 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003758 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003759 # with "\n" ignore it, else it is to blame
3760 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3761 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3762 $ok = 1;
3763 }
3764 last;
3765 }
3766 }
3767 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003768 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3769 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003770 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003771 }
3772
Joe Perches243f3802012-05-31 16:26:09 -07003773 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3774 my $orig = $1;
3775 my $level = lc($orig);
3776 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003777 my $level2 = $level;
3778 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003779 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003780 "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 -07003781 }
3782
3783 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003784 if (WARN("PREFER_PR_LEVEL",
3785 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3786 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003787 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003788 s/\bpr_warning\b/pr_warn/;
3789 }
Joe Perches243f3802012-05-31 16:26:09 -07003790 }
3791
Joe Perchesdc139312013-02-21 16:44:13 -08003792 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3793 my $orig = $1;
3794 my $level = lc($orig);
3795 $level = "warn" if ($level eq "warning");
3796 $level = "dbg" if ($level eq "debug");
3797 WARN("PREFER_DEV_LEVEL",
3798 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3799 }
3800
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003801# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3802# number of false positives, but assembly files are not checked, so at
3803# least the arch entry code will not trigger this warning.
3804 if ($line =~ /\bENOSYS\b/) {
3805 WARN("ENOSYS",
3806 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3807 }
3808
Andy Whitcroft653d4872007-06-23 17:16:34 -07003809# function brace can't be on same line, except for #defines of do while,
3810# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003811 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003812 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003813 if (ERROR("OPEN_BRACE",
3814 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3815 $fix) {
3816 fix_delete_line($fixlinenr, $rawline);
3817 my $fixed_line = $rawline;
3818 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3819 my $line1 = $1;
3820 my $line2 = $2;
3821 fix_insert_line($fixlinenr, ltrim($line1));
3822 fix_insert_line($fixlinenr, "\+{");
3823 if ($line2 !~ /^\s*$/) {
3824 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3825 }
3826 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003827 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003828
Andy Whitcroft8905a672007-11-28 16:21:06 -08003829# open braces for enum, union and struct go on the same line.
3830 if ($line =~ /^.\s*{/ &&
3831 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003832 if (ERROR("OPEN_BRACE",
3833 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3834 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3835 fix_delete_line($fixlinenr - 1, $prevrawline);
3836 fix_delete_line($fixlinenr, $rawline);
3837 my $fixedline = rtrim($prevrawline) . " {";
3838 fix_insert_line($fixlinenr, $fixedline);
3839 $fixedline = $rawline;
3840 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3841 if ($fixedline !~ /^\+\s*$/) {
3842 fix_insert_line($fixlinenr, $fixedline);
3843 }
3844 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003845 }
3846
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003847# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003848 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3849 if (WARN("SPACING",
3850 "missing space after $1 definition\n" . $herecurr) &&
3851 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003852 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003853 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3854 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003855 }
3856
Joe Perches31070b52014-01-23 15:54:49 -08003857# Function pointer declarations
3858# check spacing between type, funcptr, and args
3859# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003860 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003861 my $declare = $1;
3862 my $pre_pointer_space = $2;
3863 my $post_pointer_space = $3;
3864 my $funcname = $4;
3865 my $post_funcname_space = $5;
3866 my $pre_args_space = $6;
3867
Joe Perches91f72e92014-04-03 14:49:12 -07003868# the $Declare variable will capture all spaces after the type
3869# so check it for a missing trailing missing space but pointer return types
3870# don't need a space so don't warn for those.
3871 my $post_declare_space = "";
3872 if ($declare =~ /(\s+)$/) {
3873 $post_declare_space = $1;
3874 $declare = rtrim($declare);
3875 }
3876 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003877 WARN("SPACING",
3878 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003879 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003880 }
3881
3882# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003883# This test is not currently implemented because these declarations are
3884# equivalent to
3885# int foo(int bar, ...)
3886# and this is form shouldn't/doesn't generate a checkpatch warning.
3887#
3888# elsif ($declare =~ /\s{2,}$/) {
3889# WARN("SPACING",
3890# "Multiple spaces after return type\n" . $herecurr);
3891# }
Joe Perches31070b52014-01-23 15:54:49 -08003892
3893# unnecessary space "type ( *funcptr)(args...)"
3894 if (defined $pre_pointer_space &&
3895 $pre_pointer_space =~ /^\s/) {
3896 WARN("SPACING",
3897 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3898 }
3899
3900# unnecessary space "type (* funcptr)(args...)"
3901 if (defined $post_pointer_space &&
3902 $post_pointer_space =~ /^\s/) {
3903 WARN("SPACING",
3904 "Unnecessary space before function pointer name\n" . $herecurr);
3905 }
3906
3907# unnecessary space "type (*funcptr )(args...)"
3908 if (defined $post_funcname_space &&
3909 $post_funcname_space =~ /^\s/) {
3910 WARN("SPACING",
3911 "Unnecessary space after function pointer name\n" . $herecurr);
3912 }
3913
3914# unnecessary space "type (*funcptr) (args...)"
3915 if (defined $pre_args_space &&
3916 $pre_args_space =~ /^\s/) {
3917 WARN("SPACING",
3918 "Unnecessary space before function pointer arguments\n" . $herecurr);
3919 }
3920
3921 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003922 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003923 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003924 }
3925 }
3926
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003927# check for spacing round square brackets; allowed:
3928# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003929# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3930# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003931 while ($line =~ /(.*?\s)\[/g) {
3932 my ($where, $prefix) = ($-[1], $1);
3933 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003934 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003935 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003936 if (ERROR("BRACKET_SPACE",
3937 "space prohibited before open square bracket '['\n" . $herecurr) &&
3938 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003939 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003940 s/^(\+.*?)\s+\[/$1\[/;
3941 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003942 }
3943 }
3944
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003945# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003946 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003947 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003948 my $ctx_before = substr($line, 0, $-[1]);
3949 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003950
3951 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003952 if ($name =~ /^(?:
3953 if|for|while|switch|return|case|
3954 volatile|__volatile__|
3955 __attribute__|format|__extension__|
3956 asm|__asm__)$/x)
3957 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003958 # cpp #define statements have non-optional spaces, ie
3959 # if there is a space between the name and the open
3960 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003961 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003962
3963 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003964 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003965
3966 # If this whole things ends with a type its most
3967 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003968 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003969
3970 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003971 if (WARN("SPACING",
3972 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3973 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003974 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003975 s/\b$name\s+\(/$name\(/;
3976 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003977 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003978 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003979
Andy Whitcroft653d4872007-06-23 17:16:34 -07003980# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003981 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003982 my $fixed_line = "";
3983 my $line_fixed = 0;
3984
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003985 my $ops = qr{
3986 <<=|>>=|<=|>=|==|!=|
3987 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3988 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003989 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003990 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003991 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003992 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003993
3994## print("element count: <" . $#elements . ">\n");
3995## foreach my $el (@elements) {
3996## print("el: <$el>\n");
3997## }
3998
3999 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004000 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004001
Joe Perches3705ce52013-07-03 15:05:31 -07004002 foreach my $el (@elements) {
4003 push(@fix_elements, substr($rawline, $off, length($el)));
4004 $off += length($el);
4005 }
4006
4007 $off = 0;
4008
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004009 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004010 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004011
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004012 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004013
4014 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4015
4016## print("n: <$n> good: <$good>\n");
4017
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004018 $off += length($elements[$n]);
4019
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004020 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004021 my $ca = substr($opline, 0, $off);
4022 my $cc = '';
4023 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4024 $cc = substr($opline, $off + length($elements[$n + 1]));
4025 }
4026 my $cb = "$ca$;$cc";
4027
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004028 my $a = '';
4029 $a = 'V' if ($elements[$n] ne '');
4030 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004031 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004032 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4033 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004034 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004035
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004036 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004037
4038 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004039 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004040 $c = 'V' if ($elements[$n + 2] ne '');
4041 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004042 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004043 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4044 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004045 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004046 } else {
4047 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004048 }
4049
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004050 my $ctx = "${a}x${c}";
4051
4052 my $at = "(ctx:$ctx)";
4053
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004054 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004055 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004056
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004057 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004058 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004059
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004060 # Get the full operator variant.
4061 my $opv = $op . substr($curr_vars, $off, 1);
4062
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004063 # Ignore operators passed as parameters.
4064 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004065 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004066
Andy Whitcroftcf655042008-03-04 14:28:20 -08004067# # Ignore comments
4068# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004069
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004070 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004071 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004072 if ($ctx !~ /.x[WEBC]/ &&
4073 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004074 if (ERROR("SPACING",
4075 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004076 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004077 $line_fixed = 1;
4078 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004079 }
4080
4081 # // is a comment
4082 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004083
Joe Perchesb00e4812014-04-03 14:49:33 -07004084 # : when part of a bitfield
4085 } elsif ($opv eq ':B') {
4086 # skip the bitfield test for now
4087
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004088 # No spaces for:
4089 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004090 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004091 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004092 if (ERROR("SPACING",
4093 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004094 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004095 if (defined $fix_elements[$n + 2]) {
4096 $fix_elements[$n + 2] =~ s/^\s+//;
4097 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004098 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004099 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004100 }
4101
Joe Perches23810972014-12-10 15:51:32 -08004102 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004103 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004104 my $rtrim_before = 0;
4105 my $space_after = 0;
4106 if ($ctx =~ /Wx./) {
4107 if (ERROR("SPACING",
4108 "space prohibited before that '$op' $at\n" . $hereptr)) {
4109 $line_fixed = 1;
4110 $rtrim_before = 1;
4111 }
4112 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004113 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004114 if (ERROR("SPACING",
4115 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004116 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004117 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004118 $space_after = 1;
4119 }
4120 }
4121 if ($rtrim_before || $space_after) {
4122 if ($rtrim_before) {
4123 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4124 } else {
4125 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4126 }
4127 if ($space_after) {
4128 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004129 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004130 }
4131
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004132 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004133 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004134 #warn "'*' is part of type\n";
4135
4136 # unary operators should have a space before and
4137 # none after. May be left adjacent to another
4138 # unary operator, or a cast
4139 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004140 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004141 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004142 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004143 if (ERROR("SPACING",
4144 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004145 if ($n != $last_after + 2) {
4146 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4147 $line_fixed = 1;
4148 }
Joe Perches3705ce52013-07-03 15:05:31 -07004149 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004150 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004151 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004152 # A unary '*' may be const
4153
4154 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004155 if (ERROR("SPACING",
4156 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004157 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004158 if (defined $fix_elements[$n + 2]) {
4159 $fix_elements[$n + 2] =~ s/^\s+//;
4160 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004161 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004162 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004163 }
4164
4165 # unary ++ and unary -- are allowed no space on one side.
4166 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004167 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004168 if (ERROR("SPACING",
4169 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004170 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004171 $line_fixed = 1;
4172 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004173 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004174 if ($ctx =~ /Wx[BE]/ ||
4175 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004176 if (ERROR("SPACING",
4177 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004178 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004179 $line_fixed = 1;
4180 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004181 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004182 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004183 if (ERROR("SPACING",
4184 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004185 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004186 if (defined $fix_elements[$n + 2]) {
4187 $fix_elements[$n + 2] =~ s/^\s+//;
4188 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004189 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004190 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004191 }
4192
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004193 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004194 } elsif ($op eq '<<' or $op eq '>>' or
4195 $op eq '&' or $op eq '^' or $op eq '|' or
4196 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004197 $op eq '*' or $op eq '/' or
4198 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004199 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004200 if ($check) {
4201 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4202 if (CHK("SPACING",
4203 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4204 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4205 $fix_elements[$n + 2] =~ s/^\s+//;
4206 $line_fixed = 1;
4207 }
4208 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4209 if (CHK("SPACING",
4210 "space preferred before that '$op' $at\n" . $hereptr)) {
4211 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4212 $line_fixed = 1;
4213 }
4214 }
4215 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004216 if (ERROR("SPACING",
4217 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004218 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4219 if (defined $fix_elements[$n + 2]) {
4220 $fix_elements[$n + 2] =~ s/^\s+//;
4221 }
Joe Perches3705ce52013-07-03 15:05:31 -07004222 $line_fixed = 1;
4223 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004224 }
4225
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004226 # A colon needs no spaces before when it is
4227 # terminating a case value or a label.
4228 } elsif ($opv eq ':C' || $opv eq ':L') {
4229 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004230 if (ERROR("SPACING",
4231 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004232 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004233 $line_fixed = 1;
4234 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004235 }
4236
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004237 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004238 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004239 my $ok = 0;
4240
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004241 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004242 if (($op eq '<' &&
4243 $cc =~ /^\S+\@\S+>/) ||
4244 ($op eq '>' &&
4245 $ca =~ /<\S+\@\S+$/))
4246 {
4247 $ok = 1;
4248 }
4249
Joe Perchese0df7e12015-04-16 12:44:53 -07004250 # for asm volatile statements
4251 # ignore a colon with another
4252 # colon immediately before or after
4253 if (($op eq ':') &&
4254 ($ca =~ /:$/ || $cc =~ /^:/)) {
4255 $ok = 1;
4256 }
4257
Joe Perches84731622013-11-12 15:10:05 -08004258 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004259 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004260 my $msg_type = \&ERROR;
4261 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4262
4263 if (&{$msg_type}("SPACING",
4264 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004265 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4266 if (defined $fix_elements[$n + 2]) {
4267 $fix_elements[$n + 2] =~ s/^\s+//;
4268 }
Joe Perches3705ce52013-07-03 15:05:31 -07004269 $line_fixed = 1;
4270 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004271 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004272 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004273 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004274
4275## print("n: <$n> GOOD: <$good>\n");
4276
4277 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004278 }
Joe Perches3705ce52013-07-03 15:05:31 -07004279
4280 if (($#elements % 2) == 0) {
4281 $fixed_line = $fixed_line . $fix_elements[$#elements];
4282 }
4283
Joe Perches194f66f2014-08-06 16:11:03 -07004284 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4285 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004286 }
4287
4288
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004289 }
4290
Joe Perches786b6322013-07-03 15:05:32 -07004291# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004292 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004293 if (WARN("SPACING",
4294 "space prohibited before semicolon\n" . $herecurr) &&
4295 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004296 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004297 s/^(\+.*\S)\s+;/$1;/;
4298 }
4299 }
4300
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004301# check for multiple assignments
4302 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004303 CHK("MULTIPLE_ASSIGNMENTS",
4304 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004305 }
4306
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004307## # check for multiple declarations, allowing for a function declaration
4308## # continuation.
4309## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4310## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4311##
4312## # Remove any bracketed sections to ensure we do not
4313## # falsly report the parameters of functions.
4314## my $ln = $line;
4315## while ($ln =~ s/\([^\(\)]*\)//g) {
4316## }
4317## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004318## WARN("MULTIPLE_DECLARATION",
4319## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004320## }
4321## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004322
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004323#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004324 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004325 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004326 if (ERROR("SPACING",
4327 "space required before the open brace '{'\n" . $herecurr) &&
4328 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004329 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004330 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004331 }
4332
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004333## # check for blank lines before declarations
4334## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4335## $prevrawline =~ /^.\s*$/) {
4336## WARN("SPACING",
4337## "No blank lines before declarations\n" . $hereprev);
4338## }
4339##
4340
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004341# closing brace should have a space following it when it has anything
4342# on the line
4343 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004344 if (ERROR("SPACING",
4345 "space required after that close brace '}'\n" . $herecurr) &&
4346 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004347 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004348 s/}((?!(?:,|;|\)))\S)/} $1/;
4349 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004350 }
4351
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004352# check spacing on square brackets
4353 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004354 if (ERROR("SPACING",
4355 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4356 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004357 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004358 s/\[\s+/\[/;
4359 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004360 }
4361 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004362 if (ERROR("SPACING",
4363 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4364 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004365 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004366 s/\s+\]/\]/;
4367 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004368 }
4369
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004370# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004371 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004372 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004373 if (ERROR("SPACING",
4374 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4375 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004376 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004377 s/\(\s+/\(/;
4378 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004379 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004380 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004381 $line !~ /for\s*\(.*;\s+\)/ &&
4382 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004383 if (ERROR("SPACING",
4384 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4385 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004386 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004387 s/\s+\)/\)/;
4388 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004389 }
4390
Joe Perchese2826fd2014-08-06 16:10:48 -07004391# check unnecessary parentheses around addressof/dereference single $Lvals
4392# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4393
4394 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004395 my $var = $1;
4396 if (CHK("UNNECESSARY_PARENTHESES",
4397 "Unnecessary parentheses around $var\n" . $herecurr) &&
4398 $fix) {
4399 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4400 }
4401 }
4402
4403# check for unnecessary parentheses around function pointer uses
4404# ie: (foo->bar)(); should be foo->bar();
4405# but not "if (foo->bar) (" to avoid some false positives
4406 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4407 my $var = $2;
4408 if (CHK("UNNECESSARY_PARENTHESES",
4409 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4410 $fix) {
4411 my $var2 = deparenthesize($var);
4412 $var2 =~ s/\s//g;
4413 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4414 }
4415 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004416
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004417#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004418 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004419 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004420 if (WARN("INDENTED_LABEL",
4421 "labels should not be indented\n" . $herecurr) &&
4422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004423 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004424 s/^(.)\s+/$1/;
4425 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004426 }
4427
Joe Perches5b9553a2014-04-03 14:49:21 -07004428# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004429 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004430 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004431 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004432 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4433 my $value = $1;
4434 $value = deparenthesize($value);
4435 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4436 ERROR("RETURN_PARENTHESES",
4437 "return is not a function, parentheses are not required\n" . $herecurr);
4438 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004439 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004440 ERROR("SPACING",
4441 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004442 }
4443 }
Joe Perches507e5142013-11-12 15:10:13 -08004444
Joe Perchesb43ae212014-06-23 13:22:07 -07004445# unnecessary return in a void function
4446# at end-of-function, with the previous line a single leading tab, then return;
4447# and the line before that not a goto label target like "out:"
4448 if ($sline =~ /^[ \+]}\s*$/ &&
4449 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4450 $linenr >= 3 &&
4451 $lines[$linenr - 3] =~ /^[ +]/ &&
4452 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004453 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004454 "void function return statements are not generally useful\n" . $hereprev);
4455 }
Joe Perches9819cf22014-06-04 16:12:09 -07004456
Joe Perches189248d2014-01-23 15:54:47 -08004457# if statements using unnecessary parentheses - ie: if ((foo == bar))
4458 if ($^V && $^V ge 5.10.0 &&
4459 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4460 my $openparens = $1;
4461 my $count = $openparens =~ tr@\(@\(@;
4462 my $msg = "";
4463 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4464 my $comp = $4; #Not $1 because of $LvalOrFunc
4465 $msg = " - maybe == should be = ?" if ($comp eq "==");
4466 WARN("UNNECESSARY_PARENTHESES",
4467 "Unnecessary parentheses$msg\n" . $herecurr);
4468 }
4469 }
4470
Joe Perchesc5595fa2015-09-09 15:37:58 -07004471# comparisons with a constant or upper case identifier on the left
4472# avoid cases like "foo + BAR < baz"
4473# only fix matches surrounded by parentheses to avoid incorrect
4474# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4475 if ($^V && $^V ge 5.10.0 &&
4476 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4477 my $lead = $1;
4478 my $const = $2;
4479 my $comp = $3;
4480 my $to = $4;
4481 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004482 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004483 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4484 WARN("CONSTANT_COMPARISON",
4485 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4486 $fix) {
4487 if ($comp eq "<") {
4488 $newcomp = ">";
4489 } elsif ($comp eq "<=") {
4490 $newcomp = ">=";
4491 } elsif ($comp eq ">") {
4492 $newcomp = "<";
4493 } elsif ($comp eq ">=") {
4494 $newcomp = "<=";
4495 }
4496 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4497 }
4498 }
4499
Joe Perchesf34e4a42015-04-16 12:44:19 -07004500# Return of what appears to be an errno should normally be negative
4501 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004502 my $name = $1;
4503 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004504 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004505 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004506 }
4507 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004508
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004509# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004510 if ($line =~ /\b(if|while|for|switch)\(/) {
4511 if (ERROR("SPACING",
4512 "space required before the open parenthesis '('\n" . $herecurr) &&
4513 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004514 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004515 s/\b(if|while|for|switch)\(/$1 \(/;
4516 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004517 }
4518
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004519# Check for illegal assignment in if conditional -- and check for trailing
4520# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004521 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004522 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4523 ctx_statement_block($linenr, $realcnt, 0)
4524 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004525 my ($stat_next) = ctx_statement_block($line_nr_next,
4526 $remain_next, $off_next);
4527 $stat_next =~ s/\n./\n /g;
4528 ##print "stat<$stat> stat_next<$stat_next>\n";
4529
4530 if ($stat_next =~ /^\s*while\b/) {
4531 # If the statement carries leading newlines,
4532 # then count those as offsets.
4533 my ($whitespace) =
4534 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4535 my $offset =
4536 statement_rawlines($whitespace) - 1;
4537
4538 $suppress_whiletrailers{$line_nr_next +
4539 $offset} = 1;
4540 }
4541 }
4542 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004543 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004544 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004545 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004546
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004547 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004548 ERROR("ASSIGN_IN_IF",
4549 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004550 }
4551
4552 # Find out what is on the end of the line after the
4553 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004554 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004555 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004556 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004557 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4558 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004559 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004560 # Find out how long the conditional actually is.
4561 my @newlines = ($c =~ /\n/gs);
4562 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004563 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004564
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004565 $stat_real = raw_line($linenr, $cond_lines)
4566 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004567 if (defined($stat_real) && $cond_lines > 1) {
4568 $stat_real = "[...]\n$stat_real";
4569 }
4570
Joe Perches000d1cc12011-07-25 17:13:25 -07004571 ERROR("TRAILING_STATEMENTS",
4572 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004573 }
4574 }
4575
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004576# Check for bitwise tests written as boolean
4577 if ($line =~ /
4578 (?:
4579 (?:\[|\(|\&\&|\|\|)
4580 \s*0[xX][0-9]+\s*
4581 (?:\&\&|\|\|)
4582 |
4583 (?:\&\&|\|\|)
4584 \s*0[xX][0-9]+\s*
4585 (?:\&\&|\|\||\)|\])
4586 )/x)
4587 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004588 WARN("HEXADECIMAL_BOOLEAN_TEST",
4589 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004590 }
4591
Andy Whitcroft8905a672007-11-28 16:21:06 -08004592# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004593 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4594 my $s = $1;
4595 $s =~ s/$;//g; # Remove any comments
4596 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004597 ERROR("TRAILING_STATEMENTS",
4598 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004599 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004600 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004601# if should not continue a brace
4602 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004603 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004604 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004605 $herecurr);
4606 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004607# case and default should not have general statements after them
4608 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4609 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004610 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004611 \s*return\s+
4612 )/xg)
4613 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004614 ERROR("TRAILING_STATEMENTS",
4615 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004616 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004617
4618 # Check for }<nl>else {, these must be at the same
4619 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004620 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4621 $previndent == $indent) {
4622 if (ERROR("ELSE_AFTER_BRACE",
4623 "else should follow close brace '}'\n" . $hereprev) &&
4624 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4625 fix_delete_line($fixlinenr - 1, $prevrawline);
4626 fix_delete_line($fixlinenr, $rawline);
4627 my $fixedline = $prevrawline;
4628 $fixedline =~ s/}\s*$//;
4629 if ($fixedline !~ /^\+\s*$/) {
4630 fix_insert_line($fixlinenr, $fixedline);
4631 }
4632 $fixedline = $rawline;
4633 $fixedline =~ s/^(.\s*)else/$1} else/;
4634 fix_insert_line($fixlinenr, $fixedline);
4635 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004636 }
4637
Joe Perches8b8856f2014-08-06 16:11:14 -07004638 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4639 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004640 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4641
4642 # Find out what is on the end of the line after the
4643 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004644 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004645 $s =~ s/\n.*//g;
4646
4647 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004648 if (ERROR("WHILE_AFTER_BRACE",
4649 "while should follow close brace '}'\n" . $hereprev) &&
4650 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4651 fix_delete_line($fixlinenr - 1, $prevrawline);
4652 fix_delete_line($fixlinenr, $rawline);
4653 my $fixedline = $prevrawline;
4654 my $trailing = $rawline;
4655 $trailing =~ s/^\+//;
4656 $trailing = trim($trailing);
4657 $fixedline =~ s/}\s*$/} $trailing/;
4658 fix_insert_line($fixlinenr, $fixedline);
4659 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004660 }
4661 }
4662
Joe Perches95e2c602013-07-03 15:05:20 -07004663#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004664 while ($line =~ m{($Constant|$Lval)}g) {
4665 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004666
4667#gcc binary extension
4668 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004669 if (WARN("GCC_BINARY_CONSTANT",
4670 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4671 $fix) {
4672 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004673 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004674 s/\b$var\b/$hexval/;
4675 }
Joe Perches95e2c602013-07-03 15:05:20 -07004676 }
4677
4678#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004679 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004680 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004681#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004682 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004683#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004684 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4685#Ignore some three character SI units explicitly, like MiB and KHz
4686 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004687 while ($var =~ m{($Ident)}g) {
4688 my $word = $1;
4689 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004690 if ($check) {
4691 seed_camelcase_includes();
4692 if (!$file && !$camelcase_file_seeded) {
4693 seed_camelcase_file($realfile);
4694 $camelcase_file_seeded = 1;
4695 }
4696 }
Joe Perches7e781f62013-09-11 14:23:55 -07004697 if (!defined $camelcase{$word}) {
4698 $camelcase{$word} = 1;
4699 CHK("CAMELCASE",
4700 "Avoid CamelCase: <$word>\n" . $herecurr);
4701 }
Joe Perches34456862013-07-03 15:05:34 -07004702 }
Joe Perches323c1262012-12-17 16:02:07 -08004703 }
4704 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004705
4706#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004707 if ($line =~ /\#\s*define.*\\\s+$/) {
4708 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4709 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4710 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004711 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004712 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004713 }
4714
Fabian Frederick0e212e02015-04-16 12:44:25 -07004715# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4716# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004717 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004718 my $file = "$1.h";
4719 my $checkfile = "include/linux/$file";
4720 if (-f "$root/$checkfile" &&
4721 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004722 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004723 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004724 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4725 if ($asminclude > 0) {
4726 if ($realfile =~ m{^arch/}) {
4727 CHK("ARCH_INCLUDE_LINUX",
4728 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4729 } else {
4730 WARN("INCLUDE_LINUX",
4731 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4732 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004733 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004734 }
4735 }
4736
Andy Whitcroft653d4872007-06-23 17:16:34 -07004737# multi-statement macros should be enclosed in a do while loop, grab the
4738# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004739# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004740 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4741 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004742 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004743 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004744 my ($off, $dstat, $dcond, $rest);
4745 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004746 my $has_flow_statement = 0;
4747 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004748 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004749 ctx_statement_block($linenr, $realcnt, 0);
4750 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004751 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004752 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004753
Joe Perches08a28432014-10-13 15:51:55 -07004754 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004755 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004756
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004757 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004758 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004759 $dstat =~ s/\\\n.//g;
4760 $dstat =~ s/^\s*//s;
4761 $dstat =~ s/\s*$//s;
4762
4763 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004764 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4765 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004766 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004767 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004768 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004769
Gregory Bean02870be2011-05-11 09:11:12 -07004770 # Extremely long macros may fall off the end of the
4771 # available context without closing. Give a dangling
4772 # backslash the benefit of the doubt and allow it
4773 # to gobble any hanging open-parens.
4774 $dstat =~ s/\(.+\\$/1/;
4775
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004776 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004777 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4778 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004779 {
4780 }
4781
Joe Perches42e15292016-03-15 14:58:01 -07004782 # Make asm volatile uses seem like a generic function
4783 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4784
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004785 my $exceptions = qr{
4786 $Declare|
4787 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004788 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004789 DECLARE_PER_CPU|
4790 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004791 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004792 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004793 union|
4794 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004795 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004796 ^\"|\"$|
4797 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004798 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004799 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004800 if ($dstat ne '' &&
4801 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4802 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004803 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004804 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004805 $dstat !~ /$exceptions/ &&
4806 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004807 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004808 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004809 $dstat !~ /^for\s*$Constant$/ && # for (...)
4810 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4811 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004812 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004813 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004814 {
4815 $ctx =~ s/\n*$//;
4816 my $herectx = $here . "\n";
4817 my $cnt = statement_rawlines($ctx);
4818
4819 for (my $n = 0; $n < $cnt; $n++) {
4820 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004821 }
4822
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004823 if ($dstat =~ /;/) {
4824 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4825 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4826 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004827 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004828 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004829 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004830 }
Joe Perches5023d342012-12-17 16:01:47 -08004831
Joe Perches08a28432014-10-13 15:51:55 -07004832# check for macros with flow control, but without ## concatenation
4833# ## concatenation is commonly a macro that defines a function so ignore those
4834 if ($has_flow_statement && !$has_arg_concat) {
4835 my $herectx = $here . "\n";
4836 my $cnt = statement_rawlines($ctx);
4837
4838 for (my $n = 0; $n < $cnt; $n++) {
4839 $herectx .= raw_line($linenr, $n) . "\n";
4840 }
4841 WARN("MACRO_WITH_FLOW_CONTROL",
4842 "Macros with flow control statements should be avoided\n" . "$herectx");
4843 }
4844
Joe Perches481eb482012-12-17 16:01:56 -08004845# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004846
4847 } else {
4848 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004849 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4850 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004851 $line =~ /^\+.*\\$/) {
4852 WARN("LINE_CONTINUATIONS",
4853 "Avoid unnecessary line continuations\n" . $herecurr);
4854 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004855 }
4856
Joe Perchesb13edf72012-07-30 14:41:24 -07004857# do {} while (0) macro tests:
4858# single-statement macros do not need to be enclosed in do while (0) loop,
4859# macro should not end with a semicolon
4860 if ($^V && $^V ge 5.10.0 &&
4861 $realfile !~ m@/vmlinux.lds.h$@ &&
4862 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4863 my $ln = $linenr;
4864 my $cnt = $realcnt;
4865 my ($off, $dstat, $dcond, $rest);
4866 my $ctx = '';
4867 ($dstat, $dcond, $ln, $cnt, $off) =
4868 ctx_statement_block($linenr, $realcnt, 0);
4869 $ctx = $dstat;
4870
4871 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004872 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004873
4874 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4875 my $stmts = $2;
4876 my $semis = $3;
4877
4878 $ctx =~ s/\n*$//;
4879 my $cnt = statement_rawlines($ctx);
4880 my $herectx = $here . "\n";
4881
4882 for (my $n = 0; $n < $cnt; $n++) {
4883 $herectx .= raw_line($linenr, $n) . "\n";
4884 }
4885
Joe Perchesac8e97f2012-08-21 16:15:53 -07004886 if (($stmts =~ tr/;/;/) == 1 &&
4887 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004888 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4889 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4890 }
4891 if (defined $semis && $semis ne "") {
4892 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4893 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4894 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004895 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4896 $ctx =~ s/\n*$//;
4897 my $cnt = statement_rawlines($ctx);
4898 my $herectx = $here . "\n";
4899
4900 for (my $n = 0; $n < $cnt; $n++) {
4901 $herectx .= raw_line($linenr, $n) . "\n";
4902 }
4903
4904 WARN("TRAILING_SEMICOLON",
4905 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004906 }
4907 }
4908
Mike Frysinger080ba922009-01-06 14:41:25 -08004909# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4910# all assignments may have only one of the following with an assignment:
4911# .
4912# ALIGN(...)
4913# VMLINUX_SYMBOL(...)
4914 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004915 WARN("MISSING_VMLINUX_SYMBOL",
4916 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004917 }
4918
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004919# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004920 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4921 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004922 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004923 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004924 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4925 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004926 my @allowed = ();
4927 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004928 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004929 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004930 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004931 for my $chunk (@chunks) {
4932 my ($cond, $block) = @{$chunk};
4933
Andy Whitcroft773647a2008-03-28 14:15:58 -07004934 # If the condition carries leading newlines, then count those as offsets.
4935 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4936 my $offset = statement_rawlines($whitespace) - 1;
4937
Joe Perchesaad4f612012-03-23 15:02:19 -07004938 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004939 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4940
4941 # We have looked at and allowed this specific line.
4942 $suppress_ifbraces{$ln + $offset} = 1;
4943
4944 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004945 $ln += statement_rawlines($block) - 1;
4946
Andy Whitcroft773647a2008-03-28 14:15:58 -07004947 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004948
4949 $seen++ if ($block =~ /^\s*{/);
4950
Joe Perchesaad4f612012-03-23 15:02:19 -07004951 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004952 if (statement_lines($cond) > 1) {
4953 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004954 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004955 }
4956 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004957 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004958 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004959 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004960 if (statement_block_size($block) > 1) {
4961 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004962 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004963 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004964 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004965 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004966 if ($seen) {
4967 my $sum_allowed = 0;
4968 foreach (@allowed) {
4969 $sum_allowed += $_;
4970 }
4971 if ($sum_allowed == 0) {
4972 WARN("BRACES",
4973 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4974 } elsif ($sum_allowed != $allow &&
4975 $seen != $allow) {
4976 CHK("BRACES",
4977 "braces {} should be used on all arms of this statement\n" . $herectx);
4978 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004979 }
4980 }
4981 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004982 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004983 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004984 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004985
Andy Whitcroftcf655042008-03-04 14:28:20 -08004986 # Check the pre-context.
4987 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4988 #print "APW: ALLOWED: pre<$1>\n";
4989 $allowed = 1;
4990 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004991
4992 my ($level, $endln, @chunks) =
4993 ctx_statement_full($linenr, $realcnt, $-[0]);
4994
Andy Whitcroftcf655042008-03-04 14:28:20 -08004995 # Check the condition.
4996 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004997 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004998 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004999 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005000 }
5001 if (statement_lines($cond) > 1) {
5002 #print "APW: ALLOWED: cond<$cond>\n";
5003 $allowed = 1;
5004 }
5005 if ($block =~/\b(?:if|for|while)\b/) {
5006 #print "APW: ALLOWED: block<$block>\n";
5007 $allowed = 1;
5008 }
5009 if (statement_block_size($block) > 1) {
5010 #print "APW: ALLOWED: lines block<$block>\n";
5011 $allowed = 1;
5012 }
5013 # Check the post-context.
5014 if (defined $chunks[1]) {
5015 my ($cond, $block) = @{$chunks[1]};
5016 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005017 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005018 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005019 if ($block =~ /^\s*\{/) {
5020 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5021 $allowed = 1;
5022 }
5023 }
5024 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005025 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005026 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005027
Andy Whitcroftf0556632008-10-15 22:02:23 -07005028 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005029 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005030 }
5031
Joe Perches000d1cc12011-07-25 17:13:25 -07005032 WARN("BRACES",
5033 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005034 }
5035 }
5036
Joe Perches0979ae62012-12-17 16:01:59 -08005037# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005038 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005039 if (CHK("BRACES",
5040 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5041 $fix && $prevrawline =~ /^\+/) {
5042 fix_delete_line($fixlinenr - 1, $prevrawline);
5043 }
Joe Perches0979ae62012-12-17 16:01:59 -08005044 }
Joe Perches77b9a532013-07-03 15:05:29 -07005045 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005046 if (CHK("BRACES",
5047 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5048 $fix) {
5049 fix_delete_line($fixlinenr, $rawline);
5050 }
Joe Perches0979ae62012-12-17 16:01:59 -08005051 }
5052
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005053# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005054 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5055 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005056 WARN("VOLATILE",
5057 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005058 }
5059
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005060# Check for user-visible strings broken across lines, which breaks the ability
5061# to grep for the string. Make exceptions when the previous string ends in a
5062# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5063# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005064 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005065 $prevline =~ /"\s*$/ &&
5066 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5067 if (WARN("SPLIT_STRING",
5068 "quoted string split across lines\n" . $hereprev) &&
5069 $fix &&
5070 $prevrawline =~ /^\+.*"\s*$/ &&
5071 $last_coalesced_string_linenr != $linenr - 1) {
5072 my $extracted_string = get_quoted_string($line, $rawline);
5073 my $comma_close = "";
5074 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5075 $comma_close = $1;
5076 }
5077
5078 fix_delete_line($fixlinenr - 1, $prevrawline);
5079 fix_delete_line($fixlinenr, $rawline);
5080 my $fixedline = $prevrawline;
5081 $fixedline =~ s/"\s*$//;
5082 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5083 fix_insert_line($fixlinenr - 1, $fixedline);
5084 $fixedline = $rawline;
5085 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5086 if ($fixedline !~ /\+\s*$/) {
5087 fix_insert_line($fixlinenr, $fixedline);
5088 }
5089 $last_coalesced_string_linenr = $linenr;
5090 }
5091 }
5092
5093# check for missing a space in a string concatenation
5094 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5095 WARN('MISSING_SPACE',
5096 "break quoted strings at a space character\n" . $hereprev);
5097 }
5098
5099# check for spaces before a quoted newline
5100 if ($rawline =~ /^.*\".*\s\\n/) {
5101 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5102 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5103 $fix) {
5104 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5105 }
5106
5107 }
5108
Joe Perchesf17dba42014-10-13 15:51:51 -07005109# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005110 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005111 CHK("CONCATENATED_STRING",
5112 "Concatenated strings should use spaces between elements\n" . $herecurr);
5113 }
5114
Joe Perches90ad30e2014-12-10 15:51:59 -08005115# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005116 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005117 WARN("STRING_FRAGMENTS",
5118 "Consecutive strings are generally better as a single string\n" . $herecurr);
5119 }
5120
Joe Perches6e300752015-09-09 15:37:47 -07005121# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005122 my $string;
5123 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5124 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5125 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005126 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005127 WARN("PRINTF_L",
5128 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5129 last;
5130 }
Joe Perches6e300752015-09-09 15:37:47 -07005131 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5132 ERROR("PRINTF_0xDECIMAL",
5133 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5134 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005135 }
5136
5137# check for line continuations in quoted strings with odd counts of "
5138 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5139 WARN("LINE_CONTINUATIONS",
5140 "Avoid line continuations in quoted strings\n" . $herecurr);
5141 }
5142
Gregory Bean6c9c6842011-03-17 14:18:20 -07005143# sys_open/read/write/close are not allowed in the kernel
5144 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5145 ERROR("FILE_OPS",
5146 "$1 is inappropriate in kernel code.\n" .
5147 $herecurr);
5148 }
5149
Gregory Beana7cce0a2011-06-07 08:06:45 -07005150# filp_open is a backdoor for sys_open
5151 if ($line =~ /\b(filp_open)\b/) {
5152 ERROR("FILE_OPS",
5153 "$1 is inappropriate in kernel code.\n" .
5154 $herecurr);
5155 }
5156
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005157# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5158 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5159 ERROR("NON_RELAXED_IO",
5160 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5161 "with appropriate memory barriers instead.\n" .
5162 $herecurr);
5163 }
5164
5165# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5166 if ($line =~ /\b((in|out)([bwl]))\b/) {
5167 my ($all, $pref, $suf) = ($1, $2, $3);
5168 $pref =~ s/in/read/;
5169 $pref =~ s/out/write/;
5170 ERROR("NON_RELAXED_IO",
5171 "Use of $all is deprecated: use " .
5172 "__raw_$pref$suf\n\t" .
5173 "with appropriate memory barriers instead.\n" .
5174 $herecurr);
5175 }
5176
Gregory Bean438bf5f2011-06-15 09:32:38 -07005177# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005178 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005179 WARN("ARM_BARRIER",
5180 "Use of dsb is discouranged: prefer mb.\n" .
5181 $herecurr);
5182 }
5183
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005184# unbounded string functions are overflow risks
5185 my %str_fns = (
5186 "sprintf" => "snprintf",
5187 "strcpy" => "strlcpy",
5188 "strncpy" => "strlcpy",
5189 "strcat" => "strlcat",
5190 "strncat" => "strlcat",
5191 "vsprintf" => "vsnprintf",
5192 "strchr" => "strnchr",
5193 "strstr" => "strnstr",
5194 );
5195 foreach my $k (keys %str_fns) {
5196 if ($line =~ /\b$k\b/) {
5197 ERROR("UNBOUNDED_STRING_FNS",
5198 "Use of $k is deprecated: " .
5199 "use $str_fns{$k} instead.\n" .
5200 $herecurr);
5201 }
5202 }
5203
Andy Whitcroft00df3442007-06-08 13:47:06 -07005204# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005205 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005206 WARN("IF_0",
5207 "if this code is redundant consider removing it\n"
5208 . $herecurr);
5209 }
5210
5211# warn about #if 1
5212 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5213 WARN("IF_1",
5214 "if this code is required consider removing"
5215 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005216 }
5217
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005218# check for needless "if (<foo>) fn(<foo>)" uses
5219 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005220 my $tested = quotemeta($1);
5221 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5222 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5223 my $func = $1;
5224 if (WARN('NEEDLESS_IF',
5225 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5226 $fix) {
5227 my $do_fix = 1;
5228 my $leading_tabs = "";
5229 my $new_leading_tabs = "";
5230 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5231 $leading_tabs = $1;
5232 } else {
5233 $do_fix = 0;
5234 }
5235 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5236 $new_leading_tabs = $1;
5237 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5238 $do_fix = 0;
5239 }
5240 } else {
5241 $do_fix = 0;
5242 }
5243 if ($do_fix) {
5244 fix_delete_line($fixlinenr - 1, $prevrawline);
5245 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5246 }
5247 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005248 }
5249 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005250
Joe Perchesebfdc402014-08-06 16:10:27 -07005251# check for unnecessary "Out of Memory" messages
5252 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5253 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5254 (defined $1 || defined $3) &&
5255 $linenr > 3) {
5256 my $testval = $2;
5257 my $testline = $lines[$linenr - 3];
5258
5259 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5260# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5261
5262 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5263 WARN("OOM_MESSAGE",
5264 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5265 }
5266 }
5267
Joe Perchesf78d98f2014-10-13 15:52:01 -07005268# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005269 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005270 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5271 my $level = $1;
5272 if (WARN("UNNECESSARY_KERN_LEVEL",
5273 "Possible unnecessary $level\n" . $herecurr) &&
5274 $fix) {
5275 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5276 }
5277 }
5278
Joe Perchesabb08a52014-12-10 15:51:46 -08005279# check for mask then right shift without a parentheses
5280 if ($^V && $^V ge 5.10.0 &&
5281 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5282 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5283 WARN("MASK_THEN_SHIFT",
5284 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5285 }
5286
Joe Perchesb75ac612014-12-10 15:52:02 -08005287# check for pointer comparisons to NULL
5288 if ($^V && $^V ge 5.10.0) {
5289 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5290 my $val = $1;
5291 my $equal = "!";
5292 $equal = "" if ($4 eq "!=");
5293 if (CHK("COMPARISON_TO_NULL",
5294 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5295 $fix) {
5296 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5297 }
5298 }
5299 }
5300
Joe Perches8716de32013-09-11 14:24:05 -07005301# check for bad placement of section $InitAttribute (e.g.: __initdata)
5302 if ($line =~ /(\b$InitAttribute\b)/) {
5303 my $attr = $1;
5304 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5305 my $ptr = $1;
5306 my $var = $2;
5307 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5308 ERROR("MISPLACED_INIT",
5309 "$attr should be placed after $var\n" . $herecurr)) ||
5310 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5311 WARN("MISPLACED_INIT",
5312 "$attr should be placed after $var\n" . $herecurr))) &&
5313 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005314 $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 -07005315 }
5316 }
5317 }
5318
Joe Perchese970b882013-11-12 15:10:10 -08005319# check for $InitAttributeData (ie: __initdata) with const
5320 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5321 my $attr = $1;
5322 $attr =~ /($InitAttributePrefix)(.*)/;
5323 my $attr_prefix = $1;
5324 my $attr_type = $2;
5325 if (ERROR("INIT_ATTRIBUTE",
5326 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5327 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005328 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005329 s/$InitAttributeData/${attr_prefix}initconst/;
5330 }
5331 }
5332
5333# check for $InitAttributeConst (ie: __initconst) without const
5334 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5335 my $attr = $1;
5336 if (ERROR("INIT_ATTRIBUTE",
5337 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5338 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005339 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005340 /(^\+\s*(?:static\s+))/;
5341 $lead = rtrim($1);
5342 $lead = "$lead " if ($lead !~ /^\+$/);
5343 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005344 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005345 }
5346 }
5347
Joe Perchesc17893c2015-04-16 12:44:42 -07005348# check for __read_mostly with const non-pointer (should just be const)
5349 if ($line =~ /\b__read_mostly\b/ &&
5350 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5351 if (ERROR("CONST_READ_MOSTLY",
5352 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5353 $fix) {
5354 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5355 }
5356 }
5357
Joe Perchesfbdb8132014-04-03 14:49:14 -07005358# don't use __constant_<foo> functions outside of include/uapi/
5359 if ($realfile !~ m@^include/uapi/@ &&
5360 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5361 my $constant_func = $1;
5362 my $func = $constant_func;
5363 $func =~ s/^__constant_//;
5364 if (WARN("CONSTANT_CONVERSION",
5365 "$constant_func should be $func\n" . $herecurr) &&
5366 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005367 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005368 }
5369 }
5370
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005371# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005372 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005373 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005374 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005375 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005376 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005377 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5378 }
5379 if ($delay > 2000) {
5380 WARN("LONG_UDELAY",
5381 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005382 }
5383 }
5384
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005385# warn about unexpectedly long msleep's
5386 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5387 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005388 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005389 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005390 }
5391 }
5392
Joe Perches36ec1932013-07-03 15:05:25 -07005393# check for comparisons of jiffies
5394 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5395 WARN("JIFFIES_COMPARISON",
5396 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5397 }
5398
Joe Perches9d7a34a2013-07-03 15:05:26 -07005399# check for comparisons of get_jiffies_64()
5400 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5401 WARN("JIFFIES_COMPARISON",
5402 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5403 }
5404
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005405# check the patch for use of mdelay
5406 if ($line =~ /\bmdelay\s*\(/) {
5407 WARN("MDELAY",
5408 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5409 }
5410
Andy Whitcroft00df3442007-06-08 13:47:06 -07005411# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005412# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005413# print "#ifdef in C files should be avoided\n";
5414# print "$herecurr";
5415# $clean = 0;
5416# }
5417
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005418# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005419 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005420 if (ERROR("SPACING",
5421 "exactly one space required after that #$1\n" . $herecurr) &&
5422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005423 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005424 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5425 }
5426
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005427 }
5428
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005429# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005430 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5431 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005432 my $which = $1;
5433 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005434 CHK("UNCOMMENTED_DEFINITION",
5435 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005436 }
5437 }
5438# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005439
5440 my $barriers = qr{
5441 mb|
5442 rmb|
5443 wmb|
5444 read_barrier_depends
5445 }x;
5446 my $barrier_stems = qr{
5447 mb__before_atomic|
5448 mb__after_atomic|
5449 store_release|
5450 load_acquire|
5451 store_mb|
5452 (?:$barriers)
5453 }x;
5454 my $all_barriers = qr{
5455 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005456 smp_(?:$barrier_stems)|
5457 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005458 }x;
5459
5460 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005461 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005462 WARN("MEMORY_BARRIER",
5463 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005464 }
5465 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005466
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005467 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5468
5469 if ($realfile !~ m@^include/asm-generic/@ &&
5470 $realfile !~ m@/barrier\.h$@ &&
5471 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5472 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5473 WARN("MEMORY_BARRIER",
5474 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5475 }
5476
Joe Perchescb426e92015-06-25 15:02:46 -07005477# check for waitqueue_active without a comment.
5478 if ($line =~ /\bwaitqueue_active\s*\(/) {
5479 if (!ctx_has_comment($first_line, $linenr)) {
5480 WARN("WAITQUEUE_ACTIVE",
5481 "waitqueue_active without comment\n" . $herecurr);
5482 }
5483 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005484
5485# Check for expedited grace periods that interrupt non-idle non-nohz
5486# online CPUs. These expedited can therefore degrade real-time response
5487# if used carelessly, and should be avoided where not absolutely
5488# needed. It is always OK to use synchronize_rcu_expedited() and
5489# synchronize_sched_expedited() at boot time (before real-time applications
5490# start) and in error situations where real-time response is compromised in
5491# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5492# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5493# Of course, nothing comes for free, and srcu_read_lock() and
5494# srcu_read_unlock() do contain full memory barriers in payment for
5495# synchronize_srcu_expedited() non-interruption properties.
5496 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5497 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5498 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5499
5500 }
5501
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005502# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005503 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005504 CHK("ARCH_DEFINES",
5505 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005506 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005507
Tobias Klauserd4977c72010-05-24 14:33:30 -07005508# Check that the storage class is at the beginning of a declaration
5509 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005510 WARN("STORAGE_CLASS",
5511 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005512 }
5513
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005514# check the location of the inline attribute, that it is between
5515# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005516 if ($line =~ /\b$Type\s+$Inline\b/ ||
5517 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005518 ERROR("INLINE_LOCATION",
5519 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005520 }
5521
Andy Whitcroft8905a672007-11-28 16:21:06 -08005522# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005523 if ($realfile !~ m@\binclude/uapi/@ &&
5524 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005525 if (WARN("INLINE",
5526 "plain inline is preferred over $1\n" . $herecurr) &&
5527 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005528 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005529
5530 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005531 }
5532
Joe Perches3d130fd2011-01-12 17:00:00 -08005533# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005534 if ($realfile !~ m@\binclude/uapi/@ &&
5535 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005536 WARN("PREFER_PACKED",
5537 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005538 }
5539
Joe Perches39b7e282011-07-25 17:13:24 -07005540# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005541 if ($realfile !~ m@\binclude/uapi/@ &&
5542 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005543 WARN("PREFER_ALIGNED",
5544 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005545 }
5546
Joe Perches5f14d3b2012-01-10 15:09:52 -08005547# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005548 if ($realfile !~ m@\binclude/uapi/@ &&
5549 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005550 if (WARN("PREFER_PRINTF",
5551 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5552 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005553 $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 -07005554
5555 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005556 }
5557
Joe Perches6061d942012-03-23 15:02:16 -07005558# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005559 if ($realfile !~ m@\binclude/uapi/@ &&
5560 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005561 if (WARN("PREFER_SCANF",
5562 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5563 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005564 $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 -07005565 }
Joe Perches6061d942012-03-23 15:02:16 -07005566 }
5567
Joe Perches619a9082014-12-10 15:51:35 -08005568# Check for __attribute__ weak, or __weak declarations (may have link issues)
5569 if ($^V && $^V ge 5.10.0 &&
5570 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5571 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5572 $line =~ /\b__weak\b/)) {
5573 ERROR("WEAK_DECLARATION",
5574 "Using weak declarations can have unintended link defects\n" . $herecurr);
5575 }
5576
Joe Perchese6176fa2015-06-25 15:02:49 -07005577# check for c99 types like uint8_t used outside of uapi/
5578 if ($realfile !~ m@\binclude/uapi/@ &&
5579 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5580 my $type = $1;
5581 if ($type =~ /\b($typeC99Typedefs)\b/) {
5582 $type = $1;
5583 my $kernel_type = 'u';
5584 $kernel_type = 's' if ($type =~ /^_*[si]/);
5585 $type =~ /(\d+)/;
5586 $kernel_type .= $1;
5587 if (CHK("PREFER_KERNEL_TYPES",
5588 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5589 $fix) {
5590 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5591 }
5592 }
5593 }
5594
Joe Perches938224b2016-01-20 14:59:15 -08005595# check for cast of C90 native int or longer types constants
5596 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5597 my $cast = $1;
5598 my $const = $2;
5599 if (WARN("TYPECAST_INT_CONSTANT",
5600 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5601 $fix) {
5602 my $suffix = "";
5603 my $newconst = $const;
5604 $newconst =~ s/${Int_type}$//;
5605 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5606 if ($cast =~ /\blong\s+long\b/) {
5607 $suffix .= 'LL';
5608 } elsif ($cast =~ /\blong\b/) {
5609 $suffix .= 'L';
5610 }
5611 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5612 }
5613 }
5614
Joe Perches8f53a9b2010-03-05 13:43:48 -08005615# check for sizeof(&)
5616 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005617 WARN("SIZEOF_ADDRESS",
5618 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005619 }
5620
Joe Perches66c80b62012-07-30 14:41:22 -07005621# check for sizeof without parenthesis
5622 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005623 if (WARN("SIZEOF_PARENTHESIS",
5624 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5625 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005626 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005627 }
Joe Perches66c80b62012-07-30 14:41:22 -07005628 }
5629
Joe Perches88982fe2012-12-17 16:02:00 -08005630# check for struct spinlock declarations
5631 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5632 WARN("USE_SPINLOCK_T",
5633 "struct spinlock should be spinlock_t\n" . $herecurr);
5634 }
5635
Joe Perchesa6962d72013-04-29 16:18:13 -07005636# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005637 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005638 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005639 $fmt =~ s/%%//g;
5640 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005641 if (WARN("PREFER_SEQ_PUTS",
5642 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5643 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005644 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005645 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005646 }
5647 }
5648
Andy Whitcroft554e1652012-01-10 15:09:57 -08005649# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005650 if ($^V && $^V ge 5.10.0 &&
5651 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005652 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005653
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005654 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005655 my $ms_val = $7;
5656 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005657
Andy Whitcroft554e1652012-01-10 15:09:57 -08005658 if ($ms_size =~ /^(0x|)0$/i) {
5659 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005660 "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 -08005661 } elsif ($ms_size =~ /^(0x|)1$/i) {
5662 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005663 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5664 }
5665 }
5666
Joe Perches98a9bba2014-01-23 15:54:52 -08005667# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5668 if ($^V && $^V ge 5.10.0 &&
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005669 defined $stat &&
5670 $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
Joe Perches98a9bba2014-01-23 15:54:52 -08005671 if (WARN("PREFER_ETHER_ADDR_COPY",
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005672 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
Joe Perches98a9bba2014-01-23 15:54:52 -08005673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005674 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
Joe Perches98a9bba2014-01-23 15:54:52 -08005675 }
5676 }
5677
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005678# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5679 if ($^V && $^V ge 5.10.0 &&
5680 defined $stat &&
5681 $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5682 WARN("PREFER_ETHER_ADDR_EQUAL",
5683 "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5684 }
5685
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005686# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5687# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5688 if ($^V && $^V ge 5.10.0 &&
5689 defined $stat &&
5690 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5691
5692 my $ms_val = $7;
5693
5694 if ($ms_val =~ /^(?:0x|)0+$/i) {
5695 if (WARN("PREFER_ETH_ZERO_ADDR",
5696 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5697 $fix) {
5698 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5699 }
5700 } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5701 if (WARN("PREFER_ETH_BROADCAST_ADDR",
5702 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5703 $fix) {
5704 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5705 }
5706 }
5707 }
5708
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005709# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005710 if ($^V && $^V ge 5.10.0 &&
5711 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005712 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005713 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005714 my $call = $1;
5715 my $cast1 = deparenthesize($2);
5716 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005717 my $cast2 = deparenthesize($7);
5718 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005719 my $cast;
5720
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005721 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005722 $cast = "$cast1 or $cast2";
5723 } elsif ($cast1 ne "") {
5724 $cast = $cast1;
5725 } else {
5726 $cast = $cast2;
5727 }
5728 WARN("MINMAX",
5729 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005730 }
5731 }
5732
Joe Perches4a273192012-07-30 14:41:20 -07005733# check usleep_range arguments
5734 if ($^V && $^V ge 5.10.0 &&
5735 defined $stat &&
5736 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5737 my $min = $1;
5738 my $max = $7;
5739 if ($min eq $max) {
5740 WARN("USLEEP_RANGE",
5741 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5742 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5743 $min > $max) {
5744 WARN("USLEEP_RANGE",
5745 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5746 }
5747 }
5748
Joe Perches823b7942013-11-12 15:10:15 -08005749# check for naked sscanf
5750 if ($^V && $^V ge 5.10.0 &&
5751 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005752 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005753 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5754 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5755 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5756 my $lc = $stat =~ tr@\n@@;
5757 $lc = $lc + $linenr;
5758 my $stat_real = raw_line($linenr, 0);
5759 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5760 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5761 }
5762 WARN("NAKED_SSCANF",
5763 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5764 }
5765
Joe Perchesafc819a2014-06-04 16:12:08 -07005766# check for simple sscanf that should be kstrto<foo>
5767 if ($^V && $^V ge 5.10.0 &&
5768 defined $stat &&
5769 $line =~ /\bsscanf\b/) {
5770 my $lc = $stat =~ tr@\n@@;
5771 $lc = $lc + $linenr;
5772 my $stat_real = raw_line($linenr, 0);
5773 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5774 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5775 }
5776 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5777 my $format = $6;
5778 my $count = $format =~ tr@%@%@;
5779 if ($count == 1 &&
5780 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5781 WARN("SSCANF_TO_KSTRTO",
5782 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5783 }
5784 }
5785 }
5786
Joe Perches70dc8a42013-09-11 14:23:58 -07005787# check for new externs in .h files.
5788 if ($realfile =~ /\.h$/ &&
5789 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005790 if (CHK("AVOID_EXTERNS",
5791 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005793 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005794 }
5795 }
5796
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005797# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005798 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005799 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005800 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005801 my $function_name = $1;
5802 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005803
5804 my $s = $stat;
5805 if (defined $cond) {
5806 substr($s, 0, length($cond), '');
5807 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005808 if ($s =~ /^\s*;/ &&
5809 $function_name ne 'uninitialized_var')
5810 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005811 WARN("AVOID_EXTERNS",
5812 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005813 }
5814
5815 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005816 WARN("FUNCTION_ARGUMENTS",
5817 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005818 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005819
5820 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5821 $stat =~ /^.\s*extern\s+/)
5822 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005823 WARN("AVOID_EXTERNS",
5824 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005825 }
5826
5827# checks for new __setup's
5828 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5829 my $name = $1;
5830
5831 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005832 CHK("UNDOCUMENTED_SETUP",
5833 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005834 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005835 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005836
5837# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005838 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005839 WARN("UNNECESSARY_CASTS",
5840 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005841 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005842
Joe Perchesa640d252013-07-03 15:05:21 -07005843# alloc style
5844# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5845 if ($^V && $^V ge 5.10.0 &&
5846 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5847 CHK("ALLOC_SIZEOF_STRUCT",
5848 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5849 }
5850
Joe Perches60a55362014-06-04 16:12:07 -07005851# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5852 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005853 $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 -07005854 my $oldfunc = $3;
5855 my $a1 = $4;
5856 my $a2 = $10;
5857 my $newfunc = "kmalloc_array";
5858 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005859 my $r1 = $a1;
5860 my $r2 = $a2;
5861 if ($a1 =~ /^sizeof\s*\S/) {
5862 $r1 = $a2;
5863 $r2 = $a1;
5864 }
5865 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5866 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005867 if (WARN("ALLOC_WITH_MULTIPLY",
5868 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5869 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005870 $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 -07005871
5872 }
5873 }
5874 }
5875
Joe Perches972fdea2013-04-29 16:18:12 -07005876# check for krealloc arg reuse
5877 if ($^V && $^V ge 5.10.0 &&
5878 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5879 WARN("KREALLOC_ARG_REUSE",
5880 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5881 }
5882
Joe Perches5ce59ae2013-02-21 16:44:18 -08005883# check for alloc argument mismatch
5884 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5885 WARN("ALLOC_ARRAY_ARGS",
5886 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5887 }
5888
Joe Perchescaf2a542011-01-12 16:59:56 -08005889# check for multiple semicolons
5890 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005891 if (WARN("ONE_SEMICOLON",
5892 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5893 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005894 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005895 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005896 }
5897
Tomas Winklercec3aaa2016-08-02 14:04:39 -07005898# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5899 if ($realfile !~ m@^include/uapi/@ &&
5900 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08005901 my $ull = "";
5902 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5903 if (CHK("BIT_MACRO",
5904 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5905 $fix) {
5906 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5907 }
5908 }
5909
Joe Perches2d632742016-05-20 17:04:00 -07005910# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
5911 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
5912 my $config = $1;
5913 if (WARN("PREFER_IS_ENABLED",
5914 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
5915 $fix) {
5916 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
5917 }
5918 }
5919
Joe Perchese81f2392014-08-06 16:11:25 -07005920# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005921 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5922 my $has_break = 0;
5923 my $has_statement = 0;
5924 my $count = 0;
5925 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005926 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005927 $prevline--;
5928 my $rline = $rawlines[$prevline - 1];
5929 my $fline = $lines[$prevline - 1];
5930 last if ($fline =~ /^\@\@/);
5931 next if ($fline =~ /^\-/);
5932 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5933 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5934 next if ($fline =~ /^.[\s$;]*$/);
5935 $has_statement = 1;
5936 $count++;
5937 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5938 }
5939 if (!$has_break && $has_statement) {
5940 WARN("MISSING_BREAK",
5941 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5942 }
5943 }
5944
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005945# check for switch/default statements without a break;
5946 if ($^V && $^V ge 5.10.0 &&
5947 defined $stat &&
5948 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5949 my $ctx = '';
5950 my $herectx = $here . "\n";
5951 my $cnt = statement_rawlines($stat);
5952 for (my $n = 0; $n < $cnt; $n++) {
5953 $herectx .= raw_line($linenr, $n) . "\n";
5954 }
5955 WARN("DEFAULT_NO_BREAK",
5956 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005957 }
5958
Patrick Pannutof370e252010-07-23 13:34:18 -07005959# check for return codes on error paths
5960 if ($line =~ /\breturn\s+-\d+/) {
5961 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07005962 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07005963 }
5964
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005965# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005966 if ($line =~ /\b__FUNCTION__\b/) {
5967 if (WARN("USE_FUNC",
5968 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5969 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005970 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005971 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005972 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005973
Joe Perches62ec8182015-02-13 14:38:18 -08005974# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5975 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5976 ERROR("DATE_TIME",
5977 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5978 }
5979
Joe Perches2c924882012-03-23 15:02:20 -07005980# check for use of yield()
5981 if ($line =~ /\byield\s*\(\s*\)/) {
5982 WARN("YIELD",
5983 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5984 }
5985
Joe Perches179f8f42013-07-03 15:05:30 -07005986# check for comparisons against true and false
5987 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5988 my $lead = $1;
5989 my $arg = $2;
5990 my $test = $3;
5991 my $otype = $4;
5992 my $trail = $5;
5993 my $op = "!";
5994
5995 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5996
5997 my $type = lc($otype);
5998 if ($type =~ /^(?:true|false)$/) {
5999 if (("$test" eq "==" && "$type" eq "true") ||
6000 ("$test" eq "!=" && "$type" eq "false")) {
6001 $op = "";
6002 }
6003
6004 CHK("BOOL_COMPARISON",
6005 "Using comparison to $otype is error prone\n" . $herecurr);
6006
6007## maybe suggesting a correct construct would better
6008## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6009
6010 }
6011 }
6012
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006013# check for semaphores initialized locked
6014 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006015 WARN("CONSIDER_COMPLETION",
6016 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006017 }
Joe Perches6712d852012-03-23 15:02:20 -07006018
Joe Perches67d0a072011-10-31 17:13:10 -07006019# recommend kstrto* over simple_strto* and strict_strto*
6020 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006021 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006022 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006023 }
Joe Perches6712d852012-03-23 15:02:20 -07006024
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006025# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006026 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006027 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006028 "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 -07006029 }
Joe Perches6712d852012-03-23 15:02:20 -07006030
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006031# check for various structs that are normally const (ops, kgdb, device_tree)
6032 my $const_structs = qr{
6033 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006034 address_space_operations|
6035 backlight_ops|
6036 block_device_operations|
6037 dentry_operations|
6038 dev_pm_ops|
6039 dma_map_ops|
6040 extent_io_ops|
6041 file_lock_operations|
6042 file_operations|
6043 hv_ops|
6044 ide_dma_ops|
6045 intel_dvo_dev_ops|
6046 item_operations|
6047 iwl_ops|
6048 kgdb_arch|
6049 kgdb_io|
6050 kset_uevent_ops|
6051 lock_manager_operations|
6052 microcode_ops|
6053 mtrr_ops|
6054 neigh_ops|
6055 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006056 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08006057 pci_raw_ops|
6058 pipe_buf_operations|
6059 platform_hibernation_ops|
6060 platform_suspend_ops|
6061 proto_ops|
6062 rpc_pipe_ops|
6063 seq_operations|
6064 snd_ac97_build_ops|
6065 soc_pcmcia_socket_ops|
6066 stacktrace_ops|
6067 sysfs_ops|
6068 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07006069 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006070 usb_mon_operations|
6071 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006072 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006073 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006074 WARN("CONST_STRUCT",
6075 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006076 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006077 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006078
6079# use of NR_CPUS is usually wrong
6080# ignore definitions of NR_CPUS and usage to define arrays as likely right
6081 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006082 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6083 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006084 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6085 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6086 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006087 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006088 WARN("NR_CPUS",
6089 "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 -07006090 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006091
Joe Perches52ea8502013-11-12 15:10:09 -08006092# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6093 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6094 ERROR("DEFINE_ARCH_HAS",
6095 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6096 }
6097
Joe Perchesacd93622015-02-13 14:38:38 -08006098# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6099 if ($^V && $^V ge 5.10.0 &&
6100 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6101 WARN("LIKELY_MISUSE",
6102 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6103 }
6104
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006105# whine mightly about in_atomic
6106 if ($line =~ /\bin_atomic\s*\(/) {
6107 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006108 ERROR("IN_ATOMIC",
6109 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006110 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006111 WARN("IN_ATOMIC",
6112 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006113 }
6114 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006115
Joe Perches481aea52016-05-20 17:04:08 -07006116# whine about ACCESS_ONCE
6117 if ($^V && $^V ge 5.10.0 &&
6118 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6119 my $par = $1;
6120 my $eq = $2;
6121 my $fun = $3;
6122 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6123 if (defined($eq)) {
6124 if (WARN("PREFER_WRITE_ONCE",
6125 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6126 $fix) {
6127 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6128 }
6129 } else {
6130 if (WARN("PREFER_READ_ONCE",
6131 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6132 $fix) {
6133 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6134 }
6135 }
6136 }
6137
Peter Zijlstra1704f472010-03-19 01:37:42 +01006138# check for lockdep_set_novalidate_class
6139 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6140 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6141 if ($realfile !~ m@^kernel/lockdep@ &&
6142 $realfile !~ m@^include/linux/lockdep@ &&
6143 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006144 ERROR("LOCKDEP",
6145 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006146 }
6147 }
Dave Jones88f88312011-01-12 16:59:59 -08006148
Joe Perchesb392c642015-04-16 12:44:16 -07006149 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6150 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006151 WARN("EXPORTED_WORLD_WRITABLE",
6152 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006153 }
Joe Perches24358802014-04-03 14:49:13 -07006154
Joe Perches515a2352014-04-03 14:49:24 -07006155# Mode permission misuses where it seems decimal should be octal
6156# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6157 if ($^V && $^V ge 5.10.0 &&
6158 $line =~ /$mode_perms_search/) {
6159 foreach my $entry (@mode_permission_funcs) {
6160 my $func = $entry->[0];
6161 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006162
Joe Perches515a2352014-04-03 14:49:24 -07006163 my $skip_args = "";
6164 if ($arg_pos > 1) {
6165 $arg_pos--;
6166 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6167 }
6168 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
6169 if ($line =~ /$test/) {
6170 my $val = $1;
6171 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07006172
Joe Perches515a2352014-04-03 14:49:24 -07006173 if ($val !~ /^0$/ &&
6174 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6175 length($val) ne 4)) {
6176 ERROR("NON_OCTAL_PERMISSIONS",
6177 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08006178 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6179 ERROR("EXPORTED_WORLD_WRITABLE",
6180 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07006181 }
Joe Perches24358802014-04-03 14:49:13 -07006182 }
6183 }
6184 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006185
6186# validate content of MODULE_LICENSE against list from include/linux/module.h
6187 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6188 my $extracted_string = get_quoted_string($line, $rawline);
6189 my $valid_licenses = qr{
6190 GPL|
6191 GPL\ v2|
6192 GPL\ and\ additional\ rights|
6193 Dual\ BSD/GPL|
6194 Dual\ MIT/GPL|
6195 Dual\ MPL/GPL|
6196 Proprietary
6197 }x;
6198 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6199 WARN("MODULE_LICENSE",
6200 "unknown module license " . $extracted_string . "\n" . $herecurr);
6201 }
6202 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006203 }
6204
6205 # If we have no input at all, then there is nothing to report on
6206 # so just keep quiet.
6207 if ($#rawlines == -1) {
6208 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006209 }
6210
Andy Whitcroft8905a672007-11-28 16:21:06 -08006211 # In mailback mode only produce a report in the negative, for
6212 # things that appear to be patches.
6213 if ($mailback && ($clean == 1 || !$is_patch)) {
6214 exit(0);
6215 }
6216
6217 # This is not a patch, and we are are in 'no-patch' mode so
6218 # just keep quiet.
6219 if (!$chk_patch && !$is_patch) {
6220 exit(0);
6221 }
6222
Joe Perches06330fc2015-06-25 15:03:11 -07006223 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006224 ERROR("NOT_UNIFIED_DIFF",
6225 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006226 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006227 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006228 ERROR("MISSING_SIGN_OFF",
6229 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006230 }
6231
Andy Whitcroft8905a672007-11-28 16:21:06 -08006232 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006233 if ($summary && !($clean == 1 && $quiet == 1)) {
6234 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006235 print "total: $cnt_error errors, $cnt_warn warnings, " .
6236 (($check)? "$cnt_chk checks, " : "") .
6237 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006238 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006239
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006240 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006241 # If there were any defects found and not already fixing them
6242 if (!$clean and !$fix) {
6243 print << "EOM"
6244
6245NOTE: For some of the reported defects, checkpatch may be able to
6246 mechanically convert to the typical style using --fix or --fix-inplace.
6247EOM
6248 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006249 # If there were whitespace errors which cleanpatch can fix
6250 # then suggest that.
6251 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006252 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006253 print << "EOM"
6254
6255NOTE: Whitespace errors detected.
6256 You may wish to use scripts/cleanpatch or scripts/cleanfile
6257EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006258 }
6259 }
6260
Joe Perchesd752fcc2014-08-06 16:11:05 -07006261 if ($clean == 0 && $fix &&
6262 ("@rawlines" ne "@fixed" ||
6263 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006264 my $newfile = $filename;
6265 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006266 my $linecount = 0;
6267 my $f;
6268
Joe Perchesd752fcc2014-08-06 16:11:05 -07006269 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6270
Joe Perches3705ce52013-07-03 15:05:31 -07006271 open($f, '>', $newfile)
6272 or die "$P: Can't open $newfile for write\n";
6273 foreach my $fixed_line (@fixed) {
6274 $linecount++;
6275 if ($file) {
6276 if ($linecount > 3) {
6277 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006278 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006279 }
6280 } else {
6281 print $f $fixed_line . "\n";
6282 }
6283 }
6284 close($f);
6285
6286 if (!$quiet) {
6287 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006288
Joe Perches3705ce52013-07-03 15:05:31 -07006289Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6290
6291Do _NOT_ trust the results written to this file.
6292Do _NOT_ submit these changes without inspecting them for correctness.
6293
6294This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6295No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006296EOM
6297 }
6298 }
6299
Joe Perchesd8469f12015-06-25 15:03:00 -07006300 if ($quiet == 0) {
6301 print "\n";
6302 if ($clean == 1) {
6303 print "$vname has no obvious style problems and is ready for submission.\n";
6304 } else {
6305 print "$vname has style problems, please review.\n";
6306 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006307 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006308 return $clean;
6309}