blob: a0880ede3db90afea3da829340d669c8ceada038 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-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 Whitcroft00df344f2007-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 Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700120GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700121 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -0700161if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700162 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Perchese970b8842013-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 Perches18130872014-08-06 16:11:22 -0700312our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700313our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700315our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800316our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700317our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318
Joe Perches15662b32011-10-31 17:13:12 -0700319our $NON_ASCII_UTF8 = qr{
320 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700321 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
322 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
323 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
324 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
325 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
326 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
327}x;
328
Joe Perches15662b32011-10-31 17:13:12 -0700329our $UTF8 = qr{
330 [\x09\x0A\x0D\x20-\x7E] # ASCII
331 | $NON_ASCII_UTF8
332}x;
333
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700334our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700335 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700336 atomic_t
337)};
338
Joe Perches691e6692010-03-05 13:43:51 -0800339our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700340 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700341 (?:[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 -0700342 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700343 panic|
Joe Perches06668722013-11-12 15:10:07 -0800344 MODULE_[A-Z_]+|
345 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800346)};
347
Joe Perches20112472011-07-25 17:13:23 -0700348our $signature_tags = qr{(?xi:
349 Signed-off-by:|
350 Acked-by:|
351 Tested-by:|
352 Reviewed-by:|
353 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700354 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700355 To:|
356 Cc:
357)};
358
Joe Perches18130872014-08-06 16:11:22 -0700359our @typeListMisordered = (
360 qr{char\s+(?:un)?signed},
361 qr{int\s+(?:(?:un)?signed\s+)?short\s},
362 qr{int\s+short(?:\s+(?:un)?signed)},
363 qr{short\s+int(?:\s+(?:un)?signed)},
364 qr{(?:un)?signed\s+int\s+short},
365 qr{short\s+(?:un)?signed},
366 qr{long\s+int\s+(?:un)?signed},
367 qr{int\s+long\s+(?:un)?signed},
368 qr{long\s+(?:un)?signed\s+int},
369 qr{int\s+(?:un)?signed\s+long},
370 qr{int\s+(?:un)?signed},
371 qr{int\s+long\s+long\s+(?:un)?signed},
372 qr{long\s+long\s+int\s+(?:un)?signed},
373 qr{long\s+long\s+(?:un)?signed\s+int},
374 qr{long\s+long\s+(?:un)?signed},
375 qr{long\s+(?:un)?signed},
376);
377
Andy Whitcroft8905a672007-11-28 16:21:06 -0800378our @typeList = (
379 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700380 qr{(?:(?:un)?signed\s+)?char},
381 qr{(?:(?:un)?signed\s+)?short\s+int},
382 qr{(?:(?:un)?signed\s+)?short},
383 qr{(?:(?:un)?signed\s+)?int},
384 qr{(?:(?:un)?signed\s+)?long\s+int},
385 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
386 qr{(?:(?:un)?signed\s+)?long\s+long},
387 qr{(?:(?:un)?signed\s+)?long},
388 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389 qr{float},
390 qr{double},
391 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392 qr{struct\s+$Ident},
393 qr{union\s+$Ident},
394 qr{enum\s+$Ident},
395 qr{${Ident}_t},
396 qr{${Ident}_handler},
397 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700398 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399);
Joe Perches8716de32013-09-11 14:24:05 -0700400our @typeListWithAttr = (
401 @typeList,
402 qr{struct\s+$InitAttribute\s+$Ident},
403 qr{union\s+$InitAttribute\s+$Ident},
404);
405
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700406our @modifierList = (
407 qr{fastcall},
408);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409
Joe Perches24358802014-04-03 14:49:13 -0700410our @mode_permission_funcs = (
411 ["module_param", 3],
412 ["module_param_(?:array|named|string)", 4],
413 ["module_param_array_named", 5],
414 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
415 ["proc_create(?:_data|)", 2],
416 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
417);
418
Joe Perches515a2352014-04-03 14:49:24 -0700419#Create a search pattern for all these functions to speed up a loop below
420our $mode_perms_search = "";
421foreach my $entry (@mode_permission_funcs) {
422 $mode_perms_search .= '|' if ($mode_perms_search ne "");
423 $mode_perms_search .= $entry->[0];
424}
425
Joe Perches3f7bac02014-06-04 16:12:04 -0700426our $declaration_macros = qr{(?x:
427 (?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
428 (?:$Storage\s+)?LIST_HEAD\s*\(
429)};
430
Wolfram Sang7840a942010-08-09 17:20:57 -0700431our $allowed_asm_includes = qr{(?x:
432 irq|
433 memory
434)};
435# memory.h: ARM has a custom one
436
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700438 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
439 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700440 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700441 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700442 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800443 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700444 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800445 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800446 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700447 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700448 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800449 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700450 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800451 }x;
Joe Perches18130872014-08-06 16:11:22 -0700452 $NonptrTypeMisordered = qr{
453 (?:$Modifier\s+|const\s+)*
454 (?:
455 (?:${Misordered}\b)
456 )
457 (?:\s+$Modifier|\s+const)*
458 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700459 $NonptrTypeWithAttr = qr{
460 (?:$Modifier\s+|const\s+)*
461 (?:
462 (?:typeof|__typeof__)\s*\([^\)]*\)|
463 (?:$typeTypedefs\b)|
464 (?:${allWithAttr}\b)
465 )
466 (?:\s+$Modifier|\s+const)*
467 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700469 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700470 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700471 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800472 }x;
Joe Perches18130872014-08-06 16:11:22 -0700473 $TypeMisordered = qr{
474 $NonptrTypeMisordered
475 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
476 (?:\s+$Inline|\s+$Modifier)*
477 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700478 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700479 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800480}
481build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700482
Joe Perches7d2367a2011-07-25 17:13:22 -0700483our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700484
485# Using $balanced_parens, $LvalOrFunc, or $FuncArg
486# requires at least perl version v5.10.0
487# Any use must be runtime checked with $^V
488
489our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700490our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800491our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700492
493sub deparenthesize {
494 my ($string) = @_;
495 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700496
497 while ($string =~ /^\s*\(.*\)\s*$/) {
498 $string =~ s@^\s*\(\s*@@;
499 $string =~ s@\s*\)\s*$@@;
500 }
501
Joe Perches7d2367a2011-07-25 17:13:22 -0700502 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700503
Joe Perches7d2367a2011-07-25 17:13:22 -0700504 return $string;
505}
506
Joe Perches34456862013-07-03 15:05:34 -0700507sub seed_camelcase_file {
508 my ($file) = @_;
509
510 return if (!(-f $file));
511
512 local $/;
513
514 open(my $include_file, '<', "$file")
515 or warn "$P: Can't read '$file' $!\n";
516 my $text = <$include_file>;
517 close($include_file);
518
519 my @lines = split('\n', $text);
520
521 foreach my $line (@lines) {
522 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
523 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
524 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800525 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
526 $camelcase{$1} = 1;
527 } 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 -0700528 $camelcase{$1} = 1;
529 }
530 }
531}
532
533my $camelcase_seeded = 0;
534sub seed_camelcase_includes {
535 return if ($camelcase_seeded);
536
537 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700538 my $camelcase_cache = "";
539 my @include_files = ();
540
541 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700542
Richard Genoud3645e322014-02-10 14:25:32 -0800543 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700544 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
545 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700546 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700547 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700548 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700549 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700550 @include_files = split('\n', $files);
551 foreach my $file (@include_files) {
552 my $date = POSIX::strftime("%Y%m%d%H%M",
553 localtime((stat $file)[9]));
554 $last_mod_date = $date if ($last_mod_date < $date);
555 }
556 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700557 }
Joe Perchesc707a812013-07-08 16:00:43 -0700558
559 if ($camelcase_cache ne "" && -f $camelcase_cache) {
560 open(my $camelcase_file, '<', "$camelcase_cache")
561 or warn "$P: Can't read '$camelcase_cache' $!\n";
562 while (<$camelcase_file>) {
563 chomp;
564 $camelcase{$_} = 1;
565 }
566 close($camelcase_file);
567
568 return;
569 }
570
Richard Genoud3645e322014-02-10 14:25:32 -0800571 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700572 $files = `git ls-files "include/*.h"`;
573 @include_files = split('\n', $files);
574 }
575
Joe Perches34456862013-07-03 15:05:34 -0700576 foreach my $file (@include_files) {
577 seed_camelcase_file($file);
578 }
Joe Perches351b2a12013-07-03 15:05:36 -0700579
Joe Perchesc707a812013-07-08 16:00:43 -0700580 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700581 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700582 open(my $camelcase_file, '>', "$camelcase_cache")
583 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700584 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
585 print $camelcase_file ("$_\n");
586 }
587 close($camelcase_file);
588 }
Joe Perches34456862013-07-03 15:05:34 -0700589}
590
Joe Perchesd311cd42014-08-06 16:10:57 -0700591sub git_commit_info {
592 my ($commit, $id, $desc) = @_;
593
594 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
595
596 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
597 $output =~ s/^\s*//gm;
598 my @lines = split("\n", $output);
599
600 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
601# Maybe one day convert this block of bash into something that returns
602# all matching commit ids, but it's very slow...
603#
604# echo "checking commits $1..."
605# git rev-list --remotes | grep -i "^$1" |
606# while read line ; do
607# git log --format='%H %s' -1 $line |
608# echo "commit $(cut -c 1-12,41-)"
609# done
610 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
611 } else {
612 $id = substr($lines[0], 0, 12);
613 $desc = substr($lines[0], 41);
614 }
615
616 return ($id, $desc);
617}
618
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700619$chk_signoff = 0 if ($file);
620
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700621my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800622my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700623my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700624my @fixed_inserted = ();
625my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700626my $fixlinenr = -1;
627
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800628my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700629for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800630 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700631 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800632 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700633 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800634 } elsif ($filename eq '-') {
635 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700636 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800637 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700638 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700639 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800640 if ($filename eq '-') {
641 $vname = 'Your patch';
642 } else {
643 $vname = $filename;
644 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800645 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700646 chomp;
647 push(@rawlines, $_);
648 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800649 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800650 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700651 $exit = 1;
652 }
653 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800654 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700655 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700656 @fixed_inserted = ();
657 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700658 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700659}
660
661exit($exit);
662
663sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700664 my ($root) = @_;
665
666 my @tree_check = (
667 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
668 "README", "Documentation", "arch", "include", "drivers",
669 "fs", "init", "ipc", "kernel", "lib", "scripts",
670 );
671
672 foreach my $check (@tree_check) {
673 if (! -e $root . '/' . $check) {
674 return 0;
675 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700676 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700677 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700678}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700679
Joe Perches20112472011-07-25 17:13:23 -0700680sub parse_email {
681 my ($formatted_email) = @_;
682
683 my $name = "";
684 my $address = "";
685 my $comment = "";
686
687 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
688 $name = $1;
689 $address = $2;
690 $comment = $3 if defined $3;
691 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
692 $address = $1;
693 $comment = $2 if defined $2;
694 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
695 $address = $1;
696 $comment = $2 if defined $2;
697 $formatted_email =~ s/$address.*$//;
698 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700699 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700700 $name =~ s/^\"|\"$//g;
701 # If there's a name left after stripping spaces and
702 # leading quotes, and the address doesn't have both
703 # leading and trailing angle brackets, the address
704 # is invalid. ie:
705 # "joe smith joe@smith.com" bad
706 # "joe smith <joe@smith.com" bad
707 if ($name ne "" && $address !~ /^<[^>]+>$/) {
708 $name = "";
709 $address = "";
710 $comment = "";
711 }
712 }
713
Joe Perches3705ce52013-07-03 15:05:31 -0700714 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700715 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700716 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700717 $address =~ s/^\<|\>$//g;
718
719 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
720 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
721 $name = "\"$name\"";
722 }
723
724 return ($name, $address, $comment);
725}
726
727sub format_email {
728 my ($name, $address) = @_;
729
730 my $formatted_email;
731
Joe Perches3705ce52013-07-03 15:05:31 -0700732 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700733 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700734 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700735
736 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
737 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
738 $name = "\"$name\"";
739 }
740
741 if ("$name" eq "") {
742 $formatted_email = "$address";
743 } else {
744 $formatted_email = "$name <$address>";
745 }
746
747 return $formatted_email;
748}
749
Joe Perchesd311cd42014-08-06 16:10:57 -0700750sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700751 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700752
Joe Perchesbd474ca2014-08-06 16:11:10 -0700753 foreach my $path (split(/:/, $ENV{PATH})) {
754 if (-e "$path/$bin") {
755 return "$path/$bin";
756 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700757 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700758
Joe Perchesbd474ca2014-08-06 16:11:10 -0700759 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700760}
761
Joe Perches000d1cc12011-07-25 17:13:25 -0700762sub which_conf {
763 my ($conf) = @_;
764
765 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
766 if (-e "$path/$conf") {
767 return "$path/$conf";
768 }
769 }
770
771 return "";
772}
773
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700774sub expand_tabs {
775 my ($str) = @_;
776
777 my $res = '';
778 my $n = 0;
779 for my $c (split(//, $str)) {
780 if ($c eq "\t") {
781 $res .= ' ';
782 $n++;
783 for (; ($n % 8) != 0; $n++) {
784 $res .= ' ';
785 }
786 next;
787 }
788 $res .= $c;
789 $n++;
790 }
791
792 return $res;
793}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700794sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700795 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700796 return $res;
797}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700798
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700799sub line_stats {
800 my ($line) = @_;
801
802 # Drop the diff line leader and expand tabs
803 $line =~ s/^.//;
804 $line = expand_tabs($line);
805
806 # Pick the indent from the front of the line.
807 my ($white) = ($line =~ /^(\s*)/);
808
809 return (length($line), length($white));
810}
811
Andy Whitcroft773647a2008-03-28 14:15:58 -0700812my $sanitise_quote = '';
813
814sub sanitise_line_reset {
815 my ($in_comment) = @_;
816
817 if ($in_comment) {
818 $sanitise_quote = '*/';
819 } else {
820 $sanitise_quote = '';
821 }
822}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700823sub sanitise_line {
824 my ($line) = @_;
825
826 my $res = '';
827 my $l = '';
828
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800829 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700830 my $off = 0;
831 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700832
Andy Whitcroft773647a2008-03-28 14:15:58 -0700833 # Always copy over the diff marker.
834 $res = substr($line, 0, 1);
835
836 for ($off = 1; $off < length($line); $off++) {
837 $c = substr($line, $off, 1);
838
839 # Comments we are wacking completly including the begin
840 # and end, all to $;.
841 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
842 $sanitise_quote = '*/';
843
844 substr($res, $off, 2, "$;$;");
845 $off++;
846 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800847 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700848 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700849 $sanitise_quote = '';
850 substr($res, $off, 2, "$;$;");
851 $off++;
852 next;
853 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700854 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
855 $sanitise_quote = '//';
856
857 substr($res, $off, 2, $sanitise_quote);
858 $off++;
859 next;
860 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700861
862 # A \ in a string means ignore the next character.
863 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
864 $c eq "\\") {
865 substr($res, $off, 2, 'XX');
866 $off++;
867 next;
868 }
869 # Regular quotes.
870 if ($c eq "'" || $c eq '"') {
871 if ($sanitise_quote eq '') {
872 $sanitise_quote = $c;
873
874 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700875 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700876 } elsif ($sanitise_quote eq $c) {
877 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700878 }
879 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700880
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800881 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700882 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
883 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700884 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
885 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700886 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
887 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700888 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700889 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700890 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800891 }
892
Daniel Walker113f04a2009-09-21 17:04:35 -0700893 if ($sanitise_quote eq '//') {
894 $sanitise_quote = '';
895 }
896
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800897 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700898 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800899 my $clean = 'X' x length($1);
900 $res =~ s@\<.*\>@<$clean>@;
901
902 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700903 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800904 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700905 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800906 }
907
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700908 return $res;
909}
910
Joe Perchesa6962d72013-04-29 16:18:13 -0700911sub get_quoted_string {
912 my ($line, $rawline) = @_;
913
914 return "" if ($line !~ m/(\"[X]+\")/g);
915 return substr($rawline, $-[0], $+[0] - $-[0]);
916}
917
Andy Whitcroft8905a672007-11-28 16:21:06 -0800918sub ctx_statement_block {
919 my ($linenr, $remain, $off) = @_;
920 my $line = $linenr - 1;
921 my $blk = '';
922 my $soff = $off;
923 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700924 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800925
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800926 my $loff = 0;
927
Andy Whitcroft8905a672007-11-28 16:21:06 -0800928 my $type = '';
929 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800930 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800931 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800932 my $c;
933 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800934
935 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800936 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800937 @stack = (['', 0]) if ($#stack == -1);
938
Andy Whitcroft773647a2008-03-28 14:15:58 -0700939 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800940 # If we are about to drop off the end, pull in more
941 # context.
942 if ($off >= $len) {
943 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700944 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800945 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800946 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800947 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800948 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800949 $len = length($blk);
950 $line++;
951 last;
952 }
953 # Bail if there is no further context.
954 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800955 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800956 last;
957 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800958 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
959 $level++;
960 $type = '#';
961 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800962 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800963 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800964 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800965 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800966
Andy Whitcroft773647a2008-03-28 14:15:58 -0700967 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800968
969 # Handle nested #if/#else.
970 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
971 push(@stack, [ $type, $level ]);
972 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
973 ($type, $level) = @{$stack[$#stack - 1]};
974 } elsif ($remainder =~ /^#\s*endif\b/) {
975 ($type, $level) = @{pop(@stack)};
976 }
977
Andy Whitcroft8905a672007-11-28 16:21:06 -0800978 # Statement ends at the ';' or a close '}' at the
979 # outermost level.
980 if ($level == 0 && $c eq ';') {
981 last;
982 }
983
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800984 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700985 if ($level == 0 && $coff_set == 0 &&
986 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
987 $remainder =~ /^(else)(?:\s|{)/ &&
988 $remainder !~ /^else\s+if\b/) {
989 $coff = $off + length($1) - 1;
990 $coff_set = 1;
991 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
992 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800993 }
994
Andy Whitcroft8905a672007-11-28 16:21:06 -0800995 if (($type eq '' || $type eq '(') && $c eq '(') {
996 $level++;
997 $type = '(';
998 }
999 if ($type eq '(' && $c eq ')') {
1000 $level--;
1001 $type = ($level != 0)? '(' : '';
1002
1003 if ($level == 0 && $coff < $soff) {
1004 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001005 $coff_set = 1;
1006 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001007 }
1008 }
1009 if (($type eq '' || $type eq '{') && $c eq '{') {
1010 $level++;
1011 $type = '{';
1012 }
1013 if ($type eq '{' && $c eq '}') {
1014 $level--;
1015 $type = ($level != 0)? '{' : '';
1016
1017 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001018 if (substr($blk, $off + 1, 1) eq ';') {
1019 $off++;
1020 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001021 last;
1022 }
1023 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001024 # Preprocessor commands end at the newline unless escaped.
1025 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1026 $level--;
1027 $type = '';
1028 $off++;
1029 last;
1030 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001031 $off++;
1032 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001033 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001034 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001035 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001036 $line++;
1037 $remain--;
1038 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001039
1040 my $statement = substr($blk, $soff, $off - $soff + 1);
1041 my $condition = substr($blk, $soff, $coff - $soff + 1);
1042
1043 #warn "STATEMENT<$statement>\n";
1044 #warn "CONDITION<$condition>\n";
1045
Andy Whitcroft773647a2008-03-28 14:15:58 -07001046 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001047
1048 return ($statement, $condition,
1049 $line, $remain + 1, $off - $loff + 1, $level);
1050}
1051
Andy Whitcroftcf655042008-03-04 14:28:20 -08001052sub statement_lines {
1053 my ($stmt) = @_;
1054
1055 # Strip the diff line prefixes and rip blank lines at start and end.
1056 $stmt =~ s/(^|\n)./$1/g;
1057 $stmt =~ s/^\s*//;
1058 $stmt =~ s/\s*$//;
1059
1060 my @stmt_lines = ($stmt =~ /\n/g);
1061
1062 return $#stmt_lines + 2;
1063}
1064
1065sub statement_rawlines {
1066 my ($stmt) = @_;
1067
1068 my @stmt_lines = ($stmt =~ /\n/g);
1069
1070 return $#stmt_lines + 2;
1071}
1072
1073sub statement_block_size {
1074 my ($stmt) = @_;
1075
1076 $stmt =~ s/(^|\n)./$1/g;
1077 $stmt =~ s/^\s*{//;
1078 $stmt =~ s/}\s*$//;
1079 $stmt =~ s/^\s*//;
1080 $stmt =~ s/\s*$//;
1081
1082 my @stmt_lines = ($stmt =~ /\n/g);
1083 my @stmt_statements = ($stmt =~ /;/g);
1084
1085 my $stmt_lines = $#stmt_lines + 2;
1086 my $stmt_statements = $#stmt_statements + 1;
1087
1088 if ($stmt_lines > $stmt_statements) {
1089 return $stmt_lines;
1090 } else {
1091 return $stmt_statements;
1092 }
1093}
1094
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001095sub ctx_statement_full {
1096 my ($linenr, $remain, $off) = @_;
1097 my ($statement, $condition, $level);
1098
1099 my (@chunks);
1100
Andy Whitcroftcf655042008-03-04 14:28:20 -08001101 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001102 ($statement, $condition, $linenr, $remain, $off, $level) =
1103 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001104 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001105 push(@chunks, [ $condition, $statement ]);
1106 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1107 return ($level, $linenr, @chunks);
1108 }
1109
1110 # Pull in the following conditional/block pairs and see if they
1111 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001112 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001113 ($statement, $condition, $linenr, $remain, $off, $level) =
1114 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001115 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001116 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001117 #print "C: push\n";
1118 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001119 }
1120
1121 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001122}
1123
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001124sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001125 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001126 my $line;
1127 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001128 my $blk = '';
1129 my @o;
1130 my @c;
1131 my @res = ();
1132
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001133 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001134 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001135 for ($line = $start; $remain > 0; $line++) {
1136 next if ($rawlines[$line] =~ /^-/);
1137 $remain--;
1138
1139 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001140
1141 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001142 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001143 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001144 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001145 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001146 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001147 $level = pop(@stack);
1148 }
1149
Andy Whitcroft01464f32010-10-26 14:23:19 -07001150 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001151 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1152 if ($off > 0) {
1153 $off--;
1154 next;
1155 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001156
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001157 if ($c eq $close && $level > 0) {
1158 $level--;
1159 last if ($level == 0);
1160 } elsif ($c eq $open) {
1161 $level++;
1162 }
1163 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001164
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001165 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001166 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001167 }
1168
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001169 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001170 }
1171
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001172 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001173}
1174sub ctx_block_outer {
1175 my ($linenr, $remain) = @_;
1176
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001177 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1178 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001179}
1180sub ctx_block {
1181 my ($linenr, $remain) = @_;
1182
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001183 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1184 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001185}
1186sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001187 my ($linenr, $remain, $off) = @_;
1188
1189 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1190 return @r;
1191}
1192sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001193 my ($linenr, $remain) = @_;
1194
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001195 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001196}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001197sub ctx_statement_level {
1198 my ($linenr, $remain, $off) = @_;
1199
1200 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1201}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001202
1203sub ctx_locate_comment {
1204 my ($first_line, $end_line) = @_;
1205
1206 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001207 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001208 return $current_comment if (defined $current_comment);
1209
1210 # Look through the context and try and figure out if there is a
1211 # comment.
1212 my $in_comment = 0;
1213 $current_comment = '';
1214 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001215 my $line = $rawlines[$linenr - 1];
1216 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001217 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1218 $in_comment = 1;
1219 }
1220 if ($line =~ m@/\*@) {
1221 $in_comment = 1;
1222 }
1223 if (!$in_comment && $current_comment ne '') {
1224 $current_comment = '';
1225 }
1226 $current_comment .= $line . "\n" if ($in_comment);
1227 if ($line =~ m@\*/@) {
1228 $in_comment = 0;
1229 }
1230 }
1231
1232 chomp($current_comment);
1233 return($current_comment);
1234}
1235sub ctx_has_comment {
1236 my ($first_line, $end_line) = @_;
1237 my $cmt = ctx_locate_comment($first_line, $end_line);
1238
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001239 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001240 ##print "CMMT: $cmt\n";
1241
1242 return ($cmt ne '');
1243}
1244
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001245sub raw_line {
1246 my ($linenr, $cnt) = @_;
1247
1248 my $offset = $linenr - 1;
1249 $cnt++;
1250
1251 my $line;
1252 while ($cnt) {
1253 $line = $rawlines[$offset++];
1254 next if (defined($line) && $line =~ /^-/);
1255 $cnt--;
1256 }
1257
1258 return $line;
1259}
1260
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001261sub cat_vet {
1262 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001263 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001264
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001265 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001266 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1267 $res .= $1;
1268 if ($2 ne '') {
1269 $coded = sprintf("^%c", unpack('C', $2) + 64);
1270 $res .= $coded;
1271 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001272 }
1273 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001274
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001275 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001276}
1277
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001278my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001279my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001280my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001281my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001282
1283sub annotate_reset {
1284 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001285 $av_pending = '_';
1286 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001287 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288}
1289
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001290sub annotate_values {
1291 my ($stream, $type) = @_;
1292
1293 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001294 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001295 my $cur = $stream;
1296
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001299 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001301 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001302 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001303 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001304 print "WS($1)\n" if ($dbg_values > 1);
1305 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001306 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001307 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001308 }
1309
Florian Micklerc023e4732011-01-12 16:59:58 -08001310 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001311 print "CAST($1)\n" if ($dbg_values > 1);
1312 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001313 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001314
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001315 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001316 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001317 $type = 'T';
1318
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001319 } elsif ($cur =~ /^($Modifier)\s*/) {
1320 print "MODIFIER($1)\n" if ($dbg_values > 1);
1321 $type = 'T';
1322
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001323 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001324 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001325 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001326 push(@av_paren_type, $type);
1327 if ($2 ne '') {
1328 $av_pending = 'N';
1329 }
1330 $type = 'E';
1331
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001332 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001333 print "UNDEF($1)\n" if ($dbg_values > 1);
1334 $av_preprocessor = 1;
1335 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001336
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001337 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001338 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001339 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001340
1341 push(@av_paren_type, $type);
1342 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001343 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001344
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001345 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001346 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1347 $av_preprocessor = 1;
1348
1349 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1350
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001351 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001352
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001353 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001354 print "PRE_END($1)\n" if ($dbg_values > 1);
1355
1356 $av_preprocessor = 1;
1357
1358 # Assume all arms of the conditional end as this
1359 # one does, and continue as if the #endif was not here.
1360 pop(@av_paren_type);
1361 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001362 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001363
1364 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001365 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001366
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001367 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1368 print "ATTR($1)\n" if ($dbg_values > 1);
1369 $av_pending = $type;
1370 $type = 'N';
1371
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001374 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001376 }
1377 $type = 'N';
1378
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001379 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001380 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001381 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001382 $type = 'N';
1383
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001384 } elsif ($cur =~/^(case)/o) {
1385 print "CASE($1)\n" if ($dbg_values > 1);
1386 $av_pend_colon = 'C';
1387 $type = 'N';
1388
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001389 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001390 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001391 $type = 'N';
1392
1393 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001394 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001395 push(@av_paren_type, $av_pending);
1396 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001397 $type = 'N';
1398
1399 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001400 my $new_type = pop(@av_paren_type);
1401 if ($new_type ne '_') {
1402 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001403 print "PAREN('$1') -> $type\n"
1404 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001405 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001406 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001407 }
1408
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001409 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001410 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001411 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001412 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001414 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1415 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001416 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001417 } elsif ($type eq 'E') {
1418 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001419 }
1420 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1421 $type = 'V';
1422
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001423 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001424 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001425 $type = 'V';
1426
1427 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001428 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001429 $type = 'N';
1430
Andy Whitcroftcf655042008-03-04 14:28:20 -08001431 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001432 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001433 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001434 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001435
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001436 } elsif ($cur =~/^(,)/) {
1437 print "COMMA($1)\n" if ($dbg_values > 1);
1438 $type = 'C';
1439
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001440 } elsif ($cur =~ /^(\?)/o) {
1441 print "QUESTION($1)\n" if ($dbg_values > 1);
1442 $type = 'N';
1443
1444 } elsif ($cur =~ /^(:)/o) {
1445 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1446
1447 substr($var, length($res), 1, $av_pend_colon);
1448 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1449 $type = 'E';
1450 } else {
1451 $type = 'N';
1452 }
1453 $av_pend_colon = 'O';
1454
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001455 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001456 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001457 $type = 'N';
1458
Andy Whitcroft0d413862008-10-15 22:02:16 -07001459 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001460 my $variant;
1461
1462 print "OPV($1)\n" if ($dbg_values > 1);
1463 if ($type eq 'V') {
1464 $variant = 'B';
1465 } else {
1466 $variant = 'U';
1467 }
1468
1469 substr($var, length($res), 1, $variant);
1470 $type = 'N';
1471
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001472 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001473 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001474 if ($1 ne '++' && $1 ne '--') {
1475 $type = 'N';
1476 }
1477
1478 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001479 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001480 }
1481 if (defined $1) {
1482 $cur = substr($cur, length($1));
1483 $res .= $type x length($1);
1484 }
1485 }
1486
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001487 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488}
1489
Andy Whitcroft8905a672007-11-28 16:21:06 -08001490sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001491 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001492 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001493 ^(?:
1494 $Modifier|
1495 $Storage|
1496 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001497 DEFINE_\S+
1498 )$|
1499 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001500 goto|
1501 return|
1502 case|
1503 else|
1504 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001505 do|
1506 \#|
1507 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001508 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001509 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001510 )}x;
1511 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1512 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001513 # Check for modifiers.
1514 $possible =~ s/\s*$Storage\s*//g;
1515 $possible =~ s/\s*$Sparse\s*//g;
1516 if ($possible =~ /^\s*$/) {
1517
1518 } elsif ($possible =~ /\s/) {
1519 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001520 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001521 if ($modifier !~ $notPermitted) {
1522 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1523 push(@modifierList, $modifier);
1524 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001525 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001526
1527 } else {
1528 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1529 push(@typeList, $possible);
1530 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001531 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001532 } else {
1533 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001534 }
1535}
1536
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001537my $prefix = '';
1538
Joe Perches000d1cc12011-07-25 17:13:25 -07001539sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001540 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001541
Joe Perchescbec18a2014-04-03 14:49:19 -07001542 return defined $use_type{$type} if (scalar keys %use_type > 0);
1543
1544 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001545}
1546
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001547sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001548 my ($level, $type, $msg) = @_;
1549
1550 if (!show_type($type) ||
1551 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001552 return 0;
1553 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001554 my $line;
1555 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001556 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001557 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001558 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001559 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001560 $line = (split('\n', $line))[0] . "\n" if ($terse);
1561
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001562 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001563
1564 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001565}
Joe Perchescbec18a2014-04-03 14:49:19 -07001566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001567sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001568 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001569}
Joe Perches000d1cc12011-07-25 17:13:25 -07001570
Joe Perchesd752fcc2014-08-06 16:11:05 -07001571sub fixup_current_range {
1572 my ($lineRef, $offset, $length) = @_;
1573
1574 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1575 my $o = $1;
1576 my $l = $2;
1577 my $no = $o + $offset;
1578 my $nl = $l + $length;
1579 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1580 }
1581}
1582
1583sub fix_inserted_deleted_lines {
1584 my ($linesRef, $insertedRef, $deletedRef) = @_;
1585
1586 my $range_last_linenr = 0;
1587 my $delta_offset = 0;
1588
1589 my $old_linenr = 0;
1590 my $new_linenr = 0;
1591
1592 my $next_insert = 0;
1593 my $next_delete = 0;
1594
1595 my @lines = ();
1596
1597 my $inserted = @{$insertedRef}[$next_insert++];
1598 my $deleted = @{$deletedRef}[$next_delete++];
1599
1600 foreach my $old_line (@{$linesRef}) {
1601 my $save_line = 1;
1602 my $line = $old_line; #don't modify the array
1603 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1604 $delta_offset = 0;
1605 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1606 $range_last_linenr = $new_linenr;
1607 fixup_current_range(\$line, $delta_offset, 0);
1608 }
1609
1610 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1611 $deleted = @{$deletedRef}[$next_delete++];
1612 $save_line = 0;
1613 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1614 }
1615
1616 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1617 push(@lines, ${$inserted}{'LINE'});
1618 $inserted = @{$insertedRef}[$next_insert++];
1619 $new_linenr++;
1620 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1621 }
1622
1623 if ($save_line) {
1624 push(@lines, $line);
1625 $new_linenr++;
1626 }
1627
1628 $old_linenr++;
1629 }
1630
1631 return @lines;
1632}
1633
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001634sub fix_insert_line {
1635 my ($linenr, $line) = @_;
1636
1637 my $inserted = {
1638 LINENR => $linenr,
1639 LINE => $line,
1640 };
1641 push(@fixed_inserted, $inserted);
1642}
1643
1644sub fix_delete_line {
1645 my ($linenr, $line) = @_;
1646
1647 my $deleted = {
1648 LINENR => $linenr,
1649 LINE => $line,
1650 };
1651
1652 push(@fixed_deleted, $deleted);
1653}
1654
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001655sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001656 my ($type, $msg) = @_;
1657
1658 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001659 our $clean = 0;
1660 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001661 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001662 }
Joe Perches3705ce52013-07-03 15:05:31 -07001663 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001664}
1665sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001666 my ($type, $msg) = @_;
1667
1668 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001669 our $clean = 0;
1670 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001671 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001672 }
Joe Perches3705ce52013-07-03 15:05:31 -07001673 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001674}
1675sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001676 my ($type, $msg) = @_;
1677
1678 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001679 our $clean = 0;
1680 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001681 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001682 }
Joe Perches3705ce52013-07-03 15:05:31 -07001683 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001684}
1685
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001686sub check_absolute_file {
1687 my ($absolute, $herecurr) = @_;
1688 my $file = $absolute;
1689
1690 ##print "absolute<$absolute>\n";
1691
1692 # See if any suffix of this path is a path within the tree.
1693 while ($file =~ s@^[^/]*/@@) {
1694 if (-f "$root/$file") {
1695 ##print "file<$file>\n";
1696 last;
1697 }
1698 }
1699 if (! -f _) {
1700 return 0;
1701 }
1702
1703 # It is, so see if the prefix is acceptable.
1704 my $prefix = $absolute;
1705 substr($prefix, -length($file)) = '';
1706
1707 ##print "prefix<$prefix>\n";
1708 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001709 WARN("USE_RELATIVE_PATH",
1710 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001711 }
1712}
1713
Joe Perches3705ce52013-07-03 15:05:31 -07001714sub trim {
1715 my ($string) = @_;
1716
Joe Perchesb34c6482013-09-11 14:24:01 -07001717 $string =~ s/^\s+|\s+$//g;
1718
1719 return $string;
1720}
1721
1722sub ltrim {
1723 my ($string) = @_;
1724
1725 $string =~ s/^\s+//;
1726
1727 return $string;
1728}
1729
1730sub rtrim {
1731 my ($string) = @_;
1732
1733 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001734
1735 return $string;
1736}
1737
Joe Perches52ea8502013-11-12 15:10:09 -08001738sub string_find_replace {
1739 my ($string, $find, $replace) = @_;
1740
1741 $string =~ s/$find/$replace/g;
1742
1743 return $string;
1744}
1745
Joe Perches3705ce52013-07-03 15:05:31 -07001746sub tabify {
1747 my ($leading) = @_;
1748
1749 my $source_indent = 8;
1750 my $max_spaces_before_tab = $source_indent - 1;
1751 my $spaces_to_tab = " " x $source_indent;
1752
1753 #convert leading spaces to tabs
1754 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1755 #Remove spaces before a tab
1756 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1757
1758 return "$leading";
1759}
1760
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001761sub pos_last_openparen {
1762 my ($line) = @_;
1763
1764 my $pos = 0;
1765
1766 my $opens = $line =~ tr/\(/\(/;
1767 my $closes = $line =~ tr/\)/\)/;
1768
1769 my $last_openparen = 0;
1770
1771 if (($opens == 0) || ($closes >= $opens)) {
1772 return -1;
1773 }
1774
1775 my $len = length($line);
1776
1777 for ($pos = 0; $pos < $len; $pos++) {
1778 my $string = substr($line, $pos);
1779 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1780 $pos += length($1) - 1;
1781 } elsif (substr($line, $pos, 1) eq '(') {
1782 $last_openparen = $pos;
1783 } elsif (index($string, '(') == -1) {
1784 last;
1785 }
1786 }
1787
Joe Perches91cb5192014-04-03 14:49:32 -07001788 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001789}
1790
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001791sub process {
1792 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001793
1794 my $linenr=0;
1795 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001796 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001797 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001798 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001799
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001800 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001801 my $indent;
1802 my $previndent=0;
1803 my $stashindent=0;
1804
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001805 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001806 my $signoff = 0;
1807 my $is_patch = 0;
1808
Joe Perches29ee1b02014-08-06 16:10:35 -07001809 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001810 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001811 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001812 my $non_utf8_charset = 0;
1813
Joe Perches365dd4e2014-08-06 16:10:42 -07001814 my $last_blank_line = 0;
1815
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001816 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001817 our $cnt_lines = 0;
1818 our $cnt_error = 0;
1819 our $cnt_warn = 0;
1820 our $cnt_chk = 0;
1821
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001822 # Trace the real file/line as we go.
1823 my $realfile = '';
1824 my $realline = 0;
1825 my $realcnt = 0;
1826 my $here = '';
1827 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001829 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001830 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001831
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001832 my $prev_values = 'E';
1833
1834 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001835 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001836 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001837 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001838 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001839
Joe Perches7e51f192013-09-11 14:23:57 -07001840 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001841
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001843 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001845 my @setup_docs = ();
1846 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001847
Joe Perchesd8b07712013-11-12 15:10:06 -08001848 my $camelcase_file_seeded = 0;
1849
Andy Whitcroft773647a2008-03-28 14:15:58 -07001850 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001851 my $line;
1852 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001853 $linenr++;
1854 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001855
Joe Perches3705ce52013-07-03 15:05:31 -07001856 push(@fixed, $rawline) if ($fix);
1857
Andy Whitcroft773647a2008-03-28 14:15:58 -07001858 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001859 $setup_docs = 0;
1860 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1861 $setup_docs = 1;
1862 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001863 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001864 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001865 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1866 $realline=$1-1;
1867 if (defined $2) {
1868 $realcnt=$3+1;
1869 } else {
1870 $realcnt=1+1;
1871 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001872 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873
1874 # Guestimate if this is a continuing comment. Run
1875 # the context looking for a comment "edge". If this
1876 # edge is a close comment then we must be in a comment
1877 # at context start.
1878 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001879 my $cnt = $realcnt;
1880 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1881 next if (defined $rawlines[$ln - 1] &&
1882 $rawlines[$ln - 1] =~ /^-/);
1883 $cnt--;
1884 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001885 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001886 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1887 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1888 ($edge) = $1;
1889 last;
1890 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001891 }
1892 if (defined $edge && $edge eq '*/') {
1893 $in_comment = 1;
1894 }
1895
1896 # Guestimate if this is a continuing comment. If this
1897 # is the start of a diff block and this line starts
1898 # ' *' then it is very likely a comment.
1899 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001900 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001901 {
1902 $in_comment = 1;
1903 }
1904
1905 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1906 sanitise_line_reset($in_comment);
1907
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001908 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001909 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001910 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001911 $line = sanitise_line($rawline);
1912 }
1913 push(@lines, $line);
1914
1915 if ($realcnt > 1) {
1916 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1917 } else {
1918 $realcnt = 0;
1919 }
1920
1921 #print "==>$rawline\n";
1922 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001923
1924 if ($setup_docs && $line =~ /^\+/) {
1925 push(@setup_docs, $line);
1926 }
1927 }
1928
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001929 $prefix = '';
1930
Andy Whitcroft773647a2008-03-28 14:15:58 -07001931 $realcnt = 0;
1932 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001933 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001934 foreach my $line (@lines) {
1935 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001936 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001937 my $sline = $line; #copy of $line
1938 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001939
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001940 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001941
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001942#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001943 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001944 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001945 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001946 $realline=$1-1;
1947 if (defined $2) {
1948 $realcnt=$3+1;
1949 } else {
1950 $realcnt=1+1;
1951 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001952 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001953 $prev_values = 'E';
1954
Andy Whitcroft773647a2008-03-28 14:15:58 -07001955 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001956 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001957 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001958 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001959 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001960
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001961# track the line number as we move through the hunk, note that
1962# new versions of GNU diff omit the leading space on completely
1963# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001964 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001965 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001966 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001967
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001968 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001969 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001970
1971 # Track the previous line.
1972 ($prevline, $stashline) = ($stashline, $line);
1973 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001974 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1975
Andy Whitcroft773647a2008-03-28 14:15:58 -07001976 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001977
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001978 } elsif ($realcnt == 1) {
1979 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001980 }
1981
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001982 my $hunk_line = ($realcnt != 0);
1983
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001984#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001985 $prefix = "$filename:$realline: " if ($emacs && $file);
1986 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1987
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001988 $here = "#$linenr: " if (!$file);
1989 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001990
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001991 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001992 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001993 if ($line =~ /^diff --git.*?(\S+)$/) {
1994 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001995 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001996 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001997 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001998 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001999 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002000 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002001 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002002
2003 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002004 if (!$file && $tree && $p1_prefix ne '' &&
2005 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002006 WARN("PATCH_PREFIX",
2007 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002008 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002009
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002010 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002011 ERROR("MODIFIED_INCLUDE_ASM",
2012 "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 -07002013 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002014 $found_file = 1;
2015 }
2016
2017 if ($found_file) {
2018 if ($realfile =~ m@^(drivers/net/|net/)@) {
2019 $check = 1;
2020 } else {
2021 $check = $check_orig;
2022 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 next;
2024 }
2025
Randy Dunlap389834b2007-06-08 13:47:03 -07002026 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002027
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002028 my $hereline = "$here\n$rawline\n";
2029 my $herecurr = "$here\n$rawline\n";
2030 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002031
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002032 $cnt_lines++ if ($realcnt != 0);
2033
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002034# Check for incorrect file permissions
2035 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2036 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002037 if ($realfile !~ m@scripts/@ &&
2038 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002039 ERROR("EXECUTE_PERMISSIONS",
2040 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002041 }
2042 }
2043
Joe Perches20112472011-07-25 17:13:23 -07002044# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002045 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002046 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002047 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002048 }
2049
2050# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002051 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002052 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002053 my $space_before = $1;
2054 my $sign_off = $2;
2055 my $space_after = $3;
2056 my $email = $4;
2057 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2058
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002059 if ($sign_off !~ /$signature_tags/) {
2060 WARN("BAD_SIGN_OFF",
2061 "Non-standard signature: $sign_off\n" . $herecurr);
2062 }
Joe Perches20112472011-07-25 17:13:23 -07002063 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002064 if (WARN("BAD_SIGN_OFF",
2065 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2066 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002067 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002068 "$ucfirst_sign_off $email";
2069 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002070 }
Joe Perches20112472011-07-25 17:13:23 -07002071 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002072 if (WARN("BAD_SIGN_OFF",
2073 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2074 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002075 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002076 "$ucfirst_sign_off $email";
2077 }
2078
Joe Perches20112472011-07-25 17:13:23 -07002079 }
2080 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002081 if (WARN("BAD_SIGN_OFF",
2082 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2083 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002084 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002085 "$ucfirst_sign_off $email";
2086 }
Joe Perches20112472011-07-25 17:13:23 -07002087 }
2088
2089 my ($email_name, $email_address, $comment) = parse_email($email);
2090 my $suggested_email = format_email(($email_name, $email_address));
2091 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002092 ERROR("BAD_SIGN_OFF",
2093 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002094 } else {
2095 my $dequoted = $suggested_email;
2096 $dequoted =~ s/^"//;
2097 $dequoted =~ s/" </ </;
2098 # Don't force email to have quotes
2099 # Allow just an angle bracketed address
2100 if ("$dequoted$comment" ne $email &&
2101 "<$email_address>$comment" ne $email &&
2102 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002103 WARN("BAD_SIGN_OFF",
2104 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002105 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002106 }
Joe Perches7e51f192013-09-11 14:23:57 -07002107
2108# Check for duplicate signatures
2109 my $sig_nospace = $line;
2110 $sig_nospace =~ s/\s//g;
2111 $sig_nospace = lc($sig_nospace);
2112 if (defined $signatures{$sig_nospace}) {
2113 WARN("BAD_SIGN_OFF",
2114 "Duplicate signature\n" . $herecurr);
2115 } else {
2116 $signatures{$sig_nospace} = 1;
2117 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002118 }
2119
Joe Perches9b3189eb2014-06-04 16:12:10 -07002120# Check for old stable address
2121 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2122 ERROR("STABLE_ADDRESS",
2123 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2124 }
2125
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002126# Check for unwanted Gerrit info
2127 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2128 ERROR("GERRIT_CHANGE_ID",
2129 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2130 }
2131
Joe Perchesd311cd42014-08-06 16:10:57 -07002132# Check for improperly formed commit descriptions
2133 if ($in_commit_log &&
2134 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2135 $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2136 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2137 my $init_char = $1;
2138 my $orig_commit = lc($2);
2139 my $id = '01234567890ab';
2140 my $desc = 'commit description';
2141 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2142 ERROR("GIT_COMMIT_ID",
2143 "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2144 }
2145
Joe Perches13f19372014-08-06 16:10:59 -07002146# Check for added, moved or deleted files
2147 if (!$reported_maintainer_file && !$in_commit_log &&
2148 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2149 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2150 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2151 (defined($1) || defined($2))))) {
2152 $reported_maintainer_file = 1;
2153 WARN("FILE_PATH_CHANGES",
2154 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2155 }
2156
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002157# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002158 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002159 ERROR("CORRUPTED_PATCH",
2160 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002161 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002162 }
2163
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002164# Check for absolute kernel paths.
2165 if ($tree) {
2166 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2167 my $file = $1;
2168
2169 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2170 check_absolute_file($1, $herecurr)) {
2171 #
2172 } else {
2173 check_absolute_file($file, $herecurr);
2174 }
2175 }
2176 }
2177
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002178# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2179 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002180 $rawline !~ m/^$UTF8*$/) {
2181 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2182
2183 my $blank = copy_spacing($rawline);
2184 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2185 my $hereptr = "$hereline$ptr\n";
2186
Joe Perches34d99212011-07-25 17:13:26 -07002187 CHK("INVALID_UTF8",
2188 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002189 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002190
Joe Perches15662b32011-10-31 17:13:12 -07002191# Check if it's the start of a commit log
2192# (not a header line and we haven't seen the patch filename)
2193 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002194 !($rawline =~ /^\s+\S/ ||
2195 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002196 $in_header_lines = 0;
2197 $in_commit_log = 1;
2198 }
2199
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002200# Check if there is UTF-8 in a commit log when a mail header has explicitly
2201# declined it, i.e defined some charset where it is missing.
2202 if ($in_header_lines &&
2203 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2204 $1 !~ /utf-8/i) {
2205 $non_utf8_charset = 1;
2206 }
2207
2208 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002209 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002210 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002211 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2212 }
2213
Andy Whitcroft306708542008-10-15 22:02:28 -07002214# ignore non-hunk lines and lines being removed
2215 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002216
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002217#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002218 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002219 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002220 if (ERROR("DOS_LINE_ENDINGS",
2221 "DOS line endings\n" . $herevet) &&
2222 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002223 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002224 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002225 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2226 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002227 if (ERROR("TRAILING_WHITESPACE",
2228 "trailing whitespace\n" . $herevet) &&
2229 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002230 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002231 }
2232
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002233 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002234 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002235
Josh Triplett4783f892013-11-12 15:10:12 -08002236# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002237 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002238 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2239 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002240 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2241 my $msg_type = \&ERROR;
2242 $msg_type = \&CHK if ($file);
2243 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002244 "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 -08002245 }
2246
Andi Kleen33549572010-05-24 14:33:29 -07002247# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002248# Only applies when adding the entry originally, after that we do not have
2249# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002250 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002251 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002252 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002253 my $cnt = $realcnt;
2254 my $ln = $linenr + 1;
2255 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002256 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002257 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002258 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002259 $f = $lines[$ln - 1];
2260 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2261 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002262
2263 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002264 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002265
Joe Perches8d73e0e2014-08-06 16:10:46 -07002266 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002267 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002268 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002269 $length = -1;
2270 }
2271
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002272 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002273 $f =~ s/#.*//;
2274 $f =~ s/^\s+//;
2275 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002276 if ($f =~ /^\s*config\s/) {
2277 $is_end = 1;
2278 last;
2279 }
Andi Kleen33549572010-05-24 14:33:29 -07002280 $length++;
2281 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002282 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08002283 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2284 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002285 }
2286
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002287# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2288 if ($realfile =~ /Kconfig/ &&
2289 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2290 WARN("CONFIG_EXPERIMENTAL",
2291 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2292 }
2293
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002294 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2295 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2296 my $flag = $1;
2297 my $replacement = {
2298 'EXTRA_AFLAGS' => 'asflags-y',
2299 'EXTRA_CFLAGS' => 'ccflags-y',
2300 'EXTRA_CPPFLAGS' => 'cppflags-y',
2301 'EXTRA_LDFLAGS' => 'ldflags-y',
2302 };
2303
2304 WARN("DEPRECATED_VARIABLE",
2305 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2306 }
2307
Rob Herringbff5da42014-01-23 15:54:51 -08002308# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002309 if (defined $root &&
2310 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2311 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2312
Rob Herringbff5da42014-01-23 15:54:51 -08002313 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2314
Florian Vaussardcc933192014-04-03 14:49:27 -07002315 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2316 my $vp_file = $dt_path . "vendor-prefixes.txt";
2317
Rob Herringbff5da42014-01-23 15:54:51 -08002318 foreach my $compat (@compats) {
2319 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002320 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2321 my $compat3 = $compat;
2322 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2323 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002324 if ( $? >> 8 ) {
2325 WARN("UNDOCUMENTED_DT_STRING",
2326 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2327 }
2328
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002329 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2330 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002331 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002332 if ( $? >> 8 ) {
2333 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002334 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002335 }
2336 }
2337 }
2338
Andy Whitcroft5368df202008-10-15 22:02:27 -07002339# check we are in a valid source file if not then ignore this hunk
2340 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2341
Joe Perches6cd7f382012-12-17 16:01:54 -08002342#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002343 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002344 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002345 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002346 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002347 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002348 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002349 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002350 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002351 }
2352
Josh Triplettca56dc02012-03-23 15:02:21 -07002353# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002354# to grep for the string. Make exceptions when the previous string ends in a
2355# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2356# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002357 if ($line =~ /^\+\s*"/ &&
2358 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002359 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002360 WARN("SPLIT_STRING",
2361 "quoted string split across lines\n" . $hereprev);
2362 }
2363
Joe Perches5e79d962010-03-05 13:43:55 -08002364# check for spaces before a quoted newline
2365 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002366 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2367 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2368 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002369 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002370 }
2371
Joe Perches5e79d962010-03-05 13:43:55 -08002372 }
2373
Andy Whitcroft8905a672007-11-28 16:21:06 -08002374# check for adding lines without a newline.
2375 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002376 WARN("MISSING_EOF_NEWLINE",
2377 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002378 }
2379
Mike Frysinger42e41c52009-09-21 17:04:40 -07002380# Blackfin: use hi/lo macros
2381 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2382 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2383 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002384 ERROR("LO_MACRO",
2385 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002386 }
2387 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2388 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002389 ERROR("HI_MACRO",
2390 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002391 }
2392 }
2393
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002394# check we are in a valid source file C or perl if not then ignore this hunk
2395 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002396
2397# at the beginning of a line any tabs must come first and anything
2398# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002399 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2400 $rawline =~ /^\+\s* \s*/) {
2401 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002402 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002403 if (ERROR("CODE_INDENT",
2404 "code indent should use tabs where possible\n" . $herevet) &&
2405 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002406 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002407 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002408 }
2409
Alberto Panizzo08e44362010-03-05 13:43:54 -08002410# check for space before tabs.
2411 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2412 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002413 if (WARN("SPACE_BEFORE_TAB",
2414 "please, no space before tabs\n" . $herevet) &&
2415 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002416 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002417 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002418 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002419 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002420 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002421 }
2422
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002423# check for && or || at the start of a line
2424 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2425 CHK("LOGICAL_CONTINUATIONS",
2426 "Logical continuations should be on the previous line\n" . $hereprev);
2427 }
2428
2429# check multi-line statement indentation matches previous line
2430 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002431 $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 -07002432 $prevline =~ /^\+(\t*)(.*)$/;
2433 my $oldindent = $1;
2434 my $rest = $2;
2435
2436 my $pos = pos_last_openparen($rest);
2437 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002438 $line =~ /^(\+| )([ \t]*)/;
2439 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002440
2441 my $goodtabindent = $oldindent .
2442 "\t" x ($pos / 8) .
2443 " " x ($pos % 8);
2444 my $goodspaceindent = $oldindent . " " x $pos;
2445
2446 if ($newindent ne $goodtabindent &&
2447 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002448
2449 if (CHK("PARENTHESIS_ALIGNMENT",
2450 "Alignment should match open parenthesis\n" . $hereprev) &&
2451 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002452 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002453 s/^\+[ \t]*/\+$goodtabindent/;
2454 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002455 }
2456 }
2457 }
2458
Joe Perchesf27c95d2014-08-06 16:10:52 -07002459 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002460 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002461 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002462 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002463 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002464 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002465 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002466 }
2467
Joe Perches05880602012-10-04 17:13:35 -07002468 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002469 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002470 $rawline =~ /^\+[ \t]*\*/ &&
2471 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002472 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2473 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2474 }
2475
2476 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002477 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2478 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002479 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002480 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2481 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2482 "networking block comments start with * on subsequent lines\n" . $hereprev);
2483 }
2484
2485 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002486 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2487 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2488 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2489 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002490 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2491 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2492 }
2493
Joe Perches7f619192014-08-06 16:10:39 -07002494# check for missing blank lines after struct/union declarations
2495# with exceptions for various attributes and macros
2496 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2497 $line =~ /^\+/ &&
2498 !($line =~ /^\+\s*$/ ||
2499 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2500 $line =~ /^\+\s*MODULE_/i ||
2501 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2502 $line =~ /^\+[a-z_]*init/ ||
2503 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2504 $line =~ /^\+\s*DECLARE/ ||
2505 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002506 if (CHK("LINE_SPACING",
2507 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2508 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002509 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002510 }
Joe Perches7f619192014-08-06 16:10:39 -07002511 }
2512
Joe Perches365dd4e2014-08-06 16:10:42 -07002513# check for multiple consecutive blank lines
2514 if ($prevline =~ /^[\+ ]\s*$/ &&
2515 $line =~ /^\+\s*$/ &&
2516 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002517 if (CHK("LINE_SPACING",
2518 "Please don't use multiple blank lines\n" . $hereprev) &&
2519 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002520 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002521 }
2522
Joe Perches365dd4e2014-08-06 16:10:42 -07002523 $last_blank_line = $linenr;
2524 }
2525
Joe Perches3b617e32014-04-03 14:49:28 -07002526# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002527 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2528 # actual declarations
2529 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002530 # function pointer declarations
2531 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002532 # foo bar; where foo is some local typedef or #define
2533 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2534 # known declaration macros
2535 $prevline =~ /^\+\s+$declaration_macros/) &&
2536 # for "else if" which can look like "$Ident $Ident"
2537 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2538 # other possible extensions of declaration lines
2539 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2540 # not starting a section or a macro "\" extended line
2541 $prevline =~ /(?:\{\s*|\\)$/) &&
2542 # looks like a declaration
2543 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002544 # function pointer declarations
2545 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002546 # foo bar; where foo is some local typedef or #define
2547 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2548 # known declaration macros
2549 $sline =~ /^\+\s+$declaration_macros/ ||
2550 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002551 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002552 # start or end of block or continuation of declaration
2553 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2554 # bitfield continuation
2555 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2556 # other possible extensions of declaration lines
2557 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2558 # indentation of previous and current line are the same
2559 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002560 if (WARN("LINE_SPACING",
2561 "Missing a blank line after declarations\n" . $hereprev) &&
2562 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002563 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002564 }
Joe Perches3b617e32014-04-03 14:49:28 -07002565 }
2566
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002567# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002568# Exceptions:
2569# 1) within comments
2570# 2) indented preprocessor commands
2571# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002572 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002573 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002574 if (WARN("LEADING_SPACE",
2575 "please, no spaces at the start of a line\n" . $herevet) &&
2576 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002577 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002578 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002579 }
2580
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002581# check we are in a valid C source file if not then ignore this hunk
2582 next if ($realfile !~ /\.(h|c)$/);
2583
Joe Perches032a4c02014-08-06 16:10:29 -07002584# check indentation of any line with a bare else
2585# if the previous line is a break or return and is indented 1 tab more...
2586 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2587 my $tabs = length($1) + 1;
2588 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2589 WARN("UNNECESSARY_ELSE",
2590 "else is not generally useful after a break or return\n" . $hereprev);
2591 }
2592 }
2593
Joe Perchesc00df192014-08-06 16:11:01 -07002594# check indentation of a line with a break;
2595# if the previous line is a goto or return and is indented the same # of tabs
2596 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2597 my $tabs = $1;
2598 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2599 WARN("UNNECESSARY_BREAK",
2600 "break is not useful after a goto or return\n" . $hereprev);
2601 }
2602 }
2603
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002604# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2605 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2606 WARN("CONFIG_EXPERIMENTAL",
2607 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2608 }
2609
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002610# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002611 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002612 WARN("CVS_KEYWORD",
2613 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002614 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002615
Mike Frysinger42e41c52009-09-21 17:04:40 -07002616# Blackfin: don't use __builtin_bfin_[cs]sync
2617 if ($line =~ /__builtin_bfin_csync/) {
2618 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002619 ERROR("CSYNC",
2620 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002621 }
2622 if ($line =~ /__builtin_bfin_ssync/) {
2623 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002624 ERROR("SSYNC",
2625 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002626 }
2627
Joe Perches56e77d72013-02-21 16:44:14 -08002628# check for old HOTPLUG __dev<foo> section markings
2629 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2630 WARN("HOTPLUG_SECTION",
2631 "Using $1 is unnecessary\n" . $herecurr);
2632 }
2633
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002634# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002635 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2636 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002637#print "LINE<$line>\n";
2638 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002639 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002640 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002641 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002642 $stat =~ s/\n./\n /g;
2643 $cond =~ s/\n./\n /g;
2644
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002645#print "linenr<$linenr> <$stat>\n";
2646 # If this statement has no statement boundaries within
2647 # it there is no point in retrying a statement scan
2648 # until we hit end of it.
2649 my $frag = $stat; $frag =~ s/;+\s*$//;
2650 if ($frag !~ /(?:{|;)/) {
2651#print "skip<$line_nr_next>\n";
2652 $suppress_statement = $line_nr_next;
2653 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002654
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002655 # Find the real next line.
2656 $realline_next = $line_nr_next;
2657 if (defined $realline_next &&
2658 (!defined $lines[$realline_next - 1] ||
2659 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2660 $realline_next++;
2661 }
2662
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002663 my $s = $stat;
2664 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002665
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002666 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002667 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002668
2669 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002670 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002671
Andy Whitcroft463f2862009-09-21 17:04:34 -07002672 } elsif ($s =~ /^.\s*else\b/s) {
2673
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002674 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002675 } 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 -07002676 my $type = $1;
2677 $type =~ s/\s+/ /g;
2678 possible($type, "A:" . $s);
2679
Andy Whitcroft8905a672007-11-28 16:21:06 -08002680 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002681 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002682 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002683 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002684
2685 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002686 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002687 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002688 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002689
2690 # Check for any sort of function declaration.
2691 # int foo(something bar, other baz);
2692 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002693 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 -08002694 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002695
Andy Whitcroftcf655042008-03-04 14:28:20 -08002696 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002697 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002698 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002699
Andy Whitcroft8905a672007-11-28 16:21:06 -08002700 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002701 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002702
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002703 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002704 }
2705 }
2706 }
2707
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002708 }
2709
Andy Whitcroft653d4872007-06-23 17:16:34 -07002710#
2711# Checks which may be anchored in the context.
2712#
2713
2714# Check for switch () and associated case and default
2715# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002716 if ($line=~/\bswitch\s*\(.*\)/) {
2717 my $err = '';
2718 my $sep = '';
2719 my @ctx = ctx_block_outer($linenr, $realcnt);
2720 shift(@ctx);
2721 for my $ctx (@ctx) {
2722 my ($clen, $cindent) = line_stats($ctx);
2723 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2724 $indent != $cindent) {
2725 $err .= "$sep$ctx\n";
2726 $sep = '';
2727 } else {
2728 $sep = "[...]\n";
2729 }
2730 }
2731 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002732 ERROR("SWITCH_CASE_INDENT_LEVEL",
2733 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002734 }
2735 }
2736
2737# if/while/etc brace do not go on next line, unless defining a do while loop,
2738# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002739 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002740 my $pre_ctx = "$1$2";
2741
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002742 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002743
2744 if ($line =~ /^\+\t{6,}/) {
2745 WARN("DEEP_INDENTATION",
2746 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2747 }
2748
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002749 my $ctx_cnt = $realcnt - $#ctx - 1;
2750 my $ctx = join("\n", @ctx);
2751
Andy Whitcroft548596d2008-07-23 21:29:01 -07002752 my $ctx_ln = $linenr;
2753 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002754
Andy Whitcroft548596d2008-07-23 21:29:01 -07002755 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2756 defined $lines[$ctx_ln - 1] &&
2757 $lines[$ctx_ln - 1] =~ /^-/)) {
2758 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2759 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002760 $ctx_ln++;
2761 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002762
Andy Whitcroft53210162008-07-23 21:29:03 -07002763 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2764 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002765
Joe Perchesd752fcc2014-08-06 16:11:05 -07002766 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002767 ERROR("OPEN_BRACE",
2768 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002769 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002770 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002771 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2772 $ctx =~ /\)\s*\;\s*$/ &&
2773 defined $lines[$ctx_ln - 1])
2774 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002775 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2776 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002777 WARN("TRAILING_SEMICOLON",
2778 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002779 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002780 }
2781 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002782 }
2783
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002784# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002785 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002786 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2787 ctx_statement_block($linenr, $realcnt, 0)
2788 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002789 my ($s, $c) = ($stat, $cond);
2790
2791 substr($s, 0, length($c), '');
2792
2793 # Make sure we remove the line prefixes as we have
2794 # none on the first line, and are going to readd them
2795 # where necessary.
2796 $s =~ s/\n./\n/gs;
2797
2798 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002799 my @newlines = ($c =~ /\n/gs);
2800 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002801
2802 # We want to check the first line inside the block
2803 # starting at the end of the conditional, so remove:
2804 # 1) any blank line termination
2805 # 2) any opening brace { on end of the line
2806 # 3) any do (...) {
2807 my $continuation = 0;
2808 my $check = 0;
2809 $s =~ s/^.*\bdo\b//;
2810 $s =~ s/^\s*{//;
2811 if ($s =~ s/^\s*\\//) {
2812 $continuation = 1;
2813 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002814 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002815 $check = 1;
2816 $cond_lines++;
2817 }
2818
2819 # Also ignore a loop construct at the end of a
2820 # preprocessor statement.
2821 if (($prevline =~ /^.\s*#\s*define\s/ ||
2822 $prevline =~ /\\\s*$/) && $continuation == 0) {
2823 $check = 0;
2824 }
2825
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002826 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002827 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002828 while ($cond_ptr != $cond_lines) {
2829 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002830
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002831 # If we see an #else/#elif then the code
2832 # is not linear.
2833 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2834 $check = 0;
2835 }
2836
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002837 # Ignore:
2838 # 1) blank lines, they should be at 0,
2839 # 2) preprocessor lines, and
2840 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002841 if ($continuation ||
2842 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002843 $s =~ /^\s*#\s*?/ ||
2844 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002845 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002846 if ($s =~ s/^.*?\n//) {
2847 $cond_lines++;
2848 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002849 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002850 }
2851
2852 my (undef, $sindent) = line_stats("+" . $s);
2853 my $stat_real = raw_line($linenr, $cond_lines);
2854
2855 # Check if either of these lines are modified, else
2856 # this is not this patch's fault.
2857 if (!defined($stat_real) ||
2858 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2859 $check = 0;
2860 }
2861 if (defined($stat_real) && $cond_lines > 1) {
2862 $stat_real = "[...]\n$stat_real";
2863 }
2864
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002865 #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 -07002866
2867 if ($check && (($sindent % 8) != 0 ||
2868 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002869 WARN("SUSPECT_CODE_INDENT",
2870 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002871 }
2872 }
2873
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002874 # Track the 'values' across context and added lines.
2875 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002876 my ($curr_values, $curr_vars) =
2877 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002878 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002879 if ($dbg_values) {
2880 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002881 print "$linenr > .$outline\n";
2882 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002883 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002884 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002885 $prev_values = substr($curr_values, -1);
2886
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002887#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002888 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002889
Andy Whitcroft653d4872007-06-23 17:16:34 -07002890# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002891 if ($dbg_type) {
2892 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002893 ERROR("TEST_TYPE",
2894 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002895 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002896 ERROR("TEST_NOT_TYPE",
2897 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002898 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002899 next;
2900 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002901# TEST: allow direct testing of the attribute matcher.
2902 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002903 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002904 ERROR("TEST_ATTR",
2905 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002906 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002907 ERROR("TEST_NOT_ATTR",
2908 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002909 }
2910 next;
2911 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002912
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002913# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002914 if ($line =~ /^.\s*{/ &&
2915 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002916 if (ERROR("OPEN_BRACE",
2917 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002918 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2919 fix_delete_line($fixlinenr - 1, $prevrawline);
2920 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002921 my $fixedline = $prevrawline;
2922 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002923 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002924 $fixedline = $line;
2925 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002926 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002927 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002928 }
2929
Andy Whitcroft653d4872007-06-23 17:16:34 -07002930#
2931# Checks which are anchored on the added line.
2932#
2933
2934# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002935 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002936 my $path = $1;
2937 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002938 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002939 "malformed #include filename\n" . $herecurr);
2940 }
2941 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2942 ERROR("UAPI_INCLUDE",
2943 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002944 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002945 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002946
2947# no C99 // comments
2948 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002949 if (ERROR("C99_COMMENTS",
2950 "do not use C99 // comments\n" . $herecurr) &&
2951 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002952 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07002953 if ($line =~ /\/\/(.*)$/) {
2954 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07002955 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07002956 }
2957 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002958 }
2959 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002960 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002961 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002962
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002963# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2964# the whole statement.
2965#print "APW <$lines[$realline_next - 1]>\n";
2966 if (defined $realline_next &&
2967 exists $lines[$realline_next - 1] &&
2968 !defined $suppress_export{$realline_next} &&
2969 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2970 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002971 # Handle definitions which produce identifiers with
2972 # a prefix:
2973 # XXX(foo);
2974 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002975 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002976 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002977 $name =~ /^${Ident}_$2/) {
2978#print "FOO C name<$name>\n";
2979 $suppress_export{$realline_next} = 1;
2980
2981 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002982 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002983 ^.DEFINE_$Ident\(\Q$name\E\)|
2984 ^.DECLARE_$Ident\(\Q$name\E\)|
2985 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002986 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2987 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002988 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002989#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2990 $suppress_export{$realline_next} = 2;
2991 } else {
2992 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002993 }
2994 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002995 if (!defined $suppress_export{$linenr} &&
2996 $prevline =~ /^.\s*$/ &&
2997 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2998 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2999#print "FOO B <$lines[$linenr - 1]>\n";
3000 $suppress_export{$linenr} = 2;
3001 }
3002 if (defined $suppress_export{$linenr} &&
3003 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003004 WARN("EXPORT_SYMBOL",
3005 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003006 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003007
Joe Eloff5150bda2010-08-09 17:21:00 -07003008# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003009 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3010 if (ERROR("GLOBAL_INITIALISERS",
3011 "do not initialise globals to 0 or NULL\n" .
3012 $herecurr) &&
3013 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003014 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003015 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003016 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003017# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003018 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3019 if (ERROR("INITIALISED_STATIC",
3020 "do not initialise statics to 0 or NULL\n" .
3021 $herecurr) &&
3022 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003023 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003024 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003025 }
3026
Joe Perches18130872014-08-06 16:11:22 -07003027# check for misordered declarations of char/short/int/long with signed/unsigned
3028 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3029 my $tmp = trim($1);
3030 WARN("MISORDERED_TYPE",
3031 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3032 }
3033
Joe Perchescb710ec2010-10-26 14:23:20 -07003034# check for static const char * arrays.
3035 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003036 WARN("STATIC_CONST_CHAR_ARRAY",
3037 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003038 $herecurr);
3039 }
3040
3041# check for static char foo[] = "bar" declarations.
3042 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003043 WARN("STATIC_CONST_CHAR_ARRAY",
3044 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003045 $herecurr);
3046 }
3047
Joe Perches9b0fa602014-04-03 14:49:18 -07003048# check for non-global char *foo[] = {"bar", ...} declarations.
3049 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3050 WARN("STATIC_CONST_CHAR_ARRAY",
3051 "char * array declaration might be better as static const\n" .
3052 $herecurr);
3053 }
3054
Joe Perchesb36190c2014-01-27 17:07:18 -08003055# check for function declarations without arguments like "int foo()"
3056 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3057 if (ERROR("FUNCTION_WITHOUT_ARGS",
3058 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003060 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003061 }
3062 }
3063
Joe Perches92e112f2013-12-13 11:36:22 -07003064# check for uses of DEFINE_PCI_DEVICE_TABLE
3065 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3066 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3067 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3068 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003069 $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 -07003070 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003071 }
3072
Andy Whitcroft653d4872007-06-23 17:16:34 -07003073# check for new typedefs, only function parameters and sparse annotations
3074# make sense.
3075 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003076 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003077 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003078 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003079 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003080 WARN("NEW_TYPEDEFS",
3081 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003082 }
3083
3084# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003085 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003086 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3087 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003088 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003089
Andy Whitcroft65863862009-01-06 14:41:21 -08003090 # Should start with a space.
3091 $to =~ s/^(\S)/ $1/;
3092 # Should not end with a space.
3093 $to =~ s/\s+$//;
3094 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003095 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003096 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003097
Joe Perches3705ce52013-07-03 15:05:31 -07003098## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003099 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003100 if (ERROR("POINTER_LOCATION",
3101 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3102 $fix) {
3103 my $sub_from = $ident;
3104 my $sub_to = $ident;
3105 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003106 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003107 s@\Q$sub_from\E@$sub_to@;
3108 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003109 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003110 }
3111 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3112 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003113 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003114
Andy Whitcroft65863862009-01-06 14:41:21 -08003115 # Should start with a space.
3116 $to =~ s/^(\S)/ $1/;
3117 # Should not end with a space.
3118 $to =~ s/\s+$//;
3119 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003120 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003121 }
3122 # Modifiers should have spaces.
3123 $to =~ s/(\b$Modifier$)/$1 /;
3124
Joe Perches3705ce52013-07-03 15:05:31 -07003125## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003126 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003127 if (ERROR("POINTER_LOCATION",
3128 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3129 $fix) {
3130
3131 my $sub_from = $match;
3132 my $sub_to = $match;
3133 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003134 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003135 s@\Q$sub_from\E@$sub_to@;
3136 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003137 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003138 }
3139
3140# # no BUG() or BUG_ON()
3141# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3142# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3143# print "$herecurr";
3144# $clean = 0;
3145# }
3146
Andy Whitcroft8905a672007-11-28 16:21:06 -08003147 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003148 WARN("LINUX_VERSION_CODE",
3149 "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 -08003150 }
3151
Joe Perches17441222011-06-15 15:08:17 -07003152# check for uses of printk_ratelimit
3153 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003154 WARN("PRINTK_RATELIMITED",
3155"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003156 }
3157
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003158# printk should use KERN_* levels. Note that follow on printk's on the
3159# same line do not need a level, so we use the current block context
3160# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003161# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003162# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003163 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003164 my $ok = 0;
3165 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3166 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003167 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003168 # with "\n" ignore it, else it is to blame
3169 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3170 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3171 $ok = 1;
3172 }
3173 last;
3174 }
3175 }
3176 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003177 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3178 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003179 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003180 }
3181
Joe Perches243f3802012-05-31 16:26:09 -07003182 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3183 my $orig = $1;
3184 my $level = lc($orig);
3185 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003186 my $level2 = $level;
3187 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003188 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003189 "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 -07003190 }
3191
3192 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003193 if (WARN("PREFER_PR_LEVEL",
3194 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3195 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003196 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003197 s/\bpr_warning\b/pr_warn/;
3198 }
Joe Perches243f3802012-05-31 16:26:09 -07003199 }
3200
Joe Perchesdc139312013-02-21 16:44:13 -08003201 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3202 my $orig = $1;
3203 my $level = lc($orig);
3204 $level = "warn" if ($level eq "warning");
3205 $level = "dbg" if ($level eq "debug");
3206 WARN("PREFER_DEV_LEVEL",
3207 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3208 }
3209
Andy Whitcroft653d4872007-06-23 17:16:34 -07003210# function brace can't be on same line, except for #defines of do while,
3211# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003212 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003213 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003214 if (ERROR("OPEN_BRACE",
3215 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3216 $fix) {
3217 fix_delete_line($fixlinenr, $rawline);
3218 my $fixed_line = $rawline;
3219 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3220 my $line1 = $1;
3221 my $line2 = $2;
3222 fix_insert_line($fixlinenr, ltrim($line1));
3223 fix_insert_line($fixlinenr, "\+{");
3224 if ($line2 !~ /^\s*$/) {
3225 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3226 }
3227 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003228 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003229
Andy Whitcroft8905a672007-11-28 16:21:06 -08003230# open braces for enum, union and struct go on the same line.
3231 if ($line =~ /^.\s*{/ &&
3232 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003233 if (ERROR("OPEN_BRACE",
3234 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3235 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3236 fix_delete_line($fixlinenr - 1, $prevrawline);
3237 fix_delete_line($fixlinenr, $rawline);
3238 my $fixedline = rtrim($prevrawline) . " {";
3239 fix_insert_line($fixlinenr, $fixedline);
3240 $fixedline = $rawline;
3241 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3242 if ($fixedline !~ /^\+\s*$/) {
3243 fix_insert_line($fixlinenr, $fixedline);
3244 }
3245 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003246 }
3247
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003248# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003249 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3250 if (WARN("SPACING",
3251 "missing space after $1 definition\n" . $herecurr) &&
3252 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003253 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003254 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3255 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003256 }
3257
Joe Perches31070b52014-01-23 15:54:49 -08003258# Function pointer declarations
3259# check spacing between type, funcptr, and args
3260# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003261 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003262 my $declare = $1;
3263 my $pre_pointer_space = $2;
3264 my $post_pointer_space = $3;
3265 my $funcname = $4;
3266 my $post_funcname_space = $5;
3267 my $pre_args_space = $6;
3268
Joe Perches91f72e92014-04-03 14:49:12 -07003269# the $Declare variable will capture all spaces after the type
3270# so check it for a missing trailing missing space but pointer return types
3271# don't need a space so don't warn for those.
3272 my $post_declare_space = "";
3273 if ($declare =~ /(\s+)$/) {
3274 $post_declare_space = $1;
3275 $declare = rtrim($declare);
3276 }
3277 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003278 WARN("SPACING",
3279 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003280 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003281 }
3282
3283# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003284# This test is not currently implemented because these declarations are
3285# equivalent to
3286# int foo(int bar, ...)
3287# and this is form shouldn't/doesn't generate a checkpatch warning.
3288#
3289# elsif ($declare =~ /\s{2,}$/) {
3290# WARN("SPACING",
3291# "Multiple spaces after return type\n" . $herecurr);
3292# }
Joe Perches31070b52014-01-23 15:54:49 -08003293
3294# unnecessary space "type ( *funcptr)(args...)"
3295 if (defined $pre_pointer_space &&
3296 $pre_pointer_space =~ /^\s/) {
3297 WARN("SPACING",
3298 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3299 }
3300
3301# unnecessary space "type (* funcptr)(args...)"
3302 if (defined $post_pointer_space &&
3303 $post_pointer_space =~ /^\s/) {
3304 WARN("SPACING",
3305 "Unnecessary space before function pointer name\n" . $herecurr);
3306 }
3307
3308# unnecessary space "type (*funcptr )(args...)"
3309 if (defined $post_funcname_space &&
3310 $post_funcname_space =~ /^\s/) {
3311 WARN("SPACING",
3312 "Unnecessary space after function pointer name\n" . $herecurr);
3313 }
3314
3315# unnecessary space "type (*funcptr) (args...)"
3316 if (defined $pre_args_space &&
3317 $pre_args_space =~ /^\s/) {
3318 WARN("SPACING",
3319 "Unnecessary space before function pointer arguments\n" . $herecurr);
3320 }
3321
3322 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003323 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003324 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003325 }
3326 }
3327
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003328# check for spacing round square brackets; allowed:
3329# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003330# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3331# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003332 while ($line =~ /(.*?\s)\[/g) {
3333 my ($where, $prefix) = ($-[1], $1);
3334 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003335 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003336 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003337 if (ERROR("BRACKET_SPACE",
3338 "space prohibited before open square bracket '['\n" . $herecurr) &&
3339 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003340 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003341 s/^(\+.*?)\s+\[/$1\[/;
3342 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003343 }
3344 }
3345
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003346# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003347 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003348 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003349 my $ctx_before = substr($line, 0, $-[1]);
3350 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003351
3352 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003353 if ($name =~ /^(?:
3354 if|for|while|switch|return|case|
3355 volatile|__volatile__|
3356 __attribute__|format|__extension__|
3357 asm|__asm__)$/x)
3358 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003359 # cpp #define statements have non-optional spaces, ie
3360 # if there is a space between the name and the open
3361 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003362 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003363
3364 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003365 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003366
3367 # If this whole things ends with a type its most
3368 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003369 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003370
3371 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003372 if (WARN("SPACING",
3373 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3374 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003375 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003376 s/\b$name\s+\(/$name\(/;
3377 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003378 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003379 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003380
Andy Whitcroft653d4872007-06-23 17:16:34 -07003381# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003382 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003383 my $fixed_line = "";
3384 my $line_fixed = 0;
3385
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003386 my $ops = qr{
3387 <<=|>>=|<=|>=|==|!=|
3388 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3389 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003390 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003391 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003392 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003393 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003394
3395## print("element count: <" . $#elements . ">\n");
3396## foreach my $el (@elements) {
3397## print("el: <$el>\n");
3398## }
3399
3400 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003401 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003402
Joe Perches3705ce52013-07-03 15:05:31 -07003403 foreach my $el (@elements) {
3404 push(@fix_elements, substr($rawline, $off, length($el)));
3405 $off += length($el);
3406 }
3407
3408 $off = 0;
3409
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003410 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003411 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003412
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003413 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003414
3415 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3416
3417## print("n: <$n> good: <$good>\n");
3418
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003419 $off += length($elements[$n]);
3420
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003421 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003422 my $ca = substr($opline, 0, $off);
3423 my $cc = '';
3424 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3425 $cc = substr($opline, $off + length($elements[$n + 1]));
3426 }
3427 my $cb = "$ca$;$cc";
3428
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003429 my $a = '';
3430 $a = 'V' if ($elements[$n] ne '');
3431 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003432 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003433 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3434 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003435 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003436
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003437 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003438
3439 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003440 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003441 $c = 'V' if ($elements[$n + 2] ne '');
3442 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003443 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003444 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3445 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003446 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003447 } else {
3448 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003449 }
3450
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003451 my $ctx = "${a}x${c}";
3452
3453 my $at = "(ctx:$ctx)";
3454
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003455 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003456 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003457
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003458 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003459 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003460
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003461 # Get the full operator variant.
3462 my $opv = $op . substr($curr_vars, $off, 1);
3463
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003464 # Ignore operators passed as parameters.
3465 if ($op_type ne 'V' &&
3466 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3467
Andy Whitcroftcf655042008-03-04 14:28:20 -08003468# # Ignore comments
3469# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003470
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003471 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003472 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003473 if ($ctx !~ /.x[WEBC]/ &&
3474 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003475 if (ERROR("SPACING",
3476 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003477 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003478 $line_fixed = 1;
3479 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003480 }
3481
3482 # // is a comment
3483 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003484
Joe Perchesb00e4812014-04-03 14:49:33 -07003485 # : when part of a bitfield
3486 } elsif ($opv eq ':B') {
3487 # skip the bitfield test for now
3488
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003489 # No spaces for:
3490 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003491 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003492 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003493 if (ERROR("SPACING",
3494 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003495 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003496 if (defined $fix_elements[$n + 2]) {
3497 $fix_elements[$n + 2] =~ s/^\s+//;
3498 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003499 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003501 }
3502
3503 # , must have a space on the right.
3504 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003505 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003506 if (ERROR("SPACING",
3507 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003508 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003509 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003510 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07003511 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003512 }
3513
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003514 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003515 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003516 #warn "'*' is part of type\n";
3517
3518 # unary operators should have a space before and
3519 # none after. May be left adjacent to another
3520 # unary operator, or a cast
3521 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003522 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003523 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003524 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003525 if (ERROR("SPACING",
3526 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003527 if ($n != $last_after + 2) {
3528 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3529 $line_fixed = 1;
3530 }
Joe Perches3705ce52013-07-03 15:05:31 -07003531 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003532 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003533 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003534 # A unary '*' may be const
3535
3536 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003537 if (ERROR("SPACING",
3538 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003539 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003540 if (defined $fix_elements[$n + 2]) {
3541 $fix_elements[$n + 2] =~ s/^\s+//;
3542 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003543 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003544 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003545 }
3546
3547 # unary ++ and unary -- are allowed no space on one side.
3548 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003549 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003550 if (ERROR("SPACING",
3551 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003552 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003553 $line_fixed = 1;
3554 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003555 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003556 if ($ctx =~ /Wx[BE]/ ||
3557 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003558 if (ERROR("SPACING",
3559 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003560 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003561 $line_fixed = 1;
3562 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003563 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003564 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003565 if (ERROR("SPACING",
3566 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003567 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003568 if (defined $fix_elements[$n + 2]) {
3569 $fix_elements[$n + 2] =~ s/^\s+//;
3570 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003571 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003572 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003573 }
3574
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003575 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003576 } elsif ($op eq '<<' or $op eq '>>' or
3577 $op eq '&' or $op eq '^' or $op eq '|' or
3578 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003579 $op eq '*' or $op eq '/' or
3580 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003581 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003582 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003583 if (ERROR("SPACING",
3584 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003585 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3586 if (defined $fix_elements[$n + 2]) {
3587 $fix_elements[$n + 2] =~ s/^\s+//;
3588 }
Joe Perches3705ce52013-07-03 15:05:31 -07003589 $line_fixed = 1;
3590 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003591 }
3592
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003593 # A colon needs no spaces before when it is
3594 # terminating a case value or a label.
3595 } elsif ($opv eq ':C' || $opv eq ':L') {
3596 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003597 if (ERROR("SPACING",
3598 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003599 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003600 $line_fixed = 1;
3601 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003602 }
3603
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003604 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003605 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003606 my $ok = 0;
3607
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003608 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003609 if (($op eq '<' &&
3610 $cc =~ /^\S+\@\S+>/) ||
3611 ($op eq '>' &&
3612 $ca =~ /<\S+\@\S+$/))
3613 {
3614 $ok = 1;
3615 }
3616
Joe Perches84731622013-11-12 15:10:05 -08003617 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003618 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003619 my $msg_type = \&ERROR;
3620 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3621
3622 if (&{$msg_type}("SPACING",
3623 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003624 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3625 if (defined $fix_elements[$n + 2]) {
3626 $fix_elements[$n + 2] =~ s/^\s+//;
3627 }
Joe Perches3705ce52013-07-03 15:05:31 -07003628 $line_fixed = 1;
3629 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003630 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003631 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003632 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003633
3634## print("n: <$n> GOOD: <$good>\n");
3635
3636 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003637 }
Joe Perches3705ce52013-07-03 15:05:31 -07003638
3639 if (($#elements % 2) == 0) {
3640 $fixed_line = $fixed_line . $fix_elements[$#elements];
3641 }
3642
Joe Perches194f66f2014-08-06 16:11:03 -07003643 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3644 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003645 }
3646
3647
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003648 }
3649
Joe Perches786b6322013-07-03 15:05:32 -07003650# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003651 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003652 if (WARN("SPACING",
3653 "space prohibited before semicolon\n" . $herecurr) &&
3654 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003655 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003656 s/^(\+.*\S)\s+;/$1;/;
3657 }
3658 }
3659
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003660# check for multiple assignments
3661 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003662 CHK("MULTIPLE_ASSIGNMENTS",
3663 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003664 }
3665
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003666## # check for multiple declarations, allowing for a function declaration
3667## # continuation.
3668## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3669## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3670##
3671## # Remove any bracketed sections to ensure we do not
3672## # falsly report the parameters of functions.
3673## my $ln = $line;
3674## while ($ln =~ s/\([^\(\)]*\)//g) {
3675## }
3676## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003677## WARN("MULTIPLE_DECLARATION",
3678## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003679## }
3680## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003681
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003682#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003683 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3684 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003685 if (ERROR("SPACING",
3686 "space required before the open brace '{'\n" . $herecurr) &&
3687 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003688 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003689 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003690 }
3691
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003692## # check for blank lines before declarations
3693## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3694## $prevrawline =~ /^.\s*$/) {
3695## WARN("SPACING",
3696## "No blank lines before declarations\n" . $hereprev);
3697## }
3698##
3699
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003700# closing brace should have a space following it when it has anything
3701# on the line
3702 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003703 if (ERROR("SPACING",
3704 "space required after that close brace '}'\n" . $herecurr) &&
3705 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003706 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003707 s/}((?!(?:,|;|\)))\S)/} $1/;
3708 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003709 }
3710
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003711# check spacing on square brackets
3712 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003713 if (ERROR("SPACING",
3714 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3715 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003716 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003717 s/\[\s+/\[/;
3718 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003719 }
3720 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003721 if (ERROR("SPACING",
3722 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003724 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003725 s/\s+\]/\]/;
3726 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003727 }
3728
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003729# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003730 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3731 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003732 if (ERROR("SPACING",
3733 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3734 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003735 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003736 s/\(\s+/\(/;
3737 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003738 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003739 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003740 $line !~ /for\s*\(.*;\s+\)/ &&
3741 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003742 if (ERROR("SPACING",
3743 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3744 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003745 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3746 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003747 s/\s+\)/\)/;
3748 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003749 }
3750
Joe Perchese2826fd2014-08-06 16:10:48 -07003751# check unnecessary parentheses around addressof/dereference single $Lvals
3752# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3753
3754 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3755 CHK("UNNECESSARY_PARENTHESES",
3756 "Unnecessary parentheses around $1\n" . $herecurr);
3757 }
3758
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003759#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003760 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003761 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003762 if (WARN("INDENTED_LABEL",
3763 "labels should not be indented\n" . $herecurr) &&
3764 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003765 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003766 s/^(.)\s+/$1/;
3767 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003768 }
3769
Joe Perches5b9553a2014-04-03 14:49:21 -07003770# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003771 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003772 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003773 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003774 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3775 my $value = $1;
3776 $value = deparenthesize($value);
3777 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3778 ERROR("RETURN_PARENTHESES",
3779 "return is not a function, parentheses are not required\n" . $herecurr);
3780 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003781 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003782 ERROR("SPACING",
3783 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003784 }
3785 }
Joe Perches507e5142013-11-12 15:10:13 -08003786
Joe Perchesb43ae212014-06-23 13:22:07 -07003787# unnecessary return in a void function
3788# at end-of-function, with the previous line a single leading tab, then return;
3789# and the line before that not a goto label target like "out:"
3790 if ($sline =~ /^[ \+]}\s*$/ &&
3791 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3792 $linenr >= 3 &&
3793 $lines[$linenr - 3] =~ /^[ +]/ &&
3794 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003795 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003796 "void function return statements are not generally useful\n" . $hereprev);
3797 }
Joe Perches9819cf22014-06-04 16:12:09 -07003798
Joe Perches189248d2014-01-23 15:54:47 -08003799# if statements using unnecessary parentheses - ie: if ((foo == bar))
3800 if ($^V && $^V ge 5.10.0 &&
3801 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3802 my $openparens = $1;
3803 my $count = $openparens =~ tr@\(@\(@;
3804 my $msg = "";
3805 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3806 my $comp = $4; #Not $1 because of $LvalOrFunc
3807 $msg = " - maybe == should be = ?" if ($comp eq "==");
3808 WARN("UNNECESSARY_PARENTHESES",
3809 "Unnecessary parentheses$msg\n" . $herecurr);
3810 }
3811 }
3812
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003813# Return of what appears to be an errno should normally be -'ve
3814 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3815 my $name = $1;
3816 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003817 WARN("USE_NEGATIVE_ERRNO",
3818 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003819 }
3820 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003821
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003822# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003823 if ($line =~ /\b(if|while|for|switch)\(/) {
3824 if (ERROR("SPACING",
3825 "space required before the open parenthesis '('\n" . $herecurr) &&
3826 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003827 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003828 s/\b(if|while|for|switch)\(/$1 \(/;
3829 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003830 }
3831
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003832# Check for illegal assignment in if conditional -- and check for trailing
3833# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003834 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003835 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3836 ctx_statement_block($linenr, $realcnt, 0)
3837 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003838 my ($stat_next) = ctx_statement_block($line_nr_next,
3839 $remain_next, $off_next);
3840 $stat_next =~ s/\n./\n /g;
3841 ##print "stat<$stat> stat_next<$stat_next>\n";
3842
3843 if ($stat_next =~ /^\s*while\b/) {
3844 # If the statement carries leading newlines,
3845 # then count those as offsets.
3846 my ($whitespace) =
3847 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3848 my $offset =
3849 statement_rawlines($whitespace) - 1;
3850
3851 $suppress_whiletrailers{$line_nr_next +
3852 $offset} = 1;
3853 }
3854 }
3855 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003856 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003857 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003858 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003859
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003860 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003861 ERROR("ASSIGN_IN_IF",
3862 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003863 }
3864
3865 # Find out what is on the end of the line after the
3866 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003867 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003868 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003869 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003870 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3871 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003872 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003873 # Find out how long the conditional actually is.
3874 my @newlines = ($c =~ /\n/gs);
3875 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003876 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003877
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003878 $stat_real = raw_line($linenr, $cond_lines)
3879 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003880 if (defined($stat_real) && $cond_lines > 1) {
3881 $stat_real = "[...]\n$stat_real";
3882 }
3883
Joe Perches000d1cc12011-07-25 17:13:25 -07003884 ERROR("TRAILING_STATEMENTS",
3885 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003886 }
3887 }
3888
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003889# Check for bitwise tests written as boolean
3890 if ($line =~ /
3891 (?:
3892 (?:\[|\(|\&\&|\|\|)
3893 \s*0[xX][0-9]+\s*
3894 (?:\&\&|\|\|)
3895 |
3896 (?:\&\&|\|\|)
3897 \s*0[xX][0-9]+\s*
3898 (?:\&\&|\|\||\)|\])
3899 )/x)
3900 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003901 WARN("HEXADECIMAL_BOOLEAN_TEST",
3902 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003903 }
3904
Andy Whitcroft8905a672007-11-28 16:21:06 -08003905# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003906 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3907 my $s = $1;
3908 $s =~ s/$;//g; # Remove any comments
3909 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003910 ERROR("TRAILING_STATEMENTS",
3911 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003912 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003913 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003914# if should not continue a brace
3915 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003916 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07003917 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003918 $herecurr);
3919 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003920# case and default should not have general statements after them
3921 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3922 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003923 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003924 \s*return\s+
3925 )/xg)
3926 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003927 ERROR("TRAILING_STATEMENTS",
3928 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003929 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003930
3931 # Check for }<nl>else {, these must be at the same
3932 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07003933 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3934 $previndent == $indent) {
3935 if (ERROR("ELSE_AFTER_BRACE",
3936 "else should follow close brace '}'\n" . $hereprev) &&
3937 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3938 fix_delete_line($fixlinenr - 1, $prevrawline);
3939 fix_delete_line($fixlinenr, $rawline);
3940 my $fixedline = $prevrawline;
3941 $fixedline =~ s/}\s*$//;
3942 if ($fixedline !~ /^\+\s*$/) {
3943 fix_insert_line($fixlinenr, $fixedline);
3944 }
3945 $fixedline = $rawline;
3946 $fixedline =~ s/^(.\s*)else/$1} else/;
3947 fix_insert_line($fixlinenr, $fixedline);
3948 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003949 }
3950
Joe Perches8b8856f2014-08-06 16:11:14 -07003951 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3952 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003953 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3954
3955 # Find out what is on the end of the line after the
3956 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003957 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003958 $s =~ s/\n.*//g;
3959
3960 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07003961 if (ERROR("WHILE_AFTER_BRACE",
3962 "while should follow close brace '}'\n" . $hereprev) &&
3963 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3964 fix_delete_line($fixlinenr - 1, $prevrawline);
3965 fix_delete_line($fixlinenr, $rawline);
3966 my $fixedline = $prevrawline;
3967 my $trailing = $rawline;
3968 $trailing =~ s/^\+//;
3969 $trailing = trim($trailing);
3970 $fixedline =~ s/}\s*$/} $trailing/;
3971 fix_insert_line($fixlinenr, $fixedline);
3972 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003973 }
3974 }
3975
Joe Perches95e2c602013-07-03 15:05:20 -07003976#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003977 while ($line =~ m{($Constant|$Lval)}g) {
3978 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003979
3980#gcc binary extension
3981 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003982 if (WARN("GCC_BINARY_CONSTANT",
3983 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3984 $fix) {
3985 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07003986 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003987 s/\b$var\b/$hexval/;
3988 }
Joe Perches95e2c602013-07-03 15:05:20 -07003989 }
3990
3991#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003992 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003993 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003994#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003995 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003996#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003997 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003998 while ($var =~ m{($Ident)}g) {
3999 my $word = $1;
4000 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004001 if ($check) {
4002 seed_camelcase_includes();
4003 if (!$file && !$camelcase_file_seeded) {
4004 seed_camelcase_file($realfile);
4005 $camelcase_file_seeded = 1;
4006 }
4007 }
Joe Perches7e781f62013-09-11 14:23:55 -07004008 if (!defined $camelcase{$word}) {
4009 $camelcase{$word} = 1;
4010 CHK("CAMELCASE",
4011 "Avoid CamelCase: <$word>\n" . $herecurr);
4012 }
Joe Perches34456862013-07-03 15:05:34 -07004013 }
Joe Perches323c1262012-12-17 16:02:07 -08004014 }
4015 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004016
4017#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004018 if ($line =~ /\#\s*define.*\\\s+$/) {
4019 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4020 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4021 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004022 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004023 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004024 }
4025
Andy Whitcroft653d4872007-06-23 17:16:34 -07004026#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004027 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004028 my $file = "$1.h";
4029 my $checkfile = "include/linux/$file";
4030 if (-f "$root/$checkfile" &&
4031 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004032 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004033 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004034 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004035 CHK("ARCH_INCLUDE_LINUX",
4036 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004037 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004038 WARN("INCLUDE_LINUX",
4039 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004040 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004041 }
4042 }
4043
Andy Whitcroft653d4872007-06-23 17:16:34 -07004044# multi-statement macros should be enclosed in a do while loop, grab the
4045# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004046# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004047 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4048 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004049 my $ln = $linenr;
4050 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004051 my ($off, $dstat, $dcond, $rest);
4052 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004053 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004054 ctx_statement_block($linenr, $realcnt, 0);
4055 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004056 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004057 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004058
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004059 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004060 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004061 $dstat =~ s/\\\n.//g;
4062 $dstat =~ s/^\s*//s;
4063 $dstat =~ s/\s*$//s;
4064
4065 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004066 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4067 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004068 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004069 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004070 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004071
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004072 # Flatten any obvious string concatentation.
4073 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4074 $dstat =~ s/$Ident\s*("X*")/$1/)
4075 {
4076 }
4077
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004078 my $exceptions = qr{
4079 $Declare|
4080 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004081 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004082 DECLARE_PER_CPU|
4083 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004084 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004085 union|
4086 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004087 \.$Ident\s*=\s*|
4088 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004089 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004090 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004091 if ($dstat ne '' &&
4092 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4093 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004094 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004095 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004096 $dstat !~ /$exceptions/ &&
4097 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004098 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004099 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004100 $dstat !~ /^for\s*$Constant$/ && # for (...)
4101 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4102 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004103 $dstat !~ /^\({/ && # ({...
4104 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004105 {
4106 $ctx =~ s/\n*$//;
4107 my $herectx = $here . "\n";
4108 my $cnt = statement_rawlines($ctx);
4109
4110 for (my $n = 0; $n < $cnt; $n++) {
4111 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004112 }
4113
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004114 if ($dstat =~ /;/) {
4115 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4116 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4117 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004118 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004119 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004120 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004121 }
Joe Perches5023d342012-12-17 16:01:47 -08004122
Joe Perches481eb482012-12-17 16:01:56 -08004123# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004124
4125 } else {
4126 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004127 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4128 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004129 $line =~ /^\+.*\\$/) {
4130 WARN("LINE_CONTINUATIONS",
4131 "Avoid unnecessary line continuations\n" . $herecurr);
4132 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004133 }
4134
Joe Perchesb13edf72012-07-30 14:41:24 -07004135# do {} while (0) macro tests:
4136# single-statement macros do not need to be enclosed in do while (0) loop,
4137# macro should not end with a semicolon
4138 if ($^V && $^V ge 5.10.0 &&
4139 $realfile !~ m@/vmlinux.lds.h$@ &&
4140 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4141 my $ln = $linenr;
4142 my $cnt = $realcnt;
4143 my ($off, $dstat, $dcond, $rest);
4144 my $ctx = '';
4145 ($dstat, $dcond, $ln, $cnt, $off) =
4146 ctx_statement_block($linenr, $realcnt, 0);
4147 $ctx = $dstat;
4148
4149 $dstat =~ s/\\\n.//g;
4150
4151 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4152 my $stmts = $2;
4153 my $semis = $3;
4154
4155 $ctx =~ s/\n*$//;
4156 my $cnt = statement_rawlines($ctx);
4157 my $herectx = $here . "\n";
4158
4159 for (my $n = 0; $n < $cnt; $n++) {
4160 $herectx .= raw_line($linenr, $n) . "\n";
4161 }
4162
Joe Perchesac8e97f2012-08-21 16:15:53 -07004163 if (($stmts =~ tr/;/;/) == 1 &&
4164 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004165 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4166 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4167 }
4168 if (defined $semis && $semis ne "") {
4169 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4170 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4171 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004172 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4173 $ctx =~ s/\n*$//;
4174 my $cnt = statement_rawlines($ctx);
4175 my $herectx = $here . "\n";
4176
4177 for (my $n = 0; $n < $cnt; $n++) {
4178 $herectx .= raw_line($linenr, $n) . "\n";
4179 }
4180
4181 WARN("TRAILING_SEMICOLON",
4182 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004183 }
4184 }
4185
Mike Frysinger080ba922009-01-06 14:41:25 -08004186# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4187# all assignments may have only one of the following with an assignment:
4188# .
4189# ALIGN(...)
4190# VMLINUX_SYMBOL(...)
4191 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004192 WARN("MISSING_VMLINUX_SYMBOL",
4193 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004194 }
4195
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004196# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004197 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4198 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004199 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004200 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004201 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4202 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004203 my @allowed = ();
4204 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004205 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004206 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004207 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004208 for my $chunk (@chunks) {
4209 my ($cond, $block) = @{$chunk};
4210
Andy Whitcroft773647a2008-03-28 14:15:58 -07004211 # If the condition carries leading newlines, then count those as offsets.
4212 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4213 my $offset = statement_rawlines($whitespace) - 1;
4214
Joe Perchesaad4f612012-03-23 15:02:19 -07004215 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004216 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4217
4218 # We have looked at and allowed this specific line.
4219 $suppress_ifbraces{$ln + $offset} = 1;
4220
4221 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004222 $ln += statement_rawlines($block) - 1;
4223
Andy Whitcroft773647a2008-03-28 14:15:58 -07004224 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004225
4226 $seen++ if ($block =~ /^\s*{/);
4227
Joe Perchesaad4f612012-03-23 15:02:19 -07004228 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004229 if (statement_lines($cond) > 1) {
4230 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004231 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004232 }
4233 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004234 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004235 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004236 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004237 if (statement_block_size($block) > 1) {
4238 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004239 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004240 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004241 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004242 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004243 if ($seen) {
4244 my $sum_allowed = 0;
4245 foreach (@allowed) {
4246 $sum_allowed += $_;
4247 }
4248 if ($sum_allowed == 0) {
4249 WARN("BRACES",
4250 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4251 } elsif ($sum_allowed != $allow &&
4252 $seen != $allow) {
4253 CHK("BRACES",
4254 "braces {} should be used on all arms of this statement\n" . $herectx);
4255 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004256 }
4257 }
4258 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004259 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004260 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004261 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004262
Andy Whitcroftcf655042008-03-04 14:28:20 -08004263 # Check the pre-context.
4264 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4265 #print "APW: ALLOWED: pre<$1>\n";
4266 $allowed = 1;
4267 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004268
4269 my ($level, $endln, @chunks) =
4270 ctx_statement_full($linenr, $realcnt, $-[0]);
4271
Andy Whitcroftcf655042008-03-04 14:28:20 -08004272 # Check the condition.
4273 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004274 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004275 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004276 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004277 }
4278 if (statement_lines($cond) > 1) {
4279 #print "APW: ALLOWED: cond<$cond>\n";
4280 $allowed = 1;
4281 }
4282 if ($block =~/\b(?:if|for|while)\b/) {
4283 #print "APW: ALLOWED: block<$block>\n";
4284 $allowed = 1;
4285 }
4286 if (statement_block_size($block) > 1) {
4287 #print "APW: ALLOWED: lines block<$block>\n";
4288 $allowed = 1;
4289 }
4290 # Check the post-context.
4291 if (defined $chunks[1]) {
4292 my ($cond, $block) = @{$chunks[1]};
4293 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004294 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004295 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004296 if ($block =~ /^\s*\{/) {
4297 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4298 $allowed = 1;
4299 }
4300 }
4301 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004302 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004303 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004304
Andy Whitcroftf0556632008-10-15 22:02:23 -07004305 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004306 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004307 }
4308
Joe Perches000d1cc12011-07-25 17:13:25 -07004309 WARN("BRACES",
4310 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004311 }
4312 }
4313
Joe Perches0979ae62012-12-17 16:01:59 -08004314# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004315 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004316 CHK("BRACES",
4317 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4318 }
Joe Perches77b9a532013-07-03 15:05:29 -07004319 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004320 CHK("BRACES",
4321 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4322 }
4323
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004324# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004325 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4326 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004327 WARN("VOLATILE",
4328 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004329 }
4330
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004331# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004332 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004333 CHK("REDUNDANT_CODE",
4334 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004335 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004336 }
4337
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004338# check for needless "if (<foo>) fn(<foo>)" uses
4339 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4340 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4341 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4342 WARN('NEEDLESS_IF',
4343 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004344 }
4345 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004346
Joe Perchesebfdc402014-08-06 16:10:27 -07004347# check for unnecessary "Out of Memory" messages
4348 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4349 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4350 (defined $1 || defined $3) &&
4351 $linenr > 3) {
4352 my $testval = $2;
4353 my $testline = $lines[$linenr - 3];
4354
4355 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4356# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4357
4358 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4359 WARN("OOM_MESSAGE",
4360 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4361 }
4362 }
4363
Joe Perches8716de32013-09-11 14:24:05 -07004364# check for bad placement of section $InitAttribute (e.g.: __initdata)
4365 if ($line =~ /(\b$InitAttribute\b)/) {
4366 my $attr = $1;
4367 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4368 my $ptr = $1;
4369 my $var = $2;
4370 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4371 ERROR("MISPLACED_INIT",
4372 "$attr should be placed after $var\n" . $herecurr)) ||
4373 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4374 WARN("MISPLACED_INIT",
4375 "$attr should be placed after $var\n" . $herecurr))) &&
4376 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004377 $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 -07004378 }
4379 }
4380 }
4381
Joe Perchese970b8842013-11-12 15:10:10 -08004382# check for $InitAttributeData (ie: __initdata) with const
4383 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4384 my $attr = $1;
4385 $attr =~ /($InitAttributePrefix)(.*)/;
4386 my $attr_prefix = $1;
4387 my $attr_type = $2;
4388 if (ERROR("INIT_ATTRIBUTE",
4389 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4390 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004391 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004392 s/$InitAttributeData/${attr_prefix}initconst/;
4393 }
4394 }
4395
4396# check for $InitAttributeConst (ie: __initconst) without const
4397 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4398 my $attr = $1;
4399 if (ERROR("INIT_ATTRIBUTE",
4400 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4401 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004402 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004403 /(^\+\s*(?:static\s+))/;
4404 $lead = rtrim($1);
4405 $lead = "$lead " if ($lead !~ /^\+$/);
4406 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004407 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004408 }
4409 }
4410
Joe Perchesfbdb8132014-04-03 14:49:14 -07004411# don't use __constant_<foo> functions outside of include/uapi/
4412 if ($realfile !~ m@^include/uapi/@ &&
4413 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4414 my $constant_func = $1;
4415 my $func = $constant_func;
4416 $func =~ s/^__constant_//;
4417 if (WARN("CONSTANT_CONVERSION",
4418 "$constant_func should be $func\n" . $herecurr) &&
4419 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004420 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004421 }
4422 }
4423
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004424# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004425 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004426 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004427 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004428 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004429 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004430 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4431 }
4432 if ($delay > 2000) {
4433 WARN("LONG_UDELAY",
4434 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004435 }
4436 }
4437
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004438# warn about unexpectedly long msleep's
4439 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4440 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004441 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004442 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004443 }
4444 }
4445
Joe Perches36ec1932013-07-03 15:05:25 -07004446# check for comparisons of jiffies
4447 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4448 WARN("JIFFIES_COMPARISON",
4449 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4450 }
4451
Joe Perches9d7a34a2013-07-03 15:05:26 -07004452# check for comparisons of get_jiffies_64()
4453 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4454 WARN("JIFFIES_COMPARISON",
4455 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4456 }
4457
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004458# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004459# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004460# print "#ifdef in C files should be avoided\n";
4461# print "$herecurr";
4462# $clean = 0;
4463# }
4464
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004465# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004466 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004467 if (ERROR("SPACING",
4468 "exactly one space required after that #$1\n" . $herecurr) &&
4469 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004470 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004471 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4472 }
4473
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004474 }
4475
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004476# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004477 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4478 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004479 my $which = $1;
4480 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004481 CHK("UNCOMMENTED_DEFINITION",
4482 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004483 }
4484 }
4485# check for memory barriers without a comment.
4486 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4487 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004488 WARN("MEMORY_BARRIER",
4489 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004490 }
4491 }
4492# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004493 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004494 CHK("ARCH_DEFINES",
4495 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004496 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004497
Tobias Klauserd4977c72010-05-24 14:33:30 -07004498# Check that the storage class is at the beginning of a declaration
4499 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004500 WARN("STORAGE_CLASS",
4501 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004502 }
4503
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004504# check the location of the inline attribute, that it is between
4505# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004506 if ($line =~ /\b$Type\s+$Inline\b/ ||
4507 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004508 ERROR("INLINE_LOCATION",
4509 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004510 }
4511
Andy Whitcroft8905a672007-11-28 16:21:06 -08004512# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004513 if ($realfile !~ m@\binclude/uapi/@ &&
4514 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004515 if (WARN("INLINE",
4516 "plain inline is preferred over $1\n" . $herecurr) &&
4517 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004518 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004519
4520 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004521 }
4522
Joe Perches3d130fd2011-01-12 17:00:00 -08004523# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004524 if ($realfile !~ m@\binclude/uapi/@ &&
4525 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004526 WARN("PREFER_PACKED",
4527 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004528 }
4529
Joe Perches39b7e282011-07-25 17:13:24 -07004530# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004531 if ($realfile !~ m@\binclude/uapi/@ &&
4532 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004533 WARN("PREFER_ALIGNED",
4534 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004535 }
4536
Joe Perches5f14d3b2012-01-10 15:09:52 -08004537# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004538 if ($realfile !~ m@\binclude/uapi/@ &&
4539 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004540 if (WARN("PREFER_PRINTF",
4541 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004543 $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 -07004544
4545 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004546 }
4547
Joe Perches6061d942012-03-23 15:02:16 -07004548# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004549 if ($realfile !~ m@\binclude/uapi/@ &&
4550 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004551 if (WARN("PREFER_SCANF",
4552 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4553 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004554 $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 -07004555 }
Joe Perches6061d942012-03-23 15:02:16 -07004556 }
4557
Joe Perches8f53a9b2010-03-05 13:43:48 -08004558# check for sizeof(&)
4559 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004560 WARN("SIZEOF_ADDRESS",
4561 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004562 }
4563
Joe Perches66c80b62012-07-30 14:41:22 -07004564# check for sizeof without parenthesis
4565 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004566 if (WARN("SIZEOF_PARENTHESIS",
4567 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4568 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004569 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004570 }
Joe Perches66c80b62012-07-30 14:41:22 -07004571 }
4572
Joe Perches428e2fd2011-05-24 17:13:39 -07004573# check for line continuations in quoted strings with odd counts of "
4574 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004575 WARN("LINE_CONTINUATIONS",
4576 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004577 }
4578
Joe Perches88982fe2012-12-17 16:02:00 -08004579# check for struct spinlock declarations
4580 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4581 WARN("USE_SPINLOCK_T",
4582 "struct spinlock should be spinlock_t\n" . $herecurr);
4583 }
4584
Joe Perchesa6962d72013-04-29 16:18:13 -07004585# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004586 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004587 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004588 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004589 if (WARN("PREFER_SEQ_PUTS",
4590 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4591 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004592 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004593 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004594 }
4595 }
4596
Andy Whitcroft554e1652012-01-10 15:09:57 -08004597# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004598 if ($^V && $^V ge 5.10.0 &&
4599 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004600 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004601
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004602 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004603 my $ms_val = $7;
4604 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004605
Andy Whitcroft554e1652012-01-10 15:09:57 -08004606 if ($ms_size =~ /^(0x|)0$/i) {
4607 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004608 "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 -08004609 } elsif ($ms_size =~ /^(0x|)1$/i) {
4610 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004611 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4612 }
4613 }
4614
Joe Perches98a9bba2014-01-23 15:54:52 -08004615# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4616 if ($^V && $^V ge 5.10.0 &&
4617 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4618 if (WARN("PREFER_ETHER_ADDR_COPY",
4619 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4620 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004621 $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 -08004622 }
4623 }
4624
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004625# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004626 if ($^V && $^V ge 5.10.0 &&
4627 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004628 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004629 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004630 my $call = $1;
4631 my $cast1 = deparenthesize($2);
4632 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004633 my $cast2 = deparenthesize($7);
4634 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004635 my $cast;
4636
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004637 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004638 $cast = "$cast1 or $cast2";
4639 } elsif ($cast1 ne "") {
4640 $cast = $cast1;
4641 } else {
4642 $cast = $cast2;
4643 }
4644 WARN("MINMAX",
4645 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004646 }
4647 }
4648
Joe Perches4a273192012-07-30 14:41:20 -07004649# check usleep_range arguments
4650 if ($^V && $^V ge 5.10.0 &&
4651 defined $stat &&
4652 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4653 my $min = $1;
4654 my $max = $7;
4655 if ($min eq $max) {
4656 WARN("USLEEP_RANGE",
4657 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4658 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4659 $min > $max) {
4660 WARN("USLEEP_RANGE",
4661 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4662 }
4663 }
4664
Joe Perches823b7942013-11-12 15:10:15 -08004665# check for naked sscanf
4666 if ($^V && $^V ge 5.10.0 &&
4667 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004668 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004669 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4670 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4671 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4672 my $lc = $stat =~ tr@\n@@;
4673 $lc = $lc + $linenr;
4674 my $stat_real = raw_line($linenr, 0);
4675 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4676 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4677 }
4678 WARN("NAKED_SSCANF",
4679 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4680 }
4681
Joe Perchesafc819a2014-06-04 16:12:08 -07004682# check for simple sscanf that should be kstrto<foo>
4683 if ($^V && $^V ge 5.10.0 &&
4684 defined $stat &&
4685 $line =~ /\bsscanf\b/) {
4686 my $lc = $stat =~ tr@\n@@;
4687 $lc = $lc + $linenr;
4688 my $stat_real = raw_line($linenr, 0);
4689 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4690 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4691 }
4692 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4693 my $format = $6;
4694 my $count = $format =~ tr@%@%@;
4695 if ($count == 1 &&
4696 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4697 WARN("SSCANF_TO_KSTRTO",
4698 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4699 }
4700 }
4701 }
4702
Joe Perches70dc8a42013-09-11 14:23:58 -07004703# check for new externs in .h files.
4704 if ($realfile =~ /\.h$/ &&
4705 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004706 if (CHK("AVOID_EXTERNS",
4707 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004708 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004709 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004710 }
4711 }
4712
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004713# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004714 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004715 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004716 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004717 my $function_name = $1;
4718 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004719
4720 my $s = $stat;
4721 if (defined $cond) {
4722 substr($s, 0, length($cond), '');
4723 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004724 if ($s =~ /^\s*;/ &&
4725 $function_name ne 'uninitialized_var')
4726 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004727 WARN("AVOID_EXTERNS",
4728 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004729 }
4730
4731 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004732 WARN("FUNCTION_ARGUMENTS",
4733 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004734 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004735
4736 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4737 $stat =~ /^.\s*extern\s+/)
4738 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004739 WARN("AVOID_EXTERNS",
4740 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004741 }
4742
4743# checks for new __setup's
4744 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4745 my $name = $1;
4746
4747 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004748 CHK("UNDOCUMENTED_SETUP",
4749 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004750 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004751 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004752
4753# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004754 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004755 WARN("UNNECESSARY_CASTS",
4756 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004757 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004758
Joe Perchesa640d252013-07-03 15:05:21 -07004759# alloc style
4760# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4761 if ($^V && $^V ge 5.10.0 &&
4762 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4763 CHK("ALLOC_SIZEOF_STRUCT",
4764 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4765 }
4766
Joe Perches60a55362014-06-04 16:12:07 -07004767# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4768 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004769 $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 -07004770 my $oldfunc = $3;
4771 my $a1 = $4;
4772 my $a2 = $10;
4773 my $newfunc = "kmalloc_array";
4774 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004775 my $r1 = $a1;
4776 my $r2 = $a2;
4777 if ($a1 =~ /^sizeof\s*\S/) {
4778 $r1 = $a2;
4779 $r2 = $a1;
4780 }
4781 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4782 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004783 if (WARN("ALLOC_WITH_MULTIPLY",
4784 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4785 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004786 $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 -07004787
4788 }
4789 }
4790 }
4791
Joe Perches972fdea2013-04-29 16:18:12 -07004792# check for krealloc arg reuse
4793 if ($^V && $^V ge 5.10.0 &&
4794 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4795 WARN("KREALLOC_ARG_REUSE",
4796 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4797 }
4798
Joe Perches5ce59ae2013-02-21 16:44:18 -08004799# check for alloc argument mismatch
4800 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4801 WARN("ALLOC_ARRAY_ARGS",
4802 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4803 }
4804
Joe Perchescaf2a542011-01-12 16:59:56 -08004805# check for multiple semicolons
4806 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004807 if (WARN("ONE_SEMICOLON",
4808 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4809 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004810 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004811 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004812 }
4813
Joe Perchese81f2392014-08-06 16:11:25 -07004814# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08004815 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4816 my $has_break = 0;
4817 my $has_statement = 0;
4818 my $count = 0;
4819 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07004820 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08004821 $prevline--;
4822 my $rline = $rawlines[$prevline - 1];
4823 my $fline = $lines[$prevline - 1];
4824 last if ($fline =~ /^\@\@/);
4825 next if ($fline =~ /^\-/);
4826 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4827 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4828 next if ($fline =~ /^.[\s$;]*$/);
4829 $has_statement = 1;
4830 $count++;
4831 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4832 }
4833 if (!$has_break && $has_statement) {
4834 WARN("MISSING_BREAK",
4835 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4836 }
4837 }
4838
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004839# check for switch/default statements without a break;
4840 if ($^V && $^V ge 5.10.0 &&
4841 defined $stat &&
4842 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4843 my $ctx = '';
4844 my $herectx = $here . "\n";
4845 my $cnt = statement_rawlines($stat);
4846 for (my $n = 0; $n < $cnt; $n++) {
4847 $herectx .= raw_line($linenr, $n) . "\n";
4848 }
4849 WARN("DEFAULT_NO_BREAK",
4850 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004851 }
4852
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004853# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004854 if ($line =~ /\b__FUNCTION__\b/) {
4855 if (WARN("USE_FUNC",
4856 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4857 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004858 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004859 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004860 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004861
Joe Perches2c924882012-03-23 15:02:20 -07004862# check for use of yield()
4863 if ($line =~ /\byield\s*\(\s*\)/) {
4864 WARN("YIELD",
4865 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4866 }
4867
Joe Perches179f8f42013-07-03 15:05:30 -07004868# check for comparisons against true and false
4869 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4870 my $lead = $1;
4871 my $arg = $2;
4872 my $test = $3;
4873 my $otype = $4;
4874 my $trail = $5;
4875 my $op = "!";
4876
4877 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4878
4879 my $type = lc($otype);
4880 if ($type =~ /^(?:true|false)$/) {
4881 if (("$test" eq "==" && "$type" eq "true") ||
4882 ("$test" eq "!=" && "$type" eq "false")) {
4883 $op = "";
4884 }
4885
4886 CHK("BOOL_COMPARISON",
4887 "Using comparison to $otype is error prone\n" . $herecurr);
4888
4889## maybe suggesting a correct construct would better
4890## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4891
4892 }
4893 }
4894
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004895# check for semaphores initialized locked
4896 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004897 WARN("CONSIDER_COMPLETION",
4898 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004899 }
Joe Perches6712d852012-03-23 15:02:20 -07004900
Joe Perches67d0a072011-10-31 17:13:10 -07004901# recommend kstrto* over simple_strto* and strict_strto*
4902 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004903 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004904 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004905 }
Joe Perches6712d852012-03-23 15:02:20 -07004906
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004907# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07004908 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004909 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004910 "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 -07004911 }
Joe Perches6712d852012-03-23 15:02:20 -07004912
Emese Revfy79404842010-03-05 13:43:53 -08004913# check for various ops structs, ensure they are const.
4914 my $struct_ops = qr{acpi_dock_ops|
4915 address_space_operations|
4916 backlight_ops|
4917 block_device_operations|
4918 dentry_operations|
4919 dev_pm_ops|
4920 dma_map_ops|
4921 extent_io_ops|
4922 file_lock_operations|
4923 file_operations|
4924 hv_ops|
4925 ide_dma_ops|
4926 intel_dvo_dev_ops|
4927 item_operations|
4928 iwl_ops|
4929 kgdb_arch|
4930 kgdb_io|
4931 kset_uevent_ops|
4932 lock_manager_operations|
4933 microcode_ops|
4934 mtrr_ops|
4935 neigh_ops|
4936 nlmsvc_binding|
4937 pci_raw_ops|
4938 pipe_buf_operations|
4939 platform_hibernation_ops|
4940 platform_suspend_ops|
4941 proto_ops|
4942 rpc_pipe_ops|
4943 seq_operations|
4944 snd_ac97_build_ops|
4945 soc_pcmcia_socket_ops|
4946 stacktrace_ops|
4947 sysfs_ops|
4948 tty_operations|
4949 usb_mon_operations|
4950 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004951 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004952 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004953 WARN("CONST_STRUCT",
4954 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004955 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004956 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004957
4958# use of NR_CPUS is usually wrong
4959# ignore definitions of NR_CPUS and usage to define arrays as likely right
4960 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004961 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4962 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004963 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4964 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4965 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004966 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004967 WARN("NR_CPUS",
4968 "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 -07004969 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004970
Joe Perches52ea8502013-11-12 15:10:09 -08004971# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4972 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4973 ERROR("DEFINE_ARCH_HAS",
4974 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4975 }
4976
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004977# check for %L{u,d,i} in strings
4978 my $string;
4979 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4980 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004981 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004982 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004983 WARN("PRINTF_L",
4984 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004985 last;
4986 }
4987 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004988
4989# whine mightly about in_atomic
4990 if ($line =~ /\bin_atomic\s*\(/) {
4991 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004992 ERROR("IN_ATOMIC",
4993 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004994 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004995 WARN("IN_ATOMIC",
4996 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004997 }
4998 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004999
5000# check for lockdep_set_novalidate_class
5001 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5002 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5003 if ($realfile !~ m@^kernel/lockdep@ &&
5004 $realfile !~ m@^include/linux/lockdep@ &&
5005 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 ERROR("LOCKDEP",
5007 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005008 }
5009 }
Dave Jones88f88312011-01-12 16:59:59 -08005010
5011 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5012 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005013 WARN("EXPORTED_WORLD_WRITABLE",
5014 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005015 }
Joe Perches24358802014-04-03 14:49:13 -07005016
Joe Perches515a2352014-04-03 14:49:24 -07005017# Mode permission misuses where it seems decimal should be octal
5018# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5019 if ($^V && $^V ge 5.10.0 &&
5020 $line =~ /$mode_perms_search/) {
5021 foreach my $entry (@mode_permission_funcs) {
5022 my $func = $entry->[0];
5023 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005024
Joe Perches515a2352014-04-03 14:49:24 -07005025 my $skip_args = "";
5026 if ($arg_pos > 1) {
5027 $arg_pos--;
5028 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5029 }
5030 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5031 if ($line =~ /$test/) {
5032 my $val = $1;
5033 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005034
Joe Perches515a2352014-04-03 14:49:24 -07005035 if ($val !~ /^0$/ &&
5036 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5037 length($val) ne 4)) {
5038 ERROR("NON_OCTAL_PERMISSIONS",
5039 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5040 }
Joe Perches24358802014-04-03 14:49:13 -07005041 }
5042 }
5043 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005044 }
5045
5046 # If we have no input at all, then there is nothing to report on
5047 # so just keep quiet.
5048 if ($#rawlines == -1) {
5049 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005050 }
5051
Andy Whitcroft8905a672007-11-28 16:21:06 -08005052 # In mailback mode only produce a report in the negative, for
5053 # things that appear to be patches.
5054 if ($mailback && ($clean == 1 || !$is_patch)) {
5055 exit(0);
5056 }
5057
5058 # This is not a patch, and we are are in 'no-patch' mode so
5059 # just keep quiet.
5060 if (!$chk_patch && !$is_patch) {
5061 exit(0);
5062 }
5063
5064 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005065 ERROR("NOT_UNIFIED_DIFF",
5066 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005067 }
5068 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005069 ERROR("MISSING_SIGN_OFF",
5070 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005071 }
5072
Andy Whitcroft8905a672007-11-28 16:21:06 -08005073 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005074 if ($summary && !($clean == 1 && $quiet == 1)) {
5075 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005076 print "total: $cnt_error errors, $cnt_warn warnings, " .
5077 (($check)? "$cnt_chk checks, " : "") .
5078 "$cnt_lines lines checked\n";
5079 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005080 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005081
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005082 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005083
5084 if ($^V lt 5.10.0) {
5085 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5086 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5087 }
5088
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005089 # If there were whitespace errors which cleanpatch can fix
5090 # then suggest that.
5091 if ($rpt_cleaners) {
5092 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5093 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005094 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005095 }
5096 }
5097
Joe Perches91bfe482013-09-11 14:23:59 -07005098 hash_show_words(\%use_type, "Used");
5099 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005100
Joe Perchesd752fcc2014-08-06 16:11:05 -07005101 if ($clean == 0 && $fix &&
5102 ("@rawlines" ne "@fixed" ||
5103 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005104 my $newfile = $filename;
5105 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005106 my $linecount = 0;
5107 my $f;
5108
Joe Perchesd752fcc2014-08-06 16:11:05 -07005109 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5110
Joe Perches3705ce52013-07-03 15:05:31 -07005111 open($f, '>', $newfile)
5112 or die "$P: Can't open $newfile for write\n";
5113 foreach my $fixed_line (@fixed) {
5114 $linecount++;
5115 if ($file) {
5116 if ($linecount > 3) {
5117 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005118 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005119 }
5120 } else {
5121 print $f $fixed_line . "\n";
5122 }
5123 }
5124 close($f);
5125
5126 if (!$quiet) {
5127 print << "EOM";
5128Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5129
5130Do _NOT_ trust the results written to this file.
5131Do _NOT_ submit these changes without inspecting them for correctness.
5132
5133This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5134No warranties, expressed or implied...
5135
5136EOM
5137 }
5138 }
5139
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005140 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005141 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005142 }
5143 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005144 print << "EOM";
5145$vname has style problems, please review.
5146
5147If any of these errors are false positives, please report
5148them to the maintainer, see CHECKPATCH in MAINTAINERS.
5149EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005150 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005151
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005152 return $clean;
5153}