blob: 653c7720d6021d960a0dd822a792349332b5a70e [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;
David Keitele288d232016-03-23 20:57:21 -070017use constant CHECK_NEXT_SHORTTEXT => 3;
David Keitel72bf3e82016-03-23 20:52:44 -070018use constant SHORTTEXT_LIMIT => 75;
19
Andy Whitcroft0a920b52007-06-01 00:46:48 -070020my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080021my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070022
Joe Perches000d1cc12011-07-25 17:13:25 -070023my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070024
25use Getopt::Long qw(:config no_auto_abbrev);
26
27my $quiet = 0;
28my $tree = 1;
29my $chk_signoff = 1;
30my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080031my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070032my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080034my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070035my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070037my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070038my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070039my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070040my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080041my $summary = 1;
42my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080043my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070045my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070046my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080047my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070048my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080049my %debug;
Joe Perches34456862013-07-03 15:05:34 -070050my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070051my %use_type = ();
52my @use = ();
53my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070054my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070055my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070056my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080057my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070058my $ignore_perl_version = 0;
59my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070060my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070061my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070062my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070063my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perches57230292015-06-25 15:03:03 -070064my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070065my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070066
67sub help {
68 my ($exitcode) = @_;
69
70 print << "EOM";
71Usage: $P [OPTION]... [FILE]...
72Version: $V
73
74Options:
75 -q, --quiet quiet
76 --no-tree run without a kernel tree
77 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080078 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070079 --patch treat FILE as patchfile (default)
80 --emacs emacs compile window format
81 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070082 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070083 -g, --git treat FILE as a single commit or git revision range
84 single git commit with:
85 <rev>
86 <rev>^
87 <rev>~n
88 multiple git commits with:
89 <rev1>..<rev2>
90 <rev1>...<rev2>
91 <rev>-<count>
92 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070093 -f, --file treat FILE as regular source file
94 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070096 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070097 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070098 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080099 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700100 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700101 --root=PATH PATH to the kernel tree root
102 --no-summary suppress the per-file summary
103 --mailback only produce a report in case of warnings/errors
104 --summary-file include the filename in summary
105 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
106 'values', 'possible', 'type', and 'attr' (default
107 is all off)
108 --test-only=WORD report only warnings/errors containing WORD
109 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700110 --fix EXPERIMENTAL - may create horrible results
111 If correctable single-line errors exist, create
112 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
113 with potential errors corrected to the preferred
114 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800115 --fix-inplace EXPERIMENTAL - may create horrible results
116 Is the same as --fix, but overwrites the input
117 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700118 --ignore-perl-version override checking of perl version. expect
119 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700121 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700123 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
151 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
152 push (@types, $_);
153 }
154 @types = sort(uniq(@types));
155 print("#\tMessage type\n\n");
156 foreach my $type (@types) {
157 print(++$count . "\t" . $type . "\n");
158 }
159
160 exit($exitcode);
161}
162
Joe Perches000d1cc12011-07-25 17:13:25 -0700163my $conf = which_conf($configuration_file);
164if (-f $conf) {
165 my @conf_args;
166 open(my $conffile, '<', "$conf")
167 or warn "$P: Can't find a readable $configuration_file file $!\n";
168
169 while (<$conffile>) {
170 my $line = $_;
171
172 $line =~ s/\s*\n?$//g;
173 $line =~ s/^\s*//g;
174 $line =~ s/\s+/ /g;
175
176 next if ($line =~ m/^\s*#/);
177 next if ($line =~ m/^\s*$/);
178
179 my @words = split(" ", $line);
180 foreach my $word (@words) {
181 last if ($word =~ m/^#/);
182 push (@conf_args, $word);
183 }
184 }
185 close($conffile);
186 unshift(@ARGV, @conf_args) if @conf_args;
187}
188
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700189GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700190 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700191 'tree!' => \$tree,
192 'signoff!' => \$chk_signoff,
193 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800194 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800196 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700197 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700198 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700199 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700200 'subjective!' => \$check,
201 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700202 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700203 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700204 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700205 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800206 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700207 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800209 'summary!' => \$summary,
210 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800211 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700212 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800213 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700214 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700216 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700217 'codespell!' => \$codespell,
218 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700219 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700220 'h|help' => \$help,
221 'version' => \$help
222) or help(1);
223
224help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700225
Joe Perches3beb42e2016-05-20 17:04:14 -0700226list_types(0) if ($list_types);
227
Joe Perches9624b8d2014-01-23 15:54:44 -0800228$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700229$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800230
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700231my $exit = 0;
232
Dave Hansend62a2012013-09-11 14:23:56 -0700233if ($^V && $^V lt $minimum_perl_version) {
234 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
235 if (!$ignore_perl_version) {
236 exit(1);
237 }
238}
239
Allen Hubbe45107ff2016-08-02 14:04:47 -0700240#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700241if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700242 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243}
244
Joe Perches91bfe482013-09-11 14:23:59 -0700245sub hash_save_array_words {
246 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700247
Joe Perches91bfe482013-09-11 14:23:59 -0700248 my @array = split(/,/, join(',', @$arrayRef));
249 foreach my $word (@array) {
250 $word =~ s/\s*\n?$//g;
251 $word =~ s/^\s*//g;
252 $word =~ s/\s+/ /g;
253 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700254
Joe Perches91bfe482013-09-11 14:23:59 -0700255 next if ($word =~ m/^\s*#/);
256 next if ($word =~ m/^\s*$/);
257
258 $hashRef->{$word}++;
259 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700260}
261
Joe Perches91bfe482013-09-11 14:23:59 -0700262sub hash_show_words {
263 my ($hashRef, $prefix) = @_;
264
Joe Perches3c816e42015-06-25 15:03:29 -0700265 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700266 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700267 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700268 print " $word";
269 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700270 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700271 }
272}
273
274hash_save_array_words(\%ignore_type, \@ignore);
275hash_save_array_words(\%use_type, \@use);
276
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800277my $dbg_values = 0;
278my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700279my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700280my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800282 ## no critic
283 eval "\${dbg_$key} = '$debug{$key}';";
284 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800285}
286
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700287my $rpt_cleaners = 0;
288
Andy Whitcroft8905a672007-11-28 16:21:06 -0800289if ($terse) {
290 $emacs = 1;
291 $quiet++;
292}
293
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294if ($tree) {
295 if (defined $root) {
296 if (!top_of_kernel_tree($root)) {
297 die "$P: $root: --root does not point at a valid tree\n";
298 }
299 } else {
300 if (top_of_kernel_tree('.')) {
301 $root = '.';
302 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
303 top_of_kernel_tree($1)) {
304 $root = $1;
305 }
306 }
307
308 if (!defined $root) {
309 print "Must be run from the top-level dir. of a kernel tree\n";
310 exit(2);
311 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700312}
313
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314my $emitted_corrupt = 0;
315
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700316our $Ident = qr{
317 [A-Za-z_][A-Za-z\d_]*
318 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
319 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700320our $Storage = qr{extern|static|asmlinkage};
321our $Sparse = qr{
322 __user|
323 __kernel|
324 __force|
325 __iomem|
326 __must_check|
327 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800328 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700329 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800330 __rcu|
331 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700332 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800333our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
334our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
335our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
336our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
337our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700338
Wolfram Sang52131292010-03-05 13:43:51 -0800339# Notes to $Attribute:
340# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700341our $Attribute = qr{
342 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700343 __percpu|
344 __nocast|
345 __safe|
346 __bitwise__|
347 __packed__|
348 __packed2__|
349 __naked|
350 __maybe_unused|
351 __always_unused|
352 __noreturn|
353 __used|
354 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800355 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700356 __noclone|
357 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700358 __read_mostly|
359 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700360 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700361 ____cacheline_aligned|
362 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800363 ____cacheline_internodealigned_in_smp|
364 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700366our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700367our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700368our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
369our $Lval = qr{$Ident(?:$Member)*};
370
Joe Perches95e2c602013-07-03 15:05:20 -0700371our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
372our $Binary = qr{(?i)0b[01]+$Int_type?};
373our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
374our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700375our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800376our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800377our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
378our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
379our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800380our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700381our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800382our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700383our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700384our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700385our $Operators = qr{
386 <=|>=|==|!=|
387 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700388 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 }x;
390
Joe Perches91cb5192014-04-03 14:49:32 -0700391our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
392
Joe Perchesab7e23f2015-04-16 12:44:22 -0700393our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800394our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700395our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700396our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700398our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700400our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401
Joe Perches15662b32011-10-31 17:13:12 -0700402our $NON_ASCII_UTF8 = qr{
403 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700404 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
405 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
406 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
407 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
408 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
409 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
410}x;
411
Joe Perches15662b32011-10-31 17:13:12 -0700412our $UTF8 = qr{
413 [\x09\x0A\x0D\x20-\x7E] # ASCII
414 | $NON_ASCII_UTF8
415}x;
416
Joe Perchese6176fa2015-06-25 15:02:49 -0700417our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800418our $typeOtherOSTypedefs = qr{(?x:
419 u_(?:char|short|int|long) | # bsd
420 u(?:nchar|short|int|long) # sysv
421)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700422our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700423 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700424 atomic_t
425)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700426our $typeTypedefs = qr{(?x:
427 $typeC99Typedefs\b|
428 $typeOtherOSTypedefs\b|
429 $typeKernelTypedefs\b
430)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700431
Joe Perches6d32f7a2015-11-06 16:31:37 -0800432our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
433
Joe Perches691e6692010-03-05 13:43:51 -0800434our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700435 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700436 (?:[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 -0700437 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700438 panic|
Joe Perches06668722013-11-12 15:10:07 -0800439 MODULE_[A-Z_]+|
440 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800441)};
442
Joe Perches20112472011-07-25 17:13:23 -0700443our $signature_tags = qr{(?xi:
444 Signed-off-by:|
445 Acked-by:|
446 Tested-by:|
447 Reviewed-by:|
448 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700449 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700450 To:|
451 Cc:
452)};
453
Joe Perches18130872014-08-06 16:11:22 -0700454our @typeListMisordered = (
455 qr{char\s+(?:un)?signed},
456 qr{int\s+(?:(?:un)?signed\s+)?short\s},
457 qr{int\s+short(?:\s+(?:un)?signed)},
458 qr{short\s+int(?:\s+(?:un)?signed)},
459 qr{(?:un)?signed\s+int\s+short},
460 qr{short\s+(?:un)?signed},
461 qr{long\s+int\s+(?:un)?signed},
462 qr{int\s+long\s+(?:un)?signed},
463 qr{long\s+(?:un)?signed\s+int},
464 qr{int\s+(?:un)?signed\s+long},
465 qr{int\s+(?:un)?signed},
466 qr{int\s+long\s+long\s+(?:un)?signed},
467 qr{long\s+long\s+int\s+(?:un)?signed},
468 qr{long\s+long\s+(?:un)?signed\s+int},
469 qr{long\s+long\s+(?:un)?signed},
470 qr{long\s+(?:un)?signed},
471);
472
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473our @typeList = (
474 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700475 qr{(?:(?:un)?signed\s+)?char},
476 qr{(?:(?:un)?signed\s+)?short\s+int},
477 qr{(?:(?:un)?signed\s+)?short},
478 qr{(?:(?:un)?signed\s+)?int},
479 qr{(?:(?:un)?signed\s+)?long\s+int},
480 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
481 qr{(?:(?:un)?signed\s+)?long\s+long},
482 qr{(?:(?:un)?signed\s+)?long},
483 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800484 qr{float},
485 qr{double},
486 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487 qr{struct\s+$Ident},
488 qr{union\s+$Ident},
489 qr{enum\s+$Ident},
490 qr{${Ident}_t},
491 qr{${Ident}_handler},
492 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700493 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800494);
Joe Perches938224b2016-01-20 14:59:15 -0800495
496our $C90_int_types = qr{(?x:
497 long\s+long\s+int\s+(?:un)?signed|
498 long\s+long\s+(?:un)?signed\s+int|
499 long\s+long\s+(?:un)?signed|
500 (?:(?:un)?signed\s+)?long\s+long\s+int|
501 (?:(?:un)?signed\s+)?long\s+long|
502 int\s+long\s+long\s+(?:un)?signed|
503 int\s+(?:(?:un)?signed\s+)?long\s+long|
504
505 long\s+int\s+(?:un)?signed|
506 long\s+(?:un)?signed\s+int|
507 long\s+(?:un)?signed|
508 (?:(?:un)?signed\s+)?long\s+int|
509 (?:(?:un)?signed\s+)?long|
510 int\s+long\s+(?:un)?signed|
511 int\s+(?:(?:un)?signed\s+)?long|
512
513 int\s+(?:un)?signed|
514 (?:(?:un)?signed\s+)?int
515)};
516
Alex Dowad485ff232015-06-25 15:02:52 -0700517our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700518our @typeListWithAttr = (
519 @typeList,
520 qr{struct\s+$InitAttribute\s+$Ident},
521 qr{union\s+$InitAttribute\s+$Ident},
522);
523
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700524our @modifierList = (
525 qr{fastcall},
526);
Alex Dowad485ff232015-06-25 15:02:52 -0700527our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528
Joe Perches24358802014-04-03 14:49:13 -0700529our @mode_permission_funcs = (
530 ["module_param", 3],
531 ["module_param_(?:array|named|string)", 4],
532 ["module_param_array_named", 5],
533 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
534 ["proc_create(?:_data|)", 2],
535 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
536);
537
Joe Perches515a2352014-04-03 14:49:24 -0700538#Create a search pattern for all these functions to speed up a loop below
539our $mode_perms_search = "";
540foreach my $entry (@mode_permission_funcs) {
541 $mode_perms_search .= '|' if ($mode_perms_search ne "");
542 $mode_perms_search .= $entry->[0];
543}
544
Joe Perchesb392c642015-04-16 12:44:16 -0700545our $mode_perms_world_writable = qr{
546 S_IWUGO |
547 S_IWOTH |
548 S_IRWXUGO |
549 S_IALLUGO |
550 0[0-7][0-7][2367]
551}x;
552
Wolfram Sang7840a942010-08-09 17:20:57 -0700553our $allowed_asm_includes = qr{(?x:
554 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700555 memory|
556 time|
557 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700558)};
559# memory.h: ARM has a custom one
560
Kees Cook66b47b42014-10-13 15:51:57 -0700561# Load common spelling mistakes and build regular expression list.
562my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700563my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700564
Joe Perches36061e32014-12-10 15:51:43 -0800565if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800566 while (<$spelling>) {
567 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700568
Joe Perches36061e32014-12-10 15:51:43 -0800569 $line =~ s/\s*\n?$//g;
570 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700571
Joe Perches36061e32014-12-10 15:51:43 -0800572 next if ($line =~ m/^\s*#/);
573 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700574
Joe Perches36061e32014-12-10 15:51:43 -0800575 my ($suspect, $fix) = split(/\|\|/, $line);
576
Joe Perches36061e32014-12-10 15:51:43 -0800577 $spelling_fix{$suspect} = $fix;
578 }
579 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800580} else {
581 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700582}
Kees Cook66b47b42014-10-13 15:51:57 -0700583
Joe Perchesebfd7d62015-04-16 12:44:14 -0700584if ($codespell) {
585 if (open(my $spelling, '<', $codespellfile)) {
586 while (<$spelling>) {
587 my $line = $_;
588
589 $line =~ s/\s*\n?$//g;
590 $line =~ s/^\s*//g;
591
592 next if ($line =~ m/^\s*#/);
593 next if ($line =~ m/^\s*$/);
594 next if ($line =~ m/, disabled/i);
595
596 $line =~ s/,.*$//;
597
598 my ($suspect, $fix) = split(/->/, $line);
599
600 $spelling_fix{$suspect} = $fix;
601 }
602 close($spelling);
603 } else {
604 warn "No codespell typos will be found - file '$codespellfile': $!\n";
605 }
606}
607
608$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
609
Andy Whitcroft8905a672007-11-28 16:21:06 -0800610sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700611 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
612 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700613 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700614 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700615 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700616 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700617 (?:$typeTypedefs\b)|
618 (?:${all}\b)
619 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800620 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700621 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800622 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800623 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700624 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700625 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800626 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700627 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800628 }x;
Joe Perches18130872014-08-06 16:11:22 -0700629 $NonptrTypeMisordered = qr{
630 (?:$Modifier\s+|const\s+)*
631 (?:
632 (?:${Misordered}\b)
633 )
634 (?:\s+$Modifier|\s+const)*
635 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700636 $NonptrTypeWithAttr = qr{
637 (?:$Modifier\s+|const\s+)*
638 (?:
639 (?:typeof|__typeof__)\s*\([^\)]*\)|
640 (?:$typeTypedefs\b)|
641 (?:${allWithAttr}\b)
642 )
643 (?:\s+$Modifier|\s+const)*
644 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800645 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700646 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700647 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700648 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800649 }x;
Joe Perches18130872014-08-06 16:11:22 -0700650 $TypeMisordered = qr{
651 $NonptrTypeMisordered
652 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
653 (?:\s+$Inline|\s+$Modifier)*
654 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700655 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700656 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800657}
658build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700659
Joe Perches7d2367a2011-07-25 17:13:22 -0700660our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700661
662# Using $balanced_parens, $LvalOrFunc, or $FuncArg
663# requires at least perl version v5.10.0
664# Any use must be runtime checked with $^V
665
666our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700667our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800668our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700669
Joe Perchesf8422302014-08-06 16:11:31 -0700670our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700671 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700672 (?:$Storage\s+)?LIST_HEAD\s*\(|
673 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
674)};
675
Joe Perches7d2367a2011-07-25 17:13:22 -0700676sub deparenthesize {
677 my ($string) = @_;
678 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700679
680 while ($string =~ /^\s*\(.*\)\s*$/) {
681 $string =~ s@^\s*\(\s*@@;
682 $string =~ s@\s*\)\s*$@@;
683 }
684
Joe Perches7d2367a2011-07-25 17:13:22 -0700685 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700686
Joe Perches7d2367a2011-07-25 17:13:22 -0700687 return $string;
688}
689
Joe Perches34456862013-07-03 15:05:34 -0700690sub seed_camelcase_file {
691 my ($file) = @_;
692
693 return if (!(-f $file));
694
695 local $/;
696
697 open(my $include_file, '<', "$file")
698 or warn "$P: Can't read '$file' $!\n";
699 my $text = <$include_file>;
700 close($include_file);
701
702 my @lines = split('\n', $text);
703
704 foreach my $line (@lines) {
705 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
706 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
707 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800708 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
709 $camelcase{$1} = 1;
710 } 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 -0700711 $camelcase{$1} = 1;
712 }
713 }
714}
715
716my $camelcase_seeded = 0;
717sub seed_camelcase_includes {
718 return if ($camelcase_seeded);
719
720 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700721 my $camelcase_cache = "";
722 my @include_files = ();
723
724 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700725
Richard Genoud3645e322014-02-10 14:25:32 -0800726 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700727 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
728 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700729 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700730 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700731 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700732 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700733 @include_files = split('\n', $files);
734 foreach my $file (@include_files) {
735 my $date = POSIX::strftime("%Y%m%d%H%M",
736 localtime((stat $file)[9]));
737 $last_mod_date = $date if ($last_mod_date < $date);
738 }
739 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700740 }
Joe Perchesc707a812013-07-08 16:00:43 -0700741
742 if ($camelcase_cache ne "" && -f $camelcase_cache) {
743 open(my $camelcase_file, '<', "$camelcase_cache")
744 or warn "$P: Can't read '$camelcase_cache' $!\n";
745 while (<$camelcase_file>) {
746 chomp;
747 $camelcase{$_} = 1;
748 }
749 close($camelcase_file);
750
751 return;
752 }
753
Richard Genoud3645e322014-02-10 14:25:32 -0800754 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700755 $files = `git ls-files "include/*.h"`;
756 @include_files = split('\n', $files);
757 }
758
Joe Perches34456862013-07-03 15:05:34 -0700759 foreach my $file (@include_files) {
760 seed_camelcase_file($file);
761 }
Joe Perches351b2a12013-07-03 15:05:36 -0700762
Joe Perchesc707a812013-07-08 16:00:43 -0700763 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700764 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700765 open(my $camelcase_file, '>', "$camelcase_cache")
766 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700767 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
768 print $camelcase_file ("$_\n");
769 }
770 close($camelcase_file);
771 }
Joe Perches34456862013-07-03 15:05:34 -0700772}
773
Joe Perchesd311cd42014-08-06 16:10:57 -0700774sub git_commit_info {
775 my ($commit, $id, $desc) = @_;
776
777 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
778
779 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
780 $output =~ s/^\s*//gm;
781 my @lines = split("\n", $output);
782
Joe Perches0d7835f2015-02-13 14:38:35 -0800783 return ($id, $desc) if ($#lines < 0);
784
Joe Perchesd311cd42014-08-06 16:10:57 -0700785 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
786# Maybe one day convert this block of bash into something that returns
787# all matching commit ids, but it's very slow...
788#
789# echo "checking commits $1..."
790# git rev-list --remotes | grep -i "^$1" |
791# while read line ; do
792# git log --format='%H %s' -1 $line |
793# echo "commit $(cut -c 1-12,41-)"
794# done
795 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
796 } else {
797 $id = substr($lines[0], 0, 12);
798 $desc = substr($lines[0], 41);
799 }
800
801 return ($id, $desc);
802}
803
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700804$chk_signoff = 0 if ($file);
805
Andy Whitcroft00df3442007-06-08 13:47:06 -0700806my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800807my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700808my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700809my @fixed_inserted = ();
810my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700811my $fixlinenr = -1;
812
Du, Changbin4a593c32016-05-20 17:04:16 -0700813# If input is git commits, extract all commits from the commit expressions.
814# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
815die "$P: No git repository found\n" if ($git && !-e ".git");
816
817if ($git) {
818 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700819 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700820 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700821 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
822 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700823 } elsif ($commit_expr =~ m/\.\./) {
824 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700825 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700826 $git_range = "-1 $commit_expr";
827 }
828 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
829 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700830 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
831 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700832 my $sha1 = $1;
833 my $subject = $2;
834 unshift(@commits, $sha1);
835 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700836 }
837 }
838 die "$P: no git commits after extraction!\n" if (@commits == 0);
839 @ARGV = @commits;
840}
841
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800842my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700843for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800844 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700845 if ($git) {
846 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
847 die "$P: $filename: git format-patch failed - $!\n";
848 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800849 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700850 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800851 } elsif ($filename eq '-') {
852 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800854 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700855 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700856 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800857 if ($filename eq '-') {
858 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700859 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700860 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800861 } else {
862 $vname = $filename;
863 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800864 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700865 chomp;
866 push(@rawlines, $_);
867 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800868 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700869
870 if ($#ARGV > 0 && $quiet == 0) {
871 print '-' x length($vname) . "\n";
872 print "$vname\n";
873 print '-' x length($vname) . "\n";
874 }
875
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800876 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700877 $exit = 1;
878 }
879 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800880 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700881 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700882 @fixed_inserted = ();
883 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700884 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700885 @modifierListFile = ();
886 @typeListFile = ();
887 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700888}
889
Joe Perchesd8469f12015-06-25 15:03:00 -0700890if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700891 hash_show_words(\%use_type, "Used");
892 hash_show_words(\%ignore_type, "Ignored");
893
Joe Perchesd8469f12015-06-25 15:03:00 -0700894 if ($^V lt 5.10.0) {
895 print << "EOM"
896
897NOTE: perl $^V is not modern enough to detect all possible issues.
898 An upgrade to at least perl v5.10.0 is suggested.
899EOM
900 }
901 if ($exit) {
902 print << "EOM"
903
904NOTE: If any of the errors are false positives, please report
905 them to the maintainer, see CHECKPATCH in MAINTAINERS.
906EOM
907 }
908}
909
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700910exit($exit);
911
912sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700913 my ($root) = @_;
914
915 my @tree_check = (
916 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
917 "README", "Documentation", "arch", "include", "drivers",
918 "fs", "init", "ipc", "kernel", "lib", "scripts",
919 );
920
921 foreach my $check (@tree_check) {
922 if (! -e $root . '/' . $check) {
923 return 0;
924 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700925 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700926 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700927}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700928
Joe Perches20112472011-07-25 17:13:23 -0700929sub parse_email {
930 my ($formatted_email) = @_;
931
932 my $name = "";
933 my $address = "";
934 my $comment = "";
935
936 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
937 $name = $1;
938 $address = $2;
939 $comment = $3 if defined $3;
940 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
941 $address = $1;
942 $comment = $2 if defined $2;
943 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
944 $address = $1;
945 $comment = $2 if defined $2;
946 $formatted_email =~ s/$address.*$//;
947 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700948 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700949 $name =~ s/^\"|\"$//g;
950 # If there's a name left after stripping spaces and
951 # leading quotes, and the address doesn't have both
952 # leading and trailing angle brackets, the address
953 # is invalid. ie:
954 # "joe smith joe@smith.com" bad
955 # "joe smith <joe@smith.com" bad
956 if ($name ne "" && $address !~ /^<[^>]+>$/) {
957 $name = "";
958 $address = "";
959 $comment = "";
960 }
961 }
962
Joe Perches3705ce52013-07-03 15:05:31 -0700963 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700964 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700965 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700966 $address =~ s/^\<|\>$//g;
967
968 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
969 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
970 $name = "\"$name\"";
971 }
972
973 return ($name, $address, $comment);
974}
975
976sub format_email {
977 my ($name, $address) = @_;
978
979 my $formatted_email;
980
Joe Perches3705ce52013-07-03 15:05:31 -0700981 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700982 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700983 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700984
985 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
986 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
987 $name = "\"$name\"";
988 }
989
990 if ("$name" eq "") {
991 $formatted_email = "$address";
992 } else {
993 $formatted_email = "$name <$address>";
994 }
995
996 return $formatted_email;
997}
998
Joe Perchesd311cd42014-08-06 16:10:57 -0700999sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001000 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001001
Joe Perchesbd474ca2014-08-06 16:11:10 -07001002 foreach my $path (split(/:/, $ENV{PATH})) {
1003 if (-e "$path/$bin") {
1004 return "$path/$bin";
1005 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001006 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001007
Joe Perchesbd474ca2014-08-06 16:11:10 -07001008 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001009}
1010
Joe Perches000d1cc12011-07-25 17:13:25 -07001011sub which_conf {
1012 my ($conf) = @_;
1013
1014 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1015 if (-e "$path/$conf") {
1016 return "$path/$conf";
1017 }
1018 }
1019
1020 return "";
1021}
1022
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001023sub expand_tabs {
1024 my ($str) = @_;
1025
1026 my $res = '';
1027 my $n = 0;
1028 for my $c (split(//, $str)) {
1029 if ($c eq "\t") {
1030 $res .= ' ';
1031 $n++;
1032 for (; ($n % 8) != 0; $n++) {
1033 $res .= ' ';
1034 }
1035 next;
1036 }
1037 $res .= $c;
1038 $n++;
1039 }
1040
1041 return $res;
1042}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001044 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001045 return $res;
1046}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001047
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001048sub line_stats {
1049 my ($line) = @_;
1050
1051 # Drop the diff line leader and expand tabs
1052 $line =~ s/^.//;
1053 $line = expand_tabs($line);
1054
1055 # Pick the indent from the front of the line.
1056 my ($white) = ($line =~ /^(\s*)/);
1057
1058 return (length($line), length($white));
1059}
1060
Andy Whitcroft773647a2008-03-28 14:15:58 -07001061my $sanitise_quote = '';
1062
1063sub sanitise_line_reset {
1064 my ($in_comment) = @_;
1065
1066 if ($in_comment) {
1067 $sanitise_quote = '*/';
1068 } else {
1069 $sanitise_quote = '';
1070 }
1071}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001072sub sanitise_line {
1073 my ($line) = @_;
1074
1075 my $res = '';
1076 my $l = '';
1077
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001078 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001079 my $off = 0;
1080 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001081
Andy Whitcroft773647a2008-03-28 14:15:58 -07001082 # Always copy over the diff marker.
1083 $res = substr($line, 0, 1);
1084
1085 for ($off = 1; $off < length($line); $off++) {
1086 $c = substr($line, $off, 1);
1087
1088 # Comments we are wacking completly including the begin
1089 # and end, all to $;.
1090 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1091 $sanitise_quote = '*/';
1092
1093 substr($res, $off, 2, "$;$;");
1094 $off++;
1095 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001096 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001097 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001098 $sanitise_quote = '';
1099 substr($res, $off, 2, "$;$;");
1100 $off++;
1101 next;
1102 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001103 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1104 $sanitise_quote = '//';
1105
1106 substr($res, $off, 2, $sanitise_quote);
1107 $off++;
1108 next;
1109 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001110
1111 # A \ in a string means ignore the next character.
1112 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1113 $c eq "\\") {
1114 substr($res, $off, 2, 'XX');
1115 $off++;
1116 next;
1117 }
1118 # Regular quotes.
1119 if ($c eq "'" || $c eq '"') {
1120 if ($sanitise_quote eq '') {
1121 $sanitise_quote = $c;
1122
1123 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001124 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001125 } elsif ($sanitise_quote eq $c) {
1126 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001127 }
1128 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001129
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001130 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001131 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1132 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001133 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1134 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001135 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1136 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001137 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001138 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001139 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 }
1141
Daniel Walker113f04a2009-09-21 17:04:35 -07001142 if ($sanitise_quote eq '//') {
1143 $sanitise_quote = '';
1144 }
1145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001147 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001148 my $clean = 'X' x length($1);
1149 $res =~ s@\<.*\>@<$clean>@;
1150
1151 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001152 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001154 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155 }
1156
Joe Perchesdadf6802016-08-02 14:04:33 -07001157 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1158 my $match = $1;
1159 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1160 }
1161
Andy Whitcroft00df3442007-06-08 13:47:06 -07001162 return $res;
1163}
1164
Joe Perchesa6962d72013-04-29 16:18:13 -07001165sub get_quoted_string {
1166 my ($line, $rawline) = @_;
1167
Joe Perches33acb542015-06-25 15:02:54 -07001168 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001169 return substr($rawline, $-[0], $+[0] - $-[0]);
1170}
1171
Andy Whitcroft8905a672007-11-28 16:21:06 -08001172sub ctx_statement_block {
1173 my ($linenr, $remain, $off) = @_;
1174 my $line = $linenr - 1;
1175 my $blk = '';
1176 my $soff = $off;
1177 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001178 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001179
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001180 my $loff = 0;
1181
Andy Whitcroft8905a672007-11-28 16:21:06 -08001182 my $type = '';
1183 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001184 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001185 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001186 my $c;
1187 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001188
1189 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001190 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001191 @stack = (['', 0]) if ($#stack == -1);
1192
Andy Whitcroft773647a2008-03-28 14:15:58 -07001193 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001194 # If we are about to drop off the end, pull in more
1195 # context.
1196 if ($off >= $len) {
1197 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001198 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001200 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001201 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001202 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001203 $len = length($blk);
1204 $line++;
1205 last;
1206 }
1207 # Bail if there is no further context.
1208 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001209 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001210 last;
1211 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001212 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1213 $level++;
1214 $type = '#';
1215 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001216 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001217 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001218 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001219 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001220
Andy Whitcroft773647a2008-03-28 14:15:58 -07001221 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001222
1223 # Handle nested #if/#else.
1224 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1225 push(@stack, [ $type, $level ]);
1226 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1227 ($type, $level) = @{$stack[$#stack - 1]};
1228 } elsif ($remainder =~ /^#\s*endif\b/) {
1229 ($type, $level) = @{pop(@stack)};
1230 }
1231
Andy Whitcroft8905a672007-11-28 16:21:06 -08001232 # Statement ends at the ';' or a close '}' at the
1233 # outermost level.
1234 if ($level == 0 && $c eq ';') {
1235 last;
1236 }
1237
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001238 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 if ($level == 0 && $coff_set == 0 &&
1240 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1241 $remainder =~ /^(else)(?:\s|{)/ &&
1242 $remainder !~ /^else\s+if\b/) {
1243 $coff = $off + length($1) - 1;
1244 $coff_set = 1;
1245 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1246 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001247 }
1248
Andy Whitcroft8905a672007-11-28 16:21:06 -08001249 if (($type eq '' || $type eq '(') && $c eq '(') {
1250 $level++;
1251 $type = '(';
1252 }
1253 if ($type eq '(' && $c eq ')') {
1254 $level--;
1255 $type = ($level != 0)? '(' : '';
1256
1257 if ($level == 0 && $coff < $soff) {
1258 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001259 $coff_set = 1;
1260 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 }
1262 }
1263 if (($type eq '' || $type eq '{') && $c eq '{') {
1264 $level++;
1265 $type = '{';
1266 }
1267 if ($type eq '{' && $c eq '}') {
1268 $level--;
1269 $type = ($level != 0)? '{' : '';
1270
1271 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001272 if (substr($blk, $off + 1, 1) eq ';') {
1273 $off++;
1274 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001275 last;
1276 }
1277 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001278 # Preprocessor commands end at the newline unless escaped.
1279 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1280 $level--;
1281 $type = '';
1282 $off++;
1283 last;
1284 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 $off++;
1286 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001287 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001288 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001289 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001290 $line++;
1291 $remain--;
1292 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001293
1294 my $statement = substr($blk, $soff, $off - $soff + 1);
1295 my $condition = substr($blk, $soff, $coff - $soff + 1);
1296
1297 #warn "STATEMENT<$statement>\n";
1298 #warn "CONDITION<$condition>\n";
1299
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001301
1302 return ($statement, $condition,
1303 $line, $remain + 1, $off - $loff + 1, $level);
1304}
1305
Andy Whitcroftcf655042008-03-04 14:28:20 -08001306sub statement_lines {
1307 my ($stmt) = @_;
1308
1309 # Strip the diff line prefixes and rip blank lines at start and end.
1310 $stmt =~ s/(^|\n)./$1/g;
1311 $stmt =~ s/^\s*//;
1312 $stmt =~ s/\s*$//;
1313
1314 my @stmt_lines = ($stmt =~ /\n/g);
1315
1316 return $#stmt_lines + 2;
1317}
1318
1319sub statement_rawlines {
1320 my ($stmt) = @_;
1321
1322 my @stmt_lines = ($stmt =~ /\n/g);
1323
1324 return $#stmt_lines + 2;
1325}
1326
1327sub statement_block_size {
1328 my ($stmt) = @_;
1329
1330 $stmt =~ s/(^|\n)./$1/g;
1331 $stmt =~ s/^\s*{//;
1332 $stmt =~ s/}\s*$//;
1333 $stmt =~ s/^\s*//;
1334 $stmt =~ s/\s*$//;
1335
1336 my @stmt_lines = ($stmt =~ /\n/g);
1337 my @stmt_statements = ($stmt =~ /;/g);
1338
1339 my $stmt_lines = $#stmt_lines + 2;
1340 my $stmt_statements = $#stmt_statements + 1;
1341
1342 if ($stmt_lines > $stmt_statements) {
1343 return $stmt_lines;
1344 } else {
1345 return $stmt_statements;
1346 }
1347}
1348
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001349sub ctx_statement_full {
1350 my ($linenr, $remain, $off) = @_;
1351 my ($statement, $condition, $level);
1352
1353 my (@chunks);
1354
Andy Whitcroftcf655042008-03-04 14:28:20 -08001355 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001356 ($statement, $condition, $linenr, $remain, $off, $level) =
1357 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001358 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001359 push(@chunks, [ $condition, $statement ]);
1360 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1361 return ($level, $linenr, @chunks);
1362 }
1363
1364 # Pull in the following conditional/block pairs and see if they
1365 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001366 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001367 ($statement, $condition, $linenr, $remain, $off, $level) =
1368 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001369 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001370 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001371 #print "C: push\n";
1372 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001373 }
1374
1375 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001376}
1377
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001378sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001379 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001380 my $line;
1381 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001382 my $blk = '';
1383 my @o;
1384 my @c;
1385 my @res = ();
1386
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001387 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001388 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 for ($line = $start; $remain > 0; $line++) {
1390 next if ($rawlines[$line] =~ /^-/);
1391 $remain--;
1392
1393 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001394
1395 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001396 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001397 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001398 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001399 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001400 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001401 $level = pop(@stack);
1402 }
1403
Andy Whitcroft01464f32010-10-26 14:23:19 -07001404 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001405 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1406 if ($off > 0) {
1407 $off--;
1408 next;
1409 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001410
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001411 if ($c eq $close && $level > 0) {
1412 $level--;
1413 last if ($level == 0);
1414 } elsif ($c eq $open) {
1415 $level++;
1416 }
1417 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001418
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001419 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001420 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001421 }
1422
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001423 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001424 }
1425
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001426 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001427}
1428sub ctx_block_outer {
1429 my ($linenr, $remain) = @_;
1430
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001431 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1432 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001433}
1434sub ctx_block {
1435 my ($linenr, $remain) = @_;
1436
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001437 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1438 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001439}
1440sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001441 my ($linenr, $remain, $off) = @_;
1442
1443 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1444 return @r;
1445}
1446sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001447 my ($linenr, $remain) = @_;
1448
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001449 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001450}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001451sub ctx_statement_level {
1452 my ($linenr, $remain, $off) = @_;
1453
1454 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1455}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001456
1457sub ctx_locate_comment {
1458 my ($first_line, $end_line) = @_;
1459
1460 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001461 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001462 return $current_comment if (defined $current_comment);
1463
1464 # Look through the context and try and figure out if there is a
1465 # comment.
1466 my $in_comment = 0;
1467 $current_comment = '';
1468 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001469 my $line = $rawlines[$linenr - 1];
1470 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001471 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1472 $in_comment = 1;
1473 }
1474 if ($line =~ m@/\*@) {
1475 $in_comment = 1;
1476 }
1477 if (!$in_comment && $current_comment ne '') {
1478 $current_comment = '';
1479 }
1480 $current_comment .= $line . "\n" if ($in_comment);
1481 if ($line =~ m@\*/@) {
1482 $in_comment = 0;
1483 }
1484 }
1485
1486 chomp($current_comment);
1487 return($current_comment);
1488}
1489sub ctx_has_comment {
1490 my ($first_line, $end_line) = @_;
1491 my $cmt = ctx_locate_comment($first_line, $end_line);
1492
Andy Whitcroft00df3442007-06-08 13:47:06 -07001493 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494 ##print "CMMT: $cmt\n";
1495
1496 return ($cmt ne '');
1497}
1498
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001499sub raw_line {
1500 my ($linenr, $cnt) = @_;
1501
1502 my $offset = $linenr - 1;
1503 $cnt++;
1504
1505 my $line;
1506 while ($cnt) {
1507 $line = $rawlines[$offset++];
1508 next if (defined($line) && $line =~ /^-/);
1509 $cnt--;
1510 }
1511
1512 return $line;
1513}
1514
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001515sub cat_vet {
1516 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001517 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001518
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001519 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001520 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1521 $res .= $1;
1522 if ($2 ne '') {
1523 $coded = sprintf("^%c", unpack('C', $2) + 64);
1524 $res .= $coded;
1525 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001526 }
1527 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001528
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001529 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001530}
1531
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001532my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001533my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001534my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001535my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001536
1537sub annotate_reset {
1538 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001539 $av_pending = '_';
1540 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001541 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001542}
1543
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001544sub annotate_values {
1545 my ($stream, $type) = @_;
1546
1547 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001548 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001549 my $cur = $stream;
1550
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001551 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001552
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001553 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001554 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001555 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001556 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001557 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001558 print "WS($1)\n" if ($dbg_values > 1);
1559 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001560 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001561 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001562 }
1563
Florian Micklerc023e4732011-01-12 16:59:58 -08001564 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001565 print "CAST($1)\n" if ($dbg_values > 1);
1566 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001567 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001568
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001569 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001570 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001571 $type = 'T';
1572
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001573 } elsif ($cur =~ /^($Modifier)\s*/) {
1574 print "MODIFIER($1)\n" if ($dbg_values > 1);
1575 $type = 'T';
1576
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001577 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001578 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001579 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001580 push(@av_paren_type, $type);
1581 if ($2 ne '') {
1582 $av_pending = 'N';
1583 }
1584 $type = 'E';
1585
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001586 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001587 print "UNDEF($1)\n" if ($dbg_values > 1);
1588 $av_preprocessor = 1;
1589 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001590
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001591 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001592 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001593 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001594
1595 push(@av_paren_type, $type);
1596 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001597 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001598
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001599 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1601 $av_preprocessor = 1;
1602
1603 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1604
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001605 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001607 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001608 print "PRE_END($1)\n" if ($dbg_values > 1);
1609
1610 $av_preprocessor = 1;
1611
1612 # Assume all arms of the conditional end as this
1613 # one does, and continue as if the #endif was not here.
1614 pop(@av_paren_type);
1615 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001616 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001617
1618 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001619 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001621 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1622 print "ATTR($1)\n" if ($dbg_values > 1);
1623 $av_pending = $type;
1624 $type = 'N';
1625
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001626 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001627 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001628 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001629 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001630 }
1631 $type = 'N';
1632
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001633 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001634 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001635 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001636 $type = 'N';
1637
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001638 } elsif ($cur =~/^(case)/o) {
1639 print "CASE($1)\n" if ($dbg_values > 1);
1640 $av_pend_colon = 'C';
1641 $type = 'N';
1642
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001643 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001644 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001645 $type = 'N';
1646
1647 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001648 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001649 push(@av_paren_type, $av_pending);
1650 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001651 $type = 'N';
1652
1653 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001654 my $new_type = pop(@av_paren_type);
1655 if ($new_type ne '_') {
1656 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001657 print "PAREN('$1') -> $type\n"
1658 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001659 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001661 }
1662
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001663 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001664 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001665 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001666 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001667
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001668 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1669 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001670 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001671 } elsif ($type eq 'E') {
1672 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001673 }
1674 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1675 $type = 'V';
1676
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001677 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001678 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001679 $type = 'V';
1680
1681 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001682 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001683 $type = 'N';
1684
Andy Whitcroftcf655042008-03-04 14:28:20 -08001685 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001686 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001687 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001688 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001689
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001690 } elsif ($cur =~/^(,)/) {
1691 print "COMMA($1)\n" if ($dbg_values > 1);
1692 $type = 'C';
1693
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001694 } elsif ($cur =~ /^(\?)/o) {
1695 print "QUESTION($1)\n" if ($dbg_values > 1);
1696 $type = 'N';
1697
1698 } elsif ($cur =~ /^(:)/o) {
1699 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1700
1701 substr($var, length($res), 1, $av_pend_colon);
1702 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1703 $type = 'E';
1704 } else {
1705 $type = 'N';
1706 }
1707 $av_pend_colon = 'O';
1708
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001709 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001710 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001711 $type = 'N';
1712
Andy Whitcroft0d413862008-10-15 22:02:16 -07001713 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001714 my $variant;
1715
1716 print "OPV($1)\n" if ($dbg_values > 1);
1717 if ($type eq 'V') {
1718 $variant = 'B';
1719 } else {
1720 $variant = 'U';
1721 }
1722
1723 substr($var, length($res), 1, $variant);
1724 $type = 'N';
1725
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 if ($1 ne '++' && $1 ne '--') {
1729 $type = 'N';
1730 }
1731
1732 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001733 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734 }
1735 if (defined $1) {
1736 $cur = substr($cur, length($1));
1737 $res .= $type x length($1);
1738 }
1739 }
1740
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001741 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742}
1743
Andy Whitcroft8905a672007-11-28 16:21:06 -08001744sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001745 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001746 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001747 ^(?:
1748 $Modifier|
1749 $Storage|
1750 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001751 DEFINE_\S+
1752 )$|
1753 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001754 goto|
1755 return|
1756 case|
1757 else|
1758 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001759 do|
1760 \#|
1761 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001762 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001763 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001764 )}x;
1765 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1766 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001767 # Check for modifiers.
1768 $possible =~ s/\s*$Storage\s*//g;
1769 $possible =~ s/\s*$Sparse\s*//g;
1770 if ($possible =~ /^\s*$/) {
1771
1772 } elsif ($possible =~ /\s/) {
1773 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001774 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001775 if ($modifier !~ $notPermitted) {
1776 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001777 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001778 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001779 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001780
1781 } else {
1782 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001783 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001784 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001785 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001786 } else {
1787 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001788 }
1789}
1790
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001791my $prefix = '';
1792
Joe Perches000d1cc12011-07-25 17:13:25 -07001793sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001794 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001795
Joe Perchescbec18a2014-04-03 14:49:19 -07001796 return defined $use_type{$type} if (scalar keys %use_type > 0);
1797
1798 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001799}
1800
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001801sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001802 my ($level, $type, $msg) = @_;
1803
1804 if (!show_type($type) ||
1805 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001806 return 0;
1807 }
Joe Perches57230292015-06-25 15:03:03 -07001808 my $output = '';
1809 if (-t STDOUT && $color) {
1810 if ($level eq 'ERROR') {
1811 $output .= RED;
1812 } elsif ($level eq 'WARNING') {
1813 $output .= YELLOW;
1814 } else {
1815 $output .= GREEN;
1816 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001817 }
Joe Perches57230292015-06-25 15:03:03 -07001818 $output .= $prefix . $level . ':';
1819 if ($show_types) {
1820 $output .= BLUE if (-t STDOUT && $color);
1821 $output .= "$type:";
1822 }
1823 $output .= RESET if (-t STDOUT && $color);
1824 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001825
1826 if ($showfile) {
1827 my @lines = split("\n", $output, -1);
1828 splice(@lines, 1, 1);
1829 $output = join("\n", @lines);
1830 }
Joe Perches57230292015-06-25 15:03:03 -07001831 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001832
Joe Perches57230292015-06-25 15:03:03 -07001833 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001834
1835 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001836}
Joe Perchescbec18a2014-04-03 14:49:19 -07001837
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001838sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001839 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001840}
Joe Perches000d1cc12011-07-25 17:13:25 -07001841
Joe Perchesd752fcc2014-08-06 16:11:05 -07001842sub fixup_current_range {
1843 my ($lineRef, $offset, $length) = @_;
1844
1845 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1846 my $o = $1;
1847 my $l = $2;
1848 my $no = $o + $offset;
1849 my $nl = $l + $length;
1850 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1851 }
1852}
1853
1854sub fix_inserted_deleted_lines {
1855 my ($linesRef, $insertedRef, $deletedRef) = @_;
1856
1857 my $range_last_linenr = 0;
1858 my $delta_offset = 0;
1859
1860 my $old_linenr = 0;
1861 my $new_linenr = 0;
1862
1863 my $next_insert = 0;
1864 my $next_delete = 0;
1865
1866 my @lines = ();
1867
1868 my $inserted = @{$insertedRef}[$next_insert++];
1869 my $deleted = @{$deletedRef}[$next_delete++];
1870
1871 foreach my $old_line (@{$linesRef}) {
1872 my $save_line = 1;
1873 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001874 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001875 $delta_offset = 0;
1876 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1877 $range_last_linenr = $new_linenr;
1878 fixup_current_range(\$line, $delta_offset, 0);
1879 }
1880
1881 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1882 $deleted = @{$deletedRef}[$next_delete++];
1883 $save_line = 0;
1884 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1885 }
1886
1887 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1888 push(@lines, ${$inserted}{'LINE'});
1889 $inserted = @{$insertedRef}[$next_insert++];
1890 $new_linenr++;
1891 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1892 }
1893
1894 if ($save_line) {
1895 push(@lines, $line);
1896 $new_linenr++;
1897 }
1898
1899 $old_linenr++;
1900 }
1901
1902 return @lines;
1903}
1904
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001905sub fix_insert_line {
1906 my ($linenr, $line) = @_;
1907
1908 my $inserted = {
1909 LINENR => $linenr,
1910 LINE => $line,
1911 };
1912 push(@fixed_inserted, $inserted);
1913}
1914
1915sub fix_delete_line {
1916 my ($linenr, $line) = @_;
1917
1918 my $deleted = {
1919 LINENR => $linenr,
1920 LINE => $line,
1921 };
1922
1923 push(@fixed_deleted, $deleted);
1924}
1925
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001926sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001927 my ($type, $msg) = @_;
1928
1929 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001930 our $clean = 0;
1931 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001932 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001933 }
Joe Perches3705ce52013-07-03 15:05:31 -07001934 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001935}
1936sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001937 my ($type, $msg) = @_;
1938
1939 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940 our $clean = 0;
1941 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001942 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001943 }
Joe Perches3705ce52013-07-03 15:05:31 -07001944 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001945}
1946sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001947 my ($type, $msg) = @_;
1948
1949 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 our $clean = 0;
1951 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001952 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001953 }
Joe Perches3705ce52013-07-03 15:05:31 -07001954 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001955}
1956
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001957sub check_absolute_file {
1958 my ($absolute, $herecurr) = @_;
1959 my $file = $absolute;
1960
1961 ##print "absolute<$absolute>\n";
1962
1963 # See if any suffix of this path is a path within the tree.
1964 while ($file =~ s@^[^/]*/@@) {
1965 if (-f "$root/$file") {
1966 ##print "file<$file>\n";
1967 last;
1968 }
1969 }
1970 if (! -f _) {
1971 return 0;
1972 }
1973
1974 # It is, so see if the prefix is acceptable.
1975 my $prefix = $absolute;
1976 substr($prefix, -length($file)) = '';
1977
1978 ##print "prefix<$prefix>\n";
1979 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001980 WARN("USE_RELATIVE_PATH",
1981 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001982 }
1983}
1984
Joe Perches3705ce52013-07-03 15:05:31 -07001985sub trim {
1986 my ($string) = @_;
1987
Joe Perchesb34c6482013-09-11 14:24:01 -07001988 $string =~ s/^\s+|\s+$//g;
1989
1990 return $string;
1991}
1992
1993sub ltrim {
1994 my ($string) = @_;
1995
1996 $string =~ s/^\s+//;
1997
1998 return $string;
1999}
2000
2001sub rtrim {
2002 my ($string) = @_;
2003
2004 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002005
2006 return $string;
2007}
2008
Joe Perches52ea8502013-11-12 15:10:09 -08002009sub string_find_replace {
2010 my ($string, $find, $replace) = @_;
2011
2012 $string =~ s/$find/$replace/g;
2013
2014 return $string;
2015}
2016
Joe Perches3705ce52013-07-03 15:05:31 -07002017sub tabify {
2018 my ($leading) = @_;
2019
2020 my $source_indent = 8;
2021 my $max_spaces_before_tab = $source_indent - 1;
2022 my $spaces_to_tab = " " x $source_indent;
2023
2024 #convert leading spaces to tabs
2025 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2026 #Remove spaces before a tab
2027 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2028
2029 return "$leading";
2030}
2031
Gregory Beanf07523e2011-07-06 15:36:58 -07002032sub cleanup_continuation_headers {
2033 # Collapse any header-continuation lines into a single line so they
2034 # can be parsed meaningfully, as the parser only has one line
2035 # of context to work with.
2036 my $again;
2037 do {
2038 $again = 0;
2039 foreach my $n (0 .. scalar(@rawlines) - 2) {
2040 if ($rawlines[$n]=~/^\s*$/) {
2041 # A blank line means there's no more chance
2042 # of finding headers. Shortcut to done.
2043 return;
2044 }
2045 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2046 $rawlines[$n+1]=~/^\s+/) {
2047 # Continuation header. Collapse it.
2048 my $line = splice @rawlines, $n+1, 1;
2049 $line=~s/^\s+/ /;
2050 $rawlines[$n] .= $line;
2051 # We've 'destabilized' the list, so restart.
2052 $again = 1;
2053 last;
2054 }
2055 }
2056 } while ($again);
2057}
2058
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002059sub pos_last_openparen {
2060 my ($line) = @_;
2061
2062 my $pos = 0;
2063
2064 my $opens = $line =~ tr/\(/\(/;
2065 my $closes = $line =~ tr/\)/\)/;
2066
2067 my $last_openparen = 0;
2068
2069 if (($opens == 0) || ($closes >= $opens)) {
2070 return -1;
2071 }
2072
2073 my $len = length($line);
2074
2075 for ($pos = 0; $pos < $len; $pos++) {
2076 my $string = substr($line, $pos);
2077 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2078 $pos += length($1) - 1;
2079 } elsif (substr($line, $pos, 1) eq '(') {
2080 $last_openparen = $pos;
2081 } elsif (index($string, '(') == -1) {
2082 last;
2083 }
2084 }
2085
Joe Perches91cb5192014-04-03 14:49:32 -07002086 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002087}
2088
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002089sub process {
2090 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091
2092 my $linenr=0;
2093 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002094 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002095 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002096 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002097 my $subjectline="";
2098 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002099
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002100 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002101 my $indent;
2102 my $previndent=0;
2103 my $stashindent=0;
2104
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002105 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002106 my $signoff = 0;
2107 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002108 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002109 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002110 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002111 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002112 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002113 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002114 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002115 my $non_utf8_charset = 0;
2116
Joe Perches365dd4e2014-08-06 16:10:42 -07002117 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002118 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002119
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002120 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002121 our $cnt_lines = 0;
2122 our $cnt_error = 0;
2123 our $cnt_warn = 0;
2124 our $cnt_chk = 0;
2125
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002126 # Trace the real file/line as we go.
2127 my $realfile = '';
2128 my $realline = 0;
2129 my $realcnt = 0;
2130 my $here = '';
2131 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002132 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002133 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002134 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002135
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002136 my $prev_values = 'E';
2137
2138 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002139 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002140 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002141 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002142 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002143
Joe Perches7e51f192013-09-11 14:23:57 -07002144 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002146 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002147 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002148 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002149 my @setup_docs = ();
2150 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151
Joe Perchesd8b07712013-11-12 15:10:06 -08002152 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002153 my $shorttext = BEFORE_SHORTTEXT;
2154 my $shorttext_exspc = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002155
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002157 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002158 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002159
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002160 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002161 $linenr++;
2162 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002163
Joe Perches3705ce52013-07-03 15:05:31 -07002164 push(@fixed, $rawline) if ($fix);
2165
Andy Whitcroft773647a2008-03-28 14:15:58 -07002166 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002167 $setup_docs = 0;
2168 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2169 $setup_docs = 1;
2170 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002171 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002172 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002173 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2174 $realline=$1-1;
2175 if (defined $2) {
2176 $realcnt=$3+1;
2177 } else {
2178 $realcnt=1+1;
2179 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002180 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002181
2182 # Guestimate if this is a continuing comment. Run
2183 # the context looking for a comment "edge". If this
2184 # edge is a close comment then we must be in a comment
2185 # at context start.
2186 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002187 my $cnt = $realcnt;
2188 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2189 next if (defined $rawlines[$ln - 1] &&
2190 $rawlines[$ln - 1] =~ /^-/);
2191 $cnt--;
2192 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002193 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002194 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2195 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2196 ($edge) = $1;
2197 last;
2198 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002199 }
2200 if (defined $edge && $edge eq '*/') {
2201 $in_comment = 1;
2202 }
2203
2204 # Guestimate if this is a continuing comment. If this
2205 # is the start of a diff block and this line starts
2206 # ' *' then it is very likely a comment.
2207 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002208 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002209 {
2210 $in_comment = 1;
2211 }
2212
2213 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2214 sanitise_line_reset($in_comment);
2215
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002216 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002217 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002218 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 $line = sanitise_line($rawline);
2220 }
2221 push(@lines, $line);
2222
2223 if ($realcnt > 1) {
2224 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2225 } else {
2226 $realcnt = 0;
2227 }
2228
2229 #print "==>$rawline\n";
2230 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002231
2232 if ($setup_docs && $line =~ /^\+/) {
2233 push(@setup_docs, $line);
2234 }
2235 }
2236
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002237 $prefix = '';
2238
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 $realcnt = 0;
2240 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002241 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002242 foreach my $line (@lines) {
2243 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002244 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002245 my $sline = $line; #copy of $line
2246 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002247
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002248 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002249
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002250#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002251 if (!$in_commit_log &&
2252 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002253 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002254 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002255 $realline=$1-1;
2256 if (defined $2) {
2257 $realcnt=$3+1;
2258 } else {
2259 $realcnt=1+1;
2260 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002261 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002262 $prev_values = 'E';
2263
Andy Whitcroft773647a2008-03-28 14:15:58 -07002264 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002265 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002266 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002267 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002268 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002269
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002270# track the line number as we move through the hunk, note that
2271# new versions of GNU diff omit the leading space on completely
2272# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002273 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002274 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002275 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002276
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002277 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002278 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002279
2280 # Track the previous line.
2281 ($prevline, $stashline) = ($stashline, $line);
2282 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002283 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2284
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002286
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002287 } elsif ($realcnt == 1) {
2288 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002289 }
2290
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002291 my $hunk_line = ($realcnt != 0);
2292
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002293 $here = "#$linenr: " if (!$file);
2294 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002295
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002296 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002298 if ($line =~ /^diff --git.*?(\S+)$/) {
2299 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002300 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002301 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002302 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002303 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002304 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002305 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002306 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002307
2308 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002309 if (!$file && $tree && $p1_prefix ne '' &&
2310 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002311 WARN("PATCH_PREFIX",
2312 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002313 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002314
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002315 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002316 ERROR("MODIFIED_INCLUDE_ASM",
2317 "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 -07002318 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002319 $found_file = 1;
2320 }
2321
Joe Perches34d88152015-06-25 15:03:05 -07002322#make up the handle for any error we report on this line
2323 if ($showfile) {
2324 $prefix = "$realfile:$realline: "
2325 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002326 if ($file) {
2327 $prefix = "$filename:$realline: ";
2328 } else {
2329 $prefix = "$filename:$linenr: ";
2330 }
Joe Perches34d88152015-06-25 15:03:05 -07002331 }
2332
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002333 if ($found_file) {
Joe Perches7bd7e482015-09-09 15:37:44 -07002334 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002335 $check = 1;
2336 } else {
2337 $check = $check_orig;
2338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002339 next;
2340 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002341 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002343 my $hereline = "$here\n$rawline\n";
2344 my $herecurr = "$here\n$rawline\n";
2345 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002346
David Keitel72bf3e82016-03-23 20:52:44 -07002347 if ($shorttext != AFTER_SHORTTEXT) {
2348 if ($shorttext == IN_SHORTTEXT) {
2349 if ($line=~/^---/ || $line=~/^diff.*/) {
2350 $shorttext = AFTER_SHORTTEXT;
2351 } elsif (length($line) > (SHORTTEXT_LIMIT +
2352 $shorttext_exspc)
2353 && $line !~ /^:([0-7]{6}\s){2}
2354 ([[:xdigit:]]+\.*
2355 \s){2}\w+\s\w+/xms) {
2356 WARN("LONG_COMMIT_TEXT",
2357 "commit text line over " .
2358 SHORTTEXT_LIMIT .
2359 " characters\n" . $herecurr);
2360 }
David Keitele288d232016-03-23 20:57:21 -07002361 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
David Keitel72bf3e82016-03-23 20:52:44 -07002362 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002363# Check for Subject line followed by a blank line.
2364 if (length($line) != 0) {
2365 WARN("NONBLANK_AFTER_SUMMARY",
2366 "non-blank line after summary " .
2367 "line\n" . $sublinenr . $here .
2368 "\n" . $subjectline . "\n" .
2369 $line . "\n");
2370 }
2371 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
2372 $shorttext = CHECK_NEXT_SHORTTEXT;
2373 $subjectline = $line;
2374 $sublinenr = "#$linenr & ";
2375# Check for Subject line less than line limit
David Keitel72bf3e82016-03-23 20:52:44 -07002376 if (length($1) > SHORTTEXT_LIMIT) {
2377 WARN("LONG_SUMMARY_LINE",
2378 "summary line over " .
2379 SHORTTEXT_LIMIT .
2380 " characters\n" . $herecurr);
2381 }
2382 } elsif ($line=~/^ (.*)/) {
2383 $shorttext = IN_SHORTTEXT;
2384 $shorttext_exspc = 4;
2385 if (length($1) > SHORTTEXT_LIMIT) {
2386 WARN("LONG_SUMMARY_LINE",
2387 "summary line over " .
2388 SHORTTEXT_LIMIT .
2389 " characters\n" . $herecurr);
2390 }
2391 }
2392 }
2393
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002394 $cnt_lines++ if ($realcnt != 0);
2395
Joe Perchese518e9a2015-06-25 15:03:27 -07002396# Check if the commit log has what seems like a diff which can confuse patch
2397 if ($in_commit_log && !$commit_log_has_diff &&
2398 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2399 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2400 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2401 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2402 ERROR("DIFF_IN_COMMIT_MSG",
2403 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2404 $commit_log_has_diff = 1;
2405 }
2406
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002407# Check for incorrect file permissions
2408 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2409 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002410 if ($realfile !~ m@scripts/@ &&
2411 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002412 ERROR("EXECUTE_PERMISSIONS",
2413 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002414 }
2415 }
2416
Joe Perches20112472011-07-25 17:13:23 -07002417# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002418 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002419 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002420 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002421 }
2422
Joe Perchese0d975b2014-12-10 15:51:49 -08002423# Check if MAINTAINERS is being updated. If so, there's probably no need to
2424# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2425 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2426 $reported_maintainer_file = 1;
2427 }
2428
Joe Perches20112472011-07-25 17:13:23 -07002429# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002430 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002431 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002432 my $space_before = $1;
2433 my $sign_off = $2;
2434 my $space_after = $3;
2435 my $email = $4;
2436 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2437
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002438 if ($sign_off !~ /$signature_tags/) {
2439 WARN("BAD_SIGN_OFF",
2440 "Non-standard signature: $sign_off\n" . $herecurr);
2441 }
Joe Perches20112472011-07-25 17:13:23 -07002442 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002443 if (WARN("BAD_SIGN_OFF",
2444 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2445 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002446 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002447 "$ucfirst_sign_off $email";
2448 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002449 }
Joe Perches20112472011-07-25 17:13:23 -07002450 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002451 if (WARN("BAD_SIGN_OFF",
2452 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002454 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002455 "$ucfirst_sign_off $email";
2456 }
2457
Joe Perches20112472011-07-25 17:13:23 -07002458 }
2459 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002460 if (WARN("BAD_SIGN_OFF",
2461 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2462 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002463 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002464 "$ucfirst_sign_off $email";
2465 }
Joe Perches20112472011-07-25 17:13:23 -07002466 }
2467
2468 my ($email_name, $email_address, $comment) = parse_email($email);
2469 my $suggested_email = format_email(($email_name, $email_address));
2470 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002471 ERROR("BAD_SIGN_OFF",
2472 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002473 } else {
2474 my $dequoted = $suggested_email;
2475 $dequoted =~ s/^"//;
2476 $dequoted =~ s/" </ </;
2477 # Don't force email to have quotes
2478 # Allow just an angle bracketed address
2479 if ("$dequoted$comment" ne $email &&
2480 "<$email_address>$comment" ne $email &&
2481 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002482 WARN("BAD_SIGN_OFF",
2483 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002484 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002485 }
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002486 if ($chk_author && $line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002487 WARN("BAD_SIGN_OFF",
2488 "invalid Signed-off-by identity\n" . $line );
2489 }
Joe Perches7e51f192013-09-11 14:23:57 -07002490
2491# Check for duplicate signatures
2492 my $sig_nospace = $line;
2493 $sig_nospace =~ s/\s//g;
2494 $sig_nospace = lc($sig_nospace);
2495 if (defined $signatures{$sig_nospace}) {
2496 WARN("BAD_SIGN_OFF",
2497 "Duplicate signature\n" . $herecurr);
2498 } else {
2499 $signatures{$sig_nospace} = 1;
2500 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002501 }
2502
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002503# Check email subject for common tools that don't need to be mentioned
2504 if ($in_header_lines &&
2505 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2506 WARN("EMAIL_SUBJECT",
2507 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2508 }
2509
Joe Perches9b3189e2014-06-04 16:12:10 -07002510# Check for old stable address
2511 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2512 ERROR("STABLE_ADDRESS",
2513 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2514 }
2515
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002516# Check for unwanted Gerrit info
2517 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2518 ERROR("GERRIT_CHANGE_ID",
2519 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2520 }
2521
Joe Perches369c8dd2015-11-06 16:31:34 -08002522# Check if the commit log is in a possible stack dump
2523 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2524 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2525 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2526 # timestamp
2527 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2528 # stack dump address
2529 $commit_log_possible_stack_dump = 1;
2530 }
2531
Joe Perches2a076f42015-04-16 12:44:28 -07002532# Check for line lengths > 75 in commit log, warn once
2533 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002534 length($line) > 75 &&
2535 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2536 # file delta changes
2537 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2538 # filename then :
2539 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2540 # A Fixes: or Link: line
2541 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002542 WARN("COMMIT_LOG_LONG_LINE",
2543 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2544 $commit_log_long_line = 1;
2545 }
2546
Joe Perchesbf4daf12015-09-09 15:37:50 -07002547# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002548 if ($in_commit_log && $commit_log_possible_stack_dump &&
2549 $line =~ /^\s*$/) {
2550 $commit_log_possible_stack_dump = 0;
2551 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002552
Joe Perches0d7835f2015-02-13 14:38:35 -08002553# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002554 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002555 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002556 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002557 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002558 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2559 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002560 my $init_char = "c";
2561 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002562 my $short = 1;
2563 my $long = 0;
2564 my $case = 1;
2565 my $space = 1;
2566 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002567 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002568 my $id = '0123456789ab';
2569 my $orig_desc = "commit description";
2570 my $description = "";
2571
Joe Perchesfe043ea2015-09-09 15:37:25 -07002572 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2573 $init_char = $1;
2574 $orig_commit = lc($2);
2575 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2576 $orig_commit = lc($1);
2577 }
2578
Joe Perches0d7835f2015-02-13 14:38:35 -08002579 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2580 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2581 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2582 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2583 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2584 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002585 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002586 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2587 defined $rawlines[$linenr] &&
2588 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2589 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002590 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002591 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2592 defined $rawlines[$linenr] &&
2593 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2594 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2595 $orig_desc = $1;
2596 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2597 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002598 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002599 }
2600
2601 ($id, $description) = git_commit_info($orig_commit,
2602 $id, $orig_desc);
2603
Joe Perches19c146a2015-02-13 14:39:00 -08002604 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002605 ERROR("GIT_COMMIT_ID",
2606 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2607 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002608 }
2609
Joe Perches13f19372014-08-06 16:10:59 -07002610# Check for added, moved or deleted files
2611 if (!$reported_maintainer_file && !$in_commit_log &&
2612 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2613 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2614 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2615 (defined($1) || defined($2))))) {
2616 $reported_maintainer_file = 1;
2617 WARN("FILE_PATH_CHANGES",
2618 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2619 }
2620
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002621#check the patch for invalid author credentials
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002622 if ($chk_author && $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002623 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2624 }
2625
Andy Whitcroft00df3442007-06-08 13:47:06 -07002626# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002627 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002628 ERROR("CORRUPTED_PATCH",
2629 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002630 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002631 }
2632
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002633# Check for absolute kernel paths.
2634 if ($tree) {
2635 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2636 my $file = $1;
2637
2638 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2639 check_absolute_file($1, $herecurr)) {
2640 #
2641 } else {
2642 check_absolute_file($file, $herecurr);
2643 }
2644 }
2645 }
2646
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002647# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2648 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002649 $rawline !~ m/^$UTF8*$/) {
2650 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2651
2652 my $blank = copy_spacing($rawline);
2653 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2654 my $hereptr = "$hereline$ptr\n";
2655
Joe Perches34d99212011-07-25 17:13:26 -07002656 CHK("INVALID_UTF8",
2657 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002658 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002659
Joe Perches15662b32011-10-31 17:13:12 -07002660# Check if it's the start of a commit log
2661# (not a header line and we haven't seen the patch filename)
2662 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002663 !($rawline =~ /^\s+\S/ ||
2664 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002665 $in_header_lines = 0;
2666 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002667 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002668 }
2669
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002670# Check if there is UTF-8 in a commit log when a mail header has explicitly
2671# declined it, i.e defined some charset where it is missing.
2672 if ($in_header_lines &&
2673 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2674 $1 !~ /utf-8/i) {
2675 $non_utf8_charset = 1;
2676 }
2677
2678 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002679 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002680 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002681 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2682 }
2683
Kees Cook66b47b42014-10-13 15:51:57 -07002684# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002685 if (defined($misspellings) &&
2686 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002687 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002688 my $typo = $1;
2689 my $typo_fix = $spelling_fix{lc($typo)};
2690 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2691 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2692 my $msg_type = \&WARN;
2693 $msg_type = \&CHK if ($file);
2694 if (&{$msg_type}("TYPO_SPELLING",
2695 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2696 $fix) {
2697 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2698 }
2699 }
2700 }
2701
Andy Whitcroft306708542008-10-15 22:02:28 -07002702# ignore non-hunk lines and lines being removed
2703 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002704
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002705#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002706 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002707 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002708 if (ERROR("DOS_LINE_ENDINGS",
2709 "DOS line endings\n" . $herevet) &&
2710 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002711 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002712 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002713 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2714 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002715 if (ERROR("TRAILING_WHITESPACE",
2716 "trailing whitespace\n" . $herevet) &&
2717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002718 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002719 }
2720
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002721 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002722 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002723
Josh Triplett4783f892013-11-12 15:10:12 -08002724# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002725 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002726 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2727 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002728 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2729 my $msg_type = \&ERROR;
2730 $msg_type = \&CHK if ($file);
2731 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002732 "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 -08002733 }
2734
Andi Kleen33549572010-05-24 14:33:29 -07002735# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002736# Only applies when adding the entry originally, after that we do not have
2737# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002738 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002739 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002740 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002741 my $cnt = $realcnt;
2742 my $ln = $linenr + 1;
2743 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002744 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002745 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002746 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002747 $f = $lines[$ln - 1];
2748 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2749 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002750
2751 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002752 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002753
Joe Perches8d73e0e2014-08-06 16:10:46 -07002754 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002755 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002756 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002757 $length = -1;
2758 }
2759
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002760 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002761 $f =~ s/#.*//;
2762 $f =~ s/^\s+//;
2763 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002764 if ($f =~ /^\s*config\s/) {
2765 $is_end = 1;
2766 last;
2767 }
Andi Kleen33549572010-05-24 14:33:29 -07002768 $length++;
2769 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002770 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2771 WARN("CONFIG_DESCRIPTION",
2772 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2773 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002774 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002775 }
2776
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002777# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2778 if ($realfile =~ /Kconfig/ &&
2779 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2780 WARN("CONFIG_EXPERIMENTAL",
2781 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2782 }
2783
Christoph Jaeger327953e2015-02-13 14:38:29 -08002784# discourage the use of boolean for type definition attributes of Kconfig options
2785 if ($realfile =~ /Kconfig/ &&
2786 $line =~ /^\+\s*\bboolean\b/) {
2787 WARN("CONFIG_TYPE_BOOLEAN",
2788 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2789 }
2790
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002791 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2792 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2793 my $flag = $1;
2794 my $replacement = {
2795 'EXTRA_AFLAGS' => 'asflags-y',
2796 'EXTRA_CFLAGS' => 'ccflags-y',
2797 'EXTRA_CPPFLAGS' => 'cppflags-y',
2798 'EXTRA_LDFLAGS' => 'ldflags-y',
2799 };
2800
2801 WARN("DEPRECATED_VARIABLE",
2802 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2803 }
2804
Rob Herringbff5da42014-01-23 15:54:51 -08002805# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002806 if (defined $root &&
2807 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2808 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2809
Rob Herringbff5da42014-01-23 15:54:51 -08002810 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2811
Florian Vaussardcc933192014-04-03 14:49:27 -07002812 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2813 my $vp_file = $dt_path . "vendor-prefixes.txt";
2814
Rob Herringbff5da42014-01-23 15:54:51 -08002815 foreach my $compat (@compats) {
2816 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002817 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2818 my $compat3 = $compat;
2819 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2820 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002821 if ( $? >> 8 ) {
2822 WARN("UNDOCUMENTED_DT_STRING",
2823 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2824 }
2825
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002826 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2827 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002828 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002829 if ( $? >> 8 ) {
2830 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002831 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002832 }
2833 }
2834 }
2835
Andy Whitcroft5368df22008-10-15 22:02:27 -07002836# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002837 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002838
Joe Perches47e0c882015-06-25 15:02:57 -07002839# line length limit (with some exclusions)
2840#
2841# There are a few types of lines that may extend beyond $max_line_length:
2842# logging functions like pr_info that end in a string
2843# lines with a single string
2844# #defines that are a single string
2845#
2846# There are 3 different line length message types:
2847# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2848# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2849# LONG_LINE all other lines longer than $max_line_length
2850#
2851# if LONG_LINE is ignored, the other 2 types are also ignored
2852#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002853 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002854 my $msg_type = "LONG_LINE";
2855
2856 # Check the allowed long line types first
2857
2858 # logging functions that end in a string that starts
2859 # before $max_line_length
2860 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2861 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2862 $msg_type = "";
2863
2864 # lines with only strings (w/ possible termination)
2865 # #defines with only strings
2866 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2867 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2868 $msg_type = "";
2869
Joe Perchesd560a5f2016-08-02 14:04:31 -07002870 # EFI_GUID is another special case
2871 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2872 $msg_type = "";
2873
Joe Perches47e0c882015-06-25 15:02:57 -07002874 # Otherwise set the alternate message types
2875
2876 # a comment starts before $max_line_length
2877 } elsif ($line =~ /($;[\s$;]*)$/ &&
2878 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2879 $msg_type = "LONG_LINE_COMMENT"
2880
2881 # a quoted string starts before $max_line_length
2882 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2883 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2884 $msg_type = "LONG_LINE_STRING"
2885 }
2886
2887 if ($msg_type ne "" &&
2888 (show_type("LONG_LINE") || show_type($msg_type))) {
2889 WARN($msg_type,
2890 "line over $max_line_length characters\n" . $herecurr);
2891 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002892 }
2893
Andy Whitcroft8905a672007-11-28 16:21:06 -08002894# check for adding lines without a newline.
2895 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002896 WARN("MISSING_EOF_NEWLINE",
2897 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002898 }
2899
Mike Frysinger42e41c52009-09-21 17:04:40 -07002900# Blackfin: use hi/lo macros
2901 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2902 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2903 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002904 ERROR("LO_MACRO",
2905 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002906 }
2907 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2908 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002909 ERROR("HI_MACRO",
2910 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002911 }
2912 }
2913
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002914# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002915 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002916
2917# at the beginning of a line any tabs must come first and anything
2918# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002919 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2920 $rawline =~ /^\+\s* \s*/) {
2921 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002922 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002923 if (ERROR("CODE_INDENT",
2924 "code indent should use tabs where possible\n" . $herevet) &&
2925 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002926 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002927 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002928 }
2929
Alberto Panizzo08e44362010-03-05 13:43:54 -08002930# check for space before tabs.
2931 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2932 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002933 if (WARN("SPACE_BEFORE_TAB",
2934 "please, no space before tabs\n" . $herevet) &&
2935 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002936 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002937 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002938 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002939 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002940 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002941 }
2942
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002943# check for && or || at the start of a line
2944 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2945 CHK("LOGICAL_CONTINUATIONS",
2946 "Logical continuations should be on the previous line\n" . $hereprev);
2947 }
2948
Joe Perchesa91e8992016-05-20 17:04:05 -07002949# check indentation starts on a tab stop
2950 if ($^V && $^V ge 5.10.0 &&
2951 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2952 my $indent = length($1);
2953 if ($indent % 8) {
2954 if (WARN("TABSTOP",
2955 "Statements should start on a tabstop\n" . $herecurr) &&
2956 $fix) {
2957 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2958 }
2959 }
2960 }
2961
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002962# check multi-line statement indentation matches previous line
2963 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002964 $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 -07002965 $prevline =~ /^\+(\t*)(.*)$/;
2966 my $oldindent = $1;
2967 my $rest = $2;
2968
2969 my $pos = pos_last_openparen($rest);
2970 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002971 $line =~ /^(\+| )([ \t]*)/;
2972 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002973
2974 my $goodtabindent = $oldindent .
2975 "\t" x ($pos / 8) .
2976 " " x ($pos % 8);
2977 my $goodspaceindent = $oldindent . " " x $pos;
2978
2979 if ($newindent ne $goodtabindent &&
2980 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002981
2982 if (CHK("PARENTHESIS_ALIGNMENT",
2983 "Alignment should match open parenthesis\n" . $hereprev) &&
2984 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002985 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002986 s/^\+[ \t]*/\+$goodtabindent/;
2987 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002988 }
2989 }
2990 }
2991
Joe Perches6ab3a972015-04-16 12:44:05 -07002992# check for space after cast like "(int) foo" or "(struct foo) bar"
2993# avoid checking a few false positives:
2994# "sizeof(<type>)" or "__alignof__(<type>)"
2995# function pointer declarations like "(*foo)(int) = bar;"
2996# structure definitions like "(struct foo) { 0 };"
2997# multiline macros that define functions
2998# known attributes or the __attribute__ keyword
2999 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3000 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003001 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003002 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003003 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003004 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003005 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003006 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003007 }
3008
Joe Perches86406b12015-09-09 15:37:41 -07003009# Block comment styles
3010# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003011 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003012 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003013 $rawline =~ /^\+[ \t]*\*/ &&
3014 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003015 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3016 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3017 }
3018
Joe Perches86406b12015-09-09 15:37:41 -07003019# Block comments use * on subsequent lines
3020 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3021 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003022 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003023 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003024 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003025 WARN("BLOCK_COMMENT_STYLE",
3026 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003027 }
3028
Joe Perches86406b12015-09-09 15:37:41 -07003029# Block comments use */ on trailing lines
3030 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003031 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3032 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3033 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003034 WARN("BLOCK_COMMENT_STYLE",
3035 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003036 }
3037
Joe Perches7f619192014-08-06 16:10:39 -07003038# check for missing blank lines after struct/union declarations
3039# with exceptions for various attributes and macros
3040 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3041 $line =~ /^\+/ &&
3042 !($line =~ /^\+\s*$/ ||
3043 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3044 $line =~ /^\+\s*MODULE_/i ||
3045 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3046 $line =~ /^\+[a-z_]*init/ ||
3047 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3048 $line =~ /^\+\s*DECLARE/ ||
3049 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003050 if (CHK("LINE_SPACING",
3051 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3052 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003053 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003054 }
Joe Perches7f619192014-08-06 16:10:39 -07003055 }
3056
Joe Perches365dd4e2014-08-06 16:10:42 -07003057# check for multiple consecutive blank lines
3058 if ($prevline =~ /^[\+ ]\s*$/ &&
3059 $line =~ /^\+\s*$/ &&
3060 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003061 if (CHK("LINE_SPACING",
3062 "Please don't use multiple blank lines\n" . $hereprev) &&
3063 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003064 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003065 }
3066
Joe Perches365dd4e2014-08-06 16:10:42 -07003067 $last_blank_line = $linenr;
3068 }
3069
Joe Perches3b617e32014-04-03 14:49:28 -07003070# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003071 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3072 # actual declarations
3073 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003074 # function pointer declarations
3075 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003076 # foo bar; where foo is some local typedef or #define
3077 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3078 # known declaration macros
3079 $prevline =~ /^\+\s+$declaration_macros/) &&
3080 # for "else if" which can look like "$Ident $Ident"
3081 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3082 # other possible extensions of declaration lines
3083 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3084 # not starting a section or a macro "\" extended line
3085 $prevline =~ /(?:\{\s*|\\)$/) &&
3086 # looks like a declaration
3087 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003088 # function pointer declarations
3089 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003090 # foo bar; where foo is some local typedef or #define
3091 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3092 # known declaration macros
3093 $sline =~ /^\+\s+$declaration_macros/ ||
3094 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003095 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003096 # start or end of block or continuation of declaration
3097 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3098 # bitfield continuation
3099 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3100 # other possible extensions of declaration lines
3101 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3102 # indentation of previous and current line are the same
3103 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003104 if (WARN("LINE_SPACING",
3105 "Missing a blank line after declarations\n" . $hereprev) &&
3106 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003107 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003108 }
Joe Perches3b617e32014-04-03 14:49:28 -07003109 }
3110
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003111# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003112# Exceptions:
3113# 1) within comments
3114# 2) indented preprocessor commands
3115# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003116 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003117 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003118 if (WARN("LEADING_SPACE",
3119 "please, no spaces at the start of a line\n" . $herevet) &&
3120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003121 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003122 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003123 }
3124
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003125# check we are in a valid C source file if not then ignore this hunk
3126 next if ($realfile !~ /\.(h|c)$/);
3127
Joe Perches032a4c02014-08-06 16:10:29 -07003128# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003129# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003130# if the previous line is a break or return and is indented 1 tab more...
3131 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3132 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003133 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3134 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3135 defined $lines[$linenr] &&
3136 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003137 WARN("UNNECESSARY_ELSE",
3138 "else is not generally useful after a break or return\n" . $hereprev);
3139 }
3140 }
3141
Joe Perchesc00df192014-08-06 16:11:01 -07003142# check indentation of a line with a break;
3143# if the previous line is a goto or return and is indented the same # of tabs
3144 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3145 my $tabs = $1;
3146 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3147 WARN("UNNECESSARY_BREAK",
3148 "break is not useful after a goto or return\n" . $hereprev);
3149 }
3150 }
3151
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003152# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3153 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3154 WARN("CONFIG_EXPERIMENTAL",
3155 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3156 }
3157
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003158# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003159 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003160 WARN("CVS_KEYWORD",
3161 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003162 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003163
Mike Frysinger42e41c52009-09-21 17:04:40 -07003164# Blackfin: don't use __builtin_bfin_[cs]sync
3165 if ($line =~ /__builtin_bfin_csync/) {
3166 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003167 ERROR("CSYNC",
3168 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003169 }
3170 if ($line =~ /__builtin_bfin_ssync/) {
3171 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003172 ERROR("SSYNC",
3173 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003174 }
3175
Joe Perches56e77d72013-02-21 16:44:14 -08003176# check for old HOTPLUG __dev<foo> section markings
3177 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3178 WARN("HOTPLUG_SECTION",
3179 "Using $1 is unnecessary\n" . $herecurr);
3180 }
3181
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003182# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003183 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3184 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003185#print "LINE<$line>\n";
3186 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003187 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003188 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003189 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003190 $stat =~ s/\n./\n /g;
3191 $cond =~ s/\n./\n /g;
3192
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003193#print "linenr<$linenr> <$stat>\n";
3194 # If this statement has no statement boundaries within
3195 # it there is no point in retrying a statement scan
3196 # until we hit end of it.
3197 my $frag = $stat; $frag =~ s/;+\s*$//;
3198 if ($frag !~ /(?:{|;)/) {
3199#print "skip<$line_nr_next>\n";
3200 $suppress_statement = $line_nr_next;
3201 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003202
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003203 # Find the real next line.
3204 $realline_next = $line_nr_next;
3205 if (defined $realline_next &&
3206 (!defined $lines[$realline_next - 1] ||
3207 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3208 $realline_next++;
3209 }
3210
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003211 my $s = $stat;
3212 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003213
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003214 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003215 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003216
3217 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003218 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003219
Andy Whitcroft463f2862009-09-21 17:04:34 -07003220 } elsif ($s =~ /^.\s*else\b/s) {
3221
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003222 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003223 } 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 -07003224 my $type = $1;
3225 $type =~ s/\s+/ /g;
3226 possible($type, "A:" . $s);
3227
Andy Whitcroft8905a672007-11-28 16:21:06 -08003228 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003229 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003230 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003231 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003232
3233 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003234 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003235 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003236 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003237
3238 # Check for any sort of function declaration.
3239 # int foo(something bar, other baz);
3240 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003241 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 -08003242 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003243
Andy Whitcroftcf655042008-03-04 14:28:20 -08003244 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003245 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003246 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003247
Andy Whitcroft8905a672007-11-28 16:21:06 -08003248 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003249 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003250
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003251 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003252 }
3253 }
3254 }
3255
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003256 }
3257
Andy Whitcroft653d4872007-06-23 17:16:34 -07003258#
3259# Checks which may be anchored in the context.
3260#
3261
3262# Check for switch () and associated case and default
3263# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003264 if ($line=~/\bswitch\s*\(.*\)/) {
3265 my $err = '';
3266 my $sep = '';
3267 my @ctx = ctx_block_outer($linenr, $realcnt);
3268 shift(@ctx);
3269 for my $ctx (@ctx) {
3270 my ($clen, $cindent) = line_stats($ctx);
3271 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3272 $indent != $cindent) {
3273 $err .= "$sep$ctx\n";
3274 $sep = '';
3275 } else {
3276 $sep = "[...]\n";
3277 }
3278 }
3279 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003280 ERROR("SWITCH_CASE_INDENT_LEVEL",
3281 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003282 }
3283 }
3284
3285# if/while/etc brace do not go on next line, unless defining a do while loop,
3286# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003287 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 -07003288 my $pre_ctx = "$1$2";
3289
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003290 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003291
3292 if ($line =~ /^\+\t{6,}/) {
3293 WARN("DEEP_INDENTATION",
3294 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3295 }
3296
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003297 my $ctx_cnt = $realcnt - $#ctx - 1;
3298 my $ctx = join("\n", @ctx);
3299
Andy Whitcroft548596d2008-07-23 21:29:01 -07003300 my $ctx_ln = $linenr;
3301 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003302
Andy Whitcroft548596d2008-07-23 21:29:01 -07003303 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3304 defined $lines[$ctx_ln - 1] &&
3305 $lines[$ctx_ln - 1] =~ /^-/)) {
3306 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3307 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003308 $ctx_ln++;
3309 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003310
Andy Whitcroft53210162008-07-23 21:29:03 -07003311 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3312 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003313
Joe Perchesd752fcc2014-08-06 16:11:05 -07003314 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003315 ERROR("OPEN_BRACE",
3316 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003317 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003318 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003319 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3320 $ctx =~ /\)\s*\;\s*$/ &&
3321 defined $lines[$ctx_ln - 1])
3322 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003323 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3324 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003325 WARN("TRAILING_SEMICOLON",
3326 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003327 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003328 }
3329 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003330 }
3331
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003332# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003333 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 -08003334 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3335 ctx_statement_block($linenr, $realcnt, 0)
3336 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003337 my ($s, $c) = ($stat, $cond);
3338
3339 substr($s, 0, length($c), '');
3340
Joe Perches9f5af482015-09-09 15:37:30 -07003341 # remove inline comments
3342 $s =~ s/$;/ /g;
3343 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003344
3345 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003346 my @newlines = ($c =~ /\n/gs);
3347 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003348
Joe Perches9f5af482015-09-09 15:37:30 -07003349 # Make sure we remove the line prefixes as we have
3350 # none on the first line, and are going to readd them
3351 # where necessary.
3352 $s =~ s/\n./\n/gs;
3353 while ($s =~ /\n\s+\\\n/) {
3354 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3355 }
3356
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003357 # We want to check the first line inside the block
3358 # starting at the end of the conditional, so remove:
3359 # 1) any blank line termination
3360 # 2) any opening brace { on end of the line
3361 # 3) any do (...) {
3362 my $continuation = 0;
3363 my $check = 0;
3364 $s =~ s/^.*\bdo\b//;
3365 $s =~ s/^\s*{//;
3366 if ($s =~ s/^\s*\\//) {
3367 $continuation = 1;
3368 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003369 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003370 $check = 1;
3371 $cond_lines++;
3372 }
3373
3374 # Also ignore a loop construct at the end of a
3375 # preprocessor statement.
3376 if (($prevline =~ /^.\s*#\s*define\s/ ||
3377 $prevline =~ /\\\s*$/) && $continuation == 0) {
3378 $check = 0;
3379 }
3380
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003381 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003382 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003383 while ($cond_ptr != $cond_lines) {
3384 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003385
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003386 # If we see an #else/#elif then the code
3387 # is not linear.
3388 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3389 $check = 0;
3390 }
3391
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003392 # Ignore:
3393 # 1) blank lines, they should be at 0,
3394 # 2) preprocessor lines, and
3395 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003396 if ($continuation ||
3397 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003398 $s =~ /^\s*#\s*?/ ||
3399 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003400 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003401 if ($s =~ s/^.*?\n//) {
3402 $cond_lines++;
3403 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003404 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003405 }
3406
3407 my (undef, $sindent) = line_stats("+" . $s);
3408 my $stat_real = raw_line($linenr, $cond_lines);
3409
3410 # Check if either of these lines are modified, else
3411 # this is not this patch's fault.
3412 if (!defined($stat_real) ||
3413 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3414 $check = 0;
3415 }
3416 if (defined($stat_real) && $cond_lines > 1) {
3417 $stat_real = "[...]\n$stat_real";
3418 }
3419
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003420 #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 -07003421
Joe Perches9f5af482015-09-09 15:37:30 -07003422 if ($check && $s ne '' &&
3423 (($sindent % 8) != 0 ||
3424 ($sindent < $indent) ||
3425 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003426 WARN("SUSPECT_CODE_INDENT",
3427 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003428 }
3429 }
3430
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003431 # Track the 'values' across context and added lines.
3432 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003433 my ($curr_values, $curr_vars) =
3434 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003435 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003436 if ($dbg_values) {
3437 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003438 print "$linenr > .$outline\n";
3439 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003440 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003441 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003442 $prev_values = substr($curr_values, -1);
3443
Andy Whitcroft00df3442007-06-08 13:47:06 -07003444#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003445 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003446
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003447# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003448 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 -07003449 my $type = $1;
3450 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003451 $var = "" if (!defined $var);
3452 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003453 my $sign = $1;
3454 my $pointer = $2;
3455
3456 $pointer = "" if (!defined $pointer);
3457
3458 if (WARN("UNSPECIFIED_INT",
3459 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3460 $fix) {
3461 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003462 my $comp_pointer = $pointer;
3463 $comp_pointer =~ s/\s//g;
3464 $decl .= $comp_pointer;
3465 $decl = rtrim($decl) if ($var eq "");
3466 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003467 }
3468 }
3469 }
3470
Andy Whitcroft653d4872007-06-23 17:16:34 -07003471# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003472 if ($dbg_type) {
3473 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003474 ERROR("TEST_TYPE",
3475 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003476 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003477 ERROR("TEST_NOT_TYPE",
3478 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003479 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003480 next;
3481 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003482# TEST: allow direct testing of the attribute matcher.
3483 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003484 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003485 ERROR("TEST_ATTR",
3486 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003487 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003488 ERROR("TEST_NOT_ATTR",
3489 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003490 }
3491 next;
3492 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003494# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003495 if ($line =~ /^.\s*{/ &&
3496 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003497 if (ERROR("OPEN_BRACE",
3498 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003499 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3500 fix_delete_line($fixlinenr - 1, $prevrawline);
3501 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003502 my $fixedline = $prevrawline;
3503 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003504 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003505 $fixedline = $line;
3506 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003507 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003508 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003509 }
3510
Andy Whitcroft653d4872007-06-23 17:16:34 -07003511#
3512# Checks which are anchored on the added line.
3513#
3514
3515# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003516 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003517 my $path = $1;
3518 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003519 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003520 "malformed #include filename\n" . $herecurr);
3521 }
3522 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3523 ERROR("UAPI_INCLUDE",
3524 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003525 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003526 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003527
3528# no C99 // comments
3529 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003530 if (ERROR("C99_COMMENTS",
3531 "do not use C99 // comments\n" . $herecurr) &&
3532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003533 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003534 if ($line =~ /\/\/(.*)$/) {
3535 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003536 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003537 }
3538 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003539 }
3540 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003541 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003542 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003543
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003544# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3545# the whole statement.
3546#print "APW <$lines[$realline_next - 1]>\n";
3547 if (defined $realline_next &&
3548 exists $lines[$realline_next - 1] &&
3549 !defined $suppress_export{$realline_next} &&
3550 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3551 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003552 # Handle definitions which produce identifiers with
3553 # a prefix:
3554 # XXX(foo);
3555 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003556 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003557 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003558 $name =~ /^${Ident}_$2/) {
3559#print "FOO C name<$name>\n";
3560 $suppress_export{$realline_next} = 1;
3561
3562 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003563 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003564 ^.DEFINE_$Ident\(\Q$name\E\)|
3565 ^.DECLARE_$Ident\(\Q$name\E\)|
3566 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003567 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3568 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003569 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003570#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3571 $suppress_export{$realline_next} = 2;
3572 } else {
3573 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003574 }
3575 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003576 if (!defined $suppress_export{$linenr} &&
3577 $prevline =~ /^.\s*$/ &&
3578 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3579 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3580#print "FOO B <$lines[$linenr - 1]>\n";
3581 $suppress_export{$linenr} = 2;
3582 }
3583 if (defined $suppress_export{$linenr} &&
3584 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003585 WARN("EXPORT_SYMBOL",
3586 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003587 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003588
Joe Eloff5150bda2010-08-09 17:21:00 -07003589# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003590 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003591 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003592 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003593 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003594 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003595 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003596 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003597# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003598 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003599 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003600 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003601 $herecurr) &&
3602 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003603 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003604 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003605 }
3606
Joe Perches18130872014-08-06 16:11:22 -07003607# check for misordered declarations of char/short/int/long with signed/unsigned
3608 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3609 my $tmp = trim($1);
3610 WARN("MISORDERED_TYPE",
3611 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3612 }
3613
Joe Perchescb710ec2010-10-26 14:23:20 -07003614# check for static const char * arrays.
3615 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003616 WARN("STATIC_CONST_CHAR_ARRAY",
3617 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003618 $herecurr);
3619 }
3620
3621# check for static char foo[] = "bar" declarations.
3622 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003623 WARN("STATIC_CONST_CHAR_ARRAY",
3624 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003625 $herecurr);
3626 }
3627
Joe Perchesab7e23f2015-04-16 12:44:22 -07003628# check for const <foo> const where <foo> is not a pointer or array type
3629 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3630 my $found = $1;
3631 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3632 WARN("CONST_CONST",
3633 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3634 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3635 WARN("CONST_CONST",
3636 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3637 }
3638 }
3639
Joe Perches9b0fa602014-04-03 14:49:18 -07003640# check for non-global char *foo[] = {"bar", ...} declarations.
3641 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3642 WARN("STATIC_CONST_CHAR_ARRAY",
3643 "char * array declaration might be better as static const\n" .
3644 $herecurr);
3645 }
3646
Joe Perchesb598b672015-04-16 12:44:36 -07003647# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3648 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3649 my $array = $1;
3650 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3651 my $array_div = $1;
3652 if (WARN("ARRAY_SIZE",
3653 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3654 $fix) {
3655 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3656 }
3657 }
3658 }
3659
Joe Perchesb36190c2014-01-27 17:07:18 -08003660# check for function declarations without arguments like "int foo()"
3661 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3662 if (ERROR("FUNCTION_WITHOUT_ARGS",
3663 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3664 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003665 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003666 }
3667 }
3668
Joe Perches92e112f2013-12-13 11:36:22 -07003669# check for uses of DEFINE_PCI_DEVICE_TABLE
3670 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3671 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3672 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003674 $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 -07003675 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003676 }
3677
Andy Whitcroft653d4872007-06-23 17:16:34 -07003678# check for new typedefs, only function parameters and sparse annotations
3679# make sense.
3680 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003681 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003682 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003683 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003684 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003685 WARN("NEW_TYPEDEFS",
3686 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003687 }
3688
3689# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003690 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003691 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3692 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003693 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003694
Andy Whitcroft65863862009-01-06 14:41:21 -08003695 # Should start with a space.
3696 $to =~ s/^(\S)/ $1/;
3697 # Should not end with a space.
3698 $to =~ s/\s+$//;
3699 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003700 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003701 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003702
Joe Perches3705ce52013-07-03 15:05:31 -07003703## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003704 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003705 if (ERROR("POINTER_LOCATION",
3706 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3707 $fix) {
3708 my $sub_from = $ident;
3709 my $sub_to = $ident;
3710 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003711 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003712 s@\Q$sub_from\E@$sub_to@;
3713 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003714 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003715 }
3716 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3717 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003718 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003719
Andy Whitcroft65863862009-01-06 14:41:21 -08003720 # Should start with a space.
3721 $to =~ s/^(\S)/ $1/;
3722 # Should not end with a space.
3723 $to =~ s/\s+$//;
3724 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003725 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003726 }
3727 # Modifiers should have spaces.
3728 $to =~ s/(\b$Modifier$)/$1 /;
3729
Joe Perches3705ce52013-07-03 15:05:31 -07003730## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003731 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003732 if (ERROR("POINTER_LOCATION",
3733 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3734 $fix) {
3735
3736 my $sub_from = $match;
3737 my $sub_to = $match;
3738 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003739 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003740 s@\Q$sub_from\E@$sub_to@;
3741 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003742 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003743 }
3744
Joe Perches9d3e3c72015-09-09 15:37:27 -07003745# avoid BUG() or BUG_ON()
3746 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3747 my $msg_type = \&WARN;
3748 $msg_type = \&CHK if ($file);
3749 &{$msg_type}("AVOID_BUG",
3750 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3751 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003752
Joe Perches9d3e3c72015-09-09 15:37:27 -07003753# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003754 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003755 WARN("LINUX_VERSION_CODE",
3756 "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 -08003757 }
3758
Joe Perches17441222011-06-15 15:08:17 -07003759# check for uses of printk_ratelimit
3760 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003761 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003762 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003763 }
3764
Andy Whitcroft00df3442007-06-08 13:47:06 -07003765# printk should use KERN_* levels. Note that follow on printk's on the
3766# same line do not need a level, so we use the current block context
3767# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003768# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003769# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003770 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003771 my $ok = 0;
3772 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3773 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003774 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003775 # with "\n" ignore it, else it is to blame
3776 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3777 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3778 $ok = 1;
3779 }
3780 last;
3781 }
3782 }
3783 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003784 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3785 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003786 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003787 }
3788
Joe Perches243f3802012-05-31 16:26:09 -07003789 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3790 my $orig = $1;
3791 my $level = lc($orig);
3792 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003793 my $level2 = $level;
3794 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003795 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003796 "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 -07003797 }
3798
3799 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003800 if (WARN("PREFER_PR_LEVEL",
3801 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3802 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003803 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003804 s/\bpr_warning\b/pr_warn/;
3805 }
Joe Perches243f3802012-05-31 16:26:09 -07003806 }
3807
Joe Perchesdc139312013-02-21 16:44:13 -08003808 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3809 my $orig = $1;
3810 my $level = lc($orig);
3811 $level = "warn" if ($level eq "warning");
3812 $level = "dbg" if ($level eq "debug");
3813 WARN("PREFER_DEV_LEVEL",
3814 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3815 }
3816
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003817# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3818# number of false positives, but assembly files are not checked, so at
3819# least the arch entry code will not trigger this warning.
3820 if ($line =~ /\bENOSYS\b/) {
3821 WARN("ENOSYS",
3822 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3823 }
3824
Andy Whitcroft653d4872007-06-23 17:16:34 -07003825# function brace can't be on same line, except for #defines of do while,
3826# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003827 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003828 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003829 if (ERROR("OPEN_BRACE",
3830 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3831 $fix) {
3832 fix_delete_line($fixlinenr, $rawline);
3833 my $fixed_line = $rawline;
3834 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3835 my $line1 = $1;
3836 my $line2 = $2;
3837 fix_insert_line($fixlinenr, ltrim($line1));
3838 fix_insert_line($fixlinenr, "\+{");
3839 if ($line2 !~ /^\s*$/) {
3840 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3841 }
3842 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003843 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003844
Andy Whitcroft8905a672007-11-28 16:21:06 -08003845# open braces for enum, union and struct go on the same line.
3846 if ($line =~ /^.\s*{/ &&
3847 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003848 if (ERROR("OPEN_BRACE",
3849 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3850 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3851 fix_delete_line($fixlinenr - 1, $prevrawline);
3852 fix_delete_line($fixlinenr, $rawline);
3853 my $fixedline = rtrim($prevrawline) . " {";
3854 fix_insert_line($fixlinenr, $fixedline);
3855 $fixedline = $rawline;
3856 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3857 if ($fixedline !~ /^\+\s*$/) {
3858 fix_insert_line($fixlinenr, $fixedline);
3859 }
3860 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003861 }
3862
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003863# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003864 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3865 if (WARN("SPACING",
3866 "missing space after $1 definition\n" . $herecurr) &&
3867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003868 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003869 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3870 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003871 }
3872
Joe Perches31070b52014-01-23 15:54:49 -08003873# Function pointer declarations
3874# check spacing between type, funcptr, and args
3875# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003876 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003877 my $declare = $1;
3878 my $pre_pointer_space = $2;
3879 my $post_pointer_space = $3;
3880 my $funcname = $4;
3881 my $post_funcname_space = $5;
3882 my $pre_args_space = $6;
3883
Joe Perches91f72e92014-04-03 14:49:12 -07003884# the $Declare variable will capture all spaces after the type
3885# so check it for a missing trailing missing space but pointer return types
3886# don't need a space so don't warn for those.
3887 my $post_declare_space = "";
3888 if ($declare =~ /(\s+)$/) {
3889 $post_declare_space = $1;
3890 $declare = rtrim($declare);
3891 }
3892 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003893 WARN("SPACING",
3894 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003895 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003896 }
3897
3898# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003899# This test is not currently implemented because these declarations are
3900# equivalent to
3901# int foo(int bar, ...)
3902# and this is form shouldn't/doesn't generate a checkpatch warning.
3903#
3904# elsif ($declare =~ /\s{2,}$/) {
3905# WARN("SPACING",
3906# "Multiple spaces after return type\n" . $herecurr);
3907# }
Joe Perches31070b52014-01-23 15:54:49 -08003908
3909# unnecessary space "type ( *funcptr)(args...)"
3910 if (defined $pre_pointer_space &&
3911 $pre_pointer_space =~ /^\s/) {
3912 WARN("SPACING",
3913 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3914 }
3915
3916# unnecessary space "type (* funcptr)(args...)"
3917 if (defined $post_pointer_space &&
3918 $post_pointer_space =~ /^\s/) {
3919 WARN("SPACING",
3920 "Unnecessary space before function pointer name\n" . $herecurr);
3921 }
3922
3923# unnecessary space "type (*funcptr )(args...)"
3924 if (defined $post_funcname_space &&
3925 $post_funcname_space =~ /^\s/) {
3926 WARN("SPACING",
3927 "Unnecessary space after function pointer name\n" . $herecurr);
3928 }
3929
3930# unnecessary space "type (*funcptr) (args...)"
3931 if (defined $pre_args_space &&
3932 $pre_args_space =~ /^\s/) {
3933 WARN("SPACING",
3934 "Unnecessary space before function pointer arguments\n" . $herecurr);
3935 }
3936
3937 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003938 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003939 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003940 }
3941 }
3942
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003943# check for spacing round square brackets; allowed:
3944# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003945# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3946# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003947 while ($line =~ /(.*?\s)\[/g) {
3948 my ($where, $prefix) = ($-[1], $1);
3949 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003950 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003951 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003952 if (ERROR("BRACKET_SPACE",
3953 "space prohibited before open square bracket '['\n" . $herecurr) &&
3954 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003955 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003956 s/^(\+.*?)\s+\[/$1\[/;
3957 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003958 }
3959 }
3960
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003961# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003962 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003963 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003964 my $ctx_before = substr($line, 0, $-[1]);
3965 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003966
3967 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003968 if ($name =~ /^(?:
3969 if|for|while|switch|return|case|
3970 volatile|__volatile__|
3971 __attribute__|format|__extension__|
3972 asm|__asm__)$/x)
3973 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003974 # cpp #define statements have non-optional spaces, ie
3975 # if there is a space between the name and the open
3976 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003977 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003978
3979 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003980 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003981
3982 # If this whole things ends with a type its most
3983 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003984 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003985
3986 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003987 if (WARN("SPACING",
3988 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3989 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003990 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003991 s/\b$name\s+\(/$name\(/;
3992 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003993 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003994 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003995
Andy Whitcroft653d4872007-06-23 17:16:34 -07003996# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003997 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003998 my $fixed_line = "";
3999 my $line_fixed = 0;
4000
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004001 my $ops = qr{
4002 <<=|>>=|<=|>=|==|!=|
4003 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4004 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004005 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004006 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004007 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004008 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004009
4010## print("element count: <" . $#elements . ">\n");
4011## foreach my $el (@elements) {
4012## print("el: <$el>\n");
4013## }
4014
4015 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004016 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004017
Joe Perches3705ce52013-07-03 15:05:31 -07004018 foreach my $el (@elements) {
4019 push(@fix_elements, substr($rawline, $off, length($el)));
4020 $off += length($el);
4021 }
4022
4023 $off = 0;
4024
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004025 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004026 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004027
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004028 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004029
4030 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4031
4032## print("n: <$n> good: <$good>\n");
4033
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004034 $off += length($elements[$n]);
4035
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004036 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004037 my $ca = substr($opline, 0, $off);
4038 my $cc = '';
4039 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4040 $cc = substr($opline, $off + length($elements[$n + 1]));
4041 }
4042 my $cb = "$ca$;$cc";
4043
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004044 my $a = '';
4045 $a = 'V' if ($elements[$n] ne '');
4046 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004047 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004048 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4049 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004050 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004051
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004052 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004053
4054 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004055 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004056 $c = 'V' if ($elements[$n + 2] ne '');
4057 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004058 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004059 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4060 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004061 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004062 } else {
4063 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004064 }
4065
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004066 my $ctx = "${a}x${c}";
4067
4068 my $at = "(ctx:$ctx)";
4069
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004070 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004071 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004072
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004073 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004074 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004075
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004076 # Get the full operator variant.
4077 my $opv = $op . substr($curr_vars, $off, 1);
4078
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004079 # Ignore operators passed as parameters.
4080 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004081 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004082
Andy Whitcroftcf655042008-03-04 14:28:20 -08004083# # Ignore comments
4084# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004085
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004086 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004087 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004088 if ($ctx !~ /.x[WEBC]/ &&
4089 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004090 if (ERROR("SPACING",
4091 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004092 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004093 $line_fixed = 1;
4094 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004095 }
4096
4097 # // is a comment
4098 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004099
Joe Perchesb00e4812014-04-03 14:49:33 -07004100 # : when part of a bitfield
4101 } elsif ($opv eq ':B') {
4102 # skip the bitfield test for now
4103
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004104 # No spaces for:
4105 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004106 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004107 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004108 if (ERROR("SPACING",
4109 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004110 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004111 if (defined $fix_elements[$n + 2]) {
4112 $fix_elements[$n + 2] =~ s/^\s+//;
4113 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004114 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004115 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004116 }
4117
Joe Perches23810972014-12-10 15:51:32 -08004118 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004119 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004120 my $rtrim_before = 0;
4121 my $space_after = 0;
4122 if ($ctx =~ /Wx./) {
4123 if (ERROR("SPACING",
4124 "space prohibited before that '$op' $at\n" . $hereptr)) {
4125 $line_fixed = 1;
4126 $rtrim_before = 1;
4127 }
4128 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004129 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004130 if (ERROR("SPACING",
4131 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004132 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004133 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004134 $space_after = 1;
4135 }
4136 }
4137 if ($rtrim_before || $space_after) {
4138 if ($rtrim_before) {
4139 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4140 } else {
4141 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4142 }
4143 if ($space_after) {
4144 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004145 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004146 }
4147
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004148 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004149 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004150 #warn "'*' is part of type\n";
4151
4152 # unary operators should have a space before and
4153 # none after. May be left adjacent to another
4154 # unary operator, or a cast
4155 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004156 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004157 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004158 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004159 if (ERROR("SPACING",
4160 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004161 if ($n != $last_after + 2) {
4162 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4163 $line_fixed = 1;
4164 }
Joe Perches3705ce52013-07-03 15:05:31 -07004165 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004166 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004167 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004168 # A unary '*' may be const
4169
4170 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004171 if (ERROR("SPACING",
4172 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004173 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004174 if (defined $fix_elements[$n + 2]) {
4175 $fix_elements[$n + 2] =~ s/^\s+//;
4176 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004177 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004178 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004179 }
4180
4181 # unary ++ and unary -- are allowed no space on one side.
4182 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004183 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004184 if (ERROR("SPACING",
4185 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004186 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004187 $line_fixed = 1;
4188 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004189 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004190 if ($ctx =~ /Wx[BE]/ ||
4191 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004192 if (ERROR("SPACING",
4193 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004194 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004195 $line_fixed = 1;
4196 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004197 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004198 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004199 if (ERROR("SPACING",
4200 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004201 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004202 if (defined $fix_elements[$n + 2]) {
4203 $fix_elements[$n + 2] =~ s/^\s+//;
4204 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004205 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004206 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004207 }
4208
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004209 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004210 } elsif ($op eq '<<' or $op eq '>>' or
4211 $op eq '&' or $op eq '^' or $op eq '|' or
4212 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004213 $op eq '*' or $op eq '/' or
4214 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004215 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004216 if ($check) {
4217 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4218 if (CHK("SPACING",
4219 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4220 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4221 $fix_elements[$n + 2] =~ s/^\s+//;
4222 $line_fixed = 1;
4223 }
4224 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4225 if (CHK("SPACING",
4226 "space preferred before that '$op' $at\n" . $hereptr)) {
4227 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4228 $line_fixed = 1;
4229 }
4230 }
4231 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004232 if (ERROR("SPACING",
4233 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004234 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4235 if (defined $fix_elements[$n + 2]) {
4236 $fix_elements[$n + 2] =~ s/^\s+//;
4237 }
Joe Perches3705ce52013-07-03 15:05:31 -07004238 $line_fixed = 1;
4239 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004240 }
4241
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004242 # A colon needs no spaces before when it is
4243 # terminating a case value or a label.
4244 } elsif ($opv eq ':C' || $opv eq ':L') {
4245 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004246 if (ERROR("SPACING",
4247 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004248 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004249 $line_fixed = 1;
4250 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004251 }
4252
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004253 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004254 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004255 my $ok = 0;
4256
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004257 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004258 if (($op eq '<' &&
4259 $cc =~ /^\S+\@\S+>/) ||
4260 ($op eq '>' &&
4261 $ca =~ /<\S+\@\S+$/))
4262 {
4263 $ok = 1;
4264 }
4265
Joe Perchese0df7e12015-04-16 12:44:53 -07004266 # for asm volatile statements
4267 # ignore a colon with another
4268 # colon immediately before or after
4269 if (($op eq ':') &&
4270 ($ca =~ /:$/ || $cc =~ /^:/)) {
4271 $ok = 1;
4272 }
4273
Joe Perches84731622013-11-12 15:10:05 -08004274 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004275 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004276 my $msg_type = \&ERROR;
4277 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4278
4279 if (&{$msg_type}("SPACING",
4280 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004281 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4282 if (defined $fix_elements[$n + 2]) {
4283 $fix_elements[$n + 2] =~ s/^\s+//;
4284 }
Joe Perches3705ce52013-07-03 15:05:31 -07004285 $line_fixed = 1;
4286 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004287 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004288 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004289 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004290
4291## print("n: <$n> GOOD: <$good>\n");
4292
4293 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004294 }
Joe Perches3705ce52013-07-03 15:05:31 -07004295
4296 if (($#elements % 2) == 0) {
4297 $fixed_line = $fixed_line . $fix_elements[$#elements];
4298 }
4299
Joe Perches194f66f2014-08-06 16:11:03 -07004300 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4301 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004302 }
4303
4304
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004305 }
4306
Joe Perches786b6322013-07-03 15:05:32 -07004307# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004308 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004309 if (WARN("SPACING",
4310 "space prohibited before semicolon\n" . $herecurr) &&
4311 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004312 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004313 s/^(\+.*\S)\s+;/$1;/;
4314 }
4315 }
4316
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004317# check for multiple assignments
4318 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004319 CHK("MULTIPLE_ASSIGNMENTS",
4320 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004321 }
4322
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004323## # check for multiple declarations, allowing for a function declaration
4324## # continuation.
4325## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4326## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4327##
4328## # Remove any bracketed sections to ensure we do not
4329## # falsly report the parameters of functions.
4330## my $ln = $line;
4331## while ($ln =~ s/\([^\(\)]*\)//g) {
4332## }
4333## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004334## WARN("MULTIPLE_DECLARATION",
4335## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004336## }
4337## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004338
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004339#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004340 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004341 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004342 if (ERROR("SPACING",
4343 "space required before the open brace '{'\n" . $herecurr) &&
4344 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004345 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004346 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004347 }
4348
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004349## # check for blank lines before declarations
4350## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4351## $prevrawline =~ /^.\s*$/) {
4352## WARN("SPACING",
4353## "No blank lines before declarations\n" . $hereprev);
4354## }
4355##
4356
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004357# closing brace should have a space following it when it has anything
4358# on the line
4359 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004360 if (ERROR("SPACING",
4361 "space required after that close brace '}'\n" . $herecurr) &&
4362 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004363 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004364 s/}((?!(?:,|;|\)))\S)/} $1/;
4365 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004366 }
4367
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004368# check spacing on square brackets
4369 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004370 if (ERROR("SPACING",
4371 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4372 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004373 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004374 s/\[\s+/\[/;
4375 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004376 }
4377 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004378 if (ERROR("SPACING",
4379 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4380 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004381 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004382 s/\s+\]/\]/;
4383 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004384 }
4385
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004386# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004387 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004388 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004389 if (ERROR("SPACING",
4390 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4391 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004392 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004393 s/\(\s+/\(/;
4394 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004395 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004396 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004397 $line !~ /for\s*\(.*;\s+\)/ &&
4398 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004399 if (ERROR("SPACING",
4400 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4401 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004402 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004403 s/\s+\)/\)/;
4404 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004405 }
4406
Joe Perchese2826fd2014-08-06 16:10:48 -07004407# check unnecessary parentheses around addressof/dereference single $Lvals
4408# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4409
4410 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004411 my $var = $1;
4412 if (CHK("UNNECESSARY_PARENTHESES",
4413 "Unnecessary parentheses around $var\n" . $herecurr) &&
4414 $fix) {
4415 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4416 }
4417 }
4418
4419# check for unnecessary parentheses around function pointer uses
4420# ie: (foo->bar)(); should be foo->bar();
4421# but not "if (foo->bar) (" to avoid some false positives
4422 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4423 my $var = $2;
4424 if (CHK("UNNECESSARY_PARENTHESES",
4425 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4426 $fix) {
4427 my $var2 = deparenthesize($var);
4428 $var2 =~ s/\s//g;
4429 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4430 }
4431 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004432
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004433#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004434 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004435 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004436 if (WARN("INDENTED_LABEL",
4437 "labels should not be indented\n" . $herecurr) &&
4438 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004439 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004440 s/^(.)\s+/$1/;
4441 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004442 }
4443
Joe Perches5b9553a2014-04-03 14:49:21 -07004444# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004445 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004446 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004447 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004448 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4449 my $value = $1;
4450 $value = deparenthesize($value);
4451 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4452 ERROR("RETURN_PARENTHESES",
4453 "return is not a function, parentheses are not required\n" . $herecurr);
4454 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004455 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004456 ERROR("SPACING",
4457 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004458 }
4459 }
Joe Perches507e5142013-11-12 15:10:13 -08004460
Joe Perchesb43ae212014-06-23 13:22:07 -07004461# unnecessary return in a void function
4462# at end-of-function, with the previous line a single leading tab, then return;
4463# and the line before that not a goto label target like "out:"
4464 if ($sline =~ /^[ \+]}\s*$/ &&
4465 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4466 $linenr >= 3 &&
4467 $lines[$linenr - 3] =~ /^[ +]/ &&
4468 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004469 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004470 "void function return statements are not generally useful\n" . $hereprev);
4471 }
Joe Perches9819cf22014-06-04 16:12:09 -07004472
Joe Perches189248d2014-01-23 15:54:47 -08004473# if statements using unnecessary parentheses - ie: if ((foo == bar))
4474 if ($^V && $^V ge 5.10.0 &&
4475 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4476 my $openparens = $1;
4477 my $count = $openparens =~ tr@\(@\(@;
4478 my $msg = "";
4479 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4480 my $comp = $4; #Not $1 because of $LvalOrFunc
4481 $msg = " - maybe == should be = ?" if ($comp eq "==");
4482 WARN("UNNECESSARY_PARENTHESES",
4483 "Unnecessary parentheses$msg\n" . $herecurr);
4484 }
4485 }
4486
Joe Perchesc5595fa2015-09-09 15:37:58 -07004487# comparisons with a constant or upper case identifier on the left
4488# avoid cases like "foo + BAR < baz"
4489# only fix matches surrounded by parentheses to avoid incorrect
4490# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4491 if ($^V && $^V ge 5.10.0 &&
4492 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4493 my $lead = $1;
4494 my $const = $2;
4495 my $comp = $3;
4496 my $to = $4;
4497 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004498 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004499 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4500 WARN("CONSTANT_COMPARISON",
4501 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4502 $fix) {
4503 if ($comp eq "<") {
4504 $newcomp = ">";
4505 } elsif ($comp eq "<=") {
4506 $newcomp = ">=";
4507 } elsif ($comp eq ">") {
4508 $newcomp = "<";
4509 } elsif ($comp eq ">=") {
4510 $newcomp = "<=";
4511 }
4512 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4513 }
4514 }
4515
Joe Perchesf34e4a42015-04-16 12:44:19 -07004516# Return of what appears to be an errno should normally be negative
4517 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004518 my $name = $1;
4519 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004520 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004521 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004522 }
4523 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004524
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004525# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004526 if ($line =~ /\b(if|while|for|switch)\(/) {
4527 if (ERROR("SPACING",
4528 "space required before the open parenthesis '('\n" . $herecurr) &&
4529 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004530 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004531 s/\b(if|while|for|switch)\(/$1 \(/;
4532 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004533 }
4534
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004535# Check for illegal assignment in if conditional -- and check for trailing
4536# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004537 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004538 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4539 ctx_statement_block($linenr, $realcnt, 0)
4540 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004541 my ($stat_next) = ctx_statement_block($line_nr_next,
4542 $remain_next, $off_next);
4543 $stat_next =~ s/\n./\n /g;
4544 ##print "stat<$stat> stat_next<$stat_next>\n";
4545
4546 if ($stat_next =~ /^\s*while\b/) {
4547 # If the statement carries leading newlines,
4548 # then count those as offsets.
4549 my ($whitespace) =
4550 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4551 my $offset =
4552 statement_rawlines($whitespace) - 1;
4553
4554 $suppress_whiletrailers{$line_nr_next +
4555 $offset} = 1;
4556 }
4557 }
4558 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004559 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004560 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004561 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004562
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004563 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004564 ERROR("ASSIGN_IN_IF",
4565 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004566 }
4567
4568 # Find out what is on the end of the line after the
4569 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004570 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004571 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004572 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004573 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4574 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004575 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004576 # Find out how long the conditional actually is.
4577 my @newlines = ($c =~ /\n/gs);
4578 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004579 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004580
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004581 $stat_real = raw_line($linenr, $cond_lines)
4582 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004583 if (defined($stat_real) && $cond_lines > 1) {
4584 $stat_real = "[...]\n$stat_real";
4585 }
4586
Joe Perches000d1cc12011-07-25 17:13:25 -07004587 ERROR("TRAILING_STATEMENTS",
4588 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004589 }
4590 }
4591
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004592# Check for bitwise tests written as boolean
4593 if ($line =~ /
4594 (?:
4595 (?:\[|\(|\&\&|\|\|)
4596 \s*0[xX][0-9]+\s*
4597 (?:\&\&|\|\|)
4598 |
4599 (?:\&\&|\|\|)
4600 \s*0[xX][0-9]+\s*
4601 (?:\&\&|\|\||\)|\])
4602 )/x)
4603 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004604 WARN("HEXADECIMAL_BOOLEAN_TEST",
4605 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004606 }
4607
Andy Whitcroft8905a672007-11-28 16:21:06 -08004608# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004609 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4610 my $s = $1;
4611 $s =~ s/$;//g; # Remove any comments
4612 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004613 ERROR("TRAILING_STATEMENTS",
4614 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004615 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004616 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004617# if should not continue a brace
4618 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004619 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004620 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004621 $herecurr);
4622 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004623# case and default should not have general statements after them
4624 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4625 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004626 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004627 \s*return\s+
4628 )/xg)
4629 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004630 ERROR("TRAILING_STATEMENTS",
4631 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004632 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004633
4634 # Check for }<nl>else {, these must be at the same
4635 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004636 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4637 $previndent == $indent) {
4638 if (ERROR("ELSE_AFTER_BRACE",
4639 "else should follow close brace '}'\n" . $hereprev) &&
4640 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4641 fix_delete_line($fixlinenr - 1, $prevrawline);
4642 fix_delete_line($fixlinenr, $rawline);
4643 my $fixedline = $prevrawline;
4644 $fixedline =~ s/}\s*$//;
4645 if ($fixedline !~ /^\+\s*$/) {
4646 fix_insert_line($fixlinenr, $fixedline);
4647 }
4648 $fixedline = $rawline;
4649 $fixedline =~ s/^(.\s*)else/$1} else/;
4650 fix_insert_line($fixlinenr, $fixedline);
4651 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004652 }
4653
Joe Perches8b8856f2014-08-06 16:11:14 -07004654 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4655 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004656 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4657
4658 # Find out what is on the end of the line after the
4659 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004660 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004661 $s =~ s/\n.*//g;
4662
4663 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004664 if (ERROR("WHILE_AFTER_BRACE",
4665 "while should follow close brace '}'\n" . $hereprev) &&
4666 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4667 fix_delete_line($fixlinenr - 1, $prevrawline);
4668 fix_delete_line($fixlinenr, $rawline);
4669 my $fixedline = $prevrawline;
4670 my $trailing = $rawline;
4671 $trailing =~ s/^\+//;
4672 $trailing = trim($trailing);
4673 $fixedline =~ s/}\s*$/} $trailing/;
4674 fix_insert_line($fixlinenr, $fixedline);
4675 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004676 }
4677 }
4678
Joe Perches95e2c602013-07-03 15:05:20 -07004679#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004680 while ($line =~ m{($Constant|$Lval)}g) {
4681 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004682
4683#gcc binary extension
4684 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004685 if (WARN("GCC_BINARY_CONSTANT",
4686 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4687 $fix) {
4688 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004689 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004690 s/\b$var\b/$hexval/;
4691 }
Joe Perches95e2c602013-07-03 15:05:20 -07004692 }
4693
4694#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004695 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004696 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004697#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004698 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004699#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004700 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4701#Ignore some three character SI units explicitly, like MiB and KHz
4702 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004703 while ($var =~ m{($Ident)}g) {
4704 my $word = $1;
4705 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004706 if ($check) {
4707 seed_camelcase_includes();
4708 if (!$file && !$camelcase_file_seeded) {
4709 seed_camelcase_file($realfile);
4710 $camelcase_file_seeded = 1;
4711 }
4712 }
Joe Perches7e781f62013-09-11 14:23:55 -07004713 if (!defined $camelcase{$word}) {
4714 $camelcase{$word} = 1;
4715 CHK("CAMELCASE",
4716 "Avoid CamelCase: <$word>\n" . $herecurr);
4717 }
Joe Perches34456862013-07-03 15:05:34 -07004718 }
Joe Perches323c1262012-12-17 16:02:07 -08004719 }
4720 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004721
4722#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004723 if ($line =~ /\#\s*define.*\\\s+$/) {
4724 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4725 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4726 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004727 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004728 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004729 }
4730
Fabian Frederick0e212e02015-04-16 12:44:25 -07004731# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4732# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004733 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004734 my $file = "$1.h";
4735 my $checkfile = "include/linux/$file";
4736 if (-f "$root/$checkfile" &&
4737 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004738 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004739 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004740 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4741 if ($asminclude > 0) {
4742 if ($realfile =~ m{^arch/}) {
4743 CHK("ARCH_INCLUDE_LINUX",
4744 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4745 } else {
4746 WARN("INCLUDE_LINUX",
4747 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4748 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004749 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004750 }
4751 }
4752
Andy Whitcroft653d4872007-06-23 17:16:34 -07004753# multi-statement macros should be enclosed in a do while loop, grab the
4754# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004755# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004756 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4757 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004758 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004759 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004760 my ($off, $dstat, $dcond, $rest);
4761 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004762 my $has_flow_statement = 0;
4763 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004764 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004765 ctx_statement_block($linenr, $realcnt, 0);
4766 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004767 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004768 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004769
Joe Perches08a28432014-10-13 15:51:55 -07004770 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004771 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004772
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004773 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004774 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004775 $dstat =~ s/\\\n.//g;
4776 $dstat =~ s/^\s*//s;
4777 $dstat =~ s/\s*$//s;
4778
4779 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004780 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4781 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004782 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004783 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004784 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004785
Gregory Bean02870be2011-05-11 09:11:12 -07004786 # Extremely long macros may fall off the end of the
4787 # available context without closing. Give a dangling
4788 # backslash the benefit of the doubt and allow it
4789 # to gobble any hanging open-parens.
4790 $dstat =~ s/\(.+\\$/1/;
4791
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004792 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004793 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4794 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004795 {
4796 }
4797
Joe Perches42e15292016-03-15 14:58:01 -07004798 # Make asm volatile uses seem like a generic function
4799 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4800
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004801 my $exceptions = qr{
4802 $Declare|
4803 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004804 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004805 DECLARE_PER_CPU|
4806 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004807 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004808 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004809 union|
4810 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004811 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004812 ^\"|\"$|
4813 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004814 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004815 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004816 if ($dstat ne '' &&
4817 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4818 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004819 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004820 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004821 $dstat !~ /$exceptions/ &&
4822 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004823 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004824 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004825 $dstat !~ /^for\s*$Constant$/ && # for (...)
4826 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4827 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004828 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004829 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004830 {
4831 $ctx =~ s/\n*$//;
4832 my $herectx = $here . "\n";
4833 my $cnt = statement_rawlines($ctx);
4834
4835 for (my $n = 0; $n < $cnt; $n++) {
4836 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004837 }
4838
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004839 if ($dstat =~ /;/) {
4840 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4841 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4842 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004843 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004844 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004845 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004846 }
Joe Perches5023d342012-12-17 16:01:47 -08004847
Joe Perches08a28432014-10-13 15:51:55 -07004848# check for macros with flow control, but without ## concatenation
4849# ## concatenation is commonly a macro that defines a function so ignore those
4850 if ($has_flow_statement && !$has_arg_concat) {
4851 my $herectx = $here . "\n";
4852 my $cnt = statement_rawlines($ctx);
4853
4854 for (my $n = 0; $n < $cnt; $n++) {
4855 $herectx .= raw_line($linenr, $n) . "\n";
4856 }
4857 WARN("MACRO_WITH_FLOW_CONTROL",
4858 "Macros with flow control statements should be avoided\n" . "$herectx");
4859 }
4860
Joe Perches481eb482012-12-17 16:01:56 -08004861# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004862
4863 } else {
4864 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004865 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4866 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004867 $line =~ /^\+.*\\$/) {
4868 WARN("LINE_CONTINUATIONS",
4869 "Avoid unnecessary line continuations\n" . $herecurr);
4870 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004871 }
4872
Joe Perchesb13edf72012-07-30 14:41:24 -07004873# do {} while (0) macro tests:
4874# single-statement macros do not need to be enclosed in do while (0) loop,
4875# macro should not end with a semicolon
4876 if ($^V && $^V ge 5.10.0 &&
4877 $realfile !~ m@/vmlinux.lds.h$@ &&
4878 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4879 my $ln = $linenr;
4880 my $cnt = $realcnt;
4881 my ($off, $dstat, $dcond, $rest);
4882 my $ctx = '';
4883 ($dstat, $dcond, $ln, $cnt, $off) =
4884 ctx_statement_block($linenr, $realcnt, 0);
4885 $ctx = $dstat;
4886
4887 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004888 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004889
4890 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4891 my $stmts = $2;
4892 my $semis = $3;
4893
4894 $ctx =~ s/\n*$//;
4895 my $cnt = statement_rawlines($ctx);
4896 my $herectx = $here . "\n";
4897
4898 for (my $n = 0; $n < $cnt; $n++) {
4899 $herectx .= raw_line($linenr, $n) . "\n";
4900 }
4901
Joe Perchesac8e97f2012-08-21 16:15:53 -07004902 if (($stmts =~ tr/;/;/) == 1 &&
4903 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004904 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4905 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4906 }
4907 if (defined $semis && $semis ne "") {
4908 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4909 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4910 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004911 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4912 $ctx =~ s/\n*$//;
4913 my $cnt = statement_rawlines($ctx);
4914 my $herectx = $here . "\n";
4915
4916 for (my $n = 0; $n < $cnt; $n++) {
4917 $herectx .= raw_line($linenr, $n) . "\n";
4918 }
4919
4920 WARN("TRAILING_SEMICOLON",
4921 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004922 }
4923 }
4924
Mike Frysinger080ba922009-01-06 14:41:25 -08004925# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4926# all assignments may have only one of the following with an assignment:
4927# .
4928# ALIGN(...)
4929# VMLINUX_SYMBOL(...)
4930 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004931 WARN("MISSING_VMLINUX_SYMBOL",
4932 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004933 }
4934
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004935# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004936 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4937 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004938 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004939 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004940 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4941 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004942 my @allowed = ();
4943 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004944 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004945 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004946 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004947 for my $chunk (@chunks) {
4948 my ($cond, $block) = @{$chunk};
4949
Andy Whitcroft773647a2008-03-28 14:15:58 -07004950 # If the condition carries leading newlines, then count those as offsets.
4951 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4952 my $offset = statement_rawlines($whitespace) - 1;
4953
Joe Perchesaad4f612012-03-23 15:02:19 -07004954 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004955 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4956
4957 # We have looked at and allowed this specific line.
4958 $suppress_ifbraces{$ln + $offset} = 1;
4959
4960 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004961 $ln += statement_rawlines($block) - 1;
4962
Andy Whitcroft773647a2008-03-28 14:15:58 -07004963 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004964
4965 $seen++ if ($block =~ /^\s*{/);
4966
Joe Perchesaad4f612012-03-23 15:02:19 -07004967 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004968 if (statement_lines($cond) > 1) {
4969 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004970 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004971 }
4972 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004973 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004974 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004975 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004976 if (statement_block_size($block) > 1) {
4977 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004978 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004979 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004980 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004981 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004982 if ($seen) {
4983 my $sum_allowed = 0;
4984 foreach (@allowed) {
4985 $sum_allowed += $_;
4986 }
4987 if ($sum_allowed == 0) {
4988 WARN("BRACES",
4989 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4990 } elsif ($sum_allowed != $allow &&
4991 $seen != $allow) {
4992 CHK("BRACES",
4993 "braces {} should be used on all arms of this statement\n" . $herectx);
4994 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004995 }
4996 }
4997 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004998 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004999 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005000 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005001
Andy Whitcroftcf655042008-03-04 14:28:20 -08005002 # Check the pre-context.
5003 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5004 #print "APW: ALLOWED: pre<$1>\n";
5005 $allowed = 1;
5006 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005007
5008 my ($level, $endln, @chunks) =
5009 ctx_statement_full($linenr, $realcnt, $-[0]);
5010
Andy Whitcroftcf655042008-03-04 14:28:20 -08005011 # Check the condition.
5012 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005013 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005014 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005015 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005016 }
5017 if (statement_lines($cond) > 1) {
5018 #print "APW: ALLOWED: cond<$cond>\n";
5019 $allowed = 1;
5020 }
5021 if ($block =~/\b(?:if|for|while)\b/) {
5022 #print "APW: ALLOWED: block<$block>\n";
5023 $allowed = 1;
5024 }
5025 if (statement_block_size($block) > 1) {
5026 #print "APW: ALLOWED: lines block<$block>\n";
5027 $allowed = 1;
5028 }
5029 # Check the post-context.
5030 if (defined $chunks[1]) {
5031 my ($cond, $block) = @{$chunks[1]};
5032 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005033 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005034 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005035 if ($block =~ /^\s*\{/) {
5036 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5037 $allowed = 1;
5038 }
5039 }
5040 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005041 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005042 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005043
Andy Whitcroftf0556632008-10-15 22:02:23 -07005044 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005045 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005046 }
5047
Joe Perches000d1cc12011-07-25 17:13:25 -07005048 WARN("BRACES",
5049 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005050 }
5051 }
5052
Joe Perches0979ae62012-12-17 16:01:59 -08005053# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005054 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005055 if (CHK("BRACES",
5056 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5057 $fix && $prevrawline =~ /^\+/) {
5058 fix_delete_line($fixlinenr - 1, $prevrawline);
5059 }
Joe Perches0979ae62012-12-17 16:01:59 -08005060 }
Joe Perches77b9a532013-07-03 15:05:29 -07005061 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005062 if (CHK("BRACES",
5063 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5064 $fix) {
5065 fix_delete_line($fixlinenr, $rawline);
5066 }
Joe Perches0979ae62012-12-17 16:01:59 -08005067 }
5068
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005069# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005070 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5071 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005072 WARN("VOLATILE",
5073 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005074 }
5075
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005076# Check for user-visible strings broken across lines, which breaks the ability
5077# to grep for the string. Make exceptions when the previous string ends in a
5078# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5079# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005080 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005081 $prevline =~ /"\s*$/ &&
5082 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5083 if (WARN("SPLIT_STRING",
5084 "quoted string split across lines\n" . $hereprev) &&
5085 $fix &&
5086 $prevrawline =~ /^\+.*"\s*$/ &&
5087 $last_coalesced_string_linenr != $linenr - 1) {
5088 my $extracted_string = get_quoted_string($line, $rawline);
5089 my $comma_close = "";
5090 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5091 $comma_close = $1;
5092 }
5093
5094 fix_delete_line($fixlinenr - 1, $prevrawline);
5095 fix_delete_line($fixlinenr, $rawline);
5096 my $fixedline = $prevrawline;
5097 $fixedline =~ s/"\s*$//;
5098 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5099 fix_insert_line($fixlinenr - 1, $fixedline);
5100 $fixedline = $rawline;
5101 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5102 if ($fixedline !~ /\+\s*$/) {
5103 fix_insert_line($fixlinenr, $fixedline);
5104 }
5105 $last_coalesced_string_linenr = $linenr;
5106 }
5107 }
5108
5109# check for missing a space in a string concatenation
5110 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5111 WARN('MISSING_SPACE',
5112 "break quoted strings at a space character\n" . $hereprev);
5113 }
5114
5115# check for spaces before a quoted newline
5116 if ($rawline =~ /^.*\".*\s\\n/) {
5117 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5118 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5119 $fix) {
5120 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5121 }
5122
5123 }
5124
Joe Perchesf17dba42014-10-13 15:51:51 -07005125# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005126 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005127 CHK("CONCATENATED_STRING",
5128 "Concatenated strings should use spaces between elements\n" . $herecurr);
5129 }
5130
Joe Perches90ad30e2014-12-10 15:51:59 -08005131# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005132 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005133 WARN("STRING_FRAGMENTS",
5134 "Consecutive strings are generally better as a single string\n" . $herecurr);
5135 }
5136
Joe Perches6e300752015-09-09 15:37:47 -07005137# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005138 my $string;
5139 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5140 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5141 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005142 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005143 WARN("PRINTF_L",
5144 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5145 last;
5146 }
Joe Perches6e300752015-09-09 15:37:47 -07005147 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5148 ERROR("PRINTF_0xDECIMAL",
5149 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5150 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005151 }
5152
5153# check for line continuations in quoted strings with odd counts of "
5154 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5155 WARN("LINE_CONTINUATIONS",
5156 "Avoid line continuations in quoted strings\n" . $herecurr);
5157 }
5158
Gregory Bean6c9c6842011-03-17 14:18:20 -07005159# sys_open/read/write/close are not allowed in the kernel
5160 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5161 ERROR("FILE_OPS",
5162 "$1 is inappropriate in kernel code.\n" .
5163 $herecurr);
5164 }
5165
Gregory Beana7cce0a2011-06-07 08:06:45 -07005166# filp_open is a backdoor for sys_open
5167 if ($line =~ /\b(filp_open)\b/) {
5168 ERROR("FILE_OPS",
5169 "$1 is inappropriate in kernel code.\n" .
5170 $herecurr);
5171 }
5172
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005173# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5174 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5175 ERROR("NON_RELAXED_IO",
5176 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5177 "with appropriate memory barriers instead.\n" .
5178 $herecurr);
5179 }
5180
5181# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5182 if ($line =~ /\b((in|out)([bwl]))\b/) {
5183 my ($all, $pref, $suf) = ($1, $2, $3);
5184 $pref =~ s/in/read/;
5185 $pref =~ s/out/write/;
5186 ERROR("NON_RELAXED_IO",
5187 "Use of $all is deprecated: use " .
5188 "__raw_$pref$suf\n\t" .
5189 "with appropriate memory barriers instead.\n" .
5190 $herecurr);
5191 }
5192
Gregory Bean438bf5f2011-06-15 09:32:38 -07005193# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005194 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005195 WARN("ARM_BARRIER",
5196 "Use of dsb is discouranged: prefer mb.\n" .
5197 $herecurr);
5198 }
5199
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005200# unbounded string functions are overflow risks
5201 my %str_fns = (
5202 "sprintf" => "snprintf",
5203 "strcpy" => "strlcpy",
5204 "strncpy" => "strlcpy",
5205 "strcat" => "strlcat",
5206 "strncat" => "strlcat",
5207 "vsprintf" => "vsnprintf",
5208 "strchr" => "strnchr",
5209 "strstr" => "strnstr",
5210 );
5211 foreach my $k (keys %str_fns) {
5212 if ($line =~ /\b$k\b/) {
5213 ERROR("UNBOUNDED_STRING_FNS",
5214 "Use of $k is deprecated: " .
5215 "use $str_fns{$k} instead.\n" .
5216 $herecurr);
5217 }
5218 }
5219
Andy Whitcroft00df3442007-06-08 13:47:06 -07005220# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005221 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005222 WARN("IF_0",
5223 "if this code is redundant consider removing it\n"
5224 . $herecurr);
5225 }
5226
5227# warn about #if 1
5228 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5229 WARN("IF_1",
5230 "if this code is required consider removing"
5231 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005232 }
5233
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005234# check for needless "if (<foo>) fn(<foo>)" uses
5235 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005236 my $tested = quotemeta($1);
5237 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5238 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5239 my $func = $1;
5240 if (WARN('NEEDLESS_IF',
5241 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5242 $fix) {
5243 my $do_fix = 1;
5244 my $leading_tabs = "";
5245 my $new_leading_tabs = "";
5246 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5247 $leading_tabs = $1;
5248 } else {
5249 $do_fix = 0;
5250 }
5251 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5252 $new_leading_tabs = $1;
5253 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5254 $do_fix = 0;
5255 }
5256 } else {
5257 $do_fix = 0;
5258 }
5259 if ($do_fix) {
5260 fix_delete_line($fixlinenr - 1, $prevrawline);
5261 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5262 }
5263 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005264 }
5265 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005266
Joe Perchesebfdc402014-08-06 16:10:27 -07005267# check for unnecessary "Out of Memory" messages
5268 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5269 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5270 (defined $1 || defined $3) &&
5271 $linenr > 3) {
5272 my $testval = $2;
5273 my $testline = $lines[$linenr - 3];
5274
5275 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5276# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5277
5278 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5279 WARN("OOM_MESSAGE",
5280 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5281 }
5282 }
5283
Joe Perchesf78d98f2014-10-13 15:52:01 -07005284# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005285 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005286 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5287 my $level = $1;
5288 if (WARN("UNNECESSARY_KERN_LEVEL",
5289 "Possible unnecessary $level\n" . $herecurr) &&
5290 $fix) {
5291 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5292 }
5293 }
5294
Joe Perchesabb08a52014-12-10 15:51:46 -08005295# check for mask then right shift without a parentheses
5296 if ($^V && $^V ge 5.10.0 &&
5297 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5298 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5299 WARN("MASK_THEN_SHIFT",
5300 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5301 }
5302
Joe Perchesb75ac612014-12-10 15:52:02 -08005303# check for pointer comparisons to NULL
5304 if ($^V && $^V ge 5.10.0) {
5305 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5306 my $val = $1;
5307 my $equal = "!";
5308 $equal = "" if ($4 eq "!=");
5309 if (CHK("COMPARISON_TO_NULL",
5310 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5311 $fix) {
5312 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5313 }
5314 }
5315 }
5316
Joe Perches8716de32013-09-11 14:24:05 -07005317# check for bad placement of section $InitAttribute (e.g.: __initdata)
5318 if ($line =~ /(\b$InitAttribute\b)/) {
5319 my $attr = $1;
5320 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5321 my $ptr = $1;
5322 my $var = $2;
5323 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5324 ERROR("MISPLACED_INIT",
5325 "$attr should be placed after $var\n" . $herecurr)) ||
5326 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5327 WARN("MISPLACED_INIT",
5328 "$attr should be placed after $var\n" . $herecurr))) &&
5329 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005330 $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 -07005331 }
5332 }
5333 }
5334
Joe Perchese970b882013-11-12 15:10:10 -08005335# check for $InitAttributeData (ie: __initdata) with const
5336 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5337 my $attr = $1;
5338 $attr =~ /($InitAttributePrefix)(.*)/;
5339 my $attr_prefix = $1;
5340 my $attr_type = $2;
5341 if (ERROR("INIT_ATTRIBUTE",
5342 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5343 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005344 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005345 s/$InitAttributeData/${attr_prefix}initconst/;
5346 }
5347 }
5348
5349# check for $InitAttributeConst (ie: __initconst) without const
5350 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5351 my $attr = $1;
5352 if (ERROR("INIT_ATTRIBUTE",
5353 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5354 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005355 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005356 /(^\+\s*(?:static\s+))/;
5357 $lead = rtrim($1);
5358 $lead = "$lead " if ($lead !~ /^\+$/);
5359 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005360 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005361 }
5362 }
5363
Joe Perchesc17893c2015-04-16 12:44:42 -07005364# check for __read_mostly with const non-pointer (should just be const)
5365 if ($line =~ /\b__read_mostly\b/ &&
5366 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5367 if (ERROR("CONST_READ_MOSTLY",
5368 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5369 $fix) {
5370 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5371 }
5372 }
5373
Joe Perchesfbdb8132014-04-03 14:49:14 -07005374# don't use __constant_<foo> functions outside of include/uapi/
5375 if ($realfile !~ m@^include/uapi/@ &&
5376 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5377 my $constant_func = $1;
5378 my $func = $constant_func;
5379 $func =~ s/^__constant_//;
5380 if (WARN("CONSTANT_CONVERSION",
5381 "$constant_func should be $func\n" . $herecurr) &&
5382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005383 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005384 }
5385 }
5386
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005387# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005388 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005389 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005390 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005391 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005392 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005393 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5394 }
5395 if ($delay > 2000) {
5396 WARN("LONG_UDELAY",
5397 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005398 }
5399 }
5400
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005401# warn about unexpectedly long msleep's
5402 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5403 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005404 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005405 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005406 }
5407 }
5408
Joe Perches36ec1932013-07-03 15:05:25 -07005409# check for comparisons of jiffies
5410 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5411 WARN("JIFFIES_COMPARISON",
5412 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5413 }
5414
Joe Perches9d7a34a2013-07-03 15:05:26 -07005415# check for comparisons of get_jiffies_64()
5416 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5417 WARN("JIFFIES_COMPARISON",
5418 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5419 }
5420
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005421# check the patch for use of mdelay
5422 if ($line =~ /\bmdelay\s*\(/) {
5423 WARN("MDELAY",
5424 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5425 }
5426
Andy Whitcroft00df3442007-06-08 13:47:06 -07005427# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005428# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005429# print "#ifdef in C files should be avoided\n";
5430# print "$herecurr";
5431# $clean = 0;
5432# }
5433
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005434# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005435 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005436 if (ERROR("SPACING",
5437 "exactly one space required after that #$1\n" . $herecurr) &&
5438 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005439 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005440 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5441 }
5442
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005443 }
5444
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005445# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005446 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5447 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005448 my $which = $1;
5449 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005450 CHK("UNCOMMENTED_DEFINITION",
5451 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005452 }
5453 }
5454# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005455
5456 my $barriers = qr{
5457 mb|
5458 rmb|
5459 wmb|
5460 read_barrier_depends
5461 }x;
5462 my $barrier_stems = qr{
5463 mb__before_atomic|
5464 mb__after_atomic|
5465 store_release|
5466 load_acquire|
5467 store_mb|
5468 (?:$barriers)
5469 }x;
5470 my $all_barriers = qr{
5471 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005472 smp_(?:$barrier_stems)|
5473 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005474 }x;
5475
5476 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005477 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005478 WARN("MEMORY_BARRIER",
5479 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005480 }
5481 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005482
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005483 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5484
5485 if ($realfile !~ m@^include/asm-generic/@ &&
5486 $realfile !~ m@/barrier\.h$@ &&
5487 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5488 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5489 WARN("MEMORY_BARRIER",
5490 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5491 }
5492
Joe Perchescb426e92015-06-25 15:02:46 -07005493# check for waitqueue_active without a comment.
5494 if ($line =~ /\bwaitqueue_active\s*\(/) {
5495 if (!ctx_has_comment($first_line, $linenr)) {
5496 WARN("WAITQUEUE_ACTIVE",
5497 "waitqueue_active without comment\n" . $herecurr);
5498 }
5499 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005500
5501# Check for expedited grace periods that interrupt non-idle non-nohz
5502# online CPUs. These expedited can therefore degrade real-time response
5503# if used carelessly, and should be avoided where not absolutely
5504# needed. It is always OK to use synchronize_rcu_expedited() and
5505# synchronize_sched_expedited() at boot time (before real-time applications
5506# start) and in error situations where real-time response is compromised in
5507# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5508# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5509# Of course, nothing comes for free, and srcu_read_lock() and
5510# srcu_read_unlock() do contain full memory barriers in payment for
5511# synchronize_srcu_expedited() non-interruption properties.
5512 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5513 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5514 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5515
5516 }
5517
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005518# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005519 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005520 CHK("ARCH_DEFINES",
5521 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005522 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005523
Tobias Klauserd4977c72010-05-24 14:33:30 -07005524# Check that the storage class is at the beginning of a declaration
5525 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005526 WARN("STORAGE_CLASS",
5527 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005528 }
5529
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005530# check the location of the inline attribute, that it is between
5531# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005532 if ($line =~ /\b$Type\s+$Inline\b/ ||
5533 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005534 ERROR("INLINE_LOCATION",
5535 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005536 }
5537
Andy Whitcroft8905a672007-11-28 16:21:06 -08005538# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005539 if ($realfile !~ m@\binclude/uapi/@ &&
5540 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005541 if (WARN("INLINE",
5542 "plain inline is preferred over $1\n" . $herecurr) &&
5543 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005544 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005545
5546 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005547 }
5548
Joe Perches3d130fd2011-01-12 17:00:00 -08005549# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005550 if ($realfile !~ m@\binclude/uapi/@ &&
5551 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005552 WARN("PREFER_PACKED",
5553 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005554 }
5555
Joe Perches39b7e282011-07-25 17:13:24 -07005556# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005557 if ($realfile !~ m@\binclude/uapi/@ &&
5558 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005559 WARN("PREFER_ALIGNED",
5560 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005561 }
5562
Joe Perches5f14d3b2012-01-10 15:09:52 -08005563# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005564 if ($realfile !~ m@\binclude/uapi/@ &&
5565 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005566 if (WARN("PREFER_PRINTF",
5567 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5568 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005569 $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 -07005570
5571 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005572 }
5573
Joe Perches6061d942012-03-23 15:02:16 -07005574# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005575 if ($realfile !~ m@\binclude/uapi/@ &&
5576 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005577 if (WARN("PREFER_SCANF",
5578 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005580 $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 -07005581 }
Joe Perches6061d942012-03-23 15:02:16 -07005582 }
5583
Joe Perches619a9082014-12-10 15:51:35 -08005584# Check for __attribute__ weak, or __weak declarations (may have link issues)
5585 if ($^V && $^V ge 5.10.0 &&
5586 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5587 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5588 $line =~ /\b__weak\b/)) {
5589 ERROR("WEAK_DECLARATION",
5590 "Using weak declarations can have unintended link defects\n" . $herecurr);
5591 }
5592
Joe Perchese6176fa2015-06-25 15:02:49 -07005593# check for c99 types like uint8_t used outside of uapi/
5594 if ($realfile !~ m@\binclude/uapi/@ &&
5595 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5596 my $type = $1;
5597 if ($type =~ /\b($typeC99Typedefs)\b/) {
5598 $type = $1;
5599 my $kernel_type = 'u';
5600 $kernel_type = 's' if ($type =~ /^_*[si]/);
5601 $type =~ /(\d+)/;
5602 $kernel_type .= $1;
5603 if (CHK("PREFER_KERNEL_TYPES",
5604 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5605 $fix) {
5606 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5607 }
5608 }
5609 }
5610
Joe Perches938224b2016-01-20 14:59:15 -08005611# check for cast of C90 native int or longer types constants
5612 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5613 my $cast = $1;
5614 my $const = $2;
5615 if (WARN("TYPECAST_INT_CONSTANT",
5616 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5617 $fix) {
5618 my $suffix = "";
5619 my $newconst = $const;
5620 $newconst =~ s/${Int_type}$//;
5621 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5622 if ($cast =~ /\blong\s+long\b/) {
5623 $suffix .= 'LL';
5624 } elsif ($cast =~ /\blong\b/) {
5625 $suffix .= 'L';
5626 }
5627 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5628 }
5629 }
5630
Joe Perches8f53a9b2010-03-05 13:43:48 -08005631# check for sizeof(&)
5632 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005633 WARN("SIZEOF_ADDRESS",
5634 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005635 }
5636
Joe Perches66c80b62012-07-30 14:41:22 -07005637# check for sizeof without parenthesis
5638 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005639 if (WARN("SIZEOF_PARENTHESIS",
5640 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5641 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005642 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005643 }
Joe Perches66c80b62012-07-30 14:41:22 -07005644 }
5645
Joe Perches88982fe2012-12-17 16:02:00 -08005646# check for struct spinlock declarations
5647 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5648 WARN("USE_SPINLOCK_T",
5649 "struct spinlock should be spinlock_t\n" . $herecurr);
5650 }
5651
Joe Perchesa6962d72013-04-29 16:18:13 -07005652# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005653 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005654 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005655 $fmt =~ s/%%//g;
5656 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005657 if (WARN("PREFER_SEQ_PUTS",
5658 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5659 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005660 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005661 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005662 }
5663 }
5664
Andy Whitcroft554e1652012-01-10 15:09:57 -08005665# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005666 if ($^V && $^V ge 5.10.0 &&
5667 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005668 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005669
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005670 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005671 my $ms_val = $7;
5672 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005673
Andy Whitcroft554e1652012-01-10 15:09:57 -08005674 if ($ms_size =~ /^(0x|)0$/i) {
5675 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005676 "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 -08005677 } elsif ($ms_size =~ /^(0x|)1$/i) {
5678 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005679 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5680 }
5681 }
5682
Joe Perches98a9bba2014-01-23 15:54:52 -08005683# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5684 if ($^V && $^V ge 5.10.0 &&
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005685 defined $stat &&
5686 $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
Joe Perches98a9bba2014-01-23 15:54:52 -08005687 if (WARN("PREFER_ETHER_ADDR_COPY",
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005688 "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 -08005689 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005690 $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 -08005691 }
5692 }
5693
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005694# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5695 if ($^V && $^V ge 5.10.0 &&
5696 defined $stat &&
5697 $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5698 WARN("PREFER_ETHER_ADDR_EQUAL",
5699 "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5700 }
5701
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005702# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5703# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5704 if ($^V && $^V ge 5.10.0 &&
5705 defined $stat &&
5706 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5707
5708 my $ms_val = $7;
5709
5710 if ($ms_val =~ /^(?:0x|)0+$/i) {
5711 if (WARN("PREFER_ETH_ZERO_ADDR",
5712 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5713 $fix) {
5714 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5715 }
5716 } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5717 if (WARN("PREFER_ETH_BROADCAST_ADDR",
5718 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5719 $fix) {
5720 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5721 }
5722 }
5723 }
5724
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005725# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005726 if ($^V && $^V ge 5.10.0 &&
5727 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005728 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005729 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005730 my $call = $1;
5731 my $cast1 = deparenthesize($2);
5732 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005733 my $cast2 = deparenthesize($7);
5734 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005735 my $cast;
5736
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005737 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005738 $cast = "$cast1 or $cast2";
5739 } elsif ($cast1 ne "") {
5740 $cast = $cast1;
5741 } else {
5742 $cast = $cast2;
5743 }
5744 WARN("MINMAX",
5745 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005746 }
5747 }
5748
Joe Perches4a273192012-07-30 14:41:20 -07005749# check usleep_range arguments
5750 if ($^V && $^V ge 5.10.0 &&
5751 defined $stat &&
5752 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5753 my $min = $1;
5754 my $max = $7;
5755 if ($min eq $max) {
5756 WARN("USLEEP_RANGE",
5757 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5758 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5759 $min > $max) {
5760 WARN("USLEEP_RANGE",
5761 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5762 }
5763 }
5764
Joe Perches823b7942013-11-12 15:10:15 -08005765# check for naked sscanf
5766 if ($^V && $^V ge 5.10.0 &&
5767 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005768 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005769 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5770 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5771 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5772 my $lc = $stat =~ tr@\n@@;
5773 $lc = $lc + $linenr;
5774 my $stat_real = raw_line($linenr, 0);
5775 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5776 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5777 }
5778 WARN("NAKED_SSCANF",
5779 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5780 }
5781
Joe Perchesafc819a2014-06-04 16:12:08 -07005782# check for simple sscanf that should be kstrto<foo>
5783 if ($^V && $^V ge 5.10.0 &&
5784 defined $stat &&
5785 $line =~ /\bsscanf\b/) {
5786 my $lc = $stat =~ tr@\n@@;
5787 $lc = $lc + $linenr;
5788 my $stat_real = raw_line($linenr, 0);
5789 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5790 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5791 }
5792 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5793 my $format = $6;
5794 my $count = $format =~ tr@%@%@;
5795 if ($count == 1 &&
5796 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5797 WARN("SSCANF_TO_KSTRTO",
5798 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5799 }
5800 }
5801 }
5802
Joe Perches70dc8a42013-09-11 14:23:58 -07005803# check for new externs in .h files.
5804 if ($realfile =~ /\.h$/ &&
5805 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005806 if (CHK("AVOID_EXTERNS",
5807 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005808 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005809 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005810 }
5811 }
5812
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005813# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005814 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005815 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005816 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005817 my $function_name = $1;
5818 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005819
5820 my $s = $stat;
5821 if (defined $cond) {
5822 substr($s, 0, length($cond), '');
5823 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005824 if ($s =~ /^\s*;/ &&
5825 $function_name ne 'uninitialized_var')
5826 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005827 WARN("AVOID_EXTERNS",
5828 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005829 }
5830
5831 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005832 WARN("FUNCTION_ARGUMENTS",
5833 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005834 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005835
5836 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5837 $stat =~ /^.\s*extern\s+/)
5838 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005839 WARN("AVOID_EXTERNS",
5840 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005841 }
5842
5843# checks for new __setup's
5844 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5845 my $name = $1;
5846
5847 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005848 CHK("UNDOCUMENTED_SETUP",
5849 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005850 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005851 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005852
5853# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005854 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005855 WARN("UNNECESSARY_CASTS",
5856 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005857 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005858
Joe Perchesa640d252013-07-03 15:05:21 -07005859# alloc style
5860# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5861 if ($^V && $^V ge 5.10.0 &&
5862 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5863 CHK("ALLOC_SIZEOF_STRUCT",
5864 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5865 }
5866
Joe Perches60a55362014-06-04 16:12:07 -07005867# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5868 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005869 $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 -07005870 my $oldfunc = $3;
5871 my $a1 = $4;
5872 my $a2 = $10;
5873 my $newfunc = "kmalloc_array";
5874 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005875 my $r1 = $a1;
5876 my $r2 = $a2;
5877 if ($a1 =~ /^sizeof\s*\S/) {
5878 $r1 = $a2;
5879 $r2 = $a1;
5880 }
5881 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5882 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005883 if (WARN("ALLOC_WITH_MULTIPLY",
5884 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5885 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005886 $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 -07005887
5888 }
5889 }
5890 }
5891
Joe Perches972fdea2013-04-29 16:18:12 -07005892# check for krealloc arg reuse
5893 if ($^V && $^V ge 5.10.0 &&
5894 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5895 WARN("KREALLOC_ARG_REUSE",
5896 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5897 }
5898
Joe Perches5ce59ae2013-02-21 16:44:18 -08005899# check for alloc argument mismatch
5900 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5901 WARN("ALLOC_ARRAY_ARGS",
5902 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5903 }
5904
Joe Perchescaf2a542011-01-12 16:59:56 -08005905# check for multiple semicolons
5906 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005907 if (WARN("ONE_SEMICOLON",
5908 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5909 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005910 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005911 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005912 }
5913
Tomas Winklercec3aaa2016-08-02 14:04:39 -07005914# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5915 if ($realfile !~ m@^include/uapi/@ &&
5916 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08005917 my $ull = "";
5918 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5919 if (CHK("BIT_MACRO",
5920 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5921 $fix) {
5922 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5923 }
5924 }
5925
Joe Perches2d632742016-05-20 17:04:00 -07005926# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
5927 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
5928 my $config = $1;
5929 if (WARN("PREFER_IS_ENABLED",
5930 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
5931 $fix) {
5932 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
5933 }
5934 }
5935
Joe Perchese81f2392014-08-06 16:11:25 -07005936# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005937 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5938 my $has_break = 0;
5939 my $has_statement = 0;
5940 my $count = 0;
5941 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005942 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005943 $prevline--;
5944 my $rline = $rawlines[$prevline - 1];
5945 my $fline = $lines[$prevline - 1];
5946 last if ($fline =~ /^\@\@/);
5947 next if ($fline =~ /^\-/);
5948 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5949 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5950 next if ($fline =~ /^.[\s$;]*$/);
5951 $has_statement = 1;
5952 $count++;
5953 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5954 }
5955 if (!$has_break && $has_statement) {
5956 WARN("MISSING_BREAK",
5957 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5958 }
5959 }
5960
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005961# check for switch/default statements without a break;
5962 if ($^V && $^V ge 5.10.0 &&
5963 defined $stat &&
5964 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5965 my $ctx = '';
5966 my $herectx = $here . "\n";
5967 my $cnt = statement_rawlines($stat);
5968 for (my $n = 0; $n < $cnt; $n++) {
5969 $herectx .= raw_line($linenr, $n) . "\n";
5970 }
5971 WARN("DEFAULT_NO_BREAK",
5972 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005973 }
5974
Patrick Pannutof370e252010-07-23 13:34:18 -07005975# check for return codes on error paths
5976 if ($line =~ /\breturn\s+-\d+/) {
5977 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07005978 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07005979 }
5980
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005981# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005982 if ($line =~ /\b__FUNCTION__\b/) {
5983 if (WARN("USE_FUNC",
5984 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5985 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005986 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005987 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005988 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005989
Joe Perches62ec8182015-02-13 14:38:18 -08005990# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5991 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5992 ERROR("DATE_TIME",
5993 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5994 }
5995
Joe Perches2c924882012-03-23 15:02:20 -07005996# check for use of yield()
5997 if ($line =~ /\byield\s*\(\s*\)/) {
5998 WARN("YIELD",
5999 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6000 }
6001
Joe Perches179f8f42013-07-03 15:05:30 -07006002# check for comparisons against true and false
6003 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6004 my $lead = $1;
6005 my $arg = $2;
6006 my $test = $3;
6007 my $otype = $4;
6008 my $trail = $5;
6009 my $op = "!";
6010
6011 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6012
6013 my $type = lc($otype);
6014 if ($type =~ /^(?:true|false)$/) {
6015 if (("$test" eq "==" && "$type" eq "true") ||
6016 ("$test" eq "!=" && "$type" eq "false")) {
6017 $op = "";
6018 }
6019
6020 CHK("BOOL_COMPARISON",
6021 "Using comparison to $otype is error prone\n" . $herecurr);
6022
6023## maybe suggesting a correct construct would better
6024## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6025
6026 }
6027 }
6028
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006029# check for semaphores initialized locked
6030 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006031 WARN("CONSIDER_COMPLETION",
6032 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006033 }
Joe Perches6712d852012-03-23 15:02:20 -07006034
Joe Perches67d0a072011-10-31 17:13:10 -07006035# recommend kstrto* over simple_strto* and strict_strto*
6036 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006037 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006038 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006039 }
Joe Perches6712d852012-03-23 15:02:20 -07006040
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006041# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006042 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006043 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006044 "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 -07006045 }
Joe Perches6712d852012-03-23 15:02:20 -07006046
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006047# check for various structs that are normally const (ops, kgdb, device_tree)
6048 my $const_structs = qr{
6049 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006050 address_space_operations|
6051 backlight_ops|
6052 block_device_operations|
6053 dentry_operations|
6054 dev_pm_ops|
6055 dma_map_ops|
6056 extent_io_ops|
6057 file_lock_operations|
6058 file_operations|
6059 hv_ops|
6060 ide_dma_ops|
6061 intel_dvo_dev_ops|
6062 item_operations|
6063 iwl_ops|
6064 kgdb_arch|
6065 kgdb_io|
6066 kset_uevent_ops|
6067 lock_manager_operations|
6068 microcode_ops|
6069 mtrr_ops|
6070 neigh_ops|
6071 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006072 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08006073 pci_raw_ops|
6074 pipe_buf_operations|
6075 platform_hibernation_ops|
6076 platform_suspend_ops|
6077 proto_ops|
6078 rpc_pipe_ops|
6079 seq_operations|
6080 snd_ac97_build_ops|
6081 soc_pcmcia_socket_ops|
6082 stacktrace_ops|
6083 sysfs_ops|
6084 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07006085 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006086 usb_mon_operations|
6087 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006088 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006089 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006090 WARN("CONST_STRUCT",
6091 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006092 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006093 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006094
6095# use of NR_CPUS is usually wrong
6096# ignore definitions of NR_CPUS and usage to define arrays as likely right
6097 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006098 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6099 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006100 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6101 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6102 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006103 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006104 WARN("NR_CPUS",
6105 "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 -07006106 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006107
Joe Perches52ea8502013-11-12 15:10:09 -08006108# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6109 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6110 ERROR("DEFINE_ARCH_HAS",
6111 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6112 }
6113
Joe Perchesacd93622015-02-13 14:38:38 -08006114# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6115 if ($^V && $^V ge 5.10.0 &&
6116 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6117 WARN("LIKELY_MISUSE",
6118 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6119 }
6120
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006121# whine mightly about in_atomic
6122 if ($line =~ /\bin_atomic\s*\(/) {
6123 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006124 ERROR("IN_ATOMIC",
6125 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006126 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006127 WARN("IN_ATOMIC",
6128 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006129 }
6130 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006131
Joe Perches481aea52016-05-20 17:04:08 -07006132# whine about ACCESS_ONCE
6133 if ($^V && $^V ge 5.10.0 &&
6134 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6135 my $par = $1;
6136 my $eq = $2;
6137 my $fun = $3;
6138 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6139 if (defined($eq)) {
6140 if (WARN("PREFER_WRITE_ONCE",
6141 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6142 $fix) {
6143 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6144 }
6145 } else {
6146 if (WARN("PREFER_READ_ONCE",
6147 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6148 $fix) {
6149 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6150 }
6151 }
6152 }
6153
Peter Zijlstra1704f472010-03-19 01:37:42 +01006154# check for lockdep_set_novalidate_class
6155 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6156 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6157 if ($realfile !~ m@^kernel/lockdep@ &&
6158 $realfile !~ m@^include/linux/lockdep@ &&
6159 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006160 ERROR("LOCKDEP",
6161 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006162 }
6163 }
Dave Jones88f88312011-01-12 16:59:59 -08006164
Joe Perchesb392c642015-04-16 12:44:16 -07006165 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6166 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006167 WARN("EXPORTED_WORLD_WRITABLE",
6168 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006169 }
Joe Perches24358802014-04-03 14:49:13 -07006170
Joe Perches515a2352014-04-03 14:49:24 -07006171# Mode permission misuses where it seems decimal should be octal
6172# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6173 if ($^V && $^V ge 5.10.0 &&
6174 $line =~ /$mode_perms_search/) {
6175 foreach my $entry (@mode_permission_funcs) {
6176 my $func = $entry->[0];
6177 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006178
Joe Perches515a2352014-04-03 14:49:24 -07006179 my $skip_args = "";
6180 if ($arg_pos > 1) {
6181 $arg_pos--;
6182 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6183 }
6184 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
6185 if ($line =~ /$test/) {
6186 my $val = $1;
6187 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07006188
Joe Perches515a2352014-04-03 14:49:24 -07006189 if ($val !~ /^0$/ &&
6190 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6191 length($val) ne 4)) {
6192 ERROR("NON_OCTAL_PERMISSIONS",
6193 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08006194 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6195 ERROR("EXPORTED_WORLD_WRITABLE",
6196 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07006197 }
Joe Perches24358802014-04-03 14:49:13 -07006198 }
6199 }
6200 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006201
6202# validate content of MODULE_LICENSE against list from include/linux/module.h
6203 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6204 my $extracted_string = get_quoted_string($line, $rawline);
6205 my $valid_licenses = qr{
6206 GPL|
6207 GPL\ v2|
6208 GPL\ and\ additional\ rights|
6209 Dual\ BSD/GPL|
6210 Dual\ MIT/GPL|
6211 Dual\ MPL/GPL|
6212 Proprietary
6213 }x;
6214 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6215 WARN("MODULE_LICENSE",
6216 "unknown module license " . $extracted_string . "\n" . $herecurr);
6217 }
6218 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006219 }
6220
6221 # If we have no input at all, then there is nothing to report on
6222 # so just keep quiet.
6223 if ($#rawlines == -1) {
6224 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006225 }
6226
Andy Whitcroft8905a672007-11-28 16:21:06 -08006227 # In mailback mode only produce a report in the negative, for
6228 # things that appear to be patches.
6229 if ($mailback && ($clean == 1 || !$is_patch)) {
6230 exit(0);
6231 }
6232
6233 # This is not a patch, and we are are in 'no-patch' mode so
6234 # just keep quiet.
6235 if (!$chk_patch && !$is_patch) {
6236 exit(0);
6237 }
6238
Joe Perches06330fc2015-06-25 15:03:11 -07006239 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006240 ERROR("NOT_UNIFIED_DIFF",
6241 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006242 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006243 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006244 ERROR("MISSING_SIGN_OFF",
6245 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006246 }
6247
Andy Whitcroft8905a672007-11-28 16:21:06 -08006248 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006249 if ($summary && !($clean == 1 && $quiet == 1)) {
6250 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006251 print "total: $cnt_error errors, $cnt_warn warnings, " .
6252 (($check)? "$cnt_chk checks, " : "") .
6253 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006254 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006255
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006256 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006257 # If there were any defects found and not already fixing them
6258 if (!$clean and !$fix) {
6259 print << "EOM"
6260
6261NOTE: For some of the reported defects, checkpatch may be able to
6262 mechanically convert to the typical style using --fix or --fix-inplace.
6263EOM
6264 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006265 # If there were whitespace errors which cleanpatch can fix
6266 # then suggest that.
6267 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006268 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006269 print << "EOM"
6270
6271NOTE: Whitespace errors detected.
6272 You may wish to use scripts/cleanpatch or scripts/cleanfile
6273EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006274 }
6275 }
6276
Joe Perchesd752fcc2014-08-06 16:11:05 -07006277 if ($clean == 0 && $fix &&
6278 ("@rawlines" ne "@fixed" ||
6279 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006280 my $newfile = $filename;
6281 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006282 my $linecount = 0;
6283 my $f;
6284
Joe Perchesd752fcc2014-08-06 16:11:05 -07006285 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6286
Joe Perches3705ce52013-07-03 15:05:31 -07006287 open($f, '>', $newfile)
6288 or die "$P: Can't open $newfile for write\n";
6289 foreach my $fixed_line (@fixed) {
6290 $linecount++;
6291 if ($file) {
6292 if ($linecount > 3) {
6293 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006294 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006295 }
6296 } else {
6297 print $f $fixed_line . "\n";
6298 }
6299 }
6300 close($f);
6301
6302 if (!$quiet) {
6303 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006304
Joe Perches3705ce52013-07-03 15:05:31 -07006305Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6306
6307Do _NOT_ trust the results written to this file.
6308Do _NOT_ submit these changes without inspecting them for correctness.
6309
6310This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6311No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006312EOM
6313 }
6314 }
6315
Joe Perchesd8469f12015-06-25 15:03:00 -07006316 if ($quiet == 0) {
6317 print "\n";
6318 if ($clean == 1) {
6319 print "$vname has no obvious style problems and is ready for submission.\n";
6320 } else {
6321 print "$vname has style problems, please review.\n";
6322 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006323 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006324 return $clean;
6325}