blob: f905d7b5053040f5fa344b1c95cdaf68694a3473 [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;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
21my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070022my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080024my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $file = 0;
26my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070027my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080028my $summary = 1;
29my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080030my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070031my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070032my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080033my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080035my %debug;
Joe Perches34456862013-07-03 15:05:34 -070036my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070037my %use_type = ();
38my @use = ();
39my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070040my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070041my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070042my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080043my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070044my $ignore_perl_version = 0;
45my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070046
47sub help {
48 my ($exitcode) = @_;
49
50 print << "EOM";
51Usage: $P [OPTION]... [FILE]...
52Version: $V
53
54Options:
55 -q, --quiet quiet
56 --no-tree run without a kernel tree
57 --no-signoff do not check for 'Signed-off-by' line
58 --patch treat FILE as patchfile (default)
59 --emacs emacs compile window format
60 --terse one line per report
61 -f, --file treat FILE as regular source file
62 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070063 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070064 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080065 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070066 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070067 --root=PATH PATH to the kernel tree root
68 --no-summary suppress the per-file summary
69 --mailback only produce a report in case of warnings/errors
70 --summary-file include the filename in summary
71 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
72 'values', 'possible', 'type', and 'attr' (default
73 is all off)
74 --test-only=WORD report only warnings/errors containing WORD
75 literally
Joe Perches3705ce52013-07-03 15:05:31 -070076 --fix EXPERIMENTAL - may create horrible results
77 If correctable single-line errors exist, create
78 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
79 with potential errors corrected to the preferred
80 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080081 --fix-inplace EXPERIMENTAL - may create horrible results
82 Is the same as --fix, but overwrites the input
83 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070084 --ignore-perl-version override checking of perl version. expect
85 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070086 -h, --help, --version display this help and exit
87
88When FILE is - read standard input.
89EOM
90
91 exit($exitcode);
92}
93
Joe Perches000d1cc12011-07-25 17:13:25 -070094my $conf = which_conf($configuration_file);
95if (-f $conf) {
96 my @conf_args;
97 open(my $conffile, '<', "$conf")
98 or warn "$P: Can't find a readable $configuration_file file $!\n";
99
100 while (<$conffile>) {
101 my $line = $_;
102
103 $line =~ s/\s*\n?$//g;
104 $line =~ s/^\s*//g;
105 $line =~ s/\s+/ /g;
106
107 next if ($line =~ m/^\s*#/);
108 next if ($line =~ m/^\s*$/);
109
110 my @words = split(" ", $line);
111 foreach my $word (@words) {
112 last if ($word =~ m/^#/);
113 push (@conf_args, $word);
114 }
115 }
116 close($conffile);
117 unshift(@ARGV, @conf_args) if @conf_args;
118}
119
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700120GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700121 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700122 'tree!' => \$tree,
123 'signoff!' => \$chk_signoff,
124 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800126 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700127 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700128 'subjective!' => \$check,
129 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700130 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700131 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700132 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800133 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700134 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800135 'summary!' => \$summary,
136 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800137 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700138 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800139 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700140 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800141 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700142 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700143 'h|help' => \$help,
144 'version' => \$help
145) or help(1);
146
147help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700148
Joe Perches9624b8d2014-01-23 15:54:44 -0800149$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700150$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800151
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700152my $exit = 0;
153
Dave Hansend62a2012013-09-11 14:23:56 -0700154if ($^V && $^V lt $minimum_perl_version) {
155 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156 if (!$ignore_perl_version) {
157 exit(1);
158 }
159}
160
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700161if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700162 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700163 exit(1);
164}
165
Joe Perches91bfe482013-09-11 14:23:59 -0700166sub hash_save_array_words {
167 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700168
Joe Perches91bfe482013-09-11 14:23:59 -0700169 my @array = split(/,/, join(',', @$arrayRef));
170 foreach my $word (@array) {
171 $word =~ s/\s*\n?$//g;
172 $word =~ s/^\s*//g;
173 $word =~ s/\s+/ /g;
174 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700175
Joe Perches91bfe482013-09-11 14:23:59 -0700176 next if ($word =~ m/^\s*#/);
177 next if ($word =~ m/^\s*$/);
178
179 $hashRef->{$word}++;
180 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700181}
182
Joe Perches91bfe482013-09-11 14:23:59 -0700183sub hash_show_words {
184 my ($hashRef, $prefix) = @_;
185
Joe Perches58cb3cf2013-09-11 14:24:04 -0700186 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700187 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700188 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700189 print " $word";
190 }
191 print "\n\n";
192 }
193}
194
195hash_save_array_words(\%ignore_type, \@ignore);
196hash_save_array_words(\%use_type, \@use);
197
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800198my $dbg_values = 0;
199my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700200my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700201my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800202for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800203 ## no critic
204 eval "\${dbg_$key} = '$debug{$key}';";
205 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800206}
207
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700208my $rpt_cleaners = 0;
209
Andy Whitcroft8905a672007-11-28 16:21:06 -0800210if ($terse) {
211 $emacs = 1;
212 $quiet++;
213}
214
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700215if ($tree) {
216 if (defined $root) {
217 if (!top_of_kernel_tree($root)) {
218 die "$P: $root: --root does not point at a valid tree\n";
219 }
220 } else {
221 if (top_of_kernel_tree('.')) {
222 $root = '.';
223 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
224 top_of_kernel_tree($1)) {
225 $root = $1;
226 }
227 }
228
229 if (!defined $root) {
230 print "Must be run from the top-level dir. of a kernel tree\n";
231 exit(2);
232 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700233}
234
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700235my $emitted_corrupt = 0;
236
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700237our $Ident = qr{
238 [A-Za-z_][A-Za-z\d_]*
239 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
240 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241our $Storage = qr{extern|static|asmlinkage};
242our $Sparse = qr{
243 __user|
244 __kernel|
245 __force|
246 __iomem|
247 __must_check|
248 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800249 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700250 __ref|
251 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700252 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800253our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700258
Wolfram Sang52131292010-03-05 13:43:51 -0800259# Notes to $Attribute:
260# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700261our $Attribute = qr{
262 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700263 __percpu|
264 __nocast|
265 __safe|
266 __bitwise__|
267 __packed__|
268 __packed2__|
269 __naked|
270 __maybe_unused|
271 __always_unused|
272 __noreturn|
273 __used|
274 __cold|
275 __noclone|
276 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700277 __read_mostly|
278 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700279 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700280 ____cacheline_aligned|
281 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800282 ____cacheline_internodealigned_in_smp|
283 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700285our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700286our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700287our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
288our $Lval = qr{$Ident(?:$Member)*};
289
Joe Perches95e2c602013-07-03 15:05:20 -0700290our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
291our $Binary = qr{(?i)0b[01]+$Int_type?};
292our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
293our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700294our $Octal = qr{0[0-7]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800295our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800298our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700299our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800300our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700301our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700302our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303our $Operators = qr{
304 <=|>=|==|!=|
305 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700306 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700307 }x;
308
Joe Perches91cb5192014-04-03 14:49:32 -0700309our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
310
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311our $NonptrType;
Joe Perches8716de32013-09-11 14:24:05 -0700312our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800313our $Type;
314our $Declare;
315
Joe Perches15662b32011-10-31 17:13:12 -0700316our $NON_ASCII_UTF8 = qr{
317 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700318 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
319 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
320 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
321 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
322 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
323 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
324}x;
325
Joe Perches15662b32011-10-31 17:13:12 -0700326our $UTF8 = qr{
327 [\x09\x0A\x0D\x20-\x7E] # ASCII
328 | $NON_ASCII_UTF8
329}x;
330
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700331our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700332 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700333 atomic_t
334)};
335
Joe Perches691e6692010-03-05 13:43:51 -0800336our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700337 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700338 (?:[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 -0700339 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700340 panic|
Joe Perches06668722013-11-12 15:10:07 -0800341 MODULE_[A-Z_]+|
342 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800343)};
344
Joe Perches20112472011-07-25 17:13:23 -0700345our $signature_tags = qr{(?xi:
346 Signed-off-by:|
347 Acked-by:|
348 Tested-by:|
349 Reviewed-by:|
350 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700351 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700352 To:|
353 Cc:
354)};
355
Andy Whitcroft8905a672007-11-28 16:21:06 -0800356our @typeList = (
357 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700358 qr{(?:unsigned\s+)?char},
359 qr{(?:unsigned\s+)?short},
360 qr{(?:unsigned\s+)?int},
361 qr{(?:unsigned\s+)?long},
362 qr{(?:unsigned\s+)?long\s+int},
363 qr{(?:unsigned\s+)?long\s+long},
364 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800365 qr{unsigned},
366 qr{float},
367 qr{double},
368 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800369 qr{struct\s+$Ident},
370 qr{union\s+$Ident},
371 qr{enum\s+$Ident},
372 qr{${Ident}_t},
373 qr{${Ident}_handler},
374 qr{${Ident}_handler_fn},
375);
Joe Perches8716de32013-09-11 14:24:05 -0700376our @typeListWithAttr = (
377 @typeList,
378 qr{struct\s+$InitAttribute\s+$Ident},
379 qr{union\s+$InitAttribute\s+$Ident},
380);
381
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700382our @modifierList = (
383 qr{fastcall},
384);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800385
Joe Perches24358802014-04-03 14:49:13 -0700386our @mode_permission_funcs = (
387 ["module_param", 3],
388 ["module_param_(?:array|named|string)", 4],
389 ["module_param_array_named", 5],
390 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
391 ["proc_create(?:_data|)", 2],
392 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
393);
394
Joe Perches515a2352014-04-03 14:49:24 -0700395#Create a search pattern for all these functions to speed up a loop below
396our $mode_perms_search = "";
397foreach my $entry (@mode_permission_funcs) {
398 $mode_perms_search .= '|' if ($mode_perms_search ne "");
399 $mode_perms_search .= $entry->[0];
400}
401
Joe Perches3f7bac02014-06-04 16:12:04 -0700402our $declaration_macros = qr{(?x:
403 (?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
404 (?:$Storage\s+)?LIST_HEAD\s*\(
405)};
406
Wolfram Sang7840a942010-08-09 17:20:57 -0700407our $allowed_asm_includes = qr{(?x:
408 irq|
409 memory
410)};
411# memory.h: ARM has a custom one
412
Andy Whitcroft8905a672007-11-28 16:21:06 -0800413sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700414 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
415 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700416 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700417 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700419 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800420 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800421 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700422 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700423 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800424 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700425 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700427 $NonptrTypeWithAttr = qr{
428 (?:$Modifier\s+|const\s+)*
429 (?:
430 (?:typeof|__typeof__)\s*\([^\)]*\)|
431 (?:$typeTypedefs\b)|
432 (?:${allWithAttr}\b)
433 )
434 (?:\s+$Modifier|\s+const)*
435 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700437 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700438 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700439 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700441 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442}
443build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700444
Joe Perches7d2367a2011-07-25 17:13:22 -0700445our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700446
447# Using $balanced_parens, $LvalOrFunc, or $FuncArg
448# requires at least perl version v5.10.0
449# Any use must be runtime checked with $^V
450
451our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700452our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800453our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700454
455sub deparenthesize {
456 my ($string) = @_;
457 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700458
459 while ($string =~ /^\s*\(.*\)\s*$/) {
460 $string =~ s@^\s*\(\s*@@;
461 $string =~ s@\s*\)\s*$@@;
462 }
463
Joe Perches7d2367a2011-07-25 17:13:22 -0700464 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700465
Joe Perches7d2367a2011-07-25 17:13:22 -0700466 return $string;
467}
468
Joe Perches34456862013-07-03 15:05:34 -0700469sub seed_camelcase_file {
470 my ($file) = @_;
471
472 return if (!(-f $file));
473
474 local $/;
475
476 open(my $include_file, '<', "$file")
477 or warn "$P: Can't read '$file' $!\n";
478 my $text = <$include_file>;
479 close($include_file);
480
481 my @lines = split('\n', $text);
482
483 foreach my $line (@lines) {
484 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
485 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
486 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800487 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
488 $camelcase{$1} = 1;
489 } 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 -0700490 $camelcase{$1} = 1;
491 }
492 }
493}
494
495my $camelcase_seeded = 0;
496sub seed_camelcase_includes {
497 return if ($camelcase_seeded);
498
499 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700500 my $camelcase_cache = "";
501 my @include_files = ();
502
503 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700504
Richard Genoud3645e322014-02-10 14:25:32 -0800505 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700506 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
507 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700508 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700509 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700510 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700511 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700512 @include_files = split('\n', $files);
513 foreach my $file (@include_files) {
514 my $date = POSIX::strftime("%Y%m%d%H%M",
515 localtime((stat $file)[9]));
516 $last_mod_date = $date if ($last_mod_date < $date);
517 }
518 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700519 }
Joe Perchesc707a812013-07-08 16:00:43 -0700520
521 if ($camelcase_cache ne "" && -f $camelcase_cache) {
522 open(my $camelcase_file, '<', "$camelcase_cache")
523 or warn "$P: Can't read '$camelcase_cache' $!\n";
524 while (<$camelcase_file>) {
525 chomp;
526 $camelcase{$_} = 1;
527 }
528 close($camelcase_file);
529
530 return;
531 }
532
Richard Genoud3645e322014-02-10 14:25:32 -0800533 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700534 $files = `git ls-files "include/*.h"`;
535 @include_files = split('\n', $files);
536 }
537
Joe Perches34456862013-07-03 15:05:34 -0700538 foreach my $file (@include_files) {
539 seed_camelcase_file($file);
540 }
Joe Perches351b2a12013-07-03 15:05:36 -0700541
Joe Perchesc707a812013-07-08 16:00:43 -0700542 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700543 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700544 open(my $camelcase_file, '>', "$camelcase_cache")
545 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700546 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
547 print $camelcase_file ("$_\n");
548 }
549 close($camelcase_file);
550 }
Joe Perches34456862013-07-03 15:05:34 -0700551}
552
Joe Perchesd311cd42014-08-06 16:10:57 -0700553sub git_commit_info {
554 my ($commit, $id, $desc) = @_;
555
556 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
557
558 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
559 $output =~ s/^\s*//gm;
560 my @lines = split("\n", $output);
561
562 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
563# Maybe one day convert this block of bash into something that returns
564# all matching commit ids, but it's very slow...
565#
566# echo "checking commits $1..."
567# git rev-list --remotes | grep -i "^$1" |
568# while read line ; do
569# git log --format='%H %s' -1 $line |
570# echo "commit $(cut -c 1-12,41-)"
571# done
572 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
573 } else {
574 $id = substr($lines[0], 0, 12);
575 $desc = substr($lines[0], 41);
576 }
577
578 return ($id, $desc);
579}
580
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700581$chk_signoff = 0 if ($file);
582
Andy Whitcroft00df3442007-06-08 13:47:06 -0700583my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800584my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700585my @fixed = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700586my $fixlinenr = -1;
587
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800588my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700589for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800590 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700591 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800592 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700593 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800594 } elsif ($filename eq '-') {
595 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700596 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800597 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700598 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700599 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800600 if ($filename eq '-') {
601 $vname = 'Your patch';
602 } else {
603 $vname = $filename;
604 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800605 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700606 chomp;
607 push(@rawlines, $_);
608 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800609 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800610 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700611 $exit = 1;
612 }
613 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800614 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700615 @fixed = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700616 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700617}
618
619exit($exit);
620
621sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700622 my ($root) = @_;
623
624 my @tree_check = (
625 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
626 "README", "Documentation", "arch", "include", "drivers",
627 "fs", "init", "ipc", "kernel", "lib", "scripts",
628 );
629
630 foreach my $check (@tree_check) {
631 if (! -e $root . '/' . $check) {
632 return 0;
633 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700634 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700635 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700636}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700637
Joe Perches20112472011-07-25 17:13:23 -0700638sub parse_email {
639 my ($formatted_email) = @_;
640
641 my $name = "";
642 my $address = "";
643 my $comment = "";
644
645 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
646 $name = $1;
647 $address = $2;
648 $comment = $3 if defined $3;
649 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
650 $address = $1;
651 $comment = $2 if defined $2;
652 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
653 $address = $1;
654 $comment = $2 if defined $2;
655 $formatted_email =~ s/$address.*$//;
656 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700657 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700658 $name =~ s/^\"|\"$//g;
659 # If there's a name left after stripping spaces and
660 # leading quotes, and the address doesn't have both
661 # leading and trailing angle brackets, the address
662 # is invalid. ie:
663 # "joe smith joe@smith.com" bad
664 # "joe smith <joe@smith.com" bad
665 if ($name ne "" && $address !~ /^<[^>]+>$/) {
666 $name = "";
667 $address = "";
668 $comment = "";
669 }
670 }
671
Joe Perches3705ce52013-07-03 15:05:31 -0700672 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700673 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700674 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700675 $address =~ s/^\<|\>$//g;
676
677 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
678 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
679 $name = "\"$name\"";
680 }
681
682 return ($name, $address, $comment);
683}
684
685sub format_email {
686 my ($name, $address) = @_;
687
688 my $formatted_email;
689
Joe Perches3705ce52013-07-03 15:05:31 -0700690 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700691 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700692 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700693
694 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
695 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
696 $name = "\"$name\"";
697 }
698
699 if ("$name" eq "") {
700 $formatted_email = "$address";
701 } else {
702 $formatted_email = "$name <$address>";
703 }
704
705 return $formatted_email;
706}
707
Joe Perchesd311cd42014-08-06 16:10:57 -0700708sub which {
709 my ($bin) = @_;
710
711 foreach my $path (split(/:/, $ENV{PATH})) {
712 if (-e "$path/$bin") {
713 return "$path/$bin";
714 }
715 }
716
717 return "";
718}
719
Joe Perches000d1cc12011-07-25 17:13:25 -0700720sub which_conf {
721 my ($conf) = @_;
722
723 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
724 if (-e "$path/$conf") {
725 return "$path/$conf";
726 }
727 }
728
729 return "";
730}
731
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700732sub expand_tabs {
733 my ($str) = @_;
734
735 my $res = '';
736 my $n = 0;
737 for my $c (split(//, $str)) {
738 if ($c eq "\t") {
739 $res .= ' ';
740 $n++;
741 for (; ($n % 8) != 0; $n++) {
742 $res .= ' ';
743 }
744 next;
745 }
746 $res .= $c;
747 $n++;
748 }
749
750 return $res;
751}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700752sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700753 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700754 return $res;
755}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700756
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700757sub line_stats {
758 my ($line) = @_;
759
760 # Drop the diff line leader and expand tabs
761 $line =~ s/^.//;
762 $line = expand_tabs($line);
763
764 # Pick the indent from the front of the line.
765 my ($white) = ($line =~ /^(\s*)/);
766
767 return (length($line), length($white));
768}
769
Andy Whitcroft773647a2008-03-28 14:15:58 -0700770my $sanitise_quote = '';
771
772sub sanitise_line_reset {
773 my ($in_comment) = @_;
774
775 if ($in_comment) {
776 $sanitise_quote = '*/';
777 } else {
778 $sanitise_quote = '';
779 }
780}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700781sub sanitise_line {
782 my ($line) = @_;
783
784 my $res = '';
785 my $l = '';
786
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800787 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700788 my $off = 0;
789 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700790
Andy Whitcroft773647a2008-03-28 14:15:58 -0700791 # Always copy over the diff marker.
792 $res = substr($line, 0, 1);
793
794 for ($off = 1; $off < length($line); $off++) {
795 $c = substr($line, $off, 1);
796
797 # Comments we are wacking completly including the begin
798 # and end, all to $;.
799 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
800 $sanitise_quote = '*/';
801
802 substr($res, $off, 2, "$;$;");
803 $off++;
804 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800805 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700806 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700807 $sanitise_quote = '';
808 substr($res, $off, 2, "$;$;");
809 $off++;
810 next;
811 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700812 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
813 $sanitise_quote = '//';
814
815 substr($res, $off, 2, $sanitise_quote);
816 $off++;
817 next;
818 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700819
820 # A \ in a string means ignore the next character.
821 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
822 $c eq "\\") {
823 substr($res, $off, 2, 'XX');
824 $off++;
825 next;
826 }
827 # Regular quotes.
828 if ($c eq "'" || $c eq '"') {
829 if ($sanitise_quote eq '') {
830 $sanitise_quote = $c;
831
832 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700833 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700834 } elsif ($sanitise_quote eq $c) {
835 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700836 }
837 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800839 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700840 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
841 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700842 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
843 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700844 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
845 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700846 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700847 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700848 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800849 }
850
Daniel Walker113f04a2009-09-21 17:04:35 -0700851 if ($sanitise_quote eq '//') {
852 $sanitise_quote = '';
853 }
854
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800855 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700856 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800857 my $clean = 'X' x length($1);
858 $res =~ s@\<.*\>@<$clean>@;
859
860 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700861 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800862 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700863 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800864 }
865
Andy Whitcroft00df3442007-06-08 13:47:06 -0700866 return $res;
867}
868
Joe Perchesa6962d72013-04-29 16:18:13 -0700869sub get_quoted_string {
870 my ($line, $rawline) = @_;
871
872 return "" if ($line !~ m/(\"[X]+\")/g);
873 return substr($rawline, $-[0], $+[0] - $-[0]);
874}
875
Andy Whitcroft8905a672007-11-28 16:21:06 -0800876sub ctx_statement_block {
877 my ($linenr, $remain, $off) = @_;
878 my $line = $linenr - 1;
879 my $blk = '';
880 my $soff = $off;
881 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700882 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800883
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800884 my $loff = 0;
885
Andy Whitcroft8905a672007-11-28 16:21:06 -0800886 my $type = '';
887 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800888 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800889 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800890 my $c;
891 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800892
893 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800894 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800895 @stack = (['', 0]) if ($#stack == -1);
896
Andy Whitcroft773647a2008-03-28 14:15:58 -0700897 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800898 # If we are about to drop off the end, pull in more
899 # context.
900 if ($off >= $len) {
901 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700902 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800903 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800904 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800905 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800906 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800907 $len = length($blk);
908 $line++;
909 last;
910 }
911 # Bail if there is no further context.
912 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800913 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800914 last;
915 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800916 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
917 $level++;
918 $type = '#';
919 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800920 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800921 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800922 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800923 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800924
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800926
927 # Handle nested #if/#else.
928 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
929 push(@stack, [ $type, $level ]);
930 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
931 ($type, $level) = @{$stack[$#stack - 1]};
932 } elsif ($remainder =~ /^#\s*endif\b/) {
933 ($type, $level) = @{pop(@stack)};
934 }
935
Andy Whitcroft8905a672007-11-28 16:21:06 -0800936 # Statement ends at the ';' or a close '}' at the
937 # outermost level.
938 if ($level == 0 && $c eq ';') {
939 last;
940 }
941
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800942 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700943 if ($level == 0 && $coff_set == 0 &&
944 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
945 $remainder =~ /^(else)(?:\s|{)/ &&
946 $remainder !~ /^else\s+if\b/) {
947 $coff = $off + length($1) - 1;
948 $coff_set = 1;
949 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
950 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800951 }
952
Andy Whitcroft8905a672007-11-28 16:21:06 -0800953 if (($type eq '' || $type eq '(') && $c eq '(') {
954 $level++;
955 $type = '(';
956 }
957 if ($type eq '(' && $c eq ')') {
958 $level--;
959 $type = ($level != 0)? '(' : '';
960
961 if ($level == 0 && $coff < $soff) {
962 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700963 $coff_set = 1;
964 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800965 }
966 }
967 if (($type eq '' || $type eq '{') && $c eq '{') {
968 $level++;
969 $type = '{';
970 }
971 if ($type eq '{' && $c eq '}') {
972 $level--;
973 $type = ($level != 0)? '{' : '';
974
975 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700976 if (substr($blk, $off + 1, 1) eq ';') {
977 $off++;
978 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800979 last;
980 }
981 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800982 # Preprocessor commands end at the newline unless escaped.
983 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
984 $level--;
985 $type = '';
986 $off++;
987 last;
988 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800989 $off++;
990 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700991 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800992 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700993 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800994 $line++;
995 $remain--;
996 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800997
998 my $statement = substr($blk, $soff, $off - $soff + 1);
999 my $condition = substr($blk, $soff, $coff - $soff + 1);
1000
1001 #warn "STATEMENT<$statement>\n";
1002 #warn "CONDITION<$condition>\n";
1003
Andy Whitcroft773647a2008-03-28 14:15:58 -07001004 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001005
1006 return ($statement, $condition,
1007 $line, $remain + 1, $off - $loff + 1, $level);
1008}
1009
Andy Whitcroftcf655042008-03-04 14:28:20 -08001010sub statement_lines {
1011 my ($stmt) = @_;
1012
1013 # Strip the diff line prefixes and rip blank lines at start and end.
1014 $stmt =~ s/(^|\n)./$1/g;
1015 $stmt =~ s/^\s*//;
1016 $stmt =~ s/\s*$//;
1017
1018 my @stmt_lines = ($stmt =~ /\n/g);
1019
1020 return $#stmt_lines + 2;
1021}
1022
1023sub statement_rawlines {
1024 my ($stmt) = @_;
1025
1026 my @stmt_lines = ($stmt =~ /\n/g);
1027
1028 return $#stmt_lines + 2;
1029}
1030
1031sub statement_block_size {
1032 my ($stmt) = @_;
1033
1034 $stmt =~ s/(^|\n)./$1/g;
1035 $stmt =~ s/^\s*{//;
1036 $stmt =~ s/}\s*$//;
1037 $stmt =~ s/^\s*//;
1038 $stmt =~ s/\s*$//;
1039
1040 my @stmt_lines = ($stmt =~ /\n/g);
1041 my @stmt_statements = ($stmt =~ /;/g);
1042
1043 my $stmt_lines = $#stmt_lines + 2;
1044 my $stmt_statements = $#stmt_statements + 1;
1045
1046 if ($stmt_lines > $stmt_statements) {
1047 return $stmt_lines;
1048 } else {
1049 return $stmt_statements;
1050 }
1051}
1052
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001053sub ctx_statement_full {
1054 my ($linenr, $remain, $off) = @_;
1055 my ($statement, $condition, $level);
1056
1057 my (@chunks);
1058
Andy Whitcroftcf655042008-03-04 14:28:20 -08001059 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001060 ($statement, $condition, $linenr, $remain, $off, $level) =
1061 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001062 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001063 push(@chunks, [ $condition, $statement ]);
1064 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1065 return ($level, $linenr, @chunks);
1066 }
1067
1068 # Pull in the following conditional/block pairs and see if they
1069 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001070 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001071 ($statement, $condition, $linenr, $remain, $off, $level) =
1072 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001073 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001074 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001075 #print "C: push\n";
1076 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001077 }
1078
1079 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001080}
1081
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001082sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001083 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001084 my $line;
1085 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001086 my $blk = '';
1087 my @o;
1088 my @c;
1089 my @res = ();
1090
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001091 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001092 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001093 for ($line = $start; $remain > 0; $line++) {
1094 next if ($rawlines[$line] =~ /^-/);
1095 $remain--;
1096
1097 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001098
1099 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001100 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001101 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001102 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001103 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001104 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001105 $level = pop(@stack);
1106 }
1107
Andy Whitcroft01464f32010-10-26 14:23:19 -07001108 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001109 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1110 if ($off > 0) {
1111 $off--;
1112 next;
1113 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001114
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001115 if ($c eq $close && $level > 0) {
1116 $level--;
1117 last if ($level == 0);
1118 } elsif ($c eq $open) {
1119 $level++;
1120 }
1121 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001122
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001123 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001124 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001125 }
1126
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001127 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001128 }
1129
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001130 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001131}
1132sub ctx_block_outer {
1133 my ($linenr, $remain) = @_;
1134
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001135 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1136 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001137}
1138sub ctx_block {
1139 my ($linenr, $remain) = @_;
1140
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001141 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1142 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001143}
1144sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001145 my ($linenr, $remain, $off) = @_;
1146
1147 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1148 return @r;
1149}
1150sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001151 my ($linenr, $remain) = @_;
1152
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001153 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001154}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001155sub ctx_statement_level {
1156 my ($linenr, $remain, $off) = @_;
1157
1158 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1159}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001160
1161sub ctx_locate_comment {
1162 my ($first_line, $end_line) = @_;
1163
1164 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001165 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001166 return $current_comment if (defined $current_comment);
1167
1168 # Look through the context and try and figure out if there is a
1169 # comment.
1170 my $in_comment = 0;
1171 $current_comment = '';
1172 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001173 my $line = $rawlines[$linenr - 1];
1174 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001175 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1176 $in_comment = 1;
1177 }
1178 if ($line =~ m@/\*@) {
1179 $in_comment = 1;
1180 }
1181 if (!$in_comment && $current_comment ne '') {
1182 $current_comment = '';
1183 }
1184 $current_comment .= $line . "\n" if ($in_comment);
1185 if ($line =~ m@\*/@) {
1186 $in_comment = 0;
1187 }
1188 }
1189
1190 chomp($current_comment);
1191 return($current_comment);
1192}
1193sub ctx_has_comment {
1194 my ($first_line, $end_line) = @_;
1195 my $cmt = ctx_locate_comment($first_line, $end_line);
1196
Andy Whitcroft00df3442007-06-08 13:47:06 -07001197 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001198 ##print "CMMT: $cmt\n";
1199
1200 return ($cmt ne '');
1201}
1202
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001203sub raw_line {
1204 my ($linenr, $cnt) = @_;
1205
1206 my $offset = $linenr - 1;
1207 $cnt++;
1208
1209 my $line;
1210 while ($cnt) {
1211 $line = $rawlines[$offset++];
1212 next if (defined($line) && $line =~ /^-/);
1213 $cnt--;
1214 }
1215
1216 return $line;
1217}
1218
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001219sub cat_vet {
1220 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001221 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001222
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001223 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001224 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1225 $res .= $1;
1226 if ($2 ne '') {
1227 $coded = sprintf("^%c", unpack('C', $2) + 64);
1228 $res .= $coded;
1229 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001230 }
1231 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001232
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001233 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001234}
1235
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001236my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001237my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001238my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001239my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001240
1241sub annotate_reset {
1242 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001243 $av_pending = '_';
1244 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001245 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001246}
1247
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001248sub annotate_values {
1249 my ($stream, $type) = @_;
1250
1251 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001252 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001253 my $cur = $stream;
1254
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001255 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001256
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001257 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001258 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001259 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001260 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001261 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001262 print "WS($1)\n" if ($dbg_values > 1);
1263 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001264 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001265 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001266 }
1267
Florian Micklerc023e4732011-01-12 16:59:58 -08001268 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001269 print "CAST($1)\n" if ($dbg_values > 1);
1270 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001271 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001272
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001273 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001274 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001275 $type = 'T';
1276
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001277 } elsif ($cur =~ /^($Modifier)\s*/) {
1278 print "MODIFIER($1)\n" if ($dbg_values > 1);
1279 $type = 'T';
1280
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001281 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001282 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001284 push(@av_paren_type, $type);
1285 if ($2 ne '') {
1286 $av_pending = 'N';
1287 }
1288 $type = 'E';
1289
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001290 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001291 print "UNDEF($1)\n" if ($dbg_values > 1);
1292 $av_preprocessor = 1;
1293 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001294
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001295 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001296 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001298
1299 push(@av_paren_type, $type);
1300 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001301 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001302
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001303 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001304 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1305 $av_preprocessor = 1;
1306
1307 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1308
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001309 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001310
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001311 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001312 print "PRE_END($1)\n" if ($dbg_values > 1);
1313
1314 $av_preprocessor = 1;
1315
1316 # Assume all arms of the conditional end as this
1317 # one does, and continue as if the #endif was not here.
1318 pop(@av_paren_type);
1319 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001320 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001321
1322 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001324
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001325 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1326 print "ATTR($1)\n" if ($dbg_values > 1);
1327 $av_pending = $type;
1328 $type = 'N';
1329
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001330 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001331 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001332 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001333 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334 }
1335 $type = 'N';
1336
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001337 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001338 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001339 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001340 $type = 'N';
1341
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001342 } elsif ($cur =~/^(case)/o) {
1343 print "CASE($1)\n" if ($dbg_values > 1);
1344 $av_pend_colon = 'C';
1345 $type = 'N';
1346
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001347 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001348 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001349 $type = 'N';
1350
1351 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001353 push(@av_paren_type, $av_pending);
1354 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001355 $type = 'N';
1356
1357 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001358 my $new_type = pop(@av_paren_type);
1359 if ($new_type ne '_') {
1360 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001361 print "PAREN('$1') -> $type\n"
1362 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001363 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001365 }
1366
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001367 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001368 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001369 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001370 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001371
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001372 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1373 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001374 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001375 } elsif ($type eq 'E') {
1376 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001377 }
1378 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1379 $type = 'V';
1380
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001381 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001382 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001383 $type = 'V';
1384
1385 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001386 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001387 $type = 'N';
1388
Andy Whitcroftcf655042008-03-04 14:28:20 -08001389 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001390 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001392 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001393
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001394 } elsif ($cur =~/^(,)/) {
1395 print "COMMA($1)\n" if ($dbg_values > 1);
1396 $type = 'C';
1397
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001398 } elsif ($cur =~ /^(\?)/o) {
1399 print "QUESTION($1)\n" if ($dbg_values > 1);
1400 $type = 'N';
1401
1402 } elsif ($cur =~ /^(:)/o) {
1403 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1404
1405 substr($var, length($res), 1, $av_pend_colon);
1406 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1407 $type = 'E';
1408 } else {
1409 $type = 'N';
1410 }
1411 $av_pend_colon = 'O';
1412
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001413 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001414 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001415 $type = 'N';
1416
Andy Whitcroft0d413862008-10-15 22:02:16 -07001417 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001418 my $variant;
1419
1420 print "OPV($1)\n" if ($dbg_values > 1);
1421 if ($type eq 'V') {
1422 $variant = 'B';
1423 } else {
1424 $variant = 'U';
1425 }
1426
1427 substr($var, length($res), 1, $variant);
1428 $type = 'N';
1429
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001430 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001431 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001432 if ($1 ne '++' && $1 ne '--') {
1433 $type = 'N';
1434 }
1435
1436 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001438 }
1439 if (defined $1) {
1440 $cur = substr($cur, length($1));
1441 $res .= $type x length($1);
1442 }
1443 }
1444
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001445 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001446}
1447
Andy Whitcroft8905a672007-11-28 16:21:06 -08001448sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001449 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001450 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001451 ^(?:
1452 $Modifier|
1453 $Storage|
1454 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001455 DEFINE_\S+
1456 )$|
1457 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001458 goto|
1459 return|
1460 case|
1461 else|
1462 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001463 do|
1464 \#|
1465 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001466 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001467 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001468 )}x;
1469 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1470 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001471 # Check for modifiers.
1472 $possible =~ s/\s*$Storage\s*//g;
1473 $possible =~ s/\s*$Sparse\s*//g;
1474 if ($possible =~ /^\s*$/) {
1475
1476 } elsif ($possible =~ /\s/) {
1477 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001478 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001479 if ($modifier !~ $notPermitted) {
1480 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1481 push(@modifierList, $modifier);
1482 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001483 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001484
1485 } else {
1486 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1487 push(@typeList, $possible);
1488 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001489 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001490 } else {
1491 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001492 }
1493}
1494
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001495my $prefix = '';
1496
Joe Perches000d1cc12011-07-25 17:13:25 -07001497sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001498 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001499
Joe Perchescbec18a2014-04-03 14:49:19 -07001500 return defined $use_type{$type} if (scalar keys %use_type > 0);
1501
1502 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001503}
1504
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001505sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001506 my ($level, $type, $msg) = @_;
1507
1508 if (!show_type($type) ||
1509 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001510 return 0;
1511 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001512 my $line;
1513 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001514 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001515 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001516 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001517 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001518 $line = (split('\n', $line))[0] . "\n" if ($terse);
1519
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001520 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001521
1522 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001523}
Joe Perchescbec18a2014-04-03 14:49:19 -07001524
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001525sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001526 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001527}
Joe Perches000d1cc12011-07-25 17:13:25 -07001528
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001529sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001530 my ($type, $msg) = @_;
1531
1532 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001533 our $clean = 0;
1534 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001535 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001536 }
Joe Perches3705ce52013-07-03 15:05:31 -07001537 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001538}
1539sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001540 my ($type, $msg) = @_;
1541
1542 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001543 our $clean = 0;
1544 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001545 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001546 }
Joe Perches3705ce52013-07-03 15:05:31 -07001547 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001548}
1549sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001550 my ($type, $msg) = @_;
1551
1552 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001553 our $clean = 0;
1554 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001555 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001556 }
Joe Perches3705ce52013-07-03 15:05:31 -07001557 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001558}
1559
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001560sub check_absolute_file {
1561 my ($absolute, $herecurr) = @_;
1562 my $file = $absolute;
1563
1564 ##print "absolute<$absolute>\n";
1565
1566 # See if any suffix of this path is a path within the tree.
1567 while ($file =~ s@^[^/]*/@@) {
1568 if (-f "$root/$file") {
1569 ##print "file<$file>\n";
1570 last;
1571 }
1572 }
1573 if (! -f _) {
1574 return 0;
1575 }
1576
1577 # It is, so see if the prefix is acceptable.
1578 my $prefix = $absolute;
1579 substr($prefix, -length($file)) = '';
1580
1581 ##print "prefix<$prefix>\n";
1582 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001583 WARN("USE_RELATIVE_PATH",
1584 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001585 }
1586}
1587
Joe Perches3705ce52013-07-03 15:05:31 -07001588sub trim {
1589 my ($string) = @_;
1590
Joe Perchesb34c6482013-09-11 14:24:01 -07001591 $string =~ s/^\s+|\s+$//g;
1592
1593 return $string;
1594}
1595
1596sub ltrim {
1597 my ($string) = @_;
1598
1599 $string =~ s/^\s+//;
1600
1601 return $string;
1602}
1603
1604sub rtrim {
1605 my ($string) = @_;
1606
1607 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001608
1609 return $string;
1610}
1611
Joe Perches52ea8502013-11-12 15:10:09 -08001612sub string_find_replace {
1613 my ($string, $find, $replace) = @_;
1614
1615 $string =~ s/$find/$replace/g;
1616
1617 return $string;
1618}
1619
Joe Perches3705ce52013-07-03 15:05:31 -07001620sub tabify {
1621 my ($leading) = @_;
1622
1623 my $source_indent = 8;
1624 my $max_spaces_before_tab = $source_indent - 1;
1625 my $spaces_to_tab = " " x $source_indent;
1626
1627 #convert leading spaces to tabs
1628 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1629 #Remove spaces before a tab
1630 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1631
1632 return "$leading";
1633}
1634
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001635sub pos_last_openparen {
1636 my ($line) = @_;
1637
1638 my $pos = 0;
1639
1640 my $opens = $line =~ tr/\(/\(/;
1641 my $closes = $line =~ tr/\)/\)/;
1642
1643 my $last_openparen = 0;
1644
1645 if (($opens == 0) || ($closes >= $opens)) {
1646 return -1;
1647 }
1648
1649 my $len = length($line);
1650
1651 for ($pos = 0; $pos < $len; $pos++) {
1652 my $string = substr($line, $pos);
1653 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1654 $pos += length($1) - 1;
1655 } elsif (substr($line, $pos, 1) eq '(') {
1656 $last_openparen = $pos;
1657 } elsif (index($string, '(') == -1) {
1658 last;
1659 }
1660 }
1661
Joe Perches91cb5192014-04-03 14:49:32 -07001662 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001663}
1664
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001665sub process {
1666 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001667
1668 my $linenr=0;
1669 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001670 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001671 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001672 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001673
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001674 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001675 my $indent;
1676 my $previndent=0;
1677 my $stashindent=0;
1678
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001679 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001680 my $signoff = 0;
1681 my $is_patch = 0;
1682
Joe Perches29ee1b02014-08-06 16:10:35 -07001683 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001684 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001685 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001686 my $non_utf8_charset = 0;
1687
Joe Perches365dd4e2014-08-06 16:10:42 -07001688 my $last_blank_line = 0;
1689
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001690 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001691 our $cnt_lines = 0;
1692 our $cnt_error = 0;
1693 our $cnt_warn = 0;
1694 our $cnt_chk = 0;
1695
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001696 # Trace the real file/line as we go.
1697 my $realfile = '';
1698 my $realline = 0;
1699 my $realcnt = 0;
1700 my $here = '';
1701 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001702 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001703 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001704 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001705
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001706 my $prev_values = 'E';
1707
1708 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001709 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001710 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001711 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001712 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001713
Joe Perches7e51f192013-09-11 14:23:57 -07001714 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001715
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001716 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001717 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001719 my @setup_docs = ();
1720 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001721
Joe Perchesd8b07712013-11-12 15:10:06 -08001722 my $camelcase_file_seeded = 0;
1723
Andy Whitcroft773647a2008-03-28 14:15:58 -07001724 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 my $line;
1726 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001727 $linenr++;
1728 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729
Joe Perches3705ce52013-07-03 15:05:31 -07001730 push(@fixed, $rawline) if ($fix);
1731
Andy Whitcroft773647a2008-03-28 14:15:58 -07001732 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001733 $setup_docs = 0;
1734 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1735 $setup_docs = 1;
1736 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001737 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001738 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001739 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1740 $realline=$1-1;
1741 if (defined $2) {
1742 $realcnt=$3+1;
1743 } else {
1744 $realcnt=1+1;
1745 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001746 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001747
1748 # Guestimate if this is a continuing comment. Run
1749 # the context looking for a comment "edge". If this
1750 # edge is a close comment then we must be in a comment
1751 # at context start.
1752 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001753 my $cnt = $realcnt;
1754 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1755 next if (defined $rawlines[$ln - 1] &&
1756 $rawlines[$ln - 1] =~ /^-/);
1757 $cnt--;
1758 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001759 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001760 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1761 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1762 ($edge) = $1;
1763 last;
1764 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001765 }
1766 if (defined $edge && $edge eq '*/') {
1767 $in_comment = 1;
1768 }
1769
1770 # Guestimate if this is a continuing comment. If this
1771 # is the start of a diff block and this line starts
1772 # ' *' then it is very likely a comment.
1773 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001774 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001775 {
1776 $in_comment = 1;
1777 }
1778
1779 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1780 sanitise_line_reset($in_comment);
1781
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001782 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001783 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001784 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001785 $line = sanitise_line($rawline);
1786 }
1787 push(@lines, $line);
1788
1789 if ($realcnt > 1) {
1790 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1791 } else {
1792 $realcnt = 0;
1793 }
1794
1795 #print "==>$rawline\n";
1796 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001797
1798 if ($setup_docs && $line =~ /^\+/) {
1799 push(@setup_docs, $line);
1800 }
1801 }
1802
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 $prefix = '';
1804
Andy Whitcroft773647a2008-03-28 14:15:58 -07001805 $realcnt = 0;
1806 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001807 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001808 foreach my $line (@lines) {
1809 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001810 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001811 my $sline = $line; #copy of $line
1812 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001813
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001814 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001816#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001817 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001818 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001819 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001820 $realline=$1-1;
1821 if (defined $2) {
1822 $realcnt=$3+1;
1823 } else {
1824 $realcnt=1+1;
1825 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001826 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001827 $prev_values = 'E';
1828
Andy Whitcroft773647a2008-03-28 14:15:58 -07001829 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001830 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001831 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001832 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001833 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001834
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001835# track the line number as we move through the hunk, note that
1836# new versions of GNU diff omit the leading space on completely
1837# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001838 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001839 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001840 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001841
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001842 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001843 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001844
1845 # Track the previous line.
1846 ($prevline, $stashline) = ($stashline, $line);
1847 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1849
Andy Whitcroft773647a2008-03-28 14:15:58 -07001850 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001851
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001852 } elsif ($realcnt == 1) {
1853 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001854 }
1855
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001856 my $hunk_line = ($realcnt != 0);
1857
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001858#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001859 $prefix = "$filename:$realline: " if ($emacs && $file);
1860 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1861
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001862 $here = "#$linenr: " if (!$file);
1863 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001864
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001865 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001866 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001867 if ($line =~ /^diff --git.*?(\S+)$/) {
1868 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001869 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001870 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001871 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001872 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001874 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001875 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001876
1877 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001878 if (!$file && $tree && $p1_prefix ne '' &&
1879 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001880 WARN("PATCH_PREFIX",
1881 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001882 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001883
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001884 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001885 ERROR("MODIFIED_INCLUDE_ASM",
1886 "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 -07001887 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001888 $found_file = 1;
1889 }
1890
1891 if ($found_file) {
1892 if ($realfile =~ m@^(drivers/net/|net/)@) {
1893 $check = 1;
1894 } else {
1895 $check = $check_orig;
1896 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001897 next;
1898 }
1899
Randy Dunlap389834b2007-06-08 13:47:03 -07001900 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001901
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001902 my $hereline = "$here\n$rawline\n";
1903 my $herecurr = "$here\n$rawline\n";
1904 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001905
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001906 $cnt_lines++ if ($realcnt != 0);
1907
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001908# Check for incorrect file permissions
1909 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1910 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001911 if ($realfile !~ m@scripts/@ &&
1912 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001913 ERROR("EXECUTE_PERMISSIONS",
1914 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001915 }
1916 }
1917
Joe Perches20112472011-07-25 17:13:23 -07001918# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001919 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001920 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001921 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001922 }
1923
1924# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001925 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001926 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001927 my $space_before = $1;
1928 my $sign_off = $2;
1929 my $space_after = $3;
1930 my $email = $4;
1931 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1932
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001933 if ($sign_off !~ /$signature_tags/) {
1934 WARN("BAD_SIGN_OFF",
1935 "Non-standard signature: $sign_off\n" . $herecurr);
1936 }
Joe Perches20112472011-07-25 17:13:23 -07001937 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001938 if (WARN("BAD_SIGN_OFF",
1939 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1940 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07001941 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07001942 "$ucfirst_sign_off $email";
1943 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001944 }
Joe Perches20112472011-07-25 17:13:23 -07001945 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001946 if (WARN("BAD_SIGN_OFF",
1947 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1948 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07001949 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07001950 "$ucfirst_sign_off $email";
1951 }
1952
Joe Perches20112472011-07-25 17:13:23 -07001953 }
1954 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001955 if (WARN("BAD_SIGN_OFF",
1956 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1957 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07001958 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07001959 "$ucfirst_sign_off $email";
1960 }
Joe Perches20112472011-07-25 17:13:23 -07001961 }
1962
1963 my ($email_name, $email_address, $comment) = parse_email($email);
1964 my $suggested_email = format_email(($email_name, $email_address));
1965 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001966 ERROR("BAD_SIGN_OFF",
1967 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001968 } else {
1969 my $dequoted = $suggested_email;
1970 $dequoted =~ s/^"//;
1971 $dequoted =~ s/" </ </;
1972 # Don't force email to have quotes
1973 # Allow just an angle bracketed address
1974 if ("$dequoted$comment" ne $email &&
1975 "<$email_address>$comment" ne $email &&
1976 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001977 WARN("BAD_SIGN_OFF",
1978 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001979 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001980 }
Joe Perches7e51f192013-09-11 14:23:57 -07001981
1982# Check for duplicate signatures
1983 my $sig_nospace = $line;
1984 $sig_nospace =~ s/\s//g;
1985 $sig_nospace = lc($sig_nospace);
1986 if (defined $signatures{$sig_nospace}) {
1987 WARN("BAD_SIGN_OFF",
1988 "Duplicate signature\n" . $herecurr);
1989 } else {
1990 $signatures{$sig_nospace} = 1;
1991 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001992 }
1993
Joe Perches9b3189e2014-06-04 16:12:10 -07001994# Check for old stable address
1995 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
1996 ERROR("STABLE_ADDRESS",
1997 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
1998 }
1999
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002000# Check for unwanted Gerrit info
2001 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2002 ERROR("GERRIT_CHANGE_ID",
2003 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2004 }
2005
Joe Perchesd311cd42014-08-06 16:10:57 -07002006# Check for improperly formed commit descriptions
2007 if ($in_commit_log &&
2008 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2009 $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2010 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2011 my $init_char = $1;
2012 my $orig_commit = lc($2);
2013 my $id = '01234567890ab';
2014 my $desc = 'commit description';
2015 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2016 ERROR("GIT_COMMIT_ID",
2017 "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2018 }
2019
Joe Perches13f19372014-08-06 16:10:59 -07002020# Check for added, moved or deleted files
2021 if (!$reported_maintainer_file && !$in_commit_log &&
2022 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2023 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2024 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2025 (defined($1) || defined($2))))) {
2026 $reported_maintainer_file = 1;
2027 WARN("FILE_PATH_CHANGES",
2028 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2029 }
2030
Andy Whitcroft00df3442007-06-08 13:47:06 -07002031# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002032 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002033 ERROR("CORRUPTED_PATCH",
2034 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002035 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002036 }
2037
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002038# Check for absolute kernel paths.
2039 if ($tree) {
2040 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2041 my $file = $1;
2042
2043 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2044 check_absolute_file($1, $herecurr)) {
2045 #
2046 } else {
2047 check_absolute_file($file, $herecurr);
2048 }
2049 }
2050 }
2051
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002052# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2053 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002054 $rawline !~ m/^$UTF8*$/) {
2055 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2056
2057 my $blank = copy_spacing($rawline);
2058 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2059 my $hereptr = "$hereline$ptr\n";
2060
Joe Perches34d99212011-07-25 17:13:26 -07002061 CHK("INVALID_UTF8",
2062 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002063 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002064
Joe Perches15662b32011-10-31 17:13:12 -07002065# Check if it's the start of a commit log
2066# (not a header line and we haven't seen the patch filename)
2067 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002068 !($rawline =~ /^\s+\S/ ||
2069 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002070 $in_header_lines = 0;
2071 $in_commit_log = 1;
2072 }
2073
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002074# Check if there is UTF-8 in a commit log when a mail header has explicitly
2075# declined it, i.e defined some charset where it is missing.
2076 if ($in_header_lines &&
2077 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2078 $1 !~ /utf-8/i) {
2079 $non_utf8_charset = 1;
2080 }
2081
2082 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002083 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002084 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002085 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2086 }
2087
Andy Whitcroft306708542008-10-15 22:02:28 -07002088# ignore non-hunk lines and lines being removed
2089 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002090
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002092 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002093 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002094 if (ERROR("DOS_LINE_ENDINGS",
2095 "DOS line endings\n" . $herevet) &&
2096 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002097 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002098 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002099 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2100 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002101 if (ERROR("TRAILING_WHITESPACE",
2102 "trailing whitespace\n" . $herevet) &&
2103 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002104 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002105 }
2106
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002107 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002108 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002109
Josh Triplett4783f892013-11-12 15:10:12 -08002110# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002111 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002112 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2113 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2115 my $msg_type = \&ERROR;
2116 $msg_type = \&CHK if ($file);
2117 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002118 "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 -08002119 }
2120
Andi Kleen33549572010-05-24 14:33:29 -07002121# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002122# Only applies when adding the entry originally, after that we do not have
2123# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002124 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002125 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002126 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002127 my $cnt = $realcnt;
2128 my $ln = $linenr + 1;
2129 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002130 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002131 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002132 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002133 $f = $lines[$ln - 1];
2134 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2135 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002136
2137 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002138 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002139
Joe Perches8d73e0e2014-08-06 16:10:46 -07002140 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002141 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002142 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002143 $length = -1;
2144 }
2145
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002146 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002147 $f =~ s/#.*//;
2148 $f =~ s/^\s+//;
2149 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002150 if ($f =~ /^\s*config\s/) {
2151 $is_end = 1;
2152 last;
2153 }
Andi Kleen33549572010-05-24 14:33:29 -07002154 $length++;
2155 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002156 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08002157 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2158 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002159 }
2160
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002161# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2162 if ($realfile =~ /Kconfig/ &&
2163 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2164 WARN("CONFIG_EXPERIMENTAL",
2165 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2166 }
2167
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002168 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2169 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2170 my $flag = $1;
2171 my $replacement = {
2172 'EXTRA_AFLAGS' => 'asflags-y',
2173 'EXTRA_CFLAGS' => 'ccflags-y',
2174 'EXTRA_CPPFLAGS' => 'cppflags-y',
2175 'EXTRA_LDFLAGS' => 'ldflags-y',
2176 };
2177
2178 WARN("DEPRECATED_VARIABLE",
2179 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2180 }
2181
Rob Herringbff5da42014-01-23 15:54:51 -08002182# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002183 if (defined $root &&
2184 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2185 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2186
Rob Herringbff5da42014-01-23 15:54:51 -08002187 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2188
Florian Vaussardcc933192014-04-03 14:49:27 -07002189 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2190 my $vp_file = $dt_path . "vendor-prefixes.txt";
2191
Rob Herringbff5da42014-01-23 15:54:51 -08002192 foreach my $compat (@compats) {
2193 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002194 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2195 my $compat3 = $compat;
2196 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2197 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002198 if ( $? >> 8 ) {
2199 WARN("UNDOCUMENTED_DT_STRING",
2200 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2201 }
2202
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002203 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2204 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002205 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002206 if ( $? >> 8 ) {
2207 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002208 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002209 }
2210 }
2211 }
2212
Andy Whitcroft5368df22008-10-15 22:02:27 -07002213# check we are in a valid source file if not then ignore this hunk
2214 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2215
Joe Perches6cd7f382012-12-17 16:01:54 -08002216#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002217 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002218 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002219 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002220 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002221 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002222 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002223 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002224 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002225 }
2226
Josh Triplettca56dc02012-03-23 15:02:21 -07002227# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002228# to grep for the string. Make exceptions when the previous string ends in a
2229# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2230# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002231 if ($line =~ /^\+\s*"/ &&
2232 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002233 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002234 WARN("SPLIT_STRING",
2235 "quoted string split across lines\n" . $hereprev);
2236 }
2237
Joe Perches5e79d962010-03-05 13:43:55 -08002238# check for spaces before a quoted newline
2239 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002240 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2241 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2242 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002243 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002244 }
2245
Joe Perches5e79d962010-03-05 13:43:55 -08002246 }
2247
Andy Whitcroft8905a672007-11-28 16:21:06 -08002248# check for adding lines without a newline.
2249 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002250 WARN("MISSING_EOF_NEWLINE",
2251 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002252 }
2253
Mike Frysinger42e41c52009-09-21 17:04:40 -07002254# Blackfin: use hi/lo macros
2255 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2256 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2257 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002258 ERROR("LO_MACRO",
2259 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002260 }
2261 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2262 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002263 ERROR("HI_MACRO",
2264 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002265 }
2266 }
2267
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002268# check we are in a valid source file C or perl if not then ignore this hunk
2269 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002270
2271# at the beginning of a line any tabs must come first and anything
2272# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002273 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2274 $rawline =~ /^\+\s* \s*/) {
2275 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002276 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002277 if (ERROR("CODE_INDENT",
2278 "code indent should use tabs where possible\n" . $herevet) &&
2279 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002280 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002281 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002282 }
2283
Alberto Panizzo08e44362010-03-05 13:43:54 -08002284# check for space before tabs.
2285 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2286 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002287 if (WARN("SPACE_BEFORE_TAB",
2288 "please, no space before tabs\n" . $herevet) &&
2289 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002290 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002291 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002292 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002293 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002294 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002295 }
2296
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002297# check for && or || at the start of a line
2298 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2299 CHK("LOGICAL_CONTINUATIONS",
2300 "Logical continuations should be on the previous line\n" . $hereprev);
2301 }
2302
2303# check multi-line statement indentation matches previous line
2304 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002305 $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 -07002306 $prevline =~ /^\+(\t*)(.*)$/;
2307 my $oldindent = $1;
2308 my $rest = $2;
2309
2310 my $pos = pos_last_openparen($rest);
2311 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002312 $line =~ /^(\+| )([ \t]*)/;
2313 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002314
2315 my $goodtabindent = $oldindent .
2316 "\t" x ($pos / 8) .
2317 " " x ($pos % 8);
2318 my $goodspaceindent = $oldindent . " " x $pos;
2319
2320 if ($newindent ne $goodtabindent &&
2321 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002322
2323 if (CHK("PARENTHESIS_ALIGNMENT",
2324 "Alignment should match open parenthesis\n" . $hereprev) &&
2325 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002326 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002327 s/^\+[ \t]*/\+$goodtabindent/;
2328 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002329 }
2330 }
2331 }
2332
Joe Perchesf27c95d2014-08-06 16:10:52 -07002333 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002334 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002335 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002336 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002337 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002338 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002339 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002340 }
2341
Joe Perches05880602012-10-04 17:13:35 -07002342 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002343 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002344 $rawline =~ /^\+[ \t]*\*/ &&
2345 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002346 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2347 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2348 }
2349
2350 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002351 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2352 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002353 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002354 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2355 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2356 "networking block comments start with * on subsequent lines\n" . $hereprev);
2357 }
2358
2359 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002360 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2361 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2362 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2363 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002364 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2365 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2366 }
2367
Joe Perches7f619192014-08-06 16:10:39 -07002368# check for missing blank lines after struct/union declarations
2369# with exceptions for various attributes and macros
2370 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2371 $line =~ /^\+/ &&
2372 !($line =~ /^\+\s*$/ ||
2373 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2374 $line =~ /^\+\s*MODULE_/i ||
2375 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2376 $line =~ /^\+[a-z_]*init/ ||
2377 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2378 $line =~ /^\+\s*DECLARE/ ||
2379 $line =~ /^\+\s*__setup/)) {
2380 CHK("LINE_SPACING",
2381 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev);
2382 }
2383
Joe Perches365dd4e2014-08-06 16:10:42 -07002384# check for multiple consecutive blank lines
2385 if ($prevline =~ /^[\+ ]\s*$/ &&
2386 $line =~ /^\+\s*$/ &&
2387 $last_blank_line != ($linenr - 1)) {
2388 CHK("LINE_SPACING",
2389 "Please don't use multiple blank lines\n" . $hereprev);
2390 $last_blank_line = $linenr;
2391 }
2392
Joe Perches3b617e32014-04-03 14:49:28 -07002393# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002394 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2395 # actual declarations
2396 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002397 # function pointer declarations
2398 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002399 # foo bar; where foo is some local typedef or #define
2400 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2401 # known declaration macros
2402 $prevline =~ /^\+\s+$declaration_macros/) &&
2403 # for "else if" which can look like "$Ident $Ident"
2404 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2405 # other possible extensions of declaration lines
2406 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2407 # not starting a section or a macro "\" extended line
2408 $prevline =~ /(?:\{\s*|\\)$/) &&
2409 # looks like a declaration
2410 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002411 # function pointer declarations
2412 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002413 # foo bar; where foo is some local typedef or #define
2414 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2415 # known declaration macros
2416 $sline =~ /^\+\s+$declaration_macros/ ||
2417 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002418 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002419 # start or end of block or continuation of declaration
2420 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2421 # bitfield continuation
2422 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2423 # other possible extensions of declaration lines
2424 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2425 # indentation of previous and current line are the same
2426 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesfee0aa82014-08-06 16:10:44 -07002427 WARN("LINE_SPACING",
Joe Perches3f7bac02014-06-04 16:12:04 -07002428 "Missing a blank line after declarations\n" . $hereprev);
Joe Perches3b617e32014-04-03 14:49:28 -07002429 }
2430
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002431# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002432# Exceptions:
2433# 1) within comments
2434# 2) indented preprocessor commands
2435# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002436 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002437 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002438 if (WARN("LEADING_SPACE",
2439 "please, no spaces at the start of a line\n" . $herevet) &&
2440 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002441 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002442 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002443 }
2444
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002445# check we are in a valid C source file if not then ignore this hunk
2446 next if ($realfile !~ /\.(h|c)$/);
2447
Joe Perches032a4c02014-08-06 16:10:29 -07002448# check indentation of any line with a bare else
2449# if the previous line is a break or return and is indented 1 tab more...
2450 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2451 my $tabs = length($1) + 1;
2452 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2453 WARN("UNNECESSARY_ELSE",
2454 "else is not generally useful after a break or return\n" . $hereprev);
2455 }
2456 }
2457
Joe Perchesc00df192014-08-06 16:11:01 -07002458# check indentation of a line with a break;
2459# if the previous line is a goto or return and is indented the same # of tabs
2460 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2461 my $tabs = $1;
2462 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2463 WARN("UNNECESSARY_BREAK",
2464 "break is not useful after a goto or return\n" . $hereprev);
2465 }
2466 }
2467
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002468# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2469 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2470 WARN("CONFIG_EXPERIMENTAL",
2471 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2472 }
2473
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002474# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002475 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002476 WARN("CVS_KEYWORD",
2477 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002478 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002479
Mike Frysinger42e41c52009-09-21 17:04:40 -07002480# Blackfin: don't use __builtin_bfin_[cs]sync
2481 if ($line =~ /__builtin_bfin_csync/) {
2482 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002483 ERROR("CSYNC",
2484 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002485 }
2486 if ($line =~ /__builtin_bfin_ssync/) {
2487 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002488 ERROR("SSYNC",
2489 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002490 }
2491
Joe Perches56e77d72013-02-21 16:44:14 -08002492# check for old HOTPLUG __dev<foo> section markings
2493 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2494 WARN("HOTPLUG_SECTION",
2495 "Using $1 is unnecessary\n" . $herecurr);
2496 }
2497
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002498# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002499 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2500 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002501#print "LINE<$line>\n";
2502 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002503 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002504 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002505 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002506 $stat =~ s/\n./\n /g;
2507 $cond =~ s/\n./\n /g;
2508
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002509#print "linenr<$linenr> <$stat>\n";
2510 # If this statement has no statement boundaries within
2511 # it there is no point in retrying a statement scan
2512 # until we hit end of it.
2513 my $frag = $stat; $frag =~ s/;+\s*$//;
2514 if ($frag !~ /(?:{|;)/) {
2515#print "skip<$line_nr_next>\n";
2516 $suppress_statement = $line_nr_next;
2517 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002518
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002519 # Find the real next line.
2520 $realline_next = $line_nr_next;
2521 if (defined $realline_next &&
2522 (!defined $lines[$realline_next - 1] ||
2523 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2524 $realline_next++;
2525 }
2526
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002527 my $s = $stat;
2528 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002529
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002530 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002531 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002532
2533 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002534 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002535
Andy Whitcroft463f2862009-09-21 17:04:34 -07002536 } elsif ($s =~ /^.\s*else\b/s) {
2537
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002538 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002539 } 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 -07002540 my $type = $1;
2541 $type =~ s/\s+/ /g;
2542 possible($type, "A:" . $s);
2543
Andy Whitcroft8905a672007-11-28 16:21:06 -08002544 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002545 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002546 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002547 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002548
2549 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002550 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002551 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002552 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002553
2554 # Check for any sort of function declaration.
2555 # int foo(something bar, other baz);
2556 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002557 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 -08002558 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002559
Andy Whitcroftcf655042008-03-04 14:28:20 -08002560 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002561 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002562 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002563
Andy Whitcroft8905a672007-11-28 16:21:06 -08002564 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002565 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002566
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002567 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002568 }
2569 }
2570 }
2571
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002572 }
2573
Andy Whitcroft653d4872007-06-23 17:16:34 -07002574#
2575# Checks which may be anchored in the context.
2576#
2577
2578# Check for switch () and associated case and default
2579# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002580 if ($line=~/\bswitch\s*\(.*\)/) {
2581 my $err = '';
2582 my $sep = '';
2583 my @ctx = ctx_block_outer($linenr, $realcnt);
2584 shift(@ctx);
2585 for my $ctx (@ctx) {
2586 my ($clen, $cindent) = line_stats($ctx);
2587 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2588 $indent != $cindent) {
2589 $err .= "$sep$ctx\n";
2590 $sep = '';
2591 } else {
2592 $sep = "[...]\n";
2593 }
2594 }
2595 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002596 ERROR("SWITCH_CASE_INDENT_LEVEL",
2597 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002598 }
2599 }
2600
2601# if/while/etc brace do not go on next line, unless defining a do while loop,
2602# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002603 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002604 my $pre_ctx = "$1$2";
2605
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002606 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002607
2608 if ($line =~ /^\+\t{6,}/) {
2609 WARN("DEEP_INDENTATION",
2610 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2611 }
2612
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002613 my $ctx_cnt = $realcnt - $#ctx - 1;
2614 my $ctx = join("\n", @ctx);
2615
Andy Whitcroft548596d2008-07-23 21:29:01 -07002616 my $ctx_ln = $linenr;
2617 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002618
Andy Whitcroft548596d2008-07-23 21:29:01 -07002619 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2620 defined $lines[$ctx_ln - 1] &&
2621 $lines[$ctx_ln - 1] =~ /^-/)) {
2622 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2623 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002624 $ctx_ln++;
2625 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002626
Andy Whitcroft53210162008-07-23 21:29:03 -07002627 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2628 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002629
2630 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002631 ERROR("OPEN_BRACE",
2632 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002633 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002634 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002635 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2636 $ctx =~ /\)\s*\;\s*$/ &&
2637 defined $lines[$ctx_ln - 1])
2638 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002639 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2640 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002641 WARN("TRAILING_SEMICOLON",
2642 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002643 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002644 }
2645 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002646 }
2647
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002648# Check relative indent for conditionals and blocks.
2649 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002650 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2651 ctx_statement_block($linenr, $realcnt, 0)
2652 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002653 my ($s, $c) = ($stat, $cond);
2654
2655 substr($s, 0, length($c), '');
2656
2657 # Make sure we remove the line prefixes as we have
2658 # none on the first line, and are going to readd them
2659 # where necessary.
2660 $s =~ s/\n./\n/gs;
2661
2662 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002663 my @newlines = ($c =~ /\n/gs);
2664 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002665
2666 # We want to check the first line inside the block
2667 # starting at the end of the conditional, so remove:
2668 # 1) any blank line termination
2669 # 2) any opening brace { on end of the line
2670 # 3) any do (...) {
2671 my $continuation = 0;
2672 my $check = 0;
2673 $s =~ s/^.*\bdo\b//;
2674 $s =~ s/^\s*{//;
2675 if ($s =~ s/^\s*\\//) {
2676 $continuation = 1;
2677 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002678 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002679 $check = 1;
2680 $cond_lines++;
2681 }
2682
2683 # Also ignore a loop construct at the end of a
2684 # preprocessor statement.
2685 if (($prevline =~ /^.\s*#\s*define\s/ ||
2686 $prevline =~ /\\\s*$/) && $continuation == 0) {
2687 $check = 0;
2688 }
2689
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002690 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002691 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002692 while ($cond_ptr != $cond_lines) {
2693 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002694
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002695 # If we see an #else/#elif then the code
2696 # is not linear.
2697 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2698 $check = 0;
2699 }
2700
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002701 # Ignore:
2702 # 1) blank lines, they should be at 0,
2703 # 2) preprocessor lines, and
2704 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002705 if ($continuation ||
2706 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002707 $s =~ /^\s*#\s*?/ ||
2708 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002709 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002710 if ($s =~ s/^.*?\n//) {
2711 $cond_lines++;
2712 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002713 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002714 }
2715
2716 my (undef, $sindent) = line_stats("+" . $s);
2717 my $stat_real = raw_line($linenr, $cond_lines);
2718
2719 # Check if either of these lines are modified, else
2720 # this is not this patch's fault.
2721 if (!defined($stat_real) ||
2722 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2723 $check = 0;
2724 }
2725 if (defined($stat_real) && $cond_lines > 1) {
2726 $stat_real = "[...]\n$stat_real";
2727 }
2728
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002729 #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 -07002730
2731 if ($check && (($sindent % 8) != 0 ||
2732 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002733 WARN("SUSPECT_CODE_INDENT",
2734 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002735 }
2736 }
2737
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002738 # Track the 'values' across context and added lines.
2739 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002740 my ($curr_values, $curr_vars) =
2741 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002742 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002743 if ($dbg_values) {
2744 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002745 print "$linenr > .$outline\n";
2746 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002747 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002748 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002749 $prev_values = substr($curr_values, -1);
2750
Andy Whitcroft00df3442007-06-08 13:47:06 -07002751#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002752 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002753
Andy Whitcroft653d4872007-06-23 17:16:34 -07002754# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002755 if ($dbg_type) {
2756 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002757 ERROR("TEST_TYPE",
2758 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002759 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002760 ERROR("TEST_NOT_TYPE",
2761 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002762 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002763 next;
2764 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002765# TEST: allow direct testing of the attribute matcher.
2766 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002767 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002768 ERROR("TEST_ATTR",
2769 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002770 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002771 ERROR("TEST_NOT_ATTR",
2772 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002773 }
2774 next;
2775 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002776
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002777# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002778 if ($line =~ /^.\s*{/ &&
2779 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002780 ERROR("OPEN_BRACE",
2781 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002782 }
2783
Andy Whitcroft653d4872007-06-23 17:16:34 -07002784#
2785# Checks which are anchored on the added line.
2786#
2787
2788# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002789 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002790 my $path = $1;
2791 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002792 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002793 "malformed #include filename\n" . $herecurr);
2794 }
2795 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2796 ERROR("UAPI_INCLUDE",
2797 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002798 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002799 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002800
2801# no C99 // comments
2802 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002803 if (ERROR("C99_COMMENTS",
2804 "do not use C99 // comments\n" . $herecurr) &&
2805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002806 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07002807 if ($line =~ /\/\/(.*)$/) {
2808 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07002809 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07002810 }
2811 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002812 }
2813 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002814 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002815 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002816
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002817# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2818# the whole statement.
2819#print "APW <$lines[$realline_next - 1]>\n";
2820 if (defined $realline_next &&
2821 exists $lines[$realline_next - 1] &&
2822 !defined $suppress_export{$realline_next} &&
2823 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2824 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002825 # Handle definitions which produce identifiers with
2826 # a prefix:
2827 # XXX(foo);
2828 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002829 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002830 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002831 $name =~ /^${Ident}_$2/) {
2832#print "FOO C name<$name>\n";
2833 $suppress_export{$realline_next} = 1;
2834
2835 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002836 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002837 ^.DEFINE_$Ident\(\Q$name\E\)|
2838 ^.DECLARE_$Ident\(\Q$name\E\)|
2839 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002840 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2841 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002842 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002843#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2844 $suppress_export{$realline_next} = 2;
2845 } else {
2846 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002847 }
2848 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002849 if (!defined $suppress_export{$linenr} &&
2850 $prevline =~ /^.\s*$/ &&
2851 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2852 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2853#print "FOO B <$lines[$linenr - 1]>\n";
2854 $suppress_export{$linenr} = 2;
2855 }
2856 if (defined $suppress_export{$linenr} &&
2857 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002858 WARN("EXPORT_SYMBOL",
2859 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002860 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002861
Joe Eloff5150bda2010-08-09 17:21:00 -07002862# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002863 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2864 if (ERROR("GLOBAL_INITIALISERS",
2865 "do not initialise globals to 0 or NULL\n" .
2866 $herecurr) &&
2867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002868 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002869 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002870 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002871# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002872 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2873 if (ERROR("INITIALISED_STATIC",
2874 "do not initialise statics to 0 or NULL\n" .
2875 $herecurr) &&
2876 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002877 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002878 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002879 }
2880
Joe Perchescb710ec2010-10-26 14:23:20 -07002881# check for static const char * arrays.
2882 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002883 WARN("STATIC_CONST_CHAR_ARRAY",
2884 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002885 $herecurr);
2886 }
2887
2888# check for static char foo[] = "bar" declarations.
2889 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002890 WARN("STATIC_CONST_CHAR_ARRAY",
2891 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002892 $herecurr);
2893 }
2894
Joe Perches9b0fa602014-04-03 14:49:18 -07002895# check for non-global char *foo[] = {"bar", ...} declarations.
2896 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
2897 WARN("STATIC_CONST_CHAR_ARRAY",
2898 "char * array declaration might be better as static const\n" .
2899 $herecurr);
2900 }
2901
Joe Perchesb36190c2014-01-27 17:07:18 -08002902# check for function declarations without arguments like "int foo()"
2903 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
2904 if (ERROR("FUNCTION_WITHOUT_ARGS",
2905 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
2906 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002907 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08002908 }
2909 }
2910
Joe Perches92e112f2013-12-13 11:36:22 -07002911# check for uses of DEFINE_PCI_DEVICE_TABLE
2912 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
2913 if (WARN("DEFINE_PCI_DEVICE_TABLE",
2914 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
2915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002916 $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 -07002917 }
Joe Perches93ed0e22010-10-26 14:23:21 -07002918 }
2919
Andy Whitcroft653d4872007-06-23 17:16:34 -07002920# check for new typedefs, only function parameters and sparse annotations
2921# make sense.
2922 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002923 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002924 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002925 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002926 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002927 WARN("NEW_TYPEDEFS",
2928 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002929 }
2930
2931# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002932 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002933 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2934 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002935 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002936
Andy Whitcroft65863862009-01-06 14:41:21 -08002937 # Should start with a space.
2938 $to =~ s/^(\S)/ $1/;
2939 # Should not end with a space.
2940 $to =~ s/\s+$//;
2941 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002942 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002943 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002944
Joe Perches3705ce52013-07-03 15:05:31 -07002945## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002946 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002947 if (ERROR("POINTER_LOCATION",
2948 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2949 $fix) {
2950 my $sub_from = $ident;
2951 my $sub_to = $ident;
2952 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07002953 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002954 s@\Q$sub_from\E@$sub_to@;
2955 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002956 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002957 }
2958 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2959 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002960 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002961
Andy Whitcroft65863862009-01-06 14:41:21 -08002962 # Should start with a space.
2963 $to =~ s/^(\S)/ $1/;
2964 # Should not end with a space.
2965 $to =~ s/\s+$//;
2966 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002967 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002968 }
2969 # Modifiers should have spaces.
2970 $to =~ s/(\b$Modifier$)/$1 /;
2971
Joe Perches3705ce52013-07-03 15:05:31 -07002972## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002973 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002974 if (ERROR("POINTER_LOCATION",
2975 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2976 $fix) {
2977
2978 my $sub_from = $match;
2979 my $sub_to = $match;
2980 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07002981 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002982 s@\Q$sub_from\E@$sub_to@;
2983 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002984 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002985 }
2986
2987# # no BUG() or BUG_ON()
2988# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2989# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2990# print "$herecurr";
2991# $clean = 0;
2992# }
2993
Andy Whitcroft8905a672007-11-28 16:21:06 -08002994 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002995 WARN("LINUX_VERSION_CODE",
2996 "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 -08002997 }
2998
Joe Perches17441222011-06-15 15:08:17 -07002999# check for uses of printk_ratelimit
3000 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003001 WARN("PRINTK_RATELIMITED",
3002"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003003 }
3004
Andy Whitcroft00df3442007-06-08 13:47:06 -07003005# printk should use KERN_* levels. Note that follow on printk's on the
3006# same line do not need a level, so we use the current block context
3007# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003008# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003009# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003010 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003011 my $ok = 0;
3012 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3013 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003014 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003015 # with "\n" ignore it, else it is to blame
3016 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3017 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3018 $ok = 1;
3019 }
3020 last;
3021 }
3022 }
3023 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003024 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3025 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003026 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003027 }
3028
Joe Perches243f3802012-05-31 16:26:09 -07003029 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3030 my $orig = $1;
3031 my $level = lc($orig);
3032 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003033 my $level2 = $level;
3034 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003035 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003036 "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 -07003037 }
3038
3039 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003040 if (WARN("PREFER_PR_LEVEL",
3041 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3042 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003043 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003044 s/\bpr_warning\b/pr_warn/;
3045 }
Joe Perches243f3802012-05-31 16:26:09 -07003046 }
3047
Joe Perchesdc139312013-02-21 16:44:13 -08003048 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3049 my $orig = $1;
3050 my $level = lc($orig);
3051 $level = "warn" if ($level eq "warning");
3052 $level = "dbg" if ($level eq "debug");
3053 WARN("PREFER_DEV_LEVEL",
3054 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3055 }
3056
Andy Whitcroft653d4872007-06-23 17:16:34 -07003057# function brace can't be on same line, except for #defines of do while,
3058# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003059 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
3060 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003061 ERROR("OPEN_BRACE",
3062 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003063 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003064
Andy Whitcroft8905a672007-11-28 16:21:06 -08003065# open braces for enum, union and struct go on the same line.
3066 if ($line =~ /^.\s*{/ &&
3067 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003068 ERROR("OPEN_BRACE",
3069 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003070 }
3071
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003072# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003073 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3074 if (WARN("SPACING",
3075 "missing space after $1 definition\n" . $herecurr) &&
3076 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003077 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003078 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3079 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003080 }
3081
Joe Perches31070b52014-01-23 15:54:49 -08003082# Function pointer declarations
3083# check spacing between type, funcptr, and args
3084# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003085 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003086 my $declare = $1;
3087 my $pre_pointer_space = $2;
3088 my $post_pointer_space = $3;
3089 my $funcname = $4;
3090 my $post_funcname_space = $5;
3091 my $pre_args_space = $6;
3092
Joe Perches91f72e92014-04-03 14:49:12 -07003093# the $Declare variable will capture all spaces after the type
3094# so check it for a missing trailing missing space but pointer return types
3095# don't need a space so don't warn for those.
3096 my $post_declare_space = "";
3097 if ($declare =~ /(\s+)$/) {
3098 $post_declare_space = $1;
3099 $declare = rtrim($declare);
3100 }
3101 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003102 WARN("SPACING",
3103 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003104 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003105 }
3106
3107# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003108# This test is not currently implemented because these declarations are
3109# equivalent to
3110# int foo(int bar, ...)
3111# and this is form shouldn't/doesn't generate a checkpatch warning.
3112#
3113# elsif ($declare =~ /\s{2,}$/) {
3114# WARN("SPACING",
3115# "Multiple spaces after return type\n" . $herecurr);
3116# }
Joe Perches31070b52014-01-23 15:54:49 -08003117
3118# unnecessary space "type ( *funcptr)(args...)"
3119 if (defined $pre_pointer_space &&
3120 $pre_pointer_space =~ /^\s/) {
3121 WARN("SPACING",
3122 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3123 }
3124
3125# unnecessary space "type (* funcptr)(args...)"
3126 if (defined $post_pointer_space &&
3127 $post_pointer_space =~ /^\s/) {
3128 WARN("SPACING",
3129 "Unnecessary space before function pointer name\n" . $herecurr);
3130 }
3131
3132# unnecessary space "type (*funcptr )(args...)"
3133 if (defined $post_funcname_space &&
3134 $post_funcname_space =~ /^\s/) {
3135 WARN("SPACING",
3136 "Unnecessary space after function pointer name\n" . $herecurr);
3137 }
3138
3139# unnecessary space "type (*funcptr) (args...)"
3140 if (defined $pre_args_space &&
3141 $pre_args_space =~ /^\s/) {
3142 WARN("SPACING",
3143 "Unnecessary space before function pointer arguments\n" . $herecurr);
3144 }
3145
3146 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003147 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003148 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003149 }
3150 }
3151
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003152# check for spacing round square brackets; allowed:
3153# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003154# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3155# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003156 while ($line =~ /(.*?\s)\[/g) {
3157 my ($where, $prefix) = ($-[1], $1);
3158 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003159 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003160 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003161 if (ERROR("BRACKET_SPACE",
3162 "space prohibited before open square bracket '['\n" . $herecurr) &&
3163 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003164 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003165 s/^(\+.*?)\s+\[/$1\[/;
3166 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003167 }
3168 }
3169
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003170# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003171 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003172 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003173 my $ctx_before = substr($line, 0, $-[1]);
3174 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003175
3176 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003177 if ($name =~ /^(?:
3178 if|for|while|switch|return|case|
3179 volatile|__volatile__|
3180 __attribute__|format|__extension__|
3181 asm|__asm__)$/x)
3182 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003183 # cpp #define statements have non-optional spaces, ie
3184 # if there is a space between the name and the open
3185 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003186 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003187
3188 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003189 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003190
3191 # If this whole things ends with a type its most
3192 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003193 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003194
3195 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003196 if (WARN("SPACING",
3197 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3198 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003199 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003200 s/\b$name\s+\(/$name\(/;
3201 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003202 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003203 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003204
Andy Whitcroft653d4872007-06-23 17:16:34 -07003205# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003206 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003207 my $fixed_line = "";
3208 my $line_fixed = 0;
3209
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003210 my $ops = qr{
3211 <<=|>>=|<=|>=|==|!=|
3212 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3213 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003214 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003215 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003216 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003217 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003218
3219## print("element count: <" . $#elements . ">\n");
3220## foreach my $el (@elements) {
3221## print("el: <$el>\n");
3222## }
3223
3224 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003225 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003226
Joe Perches3705ce52013-07-03 15:05:31 -07003227 foreach my $el (@elements) {
3228 push(@fix_elements, substr($rawline, $off, length($el)));
3229 $off += length($el);
3230 }
3231
3232 $off = 0;
3233
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003234 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003235 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003236
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003237 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003238
3239 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3240
3241## print("n: <$n> good: <$good>\n");
3242
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003243 $off += length($elements[$n]);
3244
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003245 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003246 my $ca = substr($opline, 0, $off);
3247 my $cc = '';
3248 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3249 $cc = substr($opline, $off + length($elements[$n + 1]));
3250 }
3251 my $cb = "$ca$;$cc";
3252
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003253 my $a = '';
3254 $a = 'V' if ($elements[$n] ne '');
3255 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003256 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003257 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3258 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003259 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003260
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003261 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003262
3263 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003264 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003265 $c = 'V' if ($elements[$n + 2] ne '');
3266 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003267 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003268 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3269 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003270 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003271 } else {
3272 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003273 }
3274
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003275 my $ctx = "${a}x${c}";
3276
3277 my $at = "(ctx:$ctx)";
3278
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003279 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003280 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003281
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003282 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003283 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003284
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003285 # Get the full operator variant.
3286 my $opv = $op . substr($curr_vars, $off, 1);
3287
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003288 # Ignore operators passed as parameters.
3289 if ($op_type ne 'V' &&
3290 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3291
Andy Whitcroftcf655042008-03-04 14:28:20 -08003292# # Ignore comments
3293# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003294
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003295 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003296 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003297 if ($ctx !~ /.x[WEBC]/ &&
3298 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003299 if (ERROR("SPACING",
3300 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003301 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003302 $line_fixed = 1;
3303 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003304 }
3305
3306 # // is a comment
3307 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003308
Joe Perchesb00e4812014-04-03 14:49:33 -07003309 # : when part of a bitfield
3310 } elsif ($opv eq ':B') {
3311 # skip the bitfield test for now
3312
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003313 # No spaces for:
3314 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003315 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003316 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003317 if (ERROR("SPACING",
3318 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003319 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003320 if (defined $fix_elements[$n + 2]) {
3321 $fix_elements[$n + 2] =~ s/^\s+//;
3322 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003323 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003324 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003325 }
3326
3327 # , must have a space on the right.
3328 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003329 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003330 if (ERROR("SPACING",
3331 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003332 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003333 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003334 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07003335 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003336 }
3337
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003338 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003339 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003340 #warn "'*' is part of type\n";
3341
3342 # unary operators should have a space before and
3343 # none after. May be left adjacent to another
3344 # unary operator, or a cast
3345 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003346 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003347 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003348 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003349 if (ERROR("SPACING",
3350 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003351 if ($n != $last_after + 2) {
3352 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3353 $line_fixed = 1;
3354 }
Joe Perches3705ce52013-07-03 15:05:31 -07003355 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003356 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003357 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003358 # A unary '*' may be const
3359
3360 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003361 if (ERROR("SPACING",
3362 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003363 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003364 if (defined $fix_elements[$n + 2]) {
3365 $fix_elements[$n + 2] =~ s/^\s+//;
3366 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003367 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003368 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003369 }
3370
3371 # unary ++ and unary -- are allowed no space on one side.
3372 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003373 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003374 if (ERROR("SPACING",
3375 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003376 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003377 $line_fixed = 1;
3378 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003379 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003380 if ($ctx =~ /Wx[BE]/ ||
3381 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003382 if (ERROR("SPACING",
3383 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003384 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003385 $line_fixed = 1;
3386 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003387 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003388 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003389 if (ERROR("SPACING",
3390 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003391 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003392 if (defined $fix_elements[$n + 2]) {
3393 $fix_elements[$n + 2] =~ s/^\s+//;
3394 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003395 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003396 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003397 }
3398
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003399 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003400 } elsif ($op eq '<<' or $op eq '>>' or
3401 $op eq '&' or $op eq '^' or $op eq '|' or
3402 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003403 $op eq '*' or $op eq '/' or
3404 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003405 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003406 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003407 if (ERROR("SPACING",
3408 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003409 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3410 if (defined $fix_elements[$n + 2]) {
3411 $fix_elements[$n + 2] =~ s/^\s+//;
3412 }
Joe Perches3705ce52013-07-03 15:05:31 -07003413 $line_fixed = 1;
3414 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003415 }
3416
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003417 # A colon needs no spaces before when it is
3418 # terminating a case value or a label.
3419 } elsif ($opv eq ':C' || $opv eq ':L') {
3420 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003421 if (ERROR("SPACING",
3422 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003423 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003424 $line_fixed = 1;
3425 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003426 }
3427
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003428 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003429 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003430 my $ok = 0;
3431
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003432 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003433 if (($op eq '<' &&
3434 $cc =~ /^\S+\@\S+>/) ||
3435 ($op eq '>' &&
3436 $ca =~ /<\S+\@\S+$/))
3437 {
3438 $ok = 1;
3439 }
3440
Joe Perches84731622013-11-12 15:10:05 -08003441 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003442 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003443 my $msg_type = \&ERROR;
3444 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3445
3446 if (&{$msg_type}("SPACING",
3447 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003448 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3449 if (defined $fix_elements[$n + 2]) {
3450 $fix_elements[$n + 2] =~ s/^\s+//;
3451 }
Joe Perches3705ce52013-07-03 15:05:31 -07003452 $line_fixed = 1;
3453 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003454 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003455 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003456 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003457
3458## print("n: <$n> GOOD: <$good>\n");
3459
3460 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003461 }
Joe Perches3705ce52013-07-03 15:05:31 -07003462
3463 if (($#elements % 2) == 0) {
3464 $fixed_line = $fixed_line . $fix_elements[$#elements];
3465 }
3466
Joe Perches194f66f2014-08-06 16:11:03 -07003467 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3468 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003469 }
3470
3471
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003472 }
3473
Joe Perches786b6322013-07-03 15:05:32 -07003474# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003475 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003476 if (WARN("SPACING",
3477 "space prohibited before semicolon\n" . $herecurr) &&
3478 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003479 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003480 s/^(\+.*\S)\s+;/$1;/;
3481 }
3482 }
3483
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003484# check for multiple assignments
3485 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003486 CHK("MULTIPLE_ASSIGNMENTS",
3487 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003488 }
3489
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003490## # check for multiple declarations, allowing for a function declaration
3491## # continuation.
3492## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3493## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3494##
3495## # Remove any bracketed sections to ensure we do not
3496## # falsly report the parameters of functions.
3497## my $ln = $line;
3498## while ($ln =~ s/\([^\(\)]*\)//g) {
3499## }
3500## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003501## WARN("MULTIPLE_DECLARATION",
3502## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003503## }
3504## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003505
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003506#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003507 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3508 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003509 if (ERROR("SPACING",
3510 "space required before the open brace '{'\n" . $herecurr) &&
3511 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003512 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003513 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003514 }
3515
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003516## # check for blank lines before declarations
3517## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3518## $prevrawline =~ /^.\s*$/) {
3519## WARN("SPACING",
3520## "No blank lines before declarations\n" . $hereprev);
3521## }
3522##
3523
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003524# closing brace should have a space following it when it has anything
3525# on the line
3526 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003527 if (ERROR("SPACING",
3528 "space required after that close brace '}'\n" . $herecurr) &&
3529 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003530 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003531 s/}((?!(?:,|;|\)))\S)/} $1/;
3532 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003533 }
3534
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003535# check spacing on square brackets
3536 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003537 if (ERROR("SPACING",
3538 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3539 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003540 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003541 s/\[\s+/\[/;
3542 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003543 }
3544 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003545 if (ERROR("SPACING",
3546 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003549 s/\s+\]/\]/;
3550 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003551 }
3552
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003553# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003554 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3555 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003556 if (ERROR("SPACING",
3557 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3558 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003559 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003560 s/\(\s+/\(/;
3561 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003562 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003563 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003564 $line !~ /for\s*\(.*;\s+\)/ &&
3565 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003566 if (ERROR("SPACING",
3567 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3568 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003569 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3570 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003571 s/\s+\)/\)/;
3572 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003573 }
3574
Joe Perchese2826fd2014-08-06 16:10:48 -07003575# check unnecessary parentheses around addressof/dereference single $Lvals
3576# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3577
3578 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3579 CHK("UNNECESSARY_PARENTHESES",
3580 "Unnecessary parentheses around $1\n" . $herecurr);
3581 }
3582
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003583#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003584 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003585 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003586 if (WARN("INDENTED_LABEL",
3587 "labels should not be indented\n" . $herecurr) &&
3588 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003589 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003590 s/^(.)\s+/$1/;
3591 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003592 }
3593
Joe Perches5b9553a2014-04-03 14:49:21 -07003594# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003595 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003596 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003597 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003598 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3599 my $value = $1;
3600 $value = deparenthesize($value);
3601 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3602 ERROR("RETURN_PARENTHESES",
3603 "return is not a function, parentheses are not required\n" . $herecurr);
3604 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003605 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003606 ERROR("SPACING",
3607 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003608 }
3609 }
Joe Perches507e5142013-11-12 15:10:13 -08003610
Joe Perchesb43ae212014-06-23 13:22:07 -07003611# unnecessary return in a void function
3612# at end-of-function, with the previous line a single leading tab, then return;
3613# and the line before that not a goto label target like "out:"
3614 if ($sline =~ /^[ \+]}\s*$/ &&
3615 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3616 $linenr >= 3 &&
3617 $lines[$linenr - 3] =~ /^[ +]/ &&
3618 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003619 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003620 "void function return statements are not generally useful\n" . $hereprev);
3621 }
Joe Perches9819cf22014-06-04 16:12:09 -07003622
Joe Perches189248d2014-01-23 15:54:47 -08003623# if statements using unnecessary parentheses - ie: if ((foo == bar))
3624 if ($^V && $^V ge 5.10.0 &&
3625 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3626 my $openparens = $1;
3627 my $count = $openparens =~ tr@\(@\(@;
3628 my $msg = "";
3629 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3630 my $comp = $4; #Not $1 because of $LvalOrFunc
3631 $msg = " - maybe == should be = ?" if ($comp eq "==");
3632 WARN("UNNECESSARY_PARENTHESES",
3633 "Unnecessary parentheses$msg\n" . $herecurr);
3634 }
3635 }
3636
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003637# Return of what appears to be an errno should normally be -'ve
3638 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3639 my $name = $1;
3640 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003641 WARN("USE_NEGATIVE_ERRNO",
3642 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003643 }
3644 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003645
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003646# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003647 if ($line =~ /\b(if|while|for|switch)\(/) {
3648 if (ERROR("SPACING",
3649 "space required before the open parenthesis '('\n" . $herecurr) &&
3650 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003651 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003652 s/\b(if|while|for|switch)\(/$1 \(/;
3653 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003654 }
3655
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003656# Check for illegal assignment in if conditional -- and check for trailing
3657# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003658 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003659 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3660 ctx_statement_block($linenr, $realcnt, 0)
3661 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003662 my ($stat_next) = ctx_statement_block($line_nr_next,
3663 $remain_next, $off_next);
3664 $stat_next =~ s/\n./\n /g;
3665 ##print "stat<$stat> stat_next<$stat_next>\n";
3666
3667 if ($stat_next =~ /^\s*while\b/) {
3668 # If the statement carries leading newlines,
3669 # then count those as offsets.
3670 my ($whitespace) =
3671 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3672 my $offset =
3673 statement_rawlines($whitespace) - 1;
3674
3675 $suppress_whiletrailers{$line_nr_next +
3676 $offset} = 1;
3677 }
3678 }
3679 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003680 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003681 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003682 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003683
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003684 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003685 ERROR("ASSIGN_IN_IF",
3686 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003687 }
3688
3689 # Find out what is on the end of the line after the
3690 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003691 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003692 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003693 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003694 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3695 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003696 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003697 # Find out how long the conditional actually is.
3698 my @newlines = ($c =~ /\n/gs);
3699 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003700 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003701
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003702 $stat_real = raw_line($linenr, $cond_lines)
3703 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003704 if (defined($stat_real) && $cond_lines > 1) {
3705 $stat_real = "[...]\n$stat_real";
3706 }
3707
Joe Perches000d1cc12011-07-25 17:13:25 -07003708 ERROR("TRAILING_STATEMENTS",
3709 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003710 }
3711 }
3712
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003713# Check for bitwise tests written as boolean
3714 if ($line =~ /
3715 (?:
3716 (?:\[|\(|\&\&|\|\|)
3717 \s*0[xX][0-9]+\s*
3718 (?:\&\&|\|\|)
3719 |
3720 (?:\&\&|\|\|)
3721 \s*0[xX][0-9]+\s*
3722 (?:\&\&|\|\||\)|\])
3723 )/x)
3724 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003725 WARN("HEXADECIMAL_BOOLEAN_TEST",
3726 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003727 }
3728
Andy Whitcroft8905a672007-11-28 16:21:06 -08003729# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003730 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3731 my $s = $1;
3732 $s =~ s/$;//g; # Remove any comments
3733 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003734 ERROR("TRAILING_STATEMENTS",
3735 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003736 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003737 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003738# if should not continue a brace
3739 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07003741 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003742 $herecurr);
3743 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003744# case and default should not have general statements after them
3745 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3746 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003747 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003748 \s*return\s+
3749 )/xg)
3750 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003751 ERROR("TRAILING_STATEMENTS",
3752 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003753 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003754
3755 # Check for }<nl>else {, these must be at the same
3756 # indent level to be relevant to each other.
3757 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3758 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003759 ERROR("ELSE_AFTER_BRACE",
3760 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003761 }
3762
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003763 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3764 $previndent == $indent) {
3765 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3766
3767 # Find out what is on the end of the line after the
3768 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003769 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003770 $s =~ s/\n.*//g;
3771
3772 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003773 ERROR("WHILE_AFTER_BRACE",
3774 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003775 }
3776 }
3777
Joe Perches95e2c602013-07-03 15:05:20 -07003778#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003779 while ($line =~ m{($Constant|$Lval)}g) {
3780 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003781
3782#gcc binary extension
3783 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003784 if (WARN("GCC_BINARY_CONSTANT",
3785 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3786 $fix) {
3787 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07003788 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003789 s/\b$var\b/$hexval/;
3790 }
Joe Perches95e2c602013-07-03 15:05:20 -07003791 }
3792
3793#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003794 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003795 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003796#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003797 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003798#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003799 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003800 while ($var =~ m{($Ident)}g) {
3801 my $word = $1;
3802 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003803 if ($check) {
3804 seed_camelcase_includes();
3805 if (!$file && !$camelcase_file_seeded) {
3806 seed_camelcase_file($realfile);
3807 $camelcase_file_seeded = 1;
3808 }
3809 }
Joe Perches7e781f62013-09-11 14:23:55 -07003810 if (!defined $camelcase{$word}) {
3811 $camelcase{$word} = 1;
3812 CHK("CAMELCASE",
3813 "Avoid CamelCase: <$word>\n" . $herecurr);
3814 }
Joe Perches34456862013-07-03 15:05:34 -07003815 }
Joe Perches323c1262012-12-17 16:02:07 -08003816 }
3817 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003818
3819#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003820 if ($line =~ /\#\s*define.*\\\s+$/) {
3821 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3822 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3823 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003824 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003825 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003826 }
3827
Andy Whitcroft653d4872007-06-23 17:16:34 -07003828#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003829 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003830 my $file = "$1.h";
3831 my $checkfile = "include/linux/$file";
3832 if (-f "$root/$checkfile" &&
3833 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003834 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003835 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003836 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003837 CHK("ARCH_INCLUDE_LINUX",
3838 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003839 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003840 WARN("INCLUDE_LINUX",
3841 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003842 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003843 }
3844 }
3845
Andy Whitcroft653d4872007-06-23 17:16:34 -07003846# multi-statement macros should be enclosed in a do while loop, grab the
3847# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003848# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003849 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3850 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003851 my $ln = $linenr;
3852 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003853 my ($off, $dstat, $dcond, $rest);
3854 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003855 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003856 ctx_statement_block($linenr, $realcnt, 0);
3857 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003858 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003859 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003860
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003861 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003862 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003863 $dstat =~ s/\\\n.//g;
3864 $dstat =~ s/^\s*//s;
3865 $dstat =~ s/\s*$//s;
3866
3867 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003868 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3869 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003870 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003871 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003872 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003873
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003874 # Flatten any obvious string concatentation.
3875 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3876 $dstat =~ s/$Ident\s*("X*")/$1/)
3877 {
3878 }
3879
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003880 my $exceptions = qr{
3881 $Declare|
3882 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003883 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003884 DECLARE_PER_CPU|
3885 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003886 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003887 union|
3888 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003889 \.$Ident\s*=\s*|
3890 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003891 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003892 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003893 if ($dstat ne '' &&
3894 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3895 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003896 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07003897 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003898 $dstat !~ /$exceptions/ &&
3899 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003900 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003901 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003902 $dstat !~ /^for\s*$Constant$/ && # for (...)
3903 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3904 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003905 $dstat !~ /^\({/ && # ({...
3906 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003907 {
3908 $ctx =~ s/\n*$//;
3909 my $herectx = $here . "\n";
3910 my $cnt = statement_rawlines($ctx);
3911
3912 for (my $n = 0; $n < $cnt; $n++) {
3913 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003914 }
3915
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003916 if ($dstat =~ /;/) {
3917 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3918 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3919 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003920 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003921 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003922 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003923 }
Joe Perches5023d342012-12-17 16:01:47 -08003924
Joe Perches481eb482012-12-17 16:01:56 -08003925# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003926
3927 } else {
3928 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003929 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3930 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003931 $line =~ /^\+.*\\$/) {
3932 WARN("LINE_CONTINUATIONS",
3933 "Avoid unnecessary line continuations\n" . $herecurr);
3934 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003935 }
3936
Joe Perchesb13edf72012-07-30 14:41:24 -07003937# do {} while (0) macro tests:
3938# single-statement macros do not need to be enclosed in do while (0) loop,
3939# macro should not end with a semicolon
3940 if ($^V && $^V ge 5.10.0 &&
3941 $realfile !~ m@/vmlinux.lds.h$@ &&
3942 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3943 my $ln = $linenr;
3944 my $cnt = $realcnt;
3945 my ($off, $dstat, $dcond, $rest);
3946 my $ctx = '';
3947 ($dstat, $dcond, $ln, $cnt, $off) =
3948 ctx_statement_block($linenr, $realcnt, 0);
3949 $ctx = $dstat;
3950
3951 $dstat =~ s/\\\n.//g;
3952
3953 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3954 my $stmts = $2;
3955 my $semis = $3;
3956
3957 $ctx =~ s/\n*$//;
3958 my $cnt = statement_rawlines($ctx);
3959 my $herectx = $here . "\n";
3960
3961 for (my $n = 0; $n < $cnt; $n++) {
3962 $herectx .= raw_line($linenr, $n) . "\n";
3963 }
3964
Joe Perchesac8e97f2012-08-21 16:15:53 -07003965 if (($stmts =~ tr/;/;/) == 1 &&
3966 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003967 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3968 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3969 }
3970 if (defined $semis && $semis ne "") {
3971 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3972 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3973 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07003974 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
3975 $ctx =~ s/\n*$//;
3976 my $cnt = statement_rawlines($ctx);
3977 my $herectx = $here . "\n";
3978
3979 for (my $n = 0; $n < $cnt; $n++) {
3980 $herectx .= raw_line($linenr, $n) . "\n";
3981 }
3982
3983 WARN("TRAILING_SEMICOLON",
3984 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07003985 }
3986 }
3987
Mike Frysinger080ba922009-01-06 14:41:25 -08003988# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3989# all assignments may have only one of the following with an assignment:
3990# .
3991# ALIGN(...)
3992# VMLINUX_SYMBOL(...)
3993 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003994 WARN("MISSING_VMLINUX_SYMBOL",
3995 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003996 }
3997
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003998# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003999 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4000 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004001 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004002 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004003 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4004 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004005 my @allowed = ();
4006 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004007 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004008 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004009 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004010 for my $chunk (@chunks) {
4011 my ($cond, $block) = @{$chunk};
4012
Andy Whitcroft773647a2008-03-28 14:15:58 -07004013 # If the condition carries leading newlines, then count those as offsets.
4014 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4015 my $offset = statement_rawlines($whitespace) - 1;
4016
Joe Perchesaad4f612012-03-23 15:02:19 -07004017 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004018 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4019
4020 # We have looked at and allowed this specific line.
4021 $suppress_ifbraces{$ln + $offset} = 1;
4022
4023 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004024 $ln += statement_rawlines($block) - 1;
4025
Andy Whitcroft773647a2008-03-28 14:15:58 -07004026 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004027
4028 $seen++ if ($block =~ /^\s*{/);
4029
Joe Perchesaad4f612012-03-23 15:02:19 -07004030 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004031 if (statement_lines($cond) > 1) {
4032 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004033 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004034 }
4035 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004036 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004037 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004038 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004039 if (statement_block_size($block) > 1) {
4040 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004041 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004042 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004043 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004044 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004045 if ($seen) {
4046 my $sum_allowed = 0;
4047 foreach (@allowed) {
4048 $sum_allowed += $_;
4049 }
4050 if ($sum_allowed == 0) {
4051 WARN("BRACES",
4052 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4053 } elsif ($sum_allowed != $allow &&
4054 $seen != $allow) {
4055 CHK("BRACES",
4056 "braces {} should be used on all arms of this statement\n" . $herectx);
4057 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004058 }
4059 }
4060 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004061 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004062 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004063 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004064
Andy Whitcroftcf655042008-03-04 14:28:20 -08004065 # Check the pre-context.
4066 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4067 #print "APW: ALLOWED: pre<$1>\n";
4068 $allowed = 1;
4069 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004070
4071 my ($level, $endln, @chunks) =
4072 ctx_statement_full($linenr, $realcnt, $-[0]);
4073
Andy Whitcroftcf655042008-03-04 14:28:20 -08004074 # Check the condition.
4075 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004076 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004077 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004078 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004079 }
4080 if (statement_lines($cond) > 1) {
4081 #print "APW: ALLOWED: cond<$cond>\n";
4082 $allowed = 1;
4083 }
4084 if ($block =~/\b(?:if|for|while)\b/) {
4085 #print "APW: ALLOWED: block<$block>\n";
4086 $allowed = 1;
4087 }
4088 if (statement_block_size($block) > 1) {
4089 #print "APW: ALLOWED: lines block<$block>\n";
4090 $allowed = 1;
4091 }
4092 # Check the post-context.
4093 if (defined $chunks[1]) {
4094 my ($cond, $block) = @{$chunks[1]};
4095 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004096 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004097 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004098 if ($block =~ /^\s*\{/) {
4099 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4100 $allowed = 1;
4101 }
4102 }
4103 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004104 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004105 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004106
Andy Whitcroftf0556632008-10-15 22:02:23 -07004107 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004108 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004109 }
4110
Joe Perches000d1cc12011-07-25 17:13:25 -07004111 WARN("BRACES",
4112 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004113 }
4114 }
4115
Joe Perches0979ae62012-12-17 16:01:59 -08004116# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004117 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004118 CHK("BRACES",
4119 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4120 }
Joe Perches77b9a532013-07-03 15:05:29 -07004121 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004122 CHK("BRACES",
4123 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4124 }
4125
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004126# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004127 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4128 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004129 WARN("VOLATILE",
4130 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004131 }
4132
Andy Whitcroft00df3442007-06-08 13:47:06 -07004133# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004134 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004135 CHK("REDUNDANT_CODE",
4136 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004137 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004138 }
4139
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004140# check for needless "if (<foo>) fn(<foo>)" uses
4141 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4142 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4143 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4144 WARN('NEEDLESS_IF',
4145 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004146 }
4147 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004148
Joe Perchesebfdc402014-08-06 16:10:27 -07004149# check for unnecessary "Out of Memory" messages
4150 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4151 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4152 (defined $1 || defined $3) &&
4153 $linenr > 3) {
4154 my $testval = $2;
4155 my $testline = $lines[$linenr - 3];
4156
4157 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4158# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4159
4160 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4161 WARN("OOM_MESSAGE",
4162 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4163 }
4164 }
4165
Joe Perches8716de32013-09-11 14:24:05 -07004166# check for bad placement of section $InitAttribute (e.g.: __initdata)
4167 if ($line =~ /(\b$InitAttribute\b)/) {
4168 my $attr = $1;
4169 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4170 my $ptr = $1;
4171 my $var = $2;
4172 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4173 ERROR("MISPLACED_INIT",
4174 "$attr should be placed after $var\n" . $herecurr)) ||
4175 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4176 WARN("MISPLACED_INIT",
4177 "$attr should be placed after $var\n" . $herecurr))) &&
4178 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004179 $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 -07004180 }
4181 }
4182 }
4183
Joe Perchese970b882013-11-12 15:10:10 -08004184# check for $InitAttributeData (ie: __initdata) with const
4185 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4186 my $attr = $1;
4187 $attr =~ /($InitAttributePrefix)(.*)/;
4188 my $attr_prefix = $1;
4189 my $attr_type = $2;
4190 if (ERROR("INIT_ATTRIBUTE",
4191 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4192 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004193 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004194 s/$InitAttributeData/${attr_prefix}initconst/;
4195 }
4196 }
4197
4198# check for $InitAttributeConst (ie: __initconst) without const
4199 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4200 my $attr = $1;
4201 if (ERROR("INIT_ATTRIBUTE",
4202 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4203 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004204 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004205 /(^\+\s*(?:static\s+))/;
4206 $lead = rtrim($1);
4207 $lead = "$lead " if ($lead !~ /^\+$/);
4208 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004209 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004210 }
4211 }
4212
Joe Perchesfbdb8132014-04-03 14:49:14 -07004213# don't use __constant_<foo> functions outside of include/uapi/
4214 if ($realfile !~ m@^include/uapi/@ &&
4215 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4216 my $constant_func = $1;
4217 my $func = $constant_func;
4218 $func =~ s/^__constant_//;
4219 if (WARN("CONSTANT_CONVERSION",
4220 "$constant_func should be $func\n" . $herecurr) &&
4221 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004222 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004223 }
4224 }
4225
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004226# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004227 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004228 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004229 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004230 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004231 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004232 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4233 }
4234 if ($delay > 2000) {
4235 WARN("LONG_UDELAY",
4236 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004237 }
4238 }
4239
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004240# warn about unexpectedly long msleep's
4241 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4242 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004243 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004244 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004245 }
4246 }
4247
Joe Perches36ec1932013-07-03 15:05:25 -07004248# check for comparisons of jiffies
4249 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4250 WARN("JIFFIES_COMPARISON",
4251 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4252 }
4253
Joe Perches9d7a34a2013-07-03 15:05:26 -07004254# check for comparisons of get_jiffies_64()
4255 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4256 WARN("JIFFIES_COMPARISON",
4257 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4258 }
4259
Andy Whitcroft00df3442007-06-08 13:47:06 -07004260# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004261# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004262# print "#ifdef in C files should be avoided\n";
4263# print "$herecurr";
4264# $clean = 0;
4265# }
4266
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004267# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004268 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004269 if (ERROR("SPACING",
4270 "exactly one space required after that #$1\n" . $herecurr) &&
4271 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004272 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004273 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4274 }
4275
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004276 }
4277
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004278# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004279 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4280 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004281 my $which = $1;
4282 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004283 CHK("UNCOMMENTED_DEFINITION",
4284 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004285 }
4286 }
4287# check for memory barriers without a comment.
4288 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4289 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004290 WARN("MEMORY_BARRIER",
4291 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004292 }
4293 }
4294# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004295 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004296 CHK("ARCH_DEFINES",
4297 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004298 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004299
Tobias Klauserd4977c72010-05-24 14:33:30 -07004300# Check that the storage class is at the beginning of a declaration
4301 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004302 WARN("STORAGE_CLASS",
4303 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004304 }
4305
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004306# check the location of the inline attribute, that it is between
4307# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004308 if ($line =~ /\b$Type\s+$Inline\b/ ||
4309 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004310 ERROR("INLINE_LOCATION",
4311 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004312 }
4313
Andy Whitcroft8905a672007-11-28 16:21:06 -08004314# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004315 if ($realfile !~ m@\binclude/uapi/@ &&
4316 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004317 if (WARN("INLINE",
4318 "plain inline is preferred over $1\n" . $herecurr) &&
4319 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004320 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004321
4322 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004323 }
4324
Joe Perches3d130fd2011-01-12 17:00:00 -08004325# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004326 if ($realfile !~ m@\binclude/uapi/@ &&
4327 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004328 WARN("PREFER_PACKED",
4329 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004330 }
4331
Joe Perches39b7e282011-07-25 17:13:24 -07004332# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004333 if ($realfile !~ m@\binclude/uapi/@ &&
4334 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004335 WARN("PREFER_ALIGNED",
4336 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004337 }
4338
Joe Perches5f14d3b2012-01-10 15:09:52 -08004339# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004340 if ($realfile !~ m@\binclude/uapi/@ &&
4341 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004342 if (WARN("PREFER_PRINTF",
4343 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4344 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004345 $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 -07004346
4347 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004348 }
4349
Joe Perches6061d942012-03-23 15:02:16 -07004350# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004351 if ($realfile !~ m@\binclude/uapi/@ &&
4352 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004353 if (WARN("PREFER_SCANF",
4354 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4355 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004356 $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 -07004357 }
Joe Perches6061d942012-03-23 15:02:16 -07004358 }
4359
Joe Perches8f53a9b2010-03-05 13:43:48 -08004360# check for sizeof(&)
4361 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004362 WARN("SIZEOF_ADDRESS",
4363 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004364 }
4365
Joe Perches66c80b62012-07-30 14:41:22 -07004366# check for sizeof without parenthesis
4367 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004368 if (WARN("SIZEOF_PARENTHESIS",
4369 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4370 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004371 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004372 }
Joe Perches66c80b62012-07-30 14:41:22 -07004373 }
4374
Joe Perches428e2fd2011-05-24 17:13:39 -07004375# check for line continuations in quoted strings with odd counts of "
4376 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004377 WARN("LINE_CONTINUATIONS",
4378 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004379 }
4380
Joe Perches88982fe2012-12-17 16:02:00 -08004381# check for struct spinlock declarations
4382 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4383 WARN("USE_SPINLOCK_T",
4384 "struct spinlock should be spinlock_t\n" . $herecurr);
4385 }
4386
Joe Perchesa6962d72013-04-29 16:18:13 -07004387# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004388 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004389 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004390 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004391 if (WARN("PREFER_SEQ_PUTS",
4392 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4393 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004394 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004395 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004396 }
4397 }
4398
Andy Whitcroft554e1652012-01-10 15:09:57 -08004399# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004400 if ($^V && $^V ge 5.10.0 &&
4401 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004402 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004403
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004404 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004405 my $ms_val = $7;
4406 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004407
Andy Whitcroft554e1652012-01-10 15:09:57 -08004408 if ($ms_size =~ /^(0x|)0$/i) {
4409 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004410 "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 -08004411 } elsif ($ms_size =~ /^(0x|)1$/i) {
4412 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004413 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4414 }
4415 }
4416
Joe Perches98a9bba2014-01-23 15:54:52 -08004417# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4418 if ($^V && $^V ge 5.10.0 &&
4419 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4420 if (WARN("PREFER_ETHER_ADDR_COPY",
4421 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004423 $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 -08004424 }
4425 }
4426
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004427# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004428 if ($^V && $^V ge 5.10.0 &&
4429 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004430 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004431 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004432 my $call = $1;
4433 my $cast1 = deparenthesize($2);
4434 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004435 my $cast2 = deparenthesize($7);
4436 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004437 my $cast;
4438
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004439 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004440 $cast = "$cast1 or $cast2";
4441 } elsif ($cast1 ne "") {
4442 $cast = $cast1;
4443 } else {
4444 $cast = $cast2;
4445 }
4446 WARN("MINMAX",
4447 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004448 }
4449 }
4450
Joe Perches4a273192012-07-30 14:41:20 -07004451# check usleep_range arguments
4452 if ($^V && $^V ge 5.10.0 &&
4453 defined $stat &&
4454 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4455 my $min = $1;
4456 my $max = $7;
4457 if ($min eq $max) {
4458 WARN("USLEEP_RANGE",
4459 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4460 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4461 $min > $max) {
4462 WARN("USLEEP_RANGE",
4463 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4464 }
4465 }
4466
Joe Perches823b7942013-11-12 15:10:15 -08004467# check for naked sscanf
4468 if ($^V && $^V ge 5.10.0 &&
4469 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004470 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004471 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4472 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4473 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4474 my $lc = $stat =~ tr@\n@@;
4475 $lc = $lc + $linenr;
4476 my $stat_real = raw_line($linenr, 0);
4477 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4478 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4479 }
4480 WARN("NAKED_SSCANF",
4481 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4482 }
4483
Joe Perchesafc819a2014-06-04 16:12:08 -07004484# check for simple sscanf that should be kstrto<foo>
4485 if ($^V && $^V ge 5.10.0 &&
4486 defined $stat &&
4487 $line =~ /\bsscanf\b/) {
4488 my $lc = $stat =~ tr@\n@@;
4489 $lc = $lc + $linenr;
4490 my $stat_real = raw_line($linenr, 0);
4491 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4492 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4493 }
4494 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4495 my $format = $6;
4496 my $count = $format =~ tr@%@%@;
4497 if ($count == 1 &&
4498 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4499 WARN("SSCANF_TO_KSTRTO",
4500 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4501 }
4502 }
4503 }
4504
Joe Perches70dc8a42013-09-11 14:23:58 -07004505# check for new externs in .h files.
4506 if ($realfile =~ /\.h$/ &&
4507 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004508 if (CHK("AVOID_EXTERNS",
4509 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004510 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004511 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004512 }
4513 }
4514
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004515# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004516 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004517 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004518 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004519 my $function_name = $1;
4520 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004521
4522 my $s = $stat;
4523 if (defined $cond) {
4524 substr($s, 0, length($cond), '');
4525 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004526 if ($s =~ /^\s*;/ &&
4527 $function_name ne 'uninitialized_var')
4528 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004529 WARN("AVOID_EXTERNS",
4530 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004531 }
4532
4533 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004534 WARN("FUNCTION_ARGUMENTS",
4535 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004536 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004537
4538 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4539 $stat =~ /^.\s*extern\s+/)
4540 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004541 WARN("AVOID_EXTERNS",
4542 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004543 }
4544
4545# checks for new __setup's
4546 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4547 my $name = $1;
4548
4549 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004550 CHK("UNDOCUMENTED_SETUP",
4551 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004552 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004553 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004554
4555# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004556 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004557 WARN("UNNECESSARY_CASTS",
4558 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004559 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004560
Joe Perchesa640d252013-07-03 15:05:21 -07004561# alloc style
4562# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4563 if ($^V && $^V ge 5.10.0 &&
4564 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4565 CHK("ALLOC_SIZEOF_STRUCT",
4566 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4567 }
4568
Joe Perches60a55362014-06-04 16:12:07 -07004569# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4570 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004571 $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 -07004572 my $oldfunc = $3;
4573 my $a1 = $4;
4574 my $a2 = $10;
4575 my $newfunc = "kmalloc_array";
4576 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004577 my $r1 = $a1;
4578 my $r2 = $a2;
4579 if ($a1 =~ /^sizeof\s*\S/) {
4580 $r1 = $a2;
4581 $r2 = $a1;
4582 }
4583 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4584 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004585 if (WARN("ALLOC_WITH_MULTIPLY",
4586 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4587 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004588 $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 -07004589
4590 }
4591 }
4592 }
4593
Joe Perches972fdea2013-04-29 16:18:12 -07004594# check for krealloc arg reuse
4595 if ($^V && $^V ge 5.10.0 &&
4596 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4597 WARN("KREALLOC_ARG_REUSE",
4598 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4599 }
4600
Joe Perches5ce59ae2013-02-21 16:44:18 -08004601# check for alloc argument mismatch
4602 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4603 WARN("ALLOC_ARRAY_ARGS",
4604 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4605 }
4606
Joe Perchescaf2a542011-01-12 16:59:56 -08004607# check for multiple semicolons
4608 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004609 if (WARN("ONE_SEMICOLON",
4610 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4611 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004612 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004613 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004614 }
4615
Joe Perchesc34c09a2014-01-23 15:54:43 -08004616# check for case / default statements not preceeded by break/fallthrough/switch
4617 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4618 my $has_break = 0;
4619 my $has_statement = 0;
4620 my $count = 0;
4621 my $prevline = $linenr;
4622 while ($prevline > 1 && $count < 3 && !$has_break) {
4623 $prevline--;
4624 my $rline = $rawlines[$prevline - 1];
4625 my $fline = $lines[$prevline - 1];
4626 last if ($fline =~ /^\@\@/);
4627 next if ($fline =~ /^\-/);
4628 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4629 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4630 next if ($fline =~ /^.[\s$;]*$/);
4631 $has_statement = 1;
4632 $count++;
4633 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4634 }
4635 if (!$has_break && $has_statement) {
4636 WARN("MISSING_BREAK",
4637 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4638 }
4639 }
4640
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004641# check for switch/default statements without a break;
4642 if ($^V && $^V ge 5.10.0 &&
4643 defined $stat &&
4644 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4645 my $ctx = '';
4646 my $herectx = $here . "\n";
4647 my $cnt = statement_rawlines($stat);
4648 for (my $n = 0; $n < $cnt; $n++) {
4649 $herectx .= raw_line($linenr, $n) . "\n";
4650 }
4651 WARN("DEFAULT_NO_BREAK",
4652 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004653 }
4654
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004655# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004656 if ($line =~ /\b__FUNCTION__\b/) {
4657 if (WARN("USE_FUNC",
4658 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4659 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004660 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004661 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004662 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004663
Joe Perches2c924882012-03-23 15:02:20 -07004664# check for use of yield()
4665 if ($line =~ /\byield\s*\(\s*\)/) {
4666 WARN("YIELD",
4667 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4668 }
4669
Joe Perches179f8f42013-07-03 15:05:30 -07004670# check for comparisons against true and false
4671 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4672 my $lead = $1;
4673 my $arg = $2;
4674 my $test = $3;
4675 my $otype = $4;
4676 my $trail = $5;
4677 my $op = "!";
4678
4679 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4680
4681 my $type = lc($otype);
4682 if ($type =~ /^(?:true|false)$/) {
4683 if (("$test" eq "==" && "$type" eq "true") ||
4684 ("$test" eq "!=" && "$type" eq "false")) {
4685 $op = "";
4686 }
4687
4688 CHK("BOOL_COMPARISON",
4689 "Using comparison to $otype is error prone\n" . $herecurr);
4690
4691## maybe suggesting a correct construct would better
4692## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4693
4694 }
4695 }
4696
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004697# check for semaphores initialized locked
4698 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004699 WARN("CONSIDER_COMPLETION",
4700 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004701 }
Joe Perches6712d852012-03-23 15:02:20 -07004702
Joe Perches67d0a072011-10-31 17:13:10 -07004703# recommend kstrto* over simple_strto* and strict_strto*
4704 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004705 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004706 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004707 }
Joe Perches6712d852012-03-23 15:02:20 -07004708
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004709# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07004710 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004711 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004712 "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 -07004713 }
Joe Perches6712d852012-03-23 15:02:20 -07004714
Emese Revfy79404842010-03-05 13:43:53 -08004715# check for various ops structs, ensure they are const.
4716 my $struct_ops = qr{acpi_dock_ops|
4717 address_space_operations|
4718 backlight_ops|
4719 block_device_operations|
4720 dentry_operations|
4721 dev_pm_ops|
4722 dma_map_ops|
4723 extent_io_ops|
4724 file_lock_operations|
4725 file_operations|
4726 hv_ops|
4727 ide_dma_ops|
4728 intel_dvo_dev_ops|
4729 item_operations|
4730 iwl_ops|
4731 kgdb_arch|
4732 kgdb_io|
4733 kset_uevent_ops|
4734 lock_manager_operations|
4735 microcode_ops|
4736 mtrr_ops|
4737 neigh_ops|
4738 nlmsvc_binding|
4739 pci_raw_ops|
4740 pipe_buf_operations|
4741 platform_hibernation_ops|
4742 platform_suspend_ops|
4743 proto_ops|
4744 rpc_pipe_ops|
4745 seq_operations|
4746 snd_ac97_build_ops|
4747 soc_pcmcia_socket_ops|
4748 stacktrace_ops|
4749 sysfs_ops|
4750 tty_operations|
4751 usb_mon_operations|
4752 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004753 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004754 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004755 WARN("CONST_STRUCT",
4756 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004757 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004758 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004759
4760# use of NR_CPUS is usually wrong
4761# ignore definitions of NR_CPUS and usage to define arrays as likely right
4762 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004763 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4764 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004765 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4766 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4767 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004768 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769 WARN("NR_CPUS",
4770 "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 -07004771 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004772
Joe Perches52ea8502013-11-12 15:10:09 -08004773# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4774 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4775 ERROR("DEFINE_ARCH_HAS",
4776 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4777 }
4778
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004779# check for %L{u,d,i} in strings
4780 my $string;
4781 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4782 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004783 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004784 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004785 WARN("PRINTF_L",
4786 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004787 last;
4788 }
4789 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004790
4791# whine mightly about in_atomic
4792 if ($line =~ /\bin_atomic\s*\(/) {
4793 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004794 ERROR("IN_ATOMIC",
4795 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004796 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004797 WARN("IN_ATOMIC",
4798 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004799 }
4800 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004801
4802# check for lockdep_set_novalidate_class
4803 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4804 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4805 if ($realfile !~ m@^kernel/lockdep@ &&
4806 $realfile !~ m@^include/linux/lockdep@ &&
4807 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004808 ERROR("LOCKDEP",
4809 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004810 }
4811 }
Dave Jones88f88312011-01-12 16:59:59 -08004812
4813 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4814 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004815 WARN("EXPORTED_WORLD_WRITABLE",
4816 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004817 }
Joe Perches24358802014-04-03 14:49:13 -07004818
Joe Perches515a2352014-04-03 14:49:24 -07004819# Mode permission misuses where it seems decimal should be octal
4820# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4821 if ($^V && $^V ge 5.10.0 &&
4822 $line =~ /$mode_perms_search/) {
4823 foreach my $entry (@mode_permission_funcs) {
4824 my $func = $entry->[0];
4825 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07004826
Joe Perches515a2352014-04-03 14:49:24 -07004827 my $skip_args = "";
4828 if ($arg_pos > 1) {
4829 $arg_pos--;
4830 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
4831 }
4832 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4833 if ($line =~ /$test/) {
4834 my $val = $1;
4835 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07004836
Joe Perches515a2352014-04-03 14:49:24 -07004837 if ($val !~ /^0$/ &&
4838 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
4839 length($val) ne 4)) {
4840 ERROR("NON_OCTAL_PERMISSIONS",
4841 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
4842 }
Joe Perches24358802014-04-03 14:49:13 -07004843 }
4844 }
4845 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004846 }
4847
4848 # If we have no input at all, then there is nothing to report on
4849 # so just keep quiet.
4850 if ($#rawlines == -1) {
4851 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004852 }
4853
Andy Whitcroft8905a672007-11-28 16:21:06 -08004854 # In mailback mode only produce a report in the negative, for
4855 # things that appear to be patches.
4856 if ($mailback && ($clean == 1 || !$is_patch)) {
4857 exit(0);
4858 }
4859
4860 # This is not a patch, and we are are in 'no-patch' mode so
4861 # just keep quiet.
4862 if (!$chk_patch && !$is_patch) {
4863 exit(0);
4864 }
4865
4866 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004867 ERROR("NOT_UNIFIED_DIFF",
4868 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004869 }
4870 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004871 ERROR("MISSING_SIGN_OFF",
4872 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004873 }
4874
Andy Whitcroft8905a672007-11-28 16:21:06 -08004875 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004876 if ($summary && !($clean == 1 && $quiet == 1)) {
4877 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004878 print "total: $cnt_error errors, $cnt_warn warnings, " .
4879 (($check)? "$cnt_chk checks, " : "") .
4880 "$cnt_lines lines checked\n";
4881 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004882 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004883
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004884 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004885
4886 if ($^V lt 5.10.0) {
4887 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4888 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4889 }
4890
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004891 # If there were whitespace errors which cleanpatch can fix
4892 # then suggest that.
4893 if ($rpt_cleaners) {
4894 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4895 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004896 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004897 }
4898 }
4899
Joe Perches91bfe482013-09-11 14:23:59 -07004900 hash_show_words(\%use_type, "Used");
4901 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004902
Joe Perches3705ce52013-07-03 15:05:31 -07004903 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
Joe Perches9624b8d2014-01-23 15:54:44 -08004904 my $newfile = $filename;
4905 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07004906 my $linecount = 0;
4907 my $f;
4908
4909 open($f, '>', $newfile)
4910 or die "$P: Can't open $newfile for write\n";
4911 foreach my $fixed_line (@fixed) {
4912 $linecount++;
4913 if ($file) {
4914 if ($linecount > 3) {
4915 $fixed_line =~ s/^\+//;
4916 print $f $fixed_line. "\n";
4917 }
4918 } else {
4919 print $f $fixed_line . "\n";
4920 }
4921 }
4922 close($f);
4923
4924 if (!$quiet) {
4925 print << "EOM";
4926Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4927
4928Do _NOT_ trust the results written to this file.
4929Do _NOT_ submit these changes without inspecting them for correctness.
4930
4931This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4932No warranties, expressed or implied...
4933
4934EOM
4935 }
4936 }
4937
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004938 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004939 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004940 }
4941 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004942 print << "EOM";
4943$vname has style problems, please review.
4944
4945If any of these errors are false positives, please report
4946them to the maintainer, see CHECKPATCH in MAINTAINERS.
4947EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004948 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004949
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004950 return $clean;
4951}