blob: 57f10db4accde6a935e5ee37332a70df3bb54b33 [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;
Andy Whitcroft8905a672007-11-28 16:21:06 -080027my $summary = 1;
28my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080029my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070030my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070031my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080032my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080034my %debug;
Joe Perches34456862013-07-03 15:05:34 -070035my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070036my %use_type = ();
37my @use = ();
38my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070039my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070040my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080042my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070043my $ignore_perl_version = 0;
44my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070045
46sub help {
47 my ($exitcode) = @_;
48
49 print << "EOM";
50Usage: $P [OPTION]... [FILE]...
51Version: $V
52
53Options:
54 -q, --quiet quiet
55 --no-tree run without a kernel tree
56 --no-signoff do not check for 'Signed-off-by' line
57 --patch treat FILE as patchfile (default)
58 --emacs emacs compile window format
59 --terse one line per report
60 -f, --file treat FILE as regular source file
61 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070062 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070063 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080064 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070065 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070066 --root=PATH PATH to the kernel tree root
67 --no-summary suppress the per-file summary
68 --mailback only produce a report in case of warnings/errors
69 --summary-file include the filename in summary
70 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
71 'values', 'possible', 'type', and 'attr' (default
72 is all off)
73 --test-only=WORD report only warnings/errors containing WORD
74 literally
Joe Perches3705ce52013-07-03 15:05:31 -070075 --fix EXPERIMENTAL - may create horrible results
76 If correctable single-line errors exist, create
77 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
78 with potential errors corrected to the preferred
79 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080080 --fix-inplace EXPERIMENTAL - may create horrible results
81 Is the same as --fix, but overwrites the input
82 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070083 --ignore-perl-version override checking of perl version. expect
84 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070085 -h, --help, --version display this help and exit
86
87When FILE is - read standard input.
88EOM
89
90 exit($exitcode);
91}
92
Joe Perches000d1cc12011-07-25 17:13:25 -070093my $conf = which_conf($configuration_file);
94if (-f $conf) {
95 my @conf_args;
96 open(my $conffile, '<', "$conf")
97 or warn "$P: Can't find a readable $configuration_file file $!\n";
98
99 while (<$conffile>) {
100 my $line = $_;
101
102 $line =~ s/\s*\n?$//g;
103 $line =~ s/^\s*//g;
104 $line =~ s/\s+/ /g;
105
106 next if ($line =~ m/^\s*#/);
107 next if ($line =~ m/^\s*$/);
108
109 my @words = split(" ", $line);
110 foreach my $word (@words) {
111 last if ($word =~ m/^#/);
112 push (@conf_args, $word);
113 }
114 }
115 close($conffile);
116 unshift(@ARGV, @conf_args) if @conf_args;
117}
118
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700119GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700121 'tree!' => \$tree,
122 'signoff!' => \$chk_signoff,
123 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700124 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800125 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700127 'subjective!' => \$check,
128 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700129 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700130 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700131 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800132 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800134 'summary!' => \$summary,
135 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800136 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700137 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800138 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700139 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800140 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700141 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700142 'h|help' => \$help,
143 'version' => \$help
144) or help(1);
145
146help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700147
Joe Perches9624b8d2014-01-23 15:54:44 -0800148$fix = 1 if ($fix_inplace);
149
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700150my $exit = 0;
151
Dave Hansend62a2012013-09-11 14:23:56 -0700152if ($^V && $^V lt $minimum_perl_version) {
153 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
154 if (!$ignore_perl_version) {
155 exit(1);
156 }
157}
158
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700159if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700160 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700161 exit(1);
162}
163
Joe Perches91bfe482013-09-11 14:23:59 -0700164sub hash_save_array_words {
165 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700166
Joe Perches91bfe482013-09-11 14:23:59 -0700167 my @array = split(/,/, join(',', @$arrayRef));
168 foreach my $word (@array) {
169 $word =~ s/\s*\n?$//g;
170 $word =~ s/^\s*//g;
171 $word =~ s/\s+/ /g;
172 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700173
Joe Perches91bfe482013-09-11 14:23:59 -0700174 next if ($word =~ m/^\s*#/);
175 next if ($word =~ m/^\s*$/);
176
177 $hashRef->{$word}++;
178 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700179}
180
Joe Perches91bfe482013-09-11 14:23:59 -0700181sub hash_show_words {
182 my ($hashRef, $prefix) = @_;
183
Joe Perches58cb3cf2013-09-11 14:24:04 -0700184 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700185 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700186 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700187 print " $word";
188 }
189 print "\n\n";
190 }
191}
192
193hash_save_array_words(\%ignore_type, \@ignore);
194hash_save_array_words(\%use_type, \@use);
195
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800196my $dbg_values = 0;
197my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700198my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700199my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800200for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800201 ## no critic
202 eval "\${dbg_$key} = '$debug{$key}';";
203 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800204}
205
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700206my $rpt_cleaners = 0;
207
Andy Whitcroft8905a672007-11-28 16:21:06 -0800208if ($terse) {
209 $emacs = 1;
210 $quiet++;
211}
212
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700213if ($tree) {
214 if (defined $root) {
215 if (!top_of_kernel_tree($root)) {
216 die "$P: $root: --root does not point at a valid tree\n";
217 }
218 } else {
219 if (top_of_kernel_tree('.')) {
220 $root = '.';
221 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
222 top_of_kernel_tree($1)) {
223 $root = $1;
224 }
225 }
226
227 if (!defined $root) {
228 print "Must be run from the top-level dir. of a kernel tree\n";
229 exit(2);
230 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700231}
232
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233my $emitted_corrupt = 0;
234
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700235our $Ident = qr{
236 [A-Za-z_][A-Za-z\d_]*
237 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
238 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700239our $Storage = qr{extern|static|asmlinkage};
240our $Sparse = qr{
241 __user|
242 __kernel|
243 __force|
244 __iomem|
245 __must_check|
246 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800247 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700248 __ref|
249 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700250 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800251our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
252our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
253our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
254our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
255our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700256
Wolfram Sang52131292010-03-05 13:43:51 -0800257# Notes to $Attribute:
258# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700259our $Attribute = qr{
260 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700261 __percpu|
262 __nocast|
263 __safe|
264 __bitwise__|
265 __packed__|
266 __packed2__|
267 __naked|
268 __maybe_unused|
269 __always_unused|
270 __noreturn|
271 __used|
272 __cold|
273 __noclone|
274 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700275 __read_mostly|
276 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700277 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700278 ____cacheline_aligned|
279 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800280 ____cacheline_internodealigned_in_smp|
281 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700282 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700283our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700285our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
286our $Lval = qr{$Ident(?:$Member)*};
287
Joe Perches95e2c602013-07-03 15:05:20 -0700288our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
289our $Binary = qr{(?i)0b[01]+$Int_type?};
290our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
291our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800292our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
293our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
294our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800295our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700296our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800297our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800298our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700299our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700300our $Operators = qr{
301 <=|>=|==|!=|
302 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700303 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700304 }x;
305
Andy Whitcroft8905a672007-11-28 16:21:06 -0800306our $NonptrType;
Joe Perches8716de32013-09-11 14:24:05 -0700307our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800308our $Type;
309our $Declare;
310
Joe Perches15662b32011-10-31 17:13:12 -0700311our $NON_ASCII_UTF8 = qr{
312 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700313 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
314 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
315 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
316 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
317 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
318 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
319}x;
320
Joe Perches15662b32011-10-31 17:13:12 -0700321our $UTF8 = qr{
322 [\x09\x0A\x0D\x20-\x7E] # ASCII
323 | $NON_ASCII_UTF8
324}x;
325
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700326our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700327 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700328 atomic_t
329)};
330
Joe Perches691e6692010-03-05 13:43:51 -0800331our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700332 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700333 (?:[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 -0700334 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700335 panic|
Joe Perches06668722013-11-12 15:10:07 -0800336 MODULE_[A-Z_]+|
337 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800338)};
339
Joe Perches20112472011-07-25 17:13:23 -0700340our $signature_tags = qr{(?xi:
341 Signed-off-by:|
342 Acked-by:|
343 Tested-by:|
344 Reviewed-by:|
345 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700346 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700347 To:|
348 Cc:
349)};
350
Andy Whitcroft8905a672007-11-28 16:21:06 -0800351our @typeList = (
352 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700353 qr{(?:unsigned\s+)?char},
354 qr{(?:unsigned\s+)?short},
355 qr{(?:unsigned\s+)?int},
356 qr{(?:unsigned\s+)?long},
357 qr{(?:unsigned\s+)?long\s+int},
358 qr{(?:unsigned\s+)?long\s+long},
359 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800360 qr{unsigned},
361 qr{float},
362 qr{double},
363 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800364 qr{struct\s+$Ident},
365 qr{union\s+$Ident},
366 qr{enum\s+$Ident},
367 qr{${Ident}_t},
368 qr{${Ident}_handler},
369 qr{${Ident}_handler_fn},
370);
Joe Perches8716de32013-09-11 14:24:05 -0700371our @typeListWithAttr = (
372 @typeList,
373 qr{struct\s+$InitAttribute\s+$Ident},
374 qr{union\s+$InitAttribute\s+$Ident},
375);
376
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700377our @modifierList = (
378 qr{fastcall},
379);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800380
Wolfram Sang7840a942010-08-09 17:20:57 -0700381our $allowed_asm_includes = qr{(?x:
382 irq|
383 memory
384)};
385# memory.h: ARM has a custom one
386
Andy Whitcroft8905a672007-11-28 16:21:06 -0800387sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700388 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
389 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700390 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700391 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700393 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800394 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800395 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700396 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700397 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800398 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700399 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800400 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700401 $NonptrTypeWithAttr = qr{
402 (?:$Modifier\s+|const\s+)*
403 (?:
404 (?:typeof|__typeof__)\s*\([^\)]*\)|
405 (?:$typeTypedefs\b)|
406 (?:${allWithAttr}\b)
407 )
408 (?:\s+$Modifier|\s+const)*
409 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800410 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700411 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700412 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700413 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800414 }x;
415 $Declare = qr{(?:$Storage\s+)?$Type};
416}
417build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700418
Joe Perches7d2367a2011-07-25 17:13:22 -0700419our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700420
421# Using $balanced_parens, $LvalOrFunc, or $FuncArg
422# requires at least perl version v5.10.0
423# Any use must be runtime checked with $^V
424
425our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
426our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800427our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700428
429sub deparenthesize {
430 my ($string) = @_;
431 return "" if (!defined($string));
432 $string =~ s@^\s*\(\s*@@g;
433 $string =~ s@\s*\)\s*$@@g;
434 $string =~ s@\s+@ @g;
435 return $string;
436}
437
Joe Perches34456862013-07-03 15:05:34 -0700438sub seed_camelcase_file {
439 my ($file) = @_;
440
441 return if (!(-f $file));
442
443 local $/;
444
445 open(my $include_file, '<', "$file")
446 or warn "$P: Can't read '$file' $!\n";
447 my $text = <$include_file>;
448 close($include_file);
449
450 my @lines = split('\n', $text);
451
452 foreach my $line (@lines) {
453 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
454 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
455 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800456 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
457 $camelcase{$1} = 1;
458 } 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 -0700459 $camelcase{$1} = 1;
460 }
461 }
462}
463
464my $camelcase_seeded = 0;
465sub seed_camelcase_includes {
466 return if ($camelcase_seeded);
467
468 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700469 my $camelcase_cache = "";
470 my @include_files = ();
471
472 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700473
Joe Perches34456862013-07-03 15:05:34 -0700474 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700475 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
476 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700477 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700478 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700479 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700480 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700481 @include_files = split('\n', $files);
482 foreach my $file (@include_files) {
483 my $date = POSIX::strftime("%Y%m%d%H%M",
484 localtime((stat $file)[9]));
485 $last_mod_date = $date if ($last_mod_date < $date);
486 }
487 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700488 }
Joe Perchesc707a812013-07-08 16:00:43 -0700489
490 if ($camelcase_cache ne "" && -f $camelcase_cache) {
491 open(my $camelcase_file, '<', "$camelcase_cache")
492 or warn "$P: Can't read '$camelcase_cache' $!\n";
493 while (<$camelcase_file>) {
494 chomp;
495 $camelcase{$_} = 1;
496 }
497 close($camelcase_file);
498
499 return;
500 }
501
502 if (-d ".git") {
503 $files = `git ls-files "include/*.h"`;
504 @include_files = split('\n', $files);
505 }
506
Joe Perches34456862013-07-03 15:05:34 -0700507 foreach my $file (@include_files) {
508 seed_camelcase_file($file);
509 }
Joe Perches351b2a12013-07-03 15:05:36 -0700510
Joe Perchesc707a812013-07-08 16:00:43 -0700511 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700512 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700513 open(my $camelcase_file, '>', "$camelcase_cache")
514 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700515 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
516 print $camelcase_file ("$_\n");
517 }
518 close($camelcase_file);
519 }
Joe Perches34456862013-07-03 15:05:34 -0700520}
521
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700522$chk_signoff = 0 if ($file);
523
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700524my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800525my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700526my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800527my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700528for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800529 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700530 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800531 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700532 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800533 } elsif ($filename eq '-') {
534 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700535 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800536 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700537 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700538 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800539 if ($filename eq '-') {
540 $vname = 'Your patch';
541 } else {
542 $vname = $filename;
543 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800544 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700545 chomp;
546 push(@rawlines, $_);
547 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800548 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800549 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700550 $exit = 1;
551 }
552 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800553 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700554 @fixed = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700555}
556
557exit($exit);
558
559sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700560 my ($root) = @_;
561
562 my @tree_check = (
563 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
564 "README", "Documentation", "arch", "include", "drivers",
565 "fs", "init", "ipc", "kernel", "lib", "scripts",
566 );
567
568 foreach my $check (@tree_check) {
569 if (! -e $root . '/' . $check) {
570 return 0;
571 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700572 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700573 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700574}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700575
Joe Perches20112472011-07-25 17:13:23 -0700576sub parse_email {
577 my ($formatted_email) = @_;
578
579 my $name = "";
580 my $address = "";
581 my $comment = "";
582
583 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
584 $name = $1;
585 $address = $2;
586 $comment = $3 if defined $3;
587 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
588 $address = $1;
589 $comment = $2 if defined $2;
590 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
591 $address = $1;
592 $comment = $2 if defined $2;
593 $formatted_email =~ s/$address.*$//;
594 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700595 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700596 $name =~ s/^\"|\"$//g;
597 # If there's a name left after stripping spaces and
598 # leading quotes, and the address doesn't have both
599 # leading and trailing angle brackets, the address
600 # is invalid. ie:
601 # "joe smith joe@smith.com" bad
602 # "joe smith <joe@smith.com" bad
603 if ($name ne "" && $address !~ /^<[^>]+>$/) {
604 $name = "";
605 $address = "";
606 $comment = "";
607 }
608 }
609
Joe Perches3705ce52013-07-03 15:05:31 -0700610 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700611 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700612 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700613 $address =~ s/^\<|\>$//g;
614
615 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
616 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
617 $name = "\"$name\"";
618 }
619
620 return ($name, $address, $comment);
621}
622
623sub format_email {
624 my ($name, $address) = @_;
625
626 my $formatted_email;
627
Joe Perches3705ce52013-07-03 15:05:31 -0700628 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700629 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700630 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700631
632 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
633 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
634 $name = "\"$name\"";
635 }
636
637 if ("$name" eq "") {
638 $formatted_email = "$address";
639 } else {
640 $formatted_email = "$name <$address>";
641 }
642
643 return $formatted_email;
644}
645
Joe Perches000d1cc12011-07-25 17:13:25 -0700646sub which_conf {
647 my ($conf) = @_;
648
649 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
650 if (-e "$path/$conf") {
651 return "$path/$conf";
652 }
653 }
654
655 return "";
656}
657
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700658sub expand_tabs {
659 my ($str) = @_;
660
661 my $res = '';
662 my $n = 0;
663 for my $c (split(//, $str)) {
664 if ($c eq "\t") {
665 $res .= ' ';
666 $n++;
667 for (; ($n % 8) != 0; $n++) {
668 $res .= ' ';
669 }
670 next;
671 }
672 $res .= $c;
673 $n++;
674 }
675
676 return $res;
677}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700678sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700679 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700680 return $res;
681}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700682
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700683sub line_stats {
684 my ($line) = @_;
685
686 # Drop the diff line leader and expand tabs
687 $line =~ s/^.//;
688 $line = expand_tabs($line);
689
690 # Pick the indent from the front of the line.
691 my ($white) = ($line =~ /^(\s*)/);
692
693 return (length($line), length($white));
694}
695
Andy Whitcroft773647a2008-03-28 14:15:58 -0700696my $sanitise_quote = '';
697
698sub sanitise_line_reset {
699 my ($in_comment) = @_;
700
701 if ($in_comment) {
702 $sanitise_quote = '*/';
703 } else {
704 $sanitise_quote = '';
705 }
706}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700707sub sanitise_line {
708 my ($line) = @_;
709
710 my $res = '';
711 my $l = '';
712
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800713 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700714 my $off = 0;
715 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700716
Andy Whitcroft773647a2008-03-28 14:15:58 -0700717 # Always copy over the diff marker.
718 $res = substr($line, 0, 1);
719
720 for ($off = 1; $off < length($line); $off++) {
721 $c = substr($line, $off, 1);
722
723 # Comments we are wacking completly including the begin
724 # and end, all to $;.
725 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
726 $sanitise_quote = '*/';
727
728 substr($res, $off, 2, "$;$;");
729 $off++;
730 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800731 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700732 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700733 $sanitise_quote = '';
734 substr($res, $off, 2, "$;$;");
735 $off++;
736 next;
737 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700738 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
739 $sanitise_quote = '//';
740
741 substr($res, $off, 2, $sanitise_quote);
742 $off++;
743 next;
744 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700745
746 # A \ in a string means ignore the next character.
747 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
748 $c eq "\\") {
749 substr($res, $off, 2, 'XX');
750 $off++;
751 next;
752 }
753 # Regular quotes.
754 if ($c eq "'" || $c eq '"') {
755 if ($sanitise_quote eq '') {
756 $sanitise_quote = $c;
757
758 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700759 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700760 } elsif ($sanitise_quote eq $c) {
761 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700762 }
763 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700764
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800765 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700766 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
767 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700768 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
769 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700770 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
771 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700772 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700773 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700774 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800775 }
776
Daniel Walker113f04a2009-09-21 17:04:35 -0700777 if ($sanitise_quote eq '//') {
778 $sanitise_quote = '';
779 }
780
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800781 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700782 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800783 my $clean = 'X' x length($1);
784 $res =~ s@\<.*\>@<$clean>@;
785
786 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700787 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800788 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700789 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800790 }
791
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700792 return $res;
793}
794
Joe Perchesa6962d72013-04-29 16:18:13 -0700795sub get_quoted_string {
796 my ($line, $rawline) = @_;
797
798 return "" if ($line !~ m/(\"[X]+\")/g);
799 return substr($rawline, $-[0], $+[0] - $-[0]);
800}
801
Andy Whitcroft8905a672007-11-28 16:21:06 -0800802sub ctx_statement_block {
803 my ($linenr, $remain, $off) = @_;
804 my $line = $linenr - 1;
805 my $blk = '';
806 my $soff = $off;
807 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700808 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800810 my $loff = 0;
811
Andy Whitcroft8905a672007-11-28 16:21:06 -0800812 my $type = '';
813 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800814 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800815 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800816 my $c;
817 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800818
819 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800820 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800821 @stack = (['', 0]) if ($#stack == -1);
822
Andy Whitcroft773647a2008-03-28 14:15:58 -0700823 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800824 # If we are about to drop off the end, pull in more
825 # context.
826 if ($off >= $len) {
827 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700828 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800829 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800830 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800831 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800832 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800833 $len = length($blk);
834 $line++;
835 last;
836 }
837 # Bail if there is no further context.
838 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800839 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800840 last;
841 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800842 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
843 $level++;
844 $type = '#';
845 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800846 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800847 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800848 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800849 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800850
Andy Whitcroft773647a2008-03-28 14:15:58 -0700851 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800852
853 # Handle nested #if/#else.
854 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
855 push(@stack, [ $type, $level ]);
856 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
857 ($type, $level) = @{$stack[$#stack - 1]};
858 } elsif ($remainder =~ /^#\s*endif\b/) {
859 ($type, $level) = @{pop(@stack)};
860 }
861
Andy Whitcroft8905a672007-11-28 16:21:06 -0800862 # Statement ends at the ';' or a close '}' at the
863 # outermost level.
864 if ($level == 0 && $c eq ';') {
865 last;
866 }
867
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800868 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700869 if ($level == 0 && $coff_set == 0 &&
870 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
871 $remainder =~ /^(else)(?:\s|{)/ &&
872 $remainder !~ /^else\s+if\b/) {
873 $coff = $off + length($1) - 1;
874 $coff_set = 1;
875 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
876 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800877 }
878
Andy Whitcroft8905a672007-11-28 16:21:06 -0800879 if (($type eq '' || $type eq '(') && $c eq '(') {
880 $level++;
881 $type = '(';
882 }
883 if ($type eq '(' && $c eq ')') {
884 $level--;
885 $type = ($level != 0)? '(' : '';
886
887 if ($level == 0 && $coff < $soff) {
888 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700889 $coff_set = 1;
890 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800891 }
892 }
893 if (($type eq '' || $type eq '{') && $c eq '{') {
894 $level++;
895 $type = '{';
896 }
897 if ($type eq '{' && $c eq '}') {
898 $level--;
899 $type = ($level != 0)? '{' : '';
900
901 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700902 if (substr($blk, $off + 1, 1) eq ';') {
903 $off++;
904 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800905 last;
906 }
907 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800908 # Preprocessor commands end at the newline unless escaped.
909 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
910 $level--;
911 $type = '';
912 $off++;
913 last;
914 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800915 $off++;
916 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700917 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800918 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700919 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800920 $line++;
921 $remain--;
922 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800923
924 my $statement = substr($blk, $soff, $off - $soff + 1);
925 my $condition = substr($blk, $soff, $coff - $soff + 1);
926
927 #warn "STATEMENT<$statement>\n";
928 #warn "CONDITION<$condition>\n";
929
Andy Whitcroft773647a2008-03-28 14:15:58 -0700930 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800931
932 return ($statement, $condition,
933 $line, $remain + 1, $off - $loff + 1, $level);
934}
935
Andy Whitcroftcf655042008-03-04 14:28:20 -0800936sub statement_lines {
937 my ($stmt) = @_;
938
939 # Strip the diff line prefixes and rip blank lines at start and end.
940 $stmt =~ s/(^|\n)./$1/g;
941 $stmt =~ s/^\s*//;
942 $stmt =~ s/\s*$//;
943
944 my @stmt_lines = ($stmt =~ /\n/g);
945
946 return $#stmt_lines + 2;
947}
948
949sub statement_rawlines {
950 my ($stmt) = @_;
951
952 my @stmt_lines = ($stmt =~ /\n/g);
953
954 return $#stmt_lines + 2;
955}
956
957sub statement_block_size {
958 my ($stmt) = @_;
959
960 $stmt =~ s/(^|\n)./$1/g;
961 $stmt =~ s/^\s*{//;
962 $stmt =~ s/}\s*$//;
963 $stmt =~ s/^\s*//;
964 $stmt =~ s/\s*$//;
965
966 my @stmt_lines = ($stmt =~ /\n/g);
967 my @stmt_statements = ($stmt =~ /;/g);
968
969 my $stmt_lines = $#stmt_lines + 2;
970 my $stmt_statements = $#stmt_statements + 1;
971
972 if ($stmt_lines > $stmt_statements) {
973 return $stmt_lines;
974 } else {
975 return $stmt_statements;
976 }
977}
978
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800979sub ctx_statement_full {
980 my ($linenr, $remain, $off) = @_;
981 my ($statement, $condition, $level);
982
983 my (@chunks);
984
Andy Whitcroftcf655042008-03-04 14:28:20 -0800985 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800986 ($statement, $condition, $linenr, $remain, $off, $level) =
987 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700988 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800989 push(@chunks, [ $condition, $statement ]);
990 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
991 return ($level, $linenr, @chunks);
992 }
993
994 # Pull in the following conditional/block pairs and see if they
995 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800996 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800997 ($statement, $condition, $linenr, $remain, $off, $level) =
998 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800999 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001000 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001001 #print "C: push\n";
1002 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001003 }
1004
1005 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001006}
1007
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001008sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001009 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001010 my $line;
1011 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001012 my $blk = '';
1013 my @o;
1014 my @c;
1015 my @res = ();
1016
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001017 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001018 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001019 for ($line = $start; $remain > 0; $line++) {
1020 next if ($rawlines[$line] =~ /^-/);
1021 $remain--;
1022
1023 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001024
1025 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001026 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001027 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001028 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001029 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001030 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001031 $level = pop(@stack);
1032 }
1033
Andy Whitcroft01464f32010-10-26 14:23:19 -07001034 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001035 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1036 if ($off > 0) {
1037 $off--;
1038 next;
1039 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001040
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001041 if ($c eq $close && $level > 0) {
1042 $level--;
1043 last if ($level == 0);
1044 } elsif ($c eq $open) {
1045 $level++;
1046 }
1047 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001048
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001049 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001050 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001051 }
1052
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001053 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001054 }
1055
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001056 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001057}
1058sub ctx_block_outer {
1059 my ($linenr, $remain) = @_;
1060
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001061 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1062 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001063}
1064sub ctx_block {
1065 my ($linenr, $remain) = @_;
1066
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001067 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1068 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001069}
1070sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001071 my ($linenr, $remain, $off) = @_;
1072
1073 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1074 return @r;
1075}
1076sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001077 my ($linenr, $remain) = @_;
1078
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001079 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001080}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001081sub ctx_statement_level {
1082 my ($linenr, $remain, $off) = @_;
1083
1084 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1085}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001086
1087sub ctx_locate_comment {
1088 my ($first_line, $end_line) = @_;
1089
1090 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001091 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001092 return $current_comment if (defined $current_comment);
1093
1094 # Look through the context and try and figure out if there is a
1095 # comment.
1096 my $in_comment = 0;
1097 $current_comment = '';
1098 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001099 my $line = $rawlines[$linenr - 1];
1100 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001101 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1102 $in_comment = 1;
1103 }
1104 if ($line =~ m@/\*@) {
1105 $in_comment = 1;
1106 }
1107 if (!$in_comment && $current_comment ne '') {
1108 $current_comment = '';
1109 }
1110 $current_comment .= $line . "\n" if ($in_comment);
1111 if ($line =~ m@\*/@) {
1112 $in_comment = 0;
1113 }
1114 }
1115
1116 chomp($current_comment);
1117 return($current_comment);
1118}
1119sub ctx_has_comment {
1120 my ($first_line, $end_line) = @_;
1121 my $cmt = ctx_locate_comment($first_line, $end_line);
1122
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001123 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001124 ##print "CMMT: $cmt\n";
1125
1126 return ($cmt ne '');
1127}
1128
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001129sub raw_line {
1130 my ($linenr, $cnt) = @_;
1131
1132 my $offset = $linenr - 1;
1133 $cnt++;
1134
1135 my $line;
1136 while ($cnt) {
1137 $line = $rawlines[$offset++];
1138 next if (defined($line) && $line =~ /^-/);
1139 $cnt--;
1140 }
1141
1142 return $line;
1143}
1144
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001145sub cat_vet {
1146 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001147 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001148
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001149 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1151 $res .= $1;
1152 if ($2 ne '') {
1153 $coded = sprintf("^%c", unpack('C', $2) + 64);
1154 $res .= $coded;
1155 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001156 }
1157 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001158
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001159 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001160}
1161
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001163my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001164my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001165my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001166
1167sub annotate_reset {
1168 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001169 $av_pending = '_';
1170 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001171 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001172}
1173
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001174sub annotate_values {
1175 my ($stream, $type) = @_;
1176
1177 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001178 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001179 my $cur = $stream;
1180
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001181 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001182
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001184 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001185 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001186 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001187 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 print "WS($1)\n" if ($dbg_values > 1);
1189 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001190 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001191 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001192 }
1193
Florian Micklerc023e4732011-01-12 16:59:58 -08001194 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001195 print "CAST($1)\n" if ($dbg_values > 1);
1196 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001197 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001198
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001199 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001200 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001201 $type = 'T';
1202
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001203 } elsif ($cur =~ /^($Modifier)\s*/) {
1204 print "MODIFIER($1)\n" if ($dbg_values > 1);
1205 $type = 'T';
1206
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001207 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001208 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001209 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001210 push(@av_paren_type, $type);
1211 if ($2 ne '') {
1212 $av_pending = 'N';
1213 }
1214 $type = 'E';
1215
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001216 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001217 print "UNDEF($1)\n" if ($dbg_values > 1);
1218 $av_preprocessor = 1;
1219 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001220
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001221 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001222 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001223 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001224
1225 push(@av_paren_type, $type);
1226 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001227 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001228
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001229 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001230 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1231 $av_preprocessor = 1;
1232
1233 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1234
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001235 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001236
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001237 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001238 print "PRE_END($1)\n" if ($dbg_values > 1);
1239
1240 $av_preprocessor = 1;
1241
1242 # Assume all arms of the conditional end as this
1243 # one does, and continue as if the #endif was not here.
1244 pop(@av_paren_type);
1245 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001246 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001247
1248 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001249 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001250
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001251 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1252 print "ATTR($1)\n" if ($dbg_values > 1);
1253 $av_pending = $type;
1254 $type = 'N';
1255
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001256 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001257 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001258 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001259 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001260 }
1261 $type = 'N';
1262
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001263 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001264 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001265 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001266 $type = 'N';
1267
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001268 } elsif ($cur =~/^(case)/o) {
1269 print "CASE($1)\n" if ($dbg_values > 1);
1270 $av_pend_colon = 'C';
1271 $type = 'N';
1272
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001273 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001274 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001275 $type = 'N';
1276
1277 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001278 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001279 push(@av_paren_type, $av_pending);
1280 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001281 $type = 'N';
1282
1283 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 my $new_type = pop(@av_paren_type);
1285 if ($new_type ne '_') {
1286 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001287 print "PAREN('$1') -> $type\n"
1288 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001289 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001290 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001291 }
1292
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001293 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001294 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001295 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001296 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001297
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001298 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1299 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001300 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001301 } elsif ($type eq 'E') {
1302 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001303 }
1304 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1305 $type = 'V';
1306
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001307 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001308 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001309 $type = 'V';
1310
1311 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001312 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001313 $type = 'N';
1314
Andy Whitcroftcf655042008-03-04 14:28:20 -08001315 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001316 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001317 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001318 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001319
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001320 } elsif ($cur =~/^(,)/) {
1321 print "COMMA($1)\n" if ($dbg_values > 1);
1322 $type = 'C';
1323
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001324 } elsif ($cur =~ /^(\?)/o) {
1325 print "QUESTION($1)\n" if ($dbg_values > 1);
1326 $type = 'N';
1327
1328 } elsif ($cur =~ /^(:)/o) {
1329 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1330
1331 substr($var, length($res), 1, $av_pend_colon);
1332 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1333 $type = 'E';
1334 } else {
1335 $type = 'N';
1336 }
1337 $av_pend_colon = 'O';
1338
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001339 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001340 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001341 $type = 'N';
1342
Andy Whitcroft0d413862008-10-15 22:02:16 -07001343 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001344 my $variant;
1345
1346 print "OPV($1)\n" if ($dbg_values > 1);
1347 if ($type eq 'V') {
1348 $variant = 'B';
1349 } else {
1350 $variant = 'U';
1351 }
1352
1353 substr($var, length($res), 1, $variant);
1354 $type = 'N';
1355
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001356 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001357 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001358 if ($1 ne '++' && $1 ne '--') {
1359 $type = 'N';
1360 }
1361
1362 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001364 }
1365 if (defined $1) {
1366 $cur = substr($cur, length($1));
1367 $res .= $type x length($1);
1368 }
1369 }
1370
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001371 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372}
1373
Andy Whitcroft8905a672007-11-28 16:21:06 -08001374sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001375 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001376 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001377 ^(?:
1378 $Modifier|
1379 $Storage|
1380 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001381 DEFINE_\S+
1382 )$|
1383 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001384 goto|
1385 return|
1386 case|
1387 else|
1388 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001389 do|
1390 \#|
1391 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001392 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001393 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001394 )}x;
1395 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1396 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001397 # Check for modifiers.
1398 $possible =~ s/\s*$Storage\s*//g;
1399 $possible =~ s/\s*$Sparse\s*//g;
1400 if ($possible =~ /^\s*$/) {
1401
1402 } elsif ($possible =~ /\s/) {
1403 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001404 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001405 if ($modifier !~ $notPermitted) {
1406 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1407 push(@modifierList, $modifier);
1408 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001409 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001410
1411 } else {
1412 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1413 push(@typeList, $possible);
1414 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001415 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001416 } else {
1417 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001418 }
1419}
1420
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001421my $prefix = '';
1422
Joe Perches000d1cc12011-07-25 17:13:25 -07001423sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001424 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1425
1426 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001427}
1428
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001429sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001430 if (!show_type($_[1]) ||
1431 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 return 0;
1433 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001434 my $line;
1435 if ($show_types) {
1436 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1437 } else {
1438 $line = "$prefix$_[0]: $_[2]\n";
1439 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001440 $line = (split('\n', $line))[0] . "\n" if ($terse);
1441
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001442 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443
1444 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001445}
1446sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001447 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001448}
Joe Perches000d1cc12011-07-25 17:13:25 -07001449
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001450sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001451 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001452 our $clean = 0;
1453 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001454 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001455 }
Joe Perches3705ce52013-07-03 15:05:31 -07001456 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001457}
1458sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001459 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 our $clean = 0;
1461 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001462 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001463 }
Joe Perches3705ce52013-07-03 15:05:31 -07001464 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001465}
1466sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001467 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001468 our $clean = 0;
1469 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001470 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001471 }
Joe Perches3705ce52013-07-03 15:05:31 -07001472 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001473}
1474
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001475sub check_absolute_file {
1476 my ($absolute, $herecurr) = @_;
1477 my $file = $absolute;
1478
1479 ##print "absolute<$absolute>\n";
1480
1481 # See if any suffix of this path is a path within the tree.
1482 while ($file =~ s@^[^/]*/@@) {
1483 if (-f "$root/$file") {
1484 ##print "file<$file>\n";
1485 last;
1486 }
1487 }
1488 if (! -f _) {
1489 return 0;
1490 }
1491
1492 # It is, so see if the prefix is acceptable.
1493 my $prefix = $absolute;
1494 substr($prefix, -length($file)) = '';
1495
1496 ##print "prefix<$prefix>\n";
1497 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001498 WARN("USE_RELATIVE_PATH",
1499 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001500 }
1501}
1502
Joe Perches3705ce52013-07-03 15:05:31 -07001503sub trim {
1504 my ($string) = @_;
1505
Joe Perchesb34c6482013-09-11 14:24:01 -07001506 $string =~ s/^\s+|\s+$//g;
1507
1508 return $string;
1509}
1510
1511sub ltrim {
1512 my ($string) = @_;
1513
1514 $string =~ s/^\s+//;
1515
1516 return $string;
1517}
1518
1519sub rtrim {
1520 my ($string) = @_;
1521
1522 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001523
1524 return $string;
1525}
1526
Joe Perches52ea8502013-11-12 15:10:09 -08001527sub string_find_replace {
1528 my ($string, $find, $replace) = @_;
1529
1530 $string =~ s/$find/$replace/g;
1531
1532 return $string;
1533}
1534
Joe Perches3705ce52013-07-03 15:05:31 -07001535sub tabify {
1536 my ($leading) = @_;
1537
1538 my $source_indent = 8;
1539 my $max_spaces_before_tab = $source_indent - 1;
1540 my $spaces_to_tab = " " x $source_indent;
1541
1542 #convert leading spaces to tabs
1543 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1544 #Remove spaces before a tab
1545 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1546
1547 return "$leading";
1548}
1549
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001550sub pos_last_openparen {
1551 my ($line) = @_;
1552
1553 my $pos = 0;
1554
1555 my $opens = $line =~ tr/\(/\(/;
1556 my $closes = $line =~ tr/\)/\)/;
1557
1558 my $last_openparen = 0;
1559
1560 if (($opens == 0) || ($closes >= $opens)) {
1561 return -1;
1562 }
1563
1564 my $len = length($line);
1565
1566 for ($pos = 0; $pos < $len; $pos++) {
1567 my $string = substr($line, $pos);
1568 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1569 $pos += length($1) - 1;
1570 } elsif (substr($line, $pos, 1) eq '(') {
1571 $last_openparen = $pos;
1572 } elsif (index($string, '(') == -1) {
1573 last;
1574 }
1575 }
1576
1577 return $last_openparen + 1;
1578}
1579
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001580sub process {
1581 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001582
1583 my $linenr=0;
1584 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001585 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001586 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001587 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001588
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001589 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001590 my $indent;
1591 my $previndent=0;
1592 my $stashindent=0;
1593
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001594 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001595 my $signoff = 0;
1596 my $is_patch = 0;
1597
Joe Perches15662b32011-10-31 17:13:12 -07001598 my $in_header_lines = 1;
1599 my $in_commit_log = 0; #Scanning lines before patch
1600
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001601 my $non_utf8_charset = 0;
1602
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001603 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001604 our $cnt_lines = 0;
1605 our $cnt_error = 0;
1606 our $cnt_warn = 0;
1607 our $cnt_chk = 0;
1608
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001609 # Trace the real file/line as we go.
1610 my $realfile = '';
1611 my $realline = 0;
1612 my $realcnt = 0;
1613 my $here = '';
1614 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001615 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001616 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001617 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001618
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001619 my $prev_values = 'E';
1620
1621 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001622 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001623 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001624 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001625 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001626
Joe Perches7e51f192013-09-11 14:23:57 -07001627 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001628
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001629 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001630 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001631 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001632 my @setup_docs = ();
1633 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001634
Joe Perchesd8b07712013-11-12 15:10:06 -08001635 my $camelcase_file_seeded = 0;
1636
Andy Whitcroft773647a2008-03-28 14:15:58 -07001637 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001638 my $line;
1639 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001640 $linenr++;
1641 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001642
Joe Perches3705ce52013-07-03 15:05:31 -07001643 push(@fixed, $rawline) if ($fix);
1644
Andy Whitcroft773647a2008-03-28 14:15:58 -07001645 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001646 $setup_docs = 0;
1647 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1648 $setup_docs = 1;
1649 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001650 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001651 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001652 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1653 $realline=$1-1;
1654 if (defined $2) {
1655 $realcnt=$3+1;
1656 } else {
1657 $realcnt=1+1;
1658 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001659 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001660
1661 # Guestimate if this is a continuing comment. Run
1662 # the context looking for a comment "edge". If this
1663 # edge is a close comment then we must be in a comment
1664 # at context start.
1665 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001666 my $cnt = $realcnt;
1667 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1668 next if (defined $rawlines[$ln - 1] &&
1669 $rawlines[$ln - 1] =~ /^-/);
1670 $cnt--;
1671 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001672 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001673 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1674 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1675 ($edge) = $1;
1676 last;
1677 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001678 }
1679 if (defined $edge && $edge eq '*/') {
1680 $in_comment = 1;
1681 }
1682
1683 # Guestimate if this is a continuing comment. If this
1684 # is the start of a diff block and this line starts
1685 # ' *' then it is very likely a comment.
1686 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001687 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001688 {
1689 $in_comment = 1;
1690 }
1691
1692 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1693 sanitise_line_reset($in_comment);
1694
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001695 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001696 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001697 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 $line = sanitise_line($rawline);
1699 }
1700 push(@lines, $line);
1701
1702 if ($realcnt > 1) {
1703 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1704 } else {
1705 $realcnt = 0;
1706 }
1707
1708 #print "==>$rawline\n";
1709 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001710
1711 if ($setup_docs && $line =~ /^\+/) {
1712 push(@setup_docs, $line);
1713 }
1714 }
1715
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001716 $prefix = '';
1717
Andy Whitcroft773647a2008-03-28 14:15:58 -07001718 $realcnt = 0;
1719 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001720 foreach my $line (@lines) {
1721 $linenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001722 my $sline = $line; #copy of $line
1723 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001724
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001727#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001729 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001730 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001731 $realline=$1-1;
1732 if (defined $2) {
1733 $realcnt=$3+1;
1734 } else {
1735 $realcnt=1+1;
1736 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001737 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001738 $prev_values = 'E';
1739
Andy Whitcroft773647a2008-03-28 14:15:58 -07001740 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001741 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001742 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001743 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001744 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001745
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001746# track the line number as we move through the hunk, note that
1747# new versions of GNU diff omit the leading space on completely
1748# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001749 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001750 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001751 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001752
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001753 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001754 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001755
1756 # Track the previous line.
1757 ($prevline, $stashline) = ($stashline, $line);
1758 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001759 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1760
Andy Whitcroft773647a2008-03-28 14:15:58 -07001761 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001762
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001763 } elsif ($realcnt == 1) {
1764 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001765 }
1766
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001767 my $hunk_line = ($realcnt != 0);
1768
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001769#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001770 $prefix = "$filename:$realline: " if ($emacs && $file);
1771 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1772
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001773 $here = "#$linenr: " if (!$file);
1774 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001775
1776 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001777 if ($line =~ /^diff --git.*?(\S+)$/) {
1778 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001779 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001780 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001781 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001782 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001783 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001784 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001785
1786 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001787 if (!$file && $tree && $p1_prefix ne '' &&
1788 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001789 WARN("PATCH_PREFIX",
1790 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001791 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001792
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001793 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001794 ERROR("MODIFIED_INCLUDE_ASM",
1795 "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 -07001796 }
1797 next;
1798 }
1799
Randy Dunlap389834b2007-06-08 13:47:03 -07001800 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001801
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 my $hereline = "$here\n$rawline\n";
1803 my $herecurr = "$here\n$rawline\n";
1804 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001805
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001806 $cnt_lines++ if ($realcnt != 0);
1807
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001808# Check for incorrect file permissions
1809 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1810 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001811 if ($realfile !~ m@scripts/@ &&
1812 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001813 ERROR("EXECUTE_PERMISSIONS",
1814 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001815 }
1816 }
1817
Joe Perches20112472011-07-25 17:13:23 -07001818# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001819 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001820 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001821 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001822 }
1823
1824# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001825 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001826 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001827 my $space_before = $1;
1828 my $sign_off = $2;
1829 my $space_after = $3;
1830 my $email = $4;
1831 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1832
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001833 if ($sign_off !~ /$signature_tags/) {
1834 WARN("BAD_SIGN_OFF",
1835 "Non-standard signature: $sign_off\n" . $herecurr);
1836 }
Joe Perches20112472011-07-25 17:13:23 -07001837 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001838 if (WARN("BAD_SIGN_OFF",
1839 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1840 $fix) {
1841 $fixed[$linenr - 1] =
1842 "$ucfirst_sign_off $email";
1843 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001844 }
Joe Perches20112472011-07-25 17:13:23 -07001845 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001846 if (WARN("BAD_SIGN_OFF",
1847 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1848 $fix) {
1849 $fixed[$linenr - 1] =
1850 "$ucfirst_sign_off $email";
1851 }
1852
Joe Perches20112472011-07-25 17:13:23 -07001853 }
1854 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001855 if (WARN("BAD_SIGN_OFF",
1856 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1857 $fix) {
1858 $fixed[$linenr - 1] =
1859 "$ucfirst_sign_off $email";
1860 }
Joe Perches20112472011-07-25 17:13:23 -07001861 }
1862
1863 my ($email_name, $email_address, $comment) = parse_email($email);
1864 my $suggested_email = format_email(($email_name, $email_address));
1865 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001866 ERROR("BAD_SIGN_OFF",
1867 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001868 } else {
1869 my $dequoted = $suggested_email;
1870 $dequoted =~ s/^"//;
1871 $dequoted =~ s/" </ </;
1872 # Don't force email to have quotes
1873 # Allow just an angle bracketed address
1874 if ("$dequoted$comment" ne $email &&
1875 "<$email_address>$comment" ne $email &&
1876 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001877 WARN("BAD_SIGN_OFF",
1878 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001879 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001880 }
Joe Perches7e51f192013-09-11 14:23:57 -07001881
1882# Check for duplicate signatures
1883 my $sig_nospace = $line;
1884 $sig_nospace =~ s/\s//g;
1885 $sig_nospace = lc($sig_nospace);
1886 if (defined $signatures{$sig_nospace}) {
1887 WARN("BAD_SIGN_OFF",
1888 "Duplicate signature\n" . $herecurr);
1889 } else {
1890 $signatures{$sig_nospace} = 1;
1891 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001892 }
1893
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001894# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001895 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001896 ERROR("CORRUPTED_PATCH",
1897 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001899 }
1900
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001901# Check for absolute kernel paths.
1902 if ($tree) {
1903 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1904 my $file = $1;
1905
1906 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1907 check_absolute_file($1, $herecurr)) {
1908 #
1909 } else {
1910 check_absolute_file($file, $herecurr);
1911 }
1912 }
1913 }
1914
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001915# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1916 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001917 $rawline !~ m/^$UTF8*$/) {
1918 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1919
1920 my $blank = copy_spacing($rawline);
1921 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1922 my $hereptr = "$hereline$ptr\n";
1923
Joe Perches34d99212011-07-25 17:13:26 -07001924 CHK("INVALID_UTF8",
1925 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001926 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001927
Joe Perches15662b32011-10-31 17:13:12 -07001928# Check if it's the start of a commit log
1929# (not a header line and we haven't seen the patch filename)
1930 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001931 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001932 $in_header_lines = 0;
1933 $in_commit_log = 1;
1934 }
1935
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001936# Check if there is UTF-8 in a commit log when a mail header has explicitly
1937# declined it, i.e defined some charset where it is missing.
1938 if ($in_header_lines &&
1939 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1940 $1 !~ /utf-8/i) {
1941 $non_utf8_charset = 1;
1942 }
1943
1944 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001945 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001946 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001947 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1948 }
1949
Andy Whitcroft306708542008-10-15 22:02:28 -07001950# ignore non-hunk lines and lines being removed
1951 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001952
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001953#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001954 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001955 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001956 if (ERROR("DOS_LINE_ENDINGS",
1957 "DOS line endings\n" . $herevet) &&
1958 $fix) {
1959 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1960 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001961 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1962 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001963 if (ERROR("TRAILING_WHITESPACE",
1964 "trailing whitespace\n" . $herevet) &&
1965 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001966 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001967 }
1968
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001969 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001970 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001971
Josh Triplett4783f892013-11-12 15:10:12 -08001972# Check for FSF mailing addresses.
1973 if ($rawline =~ /You should have received a copy/ ||
1974 $rawline =~ /write to the Free Software/ ||
1975 $rawline =~ /59 Temple Place/ ||
1976 $rawline =~ /51 Franklin Street/) {
1977 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1978 my $msg_type = \&ERROR;
1979 $msg_type = \&CHK if ($file);
1980 &{$msg_type}("FSF_MAILING_ADDRESS",
1981 "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)
1982 }
1983
Andi Kleen33549572010-05-24 14:33:29 -07001984# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001985# Only applies when adding the entry originally, after that we do not have
1986# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001987 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001988 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001989 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001990 my $cnt = $realcnt;
1991 my $ln = $linenr + 1;
1992 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001993 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001994 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001995 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001996 $f = $lines[$ln - 1];
1997 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1998 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001999
2000 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002001
2002 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
2003 $is_start = 1;
2004 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
2005 $length = -1;
2006 }
2007
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002008 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002009 $f =~ s/#.*//;
2010 $f =~ s/^\s+//;
2011 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002012 if ($f =~ /^\s*config\s/) {
2013 $is_end = 1;
2014 last;
2015 }
Andi Kleen33549572010-05-24 14:33:29 -07002016 $length++;
2017 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002018 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08002019 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2020 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002021 }
2022
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002023# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2024 if ($realfile =~ /Kconfig/ &&
2025 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2026 WARN("CONFIG_EXPERIMENTAL",
2027 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2028 }
2029
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002030 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2031 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2032 my $flag = $1;
2033 my $replacement = {
2034 'EXTRA_AFLAGS' => 'asflags-y',
2035 'EXTRA_CFLAGS' => 'ccflags-y',
2036 'EXTRA_CPPFLAGS' => 'cppflags-y',
2037 'EXTRA_LDFLAGS' => 'ldflags-y',
2038 };
2039
2040 WARN("DEPRECATED_VARIABLE",
2041 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2042 }
2043
Andy Whitcroft5368df202008-10-15 22:02:27 -07002044# check we are in a valid source file if not then ignore this hunk
2045 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2046
Joe Perches6cd7f382012-12-17 16:01:54 -08002047#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002048 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002049 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002050 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002051 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002052 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002053 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002054 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002055 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002056 }
2057
Josh Triplettca56dc02012-03-23 15:02:21 -07002058# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002059# to grep for the string. Make exceptions when the previous string ends in a
2060# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2061# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002062 if ($line =~ /^\+\s*"/ &&
2063 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002064 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002065 WARN("SPLIT_STRING",
2066 "quoted string split across lines\n" . $hereprev);
2067 }
2068
Joe Perches5e79d962010-03-05 13:43:55 -08002069# check for spaces before a quoted newline
2070 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002071 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2072 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2073 $fix) {
2074 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2075 }
2076
Joe Perches5e79d962010-03-05 13:43:55 -08002077 }
2078
Andy Whitcroft8905a672007-11-28 16:21:06 -08002079# check for adding lines without a newline.
2080 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002081 WARN("MISSING_EOF_NEWLINE",
2082 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002083 }
2084
Mike Frysinger42e41c52009-09-21 17:04:40 -07002085# Blackfin: use hi/lo macros
2086 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2087 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2088 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002089 ERROR("LO_MACRO",
2090 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002091 }
2092 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2093 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002094 ERROR("HI_MACRO",
2095 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002096 }
2097 }
2098
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002099# check we are in a valid source file C or perl if not then ignore this hunk
2100 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002101
2102# at the beginning of a line any tabs must come first and anything
2103# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002104 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2105 $rawline =~ /^\+\s* \s*/) {
2106 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002107 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002108 if (ERROR("CODE_INDENT",
2109 "code indent should use tabs where possible\n" . $herevet) &&
2110 $fix) {
2111 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002113 }
2114
Alberto Panizzo08e44362010-03-05 13:43:54 -08002115# check for space before tabs.
2116 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2117 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002118 if (WARN("SPACE_BEFORE_TAB",
2119 "please, no space before tabs\n" . $herevet) &&
2120 $fix) {
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002121 while ($fixed[$linenr - 1] =~
2122 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2123 while ($fixed[$linenr - 1] =~
2124 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002125 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002126 }
2127
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002128# check for && or || at the start of a line
2129 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2130 CHK("LOGICAL_CONTINUATIONS",
2131 "Logical continuations should be on the previous line\n" . $hereprev);
2132 }
2133
2134# check multi-line statement indentation matches previous line
2135 if ($^V && $^V ge 5.10.0 &&
2136 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2137 $prevline =~ /^\+(\t*)(.*)$/;
2138 my $oldindent = $1;
2139 my $rest = $2;
2140
2141 my $pos = pos_last_openparen($rest);
2142 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002143 $line =~ /^(\+| )([ \t]*)/;
2144 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002145
2146 my $goodtabindent = $oldindent .
2147 "\t" x ($pos / 8) .
2148 " " x ($pos % 8);
2149 my $goodspaceindent = $oldindent . " " x $pos;
2150
2151 if ($newindent ne $goodtabindent &&
2152 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002153
2154 if (CHK("PARENTHESIS_ALIGNMENT",
2155 "Alignment should match open parenthesis\n" . $hereprev) &&
2156 $fix && $line =~ /^\+/) {
2157 $fixed[$linenr - 1] =~
2158 s/^\+[ \t]*/\+$goodtabindent/;
2159 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002160 }
2161 }
2162 }
2163
Joe Perches23f780c2013-07-03 15:05:31 -07002164 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002165 if (CHK("SPACING",
2166 "No space is necessary after a cast\n" . $hereprev) &&
2167 $fix) {
2168 $fixed[$linenr - 1] =~
2169 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2170 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002171 }
2172
Joe Perches05880602012-10-04 17:13:35 -07002173 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002174 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2175 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002176 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2177 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2178 }
2179
2180 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002181 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2182 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002183 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002184 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2185 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2186 "networking block comments start with * on subsequent lines\n" . $hereprev);
2187 }
2188
2189 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002190 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2191 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2192 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2193 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002194 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2195 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2196 }
2197
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002198# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002199# Exceptions:
2200# 1) within comments
2201# 2) indented preprocessor commands
2202# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002203 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002204 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002205 if (WARN("LEADING_SPACE",
2206 "please, no spaces at the start of a line\n" . $herevet) &&
2207 $fix) {
2208 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2209 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002210 }
2211
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002212# check we are in a valid C source file if not then ignore this hunk
2213 next if ($realfile !~ /\.(h|c)$/);
2214
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002215# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2216 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2217 WARN("CONFIG_EXPERIMENTAL",
2218 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2219 }
2220
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002221# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002222 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002223 WARN("CVS_KEYWORD",
2224 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002225 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002226
Mike Frysinger42e41c52009-09-21 17:04:40 -07002227# Blackfin: don't use __builtin_bfin_[cs]sync
2228 if ($line =~ /__builtin_bfin_csync/) {
2229 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002230 ERROR("CSYNC",
2231 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002232 }
2233 if ($line =~ /__builtin_bfin_ssync/) {
2234 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002235 ERROR("SSYNC",
2236 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002237 }
2238
Joe Perches56e77d72013-02-21 16:44:14 -08002239# check for old HOTPLUG __dev<foo> section markings
2240 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2241 WARN("HOTPLUG_SECTION",
2242 "Using $1 is unnecessary\n" . $herecurr);
2243 }
2244
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002245# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002246 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2247 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002248#print "LINE<$line>\n";
2249 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002250 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002251 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002252 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002253 $stat =~ s/\n./\n /g;
2254 $cond =~ s/\n./\n /g;
2255
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002256#print "linenr<$linenr> <$stat>\n";
2257 # If this statement has no statement boundaries within
2258 # it there is no point in retrying a statement scan
2259 # until we hit end of it.
2260 my $frag = $stat; $frag =~ s/;+\s*$//;
2261 if ($frag !~ /(?:{|;)/) {
2262#print "skip<$line_nr_next>\n";
2263 $suppress_statement = $line_nr_next;
2264 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002265
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002266 # Find the real next line.
2267 $realline_next = $line_nr_next;
2268 if (defined $realline_next &&
2269 (!defined $lines[$realline_next - 1] ||
2270 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2271 $realline_next++;
2272 }
2273
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002274 my $s = $stat;
2275 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002276
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002277 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002278 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002279
2280 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002281 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002282
Andy Whitcroft463f2862009-09-21 17:04:34 -07002283 } elsif ($s =~ /^.\s*else\b/s) {
2284
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002285 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002286 } 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 -07002287 my $type = $1;
2288 $type =~ s/\s+/ /g;
2289 possible($type, "A:" . $s);
2290
Andy Whitcroft8905a672007-11-28 16:21:06 -08002291 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002292 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002293 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002294 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002295
2296 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002297 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002298 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002299 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002300
2301 # Check for any sort of function declaration.
2302 # int foo(something bar, other baz);
2303 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002304 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 -08002305 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002306
Andy Whitcroftcf655042008-03-04 14:28:20 -08002307 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002308 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002309 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002310
Andy Whitcroft8905a672007-11-28 16:21:06 -08002311 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002312 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002313
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002314 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002315 }
2316 }
2317 }
2318
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002319 }
2320
Andy Whitcroft653d4872007-06-23 17:16:34 -07002321#
2322# Checks which may be anchored in the context.
2323#
2324
2325# Check for switch () and associated case and default
2326# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002327 if ($line=~/\bswitch\s*\(.*\)/) {
2328 my $err = '';
2329 my $sep = '';
2330 my @ctx = ctx_block_outer($linenr, $realcnt);
2331 shift(@ctx);
2332 for my $ctx (@ctx) {
2333 my ($clen, $cindent) = line_stats($ctx);
2334 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2335 $indent != $cindent) {
2336 $err .= "$sep$ctx\n";
2337 $sep = '';
2338 } else {
2339 $sep = "[...]\n";
2340 }
2341 }
2342 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002343 ERROR("SWITCH_CASE_INDENT_LEVEL",
2344 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002345 }
2346 }
2347
2348# if/while/etc brace do not go on next line, unless defining a do while loop,
2349# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002350 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 my $pre_ctx = "$1$2";
2352
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002353 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002354
2355 if ($line =~ /^\+\t{6,}/) {
2356 WARN("DEEP_INDENTATION",
2357 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2358 }
2359
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002360 my $ctx_cnt = $realcnt - $#ctx - 1;
2361 my $ctx = join("\n", @ctx);
2362
Andy Whitcroft548596d2008-07-23 21:29:01 -07002363 my $ctx_ln = $linenr;
2364 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002365
Andy Whitcroft548596d2008-07-23 21:29:01 -07002366 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2367 defined $lines[$ctx_ln - 1] &&
2368 $lines[$ctx_ln - 1] =~ /^-/)) {
2369 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2370 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002371 $ctx_ln++;
2372 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002373
Andy Whitcroft53210162008-07-23 21:29:03 -07002374 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2375 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002376
2377 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002378 ERROR("OPEN_BRACE",
2379 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002380 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002381 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002382 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2383 $ctx =~ /\)\s*\;\s*$/ &&
2384 defined $lines[$ctx_ln - 1])
2385 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002386 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2387 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002388 WARN("TRAILING_SEMICOLON",
2389 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002390 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002391 }
2392 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002393 }
2394
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002395# Check relative indent for conditionals and blocks.
2396 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002397 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2398 ctx_statement_block($linenr, $realcnt, 0)
2399 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002400 my ($s, $c) = ($stat, $cond);
2401
2402 substr($s, 0, length($c), '');
2403
2404 # Make sure we remove the line prefixes as we have
2405 # none on the first line, and are going to readd them
2406 # where necessary.
2407 $s =~ s/\n./\n/gs;
2408
2409 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002410 my @newlines = ($c =~ /\n/gs);
2411 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002412
2413 # We want to check the first line inside the block
2414 # starting at the end of the conditional, so remove:
2415 # 1) any blank line termination
2416 # 2) any opening brace { on end of the line
2417 # 3) any do (...) {
2418 my $continuation = 0;
2419 my $check = 0;
2420 $s =~ s/^.*\bdo\b//;
2421 $s =~ s/^\s*{//;
2422 if ($s =~ s/^\s*\\//) {
2423 $continuation = 1;
2424 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002425 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002426 $check = 1;
2427 $cond_lines++;
2428 }
2429
2430 # Also ignore a loop construct at the end of a
2431 # preprocessor statement.
2432 if (($prevline =~ /^.\s*#\s*define\s/ ||
2433 $prevline =~ /\\\s*$/) && $continuation == 0) {
2434 $check = 0;
2435 }
2436
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002437 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002438 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002439 while ($cond_ptr != $cond_lines) {
2440 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002441
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002442 # If we see an #else/#elif then the code
2443 # is not linear.
2444 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2445 $check = 0;
2446 }
2447
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002448 # Ignore:
2449 # 1) blank lines, they should be at 0,
2450 # 2) preprocessor lines, and
2451 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002452 if ($continuation ||
2453 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002454 $s =~ /^\s*#\s*?/ ||
2455 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002456 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002457 if ($s =~ s/^.*?\n//) {
2458 $cond_lines++;
2459 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002460 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002461 }
2462
2463 my (undef, $sindent) = line_stats("+" . $s);
2464 my $stat_real = raw_line($linenr, $cond_lines);
2465
2466 # Check if either of these lines are modified, else
2467 # this is not this patch's fault.
2468 if (!defined($stat_real) ||
2469 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2470 $check = 0;
2471 }
2472 if (defined($stat_real) && $cond_lines > 1) {
2473 $stat_real = "[...]\n$stat_real";
2474 }
2475
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002476 #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 -07002477
2478 if ($check && (($sindent % 8) != 0 ||
2479 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002480 WARN("SUSPECT_CODE_INDENT",
2481 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002482 }
2483 }
2484
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002485 # Track the 'values' across context and added lines.
2486 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002487 my ($curr_values, $curr_vars) =
2488 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002489 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002490 if ($dbg_values) {
2491 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002492 print "$linenr > .$outline\n";
2493 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002494 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002495 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002496 $prev_values = substr($curr_values, -1);
2497
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002498#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002499 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002500
Andy Whitcroft653d4872007-06-23 17:16:34 -07002501# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002502 if ($dbg_type) {
2503 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002504 ERROR("TEST_TYPE",
2505 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002506 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002507 ERROR("TEST_NOT_TYPE",
2508 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002509 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002510 next;
2511 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002512# TEST: allow direct testing of the attribute matcher.
2513 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002514 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002515 ERROR("TEST_ATTR",
2516 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002517 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002518 ERROR("TEST_NOT_ATTR",
2519 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002520 }
2521 next;
2522 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002523
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002524# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002525 if ($line =~ /^.\s*{/ &&
2526 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002527 ERROR("OPEN_BRACE",
2528 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002529 }
2530
Andy Whitcroft653d4872007-06-23 17:16:34 -07002531#
2532# Checks which are anchored on the added line.
2533#
2534
2535# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002536 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002537 my $path = $1;
2538 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002539 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002540 "malformed #include filename\n" . $herecurr);
2541 }
2542 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2543 ERROR("UAPI_INCLUDE",
2544 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002545 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002546 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002547
2548# no C99 // comments
2549 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002550 if (ERROR("C99_COMMENTS",
2551 "do not use C99 // comments\n" . $herecurr) &&
2552 $fix) {
2553 my $line = $fixed[$linenr - 1];
2554 if ($line =~ /\/\/(.*)$/) {
2555 my $comment = trim($1);
2556 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2557 }
2558 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002559 }
2560 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002561 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002562 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002563
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002564# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2565# the whole statement.
2566#print "APW <$lines[$realline_next - 1]>\n";
2567 if (defined $realline_next &&
2568 exists $lines[$realline_next - 1] &&
2569 !defined $suppress_export{$realline_next} &&
2570 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2571 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002572 # Handle definitions which produce identifiers with
2573 # a prefix:
2574 # XXX(foo);
2575 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002576 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002577 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002578 $name =~ /^${Ident}_$2/) {
2579#print "FOO C name<$name>\n";
2580 $suppress_export{$realline_next} = 1;
2581
2582 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002583 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002584 ^.DEFINE_$Ident\(\Q$name\E\)|
2585 ^.DECLARE_$Ident\(\Q$name\E\)|
2586 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002587 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2588 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002589 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002590#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2591 $suppress_export{$realline_next} = 2;
2592 } else {
2593 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002594 }
2595 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002596 if (!defined $suppress_export{$linenr} &&
2597 $prevline =~ /^.\s*$/ &&
2598 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2599 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2600#print "FOO B <$lines[$linenr - 1]>\n";
2601 $suppress_export{$linenr} = 2;
2602 }
2603 if (defined $suppress_export{$linenr} &&
2604 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002605 WARN("EXPORT_SYMBOL",
2606 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002607 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002608
Joe Eloff5150bda2010-08-09 17:21:00 -07002609# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002610 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2611 if (ERROR("GLOBAL_INITIALISERS",
2612 "do not initialise globals to 0 or NULL\n" .
2613 $herecurr) &&
2614 $fix) {
2615 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2616 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002617 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002618# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002619 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2620 if (ERROR("INITIALISED_STATIC",
2621 "do not initialise statics to 0 or NULL\n" .
2622 $herecurr) &&
2623 $fix) {
2624 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2625 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002626 }
2627
Joe Perchescb710ec2010-10-26 14:23:20 -07002628# check for static const char * arrays.
2629 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002630 WARN("STATIC_CONST_CHAR_ARRAY",
2631 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002632 $herecurr);
2633 }
2634
2635# check for static char foo[] = "bar" declarations.
2636 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002637 WARN("STATIC_CONST_CHAR_ARRAY",
2638 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002639 $herecurr);
2640 }
2641
Joe Perches92e112f2013-12-13 11:36:22 -07002642# check for uses of DEFINE_PCI_DEVICE_TABLE
2643 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
2644 if (WARN("DEFINE_PCI_DEVICE_TABLE",
2645 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
2646 $fix) {
2647 $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
2648 }
Joe Perches93ed0e22010-10-26 14:23:21 -07002649 }
2650
Andy Whitcroft653d4872007-06-23 17:16:34 -07002651# check for new typedefs, only function parameters and sparse annotations
2652# make sense.
2653 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002654 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002655 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002656 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002657 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002658 WARN("NEW_TYPEDEFS",
2659 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002660 }
2661
2662# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002663 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002664 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2665 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002666 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002667
Andy Whitcroft65863862009-01-06 14:41:21 -08002668 # Should start with a space.
2669 $to =~ s/^(\S)/ $1/;
2670 # Should not end with a space.
2671 $to =~ s/\s+$//;
2672 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002673 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002674 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002675
Joe Perches3705ce52013-07-03 15:05:31 -07002676## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002677 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002678 if (ERROR("POINTER_LOCATION",
2679 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2680 $fix) {
2681 my $sub_from = $ident;
2682 my $sub_to = $ident;
2683 $sub_to =~ s/\Q$from\E/$to/;
2684 $fixed[$linenr - 1] =~
2685 s@\Q$sub_from\E@$sub_to@;
2686 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002687 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002688 }
2689 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2690 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002691 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002692
Andy Whitcroft65863862009-01-06 14:41:21 -08002693 # Should start with a space.
2694 $to =~ s/^(\S)/ $1/;
2695 # Should not end with a space.
2696 $to =~ s/\s+$//;
2697 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002698 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002699 }
2700 # Modifiers should have spaces.
2701 $to =~ s/(\b$Modifier$)/$1 /;
2702
Joe Perches3705ce52013-07-03 15:05:31 -07002703## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002704 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002705 if (ERROR("POINTER_LOCATION",
2706 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2707 $fix) {
2708
2709 my $sub_from = $match;
2710 my $sub_to = $match;
2711 $sub_to =~ s/\Q$from\E/$to/;
2712 $fixed[$linenr - 1] =~
2713 s@\Q$sub_from\E@$sub_to@;
2714 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002715 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002716 }
2717
2718# # no BUG() or BUG_ON()
2719# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2720# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2721# print "$herecurr";
2722# $clean = 0;
2723# }
2724
Andy Whitcroft8905a672007-11-28 16:21:06 -08002725 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002726 WARN("LINUX_VERSION_CODE",
2727 "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 -08002728 }
2729
Joe Perches17441222011-06-15 15:08:17 -07002730# check for uses of printk_ratelimit
2731 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002732 WARN("PRINTK_RATELIMITED",
2733"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002734 }
2735
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002736# printk should use KERN_* levels. Note that follow on printk's on the
2737# same line do not need a level, so we use the current block context
2738# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002739# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002740# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002741 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002742 my $ok = 0;
2743 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2744 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002745 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002746 # with "\n" ignore it, else it is to blame
2747 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2748 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2749 $ok = 1;
2750 }
2751 last;
2752 }
2753 }
2754 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002755 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2756 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002757 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002758 }
2759
Joe Perches243f3802012-05-31 16:26:09 -07002760 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2761 my $orig = $1;
2762 my $level = lc($orig);
2763 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002764 my $level2 = $level;
2765 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002766 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002767 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002768 }
2769
2770 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002771 if (WARN("PREFER_PR_LEVEL",
2772 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2773 $fix) {
2774 $fixed[$linenr - 1] =~
2775 s/\bpr_warning\b/pr_warn/;
2776 }
Joe Perches243f3802012-05-31 16:26:09 -07002777 }
2778
Joe Perchesdc139312013-02-21 16:44:13 -08002779 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2780 my $orig = $1;
2781 my $level = lc($orig);
2782 $level = "warn" if ($level eq "warning");
2783 $level = "dbg" if ($level eq "debug");
2784 WARN("PREFER_DEV_LEVEL",
2785 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2786 }
2787
Andy Whitcroft653d4872007-06-23 17:16:34 -07002788# function brace can't be on same line, except for #defines of do while,
2789# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002790 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2791 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002792 ERROR("OPEN_BRACE",
2793 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002794 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002795
Andy Whitcroft8905a672007-11-28 16:21:06 -08002796# open braces for enum, union and struct go on the same line.
2797 if ($line =~ /^.\s*{/ &&
2798 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002799 ERROR("OPEN_BRACE",
2800 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002801 }
2802
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002803# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002804 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2805 if (WARN("SPACING",
2806 "missing space after $1 definition\n" . $herecurr) &&
2807 $fix) {
2808 $fixed[$linenr - 1] =~
2809 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2810 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002811 }
2812
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002813# check for spacing round square brackets; allowed:
2814# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002815# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2816# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002817 while ($line =~ /(.*?\s)\[/g) {
2818 my ($where, $prefix) = ($-[1], $1);
2819 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002820 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002821 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002822 if (ERROR("BRACKET_SPACE",
2823 "space prohibited before open square bracket '['\n" . $herecurr) &&
2824 $fix) {
2825 $fixed[$linenr - 1] =~
2826 s/^(\+.*?)\s+\[/$1\[/;
2827 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002828 }
2829 }
2830
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002831# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002832 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002833 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002834 my $ctx_before = substr($line, 0, $-[1]);
2835 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002836
2837 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002838 if ($name =~ /^(?:
2839 if|for|while|switch|return|case|
2840 volatile|__volatile__|
2841 __attribute__|format|__extension__|
2842 asm|__asm__)$/x)
2843 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002844 # cpp #define statements have non-optional spaces, ie
2845 # if there is a space between the name and the open
2846 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002847 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002848
2849 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002850 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002851
2852 # If this whole things ends with a type its most
2853 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002854 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002855
2856 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002857 if (WARN("SPACING",
2858 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2859 $fix) {
2860 $fixed[$linenr - 1] =~
2861 s/\b$name\s+\(/$name\(/;
2862 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002863 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002864 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002865
Andy Whitcroft653d4872007-06-23 17:16:34 -07002866# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002867 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002868 my $fixed_line = "";
2869 my $line_fixed = 0;
2870
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002871 my $ops = qr{
2872 <<=|>>=|<=|>=|==|!=|
2873 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2874 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002875 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08002876 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002877 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002878 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002879
2880## print("element count: <" . $#elements . ">\n");
2881## foreach my $el (@elements) {
2882## print("el: <$el>\n");
2883## }
2884
2885 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002886 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002887
Joe Perches3705ce52013-07-03 15:05:31 -07002888 foreach my $el (@elements) {
2889 push(@fix_elements, substr($rawline, $off, length($el)));
2890 $off += length($el);
2891 }
2892
2893 $off = 0;
2894
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002895 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07002896 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002897
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002898 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002899
2900 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2901
2902## print("n: <$n> good: <$good>\n");
2903
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002904 $off += length($elements[$n]);
2905
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002906 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002907 my $ca = substr($opline, 0, $off);
2908 my $cc = '';
2909 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2910 $cc = substr($opline, $off + length($elements[$n + 1]));
2911 }
2912 my $cb = "$ca$;$cc";
2913
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002914 my $a = '';
2915 $a = 'V' if ($elements[$n] ne '');
2916 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002917 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002918 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2919 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002920 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002921
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002922 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002923
2924 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002925 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002926 $c = 'V' if ($elements[$n + 2] ne '');
2927 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002928 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002929 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2930 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002931 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002932 } else {
2933 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002934 }
2935
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002936 my $ctx = "${a}x${c}";
2937
2938 my $at = "(ctx:$ctx)";
2939
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002940 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002941 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002942
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002943 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002944 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002945
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002946 # Get the full operator variant.
2947 my $opv = $op . substr($curr_vars, $off, 1);
2948
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002949 # Ignore operators passed as parameters.
2950 if ($op_type ne 'V' &&
2951 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2952
Andy Whitcroftcf655042008-03-04 14:28:20 -08002953# # Ignore comments
2954# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002955
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002956 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002957 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002958 if ($ctx !~ /.x[WEBC]/ &&
2959 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002960 if (ERROR("SPACING",
2961 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002962 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002963 $line_fixed = 1;
2964 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002965 }
2966
2967 # // is a comment
2968 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002969
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002970 # No spaces for:
2971 # ->
2972 # : when part of a bitfield
2973 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002974 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002975 if (ERROR("SPACING",
2976 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002977 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002978 if (defined $fix_elements[$n + 2]) {
2979 $fix_elements[$n + 2] =~ s/^\s+//;
2980 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002981 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002982 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002983 }
2984
2985 # , must have a space on the right.
2986 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002987 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002988 if (ERROR("SPACING",
2989 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002990 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002991 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07002992 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07002993 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002994 }
2995
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002996 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002997 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002998 #warn "'*' is part of type\n";
2999
3000 # unary operators should have a space before and
3001 # none after. May be left adjacent to another
3002 # unary operator, or a cast
3003 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003004 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003005 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003006 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003007 if (ERROR("SPACING",
3008 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003009 if ($n != $last_after + 2) {
3010 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3011 $line_fixed = 1;
3012 }
Joe Perches3705ce52013-07-03 15:05:31 -07003013 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003014 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003015 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003016 # A unary '*' may be const
3017
3018 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003019 if (ERROR("SPACING",
3020 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003021 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003022 if (defined $fix_elements[$n + 2]) {
3023 $fix_elements[$n + 2] =~ s/^\s+//;
3024 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003025 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003026 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003027 }
3028
3029 # unary ++ and unary -- are allowed no space on one side.
3030 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003031 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003032 if (ERROR("SPACING",
3033 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003034 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003035 $line_fixed = 1;
3036 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003037 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003038 if ($ctx =~ /Wx[BE]/ ||
3039 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003040 if (ERROR("SPACING",
3041 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003042 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003043 $line_fixed = 1;
3044 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003045 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003046 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003047 if (ERROR("SPACING",
3048 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003049 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003050 if (defined $fix_elements[$n + 2]) {
3051 $fix_elements[$n + 2] =~ s/^\s+//;
3052 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003053 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003054 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003055 }
3056
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003057 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003058 } elsif ($op eq '<<' or $op eq '>>' or
3059 $op eq '&' or $op eq '^' or $op eq '|' or
3060 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003061 $op eq '*' or $op eq '/' or
3062 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003063 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003064 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003065 if (ERROR("SPACING",
3066 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003067 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3068 if (defined $fix_elements[$n + 2]) {
3069 $fix_elements[$n + 2] =~ s/^\s+//;
3070 }
Joe Perches3705ce52013-07-03 15:05:31 -07003071 $line_fixed = 1;
3072 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003073 }
3074
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003075 # A colon needs no spaces before when it is
3076 # terminating a case value or a label.
3077 } elsif ($opv eq ':C' || $opv eq ':L') {
3078 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003079 if (ERROR("SPACING",
3080 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003081 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003082 $line_fixed = 1;
3083 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003084 }
3085
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003086 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003087 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003088 my $ok = 0;
3089
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003090 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003091 if (($op eq '<' &&
3092 $cc =~ /^\S+\@\S+>/) ||
3093 ($op eq '>' &&
3094 $ca =~ /<\S+\@\S+$/))
3095 {
3096 $ok = 1;
3097 }
3098
Joe Perches84731622013-11-12 15:10:05 -08003099 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003100 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003101 my $msg_type = \&ERROR;
3102 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3103
3104 if (&{$msg_type}("SPACING",
3105 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003106 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3107 if (defined $fix_elements[$n + 2]) {
3108 $fix_elements[$n + 2] =~ s/^\s+//;
3109 }
Joe Perches3705ce52013-07-03 15:05:31 -07003110 $line_fixed = 1;
3111 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003113 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003114 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003115
3116## print("n: <$n> GOOD: <$good>\n");
3117
3118 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003119 }
Joe Perches3705ce52013-07-03 15:05:31 -07003120
3121 if (($#elements % 2) == 0) {
3122 $fixed_line = $fixed_line . $fix_elements[$#elements];
3123 }
3124
3125 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3126 $fixed[$linenr - 1] = $fixed_line;
3127 }
3128
3129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003130 }
3131
Joe Perches786b6322013-07-03 15:05:32 -07003132# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003133 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003134 if (WARN("SPACING",
3135 "space prohibited before semicolon\n" . $herecurr) &&
3136 $fix) {
3137 1 while $fixed[$linenr - 1] =~
3138 s/^(\+.*\S)\s+;/$1;/;
3139 }
3140 }
3141
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003142# check for multiple assignments
3143 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003144 CHK("MULTIPLE_ASSIGNMENTS",
3145 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003146 }
3147
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003148## # check for multiple declarations, allowing for a function declaration
3149## # continuation.
3150## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3151## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3152##
3153## # Remove any bracketed sections to ensure we do not
3154## # falsly report the parameters of functions.
3155## my $ln = $line;
3156## while ($ln =~ s/\([^\(\)]*\)//g) {
3157## }
3158## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003159## WARN("MULTIPLE_DECLARATION",
3160## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003161## }
3162## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003163
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003164#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003165 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3166 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003167 if (ERROR("SPACING",
3168 "space required before the open brace '{'\n" . $herecurr) &&
3169 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003170 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003171 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003172 }
3173
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003174## # check for blank lines before declarations
3175## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3176## $prevrawline =~ /^.\s*$/) {
3177## WARN("SPACING",
3178## "No blank lines before declarations\n" . $hereprev);
3179## }
3180##
3181
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003182# closing brace should have a space following it when it has anything
3183# on the line
3184 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003185 if (ERROR("SPACING",
3186 "space required after that close brace '}'\n" . $herecurr) &&
3187 $fix) {
3188 $fixed[$linenr - 1] =~
3189 s/}((?!(?:,|;|\)))\S)/} $1/;
3190 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003191 }
3192
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003193# check spacing on square brackets
3194 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003195 if (ERROR("SPACING",
3196 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3197 $fix) {
3198 $fixed[$linenr - 1] =~
3199 s/\[\s+/\[/;
3200 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003201 }
3202 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003203 if (ERROR("SPACING",
3204 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3205 $fix) {
3206 $fixed[$linenr - 1] =~
3207 s/\s+\]/\]/;
3208 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003209 }
3210
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003211# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003212 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3213 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003214 if (ERROR("SPACING",
3215 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3216 $fix) {
3217 $fixed[$linenr - 1] =~
3218 s/\(\s+/\(/;
3219 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003220 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003221 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003222 $line !~ /for\s*\(.*;\s+\)/ &&
3223 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003224 if (ERROR("SPACING",
3225 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3226 $fix) {
3227 $fixed[$linenr - 1] =~
3228 s/\s+\)/\)/;
3229 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003230 }
3231
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003232#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003233 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003234 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003235 if (WARN("INDENTED_LABEL",
3236 "labels should not be indented\n" . $herecurr) &&
3237 $fix) {
3238 $fixed[$linenr - 1] =~
3239 s/^(.)\s+/$1/;
3240 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003241 }
3242
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003243# Return is not a function.
Joe Perches507e5142013-11-12 15:10:13 -08003244 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003245 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003246 if ($^V && $^V ge 5.10.0 &&
3247 $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003248 ERROR("RETURN_PARENTHESES",
3249 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003250
3251 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003252 ERROR("SPACING",
3253 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003254 }
3255 }
Joe Perches507e5142013-11-12 15:10:13 -08003256
Joe Perches189248d2014-01-23 15:54:47 -08003257# if statements using unnecessary parentheses - ie: if ((foo == bar))
3258 if ($^V && $^V ge 5.10.0 &&
3259 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3260 my $openparens = $1;
3261 my $count = $openparens =~ tr@\(@\(@;
3262 my $msg = "";
3263 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3264 my $comp = $4; #Not $1 because of $LvalOrFunc
3265 $msg = " - maybe == should be = ?" if ($comp eq "==");
3266 WARN("UNNECESSARY_PARENTHESES",
3267 "Unnecessary parentheses$msg\n" . $herecurr);
3268 }
3269 }
3270
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003271# Return of what appears to be an errno should normally be -'ve
3272 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3273 my $name = $1;
3274 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003275 WARN("USE_NEGATIVE_ERRNO",
3276 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003277 }
3278 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003279
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003280# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003281 if ($line =~ /\b(if|while|for|switch)\(/) {
3282 if (ERROR("SPACING",
3283 "space required before the open parenthesis '('\n" . $herecurr) &&
3284 $fix) {
3285 $fixed[$linenr - 1] =~
3286 s/\b(if|while|for|switch)\(/$1 \(/;
3287 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003288 }
3289
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003290# Check for illegal assignment in if conditional -- and check for trailing
3291# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003292 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003293 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3294 ctx_statement_block($linenr, $realcnt, 0)
3295 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003296 my ($stat_next) = ctx_statement_block($line_nr_next,
3297 $remain_next, $off_next);
3298 $stat_next =~ s/\n./\n /g;
3299 ##print "stat<$stat> stat_next<$stat_next>\n";
3300
3301 if ($stat_next =~ /^\s*while\b/) {
3302 # If the statement carries leading newlines,
3303 # then count those as offsets.
3304 my ($whitespace) =
3305 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3306 my $offset =
3307 statement_rawlines($whitespace) - 1;
3308
3309 $suppress_whiletrailers{$line_nr_next +
3310 $offset} = 1;
3311 }
3312 }
3313 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003314 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003315 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003316 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003317
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003318 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003319 ERROR("ASSIGN_IN_IF",
3320 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003321 }
3322
3323 # Find out what is on the end of the line after the
3324 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003325 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003326 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003327 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003328 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3329 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003330 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003331 # Find out how long the conditional actually is.
3332 my @newlines = ($c =~ /\n/gs);
3333 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003334 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003335
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003336 $stat_real = raw_line($linenr, $cond_lines)
3337 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003338 if (defined($stat_real) && $cond_lines > 1) {
3339 $stat_real = "[...]\n$stat_real";
3340 }
3341
Joe Perches000d1cc12011-07-25 17:13:25 -07003342 ERROR("TRAILING_STATEMENTS",
3343 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003344 }
3345 }
3346
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003347# Check for bitwise tests written as boolean
3348 if ($line =~ /
3349 (?:
3350 (?:\[|\(|\&\&|\|\|)
3351 \s*0[xX][0-9]+\s*
3352 (?:\&\&|\|\|)
3353 |
3354 (?:\&\&|\|\|)
3355 \s*0[xX][0-9]+\s*
3356 (?:\&\&|\|\||\)|\])
3357 )/x)
3358 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003359 WARN("HEXADECIMAL_BOOLEAN_TEST",
3360 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003361 }
3362
Andy Whitcroft8905a672007-11-28 16:21:06 -08003363# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003364 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3365 my $s = $1;
3366 $s =~ s/$;//g; # Remove any comments
3367 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003368 ERROR("TRAILING_STATEMENTS",
3369 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003370 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003371 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003372# if should not continue a brace
3373 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003374 ERROR("TRAILING_STATEMENTS",
3375 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003376 $herecurr);
3377 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003378# case and default should not have general statements after them
3379 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3380 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003381 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003382 \s*return\s+
3383 )/xg)
3384 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003385 ERROR("TRAILING_STATEMENTS",
3386 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003387 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003388
3389 # Check for }<nl>else {, these must be at the same
3390 # indent level to be relevant to each other.
3391 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3392 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003393 ERROR("ELSE_AFTER_BRACE",
3394 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003395 }
3396
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003397 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3398 $previndent == $indent) {
3399 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3400
3401 # Find out what is on the end of the line after the
3402 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003403 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003404 $s =~ s/\n.*//g;
3405
3406 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003407 ERROR("WHILE_AFTER_BRACE",
3408 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003409 }
3410 }
3411
Joe Perches95e2c602013-07-03 15:05:20 -07003412#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003413 while ($line =~ m{($Constant|$Lval)}g) {
3414 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003415
3416#gcc binary extension
3417 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003418 if (WARN("GCC_BINARY_CONSTANT",
3419 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3420 $fix) {
3421 my $hexval = sprintf("0x%x", oct($var));
3422 $fixed[$linenr - 1] =~
3423 s/\b$var\b/$hexval/;
3424 }
Joe Perches95e2c602013-07-03 15:05:20 -07003425 }
3426
3427#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003428 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003429 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003430#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003431 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003432#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003433 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003434 while ($var =~ m{($Ident)}g) {
3435 my $word = $1;
3436 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003437 if ($check) {
3438 seed_camelcase_includes();
3439 if (!$file && !$camelcase_file_seeded) {
3440 seed_camelcase_file($realfile);
3441 $camelcase_file_seeded = 1;
3442 }
3443 }
Joe Perches7e781f62013-09-11 14:23:55 -07003444 if (!defined $camelcase{$word}) {
3445 $camelcase{$word} = 1;
3446 CHK("CAMELCASE",
3447 "Avoid CamelCase: <$word>\n" . $herecurr);
3448 }
Joe Perches34456862013-07-03 15:05:34 -07003449 }
Joe Perches323c1262012-12-17 16:02:07 -08003450 }
3451 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003452
3453#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003454 if ($line =~ /\#\s*define.*\\\s+$/) {
3455 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3456 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3457 $fix) {
3458 $fixed[$linenr - 1] =~ s/\s+$//;
3459 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003460 }
3461
Andy Whitcroft653d4872007-06-23 17:16:34 -07003462#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003463 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003464 my $file = "$1.h";
3465 my $checkfile = "include/linux/$file";
3466 if (-f "$root/$checkfile" &&
3467 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003468 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003469 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003470 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003471 CHK("ARCH_INCLUDE_LINUX",
3472 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003473 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003474 WARN("INCLUDE_LINUX",
3475 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003476 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003477 }
3478 }
3479
Andy Whitcroft653d4872007-06-23 17:16:34 -07003480# multi-statement macros should be enclosed in a do while loop, grab the
3481# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003482# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003483 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3484 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003485 my $ln = $linenr;
3486 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003487 my ($off, $dstat, $dcond, $rest);
3488 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003489 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003490 ctx_statement_block($linenr, $realcnt, 0);
3491 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003492 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003493 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003494
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003495 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003496 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003497 $dstat =~ s/\\\n.//g;
3498 $dstat =~ s/^\s*//s;
3499 $dstat =~ s/\s*$//s;
3500
3501 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003502 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3503 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003504 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003505 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003506 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003507
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003508 # Flatten any obvious string concatentation.
3509 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3510 $dstat =~ s/$Ident\s*("X*")/$1/)
3511 {
3512 }
3513
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003514 my $exceptions = qr{
3515 $Declare|
3516 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003517 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003518 DECLARE_PER_CPU|
3519 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003520 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003521 union|
3522 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003523 \.$Ident\s*=\s*|
3524 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003525 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003526 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003527 if ($dstat ne '' &&
3528 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3529 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003530 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003531 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003532 $dstat !~ /$exceptions/ &&
3533 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003534 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003535 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003536 $dstat !~ /^for\s*$Constant$/ && # for (...)
3537 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3538 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003539 $dstat !~ /^\({/ && # ({...
3540 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003541 {
3542 $ctx =~ s/\n*$//;
3543 my $herectx = $here . "\n";
3544 my $cnt = statement_rawlines($ctx);
3545
3546 for (my $n = 0; $n < $cnt; $n++) {
3547 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003548 }
3549
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003550 if ($dstat =~ /;/) {
3551 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3552 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3553 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003554 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003555 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003556 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003557 }
Joe Perches5023d342012-12-17 16:01:47 -08003558
Joe Perches481eb482012-12-17 16:01:56 -08003559# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003560
3561 } else {
3562 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003563 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3564 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003565 $line =~ /^\+.*\\$/) {
3566 WARN("LINE_CONTINUATIONS",
3567 "Avoid unnecessary line continuations\n" . $herecurr);
3568 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003569 }
3570
Joe Perchesb13edf72012-07-30 14:41:24 -07003571# do {} while (0) macro tests:
3572# single-statement macros do not need to be enclosed in do while (0) loop,
3573# macro should not end with a semicolon
3574 if ($^V && $^V ge 5.10.0 &&
3575 $realfile !~ m@/vmlinux.lds.h$@ &&
3576 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3577 my $ln = $linenr;
3578 my $cnt = $realcnt;
3579 my ($off, $dstat, $dcond, $rest);
3580 my $ctx = '';
3581 ($dstat, $dcond, $ln, $cnt, $off) =
3582 ctx_statement_block($linenr, $realcnt, 0);
3583 $ctx = $dstat;
3584
3585 $dstat =~ s/\\\n.//g;
3586
3587 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3588 my $stmts = $2;
3589 my $semis = $3;
3590
3591 $ctx =~ s/\n*$//;
3592 my $cnt = statement_rawlines($ctx);
3593 my $herectx = $here . "\n";
3594
3595 for (my $n = 0; $n < $cnt; $n++) {
3596 $herectx .= raw_line($linenr, $n) . "\n";
3597 }
3598
Joe Perchesac8e97f2012-08-21 16:15:53 -07003599 if (($stmts =~ tr/;/;/) == 1 &&
3600 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003601 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3602 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3603 }
3604 if (defined $semis && $semis ne "") {
3605 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3606 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3607 }
3608 }
3609 }
3610
Mike Frysinger080ba922009-01-06 14:41:25 -08003611# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3612# all assignments may have only one of the following with an assignment:
3613# .
3614# ALIGN(...)
3615# VMLINUX_SYMBOL(...)
3616 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003617 WARN("MISSING_VMLINUX_SYMBOL",
3618 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003619 }
3620
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003621# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003622 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3623 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003624 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003625 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003626 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3627 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003628 my @allowed = ();
3629 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003630 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003631 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003632 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003633 for my $chunk (@chunks) {
3634 my ($cond, $block) = @{$chunk};
3635
Andy Whitcroft773647a2008-03-28 14:15:58 -07003636 # If the condition carries leading newlines, then count those as offsets.
3637 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3638 my $offset = statement_rawlines($whitespace) - 1;
3639
Joe Perchesaad4f612012-03-23 15:02:19 -07003640 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003641 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3642
3643 # We have looked at and allowed this specific line.
3644 $suppress_ifbraces{$ln + $offset} = 1;
3645
3646 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003647 $ln += statement_rawlines($block) - 1;
3648
Andy Whitcroft773647a2008-03-28 14:15:58 -07003649 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003650
3651 $seen++ if ($block =~ /^\s*{/);
3652
Joe Perchesaad4f612012-03-23 15:02:19 -07003653 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003654 if (statement_lines($cond) > 1) {
3655 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003656 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003657 }
3658 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003659 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003660 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003661 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003662 if (statement_block_size($block) > 1) {
3663 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003664 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003665 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003666 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003667 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003668 if ($seen) {
3669 my $sum_allowed = 0;
3670 foreach (@allowed) {
3671 $sum_allowed += $_;
3672 }
3673 if ($sum_allowed == 0) {
3674 WARN("BRACES",
3675 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3676 } elsif ($sum_allowed != $allow &&
3677 $seen != $allow) {
3678 CHK("BRACES",
3679 "braces {} should be used on all arms of this statement\n" . $herectx);
3680 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003681 }
3682 }
3683 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003684 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003685 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003686 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003687
Andy Whitcroftcf655042008-03-04 14:28:20 -08003688 # Check the pre-context.
3689 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3690 #print "APW: ALLOWED: pre<$1>\n";
3691 $allowed = 1;
3692 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003693
3694 my ($level, $endln, @chunks) =
3695 ctx_statement_full($linenr, $realcnt, $-[0]);
3696
Andy Whitcroftcf655042008-03-04 14:28:20 -08003697 # Check the condition.
3698 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003699 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003700 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003701 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003702 }
3703 if (statement_lines($cond) > 1) {
3704 #print "APW: ALLOWED: cond<$cond>\n";
3705 $allowed = 1;
3706 }
3707 if ($block =~/\b(?:if|for|while)\b/) {
3708 #print "APW: ALLOWED: block<$block>\n";
3709 $allowed = 1;
3710 }
3711 if (statement_block_size($block) > 1) {
3712 #print "APW: ALLOWED: lines block<$block>\n";
3713 $allowed = 1;
3714 }
3715 # Check the post-context.
3716 if (defined $chunks[1]) {
3717 my ($cond, $block) = @{$chunks[1]};
3718 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003719 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003720 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003721 if ($block =~ /^\s*\{/) {
3722 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3723 $allowed = 1;
3724 }
3725 }
3726 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003727 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003728 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003729
Andy Whitcroftf0556632008-10-15 22:02:23 -07003730 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003731 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003732 }
3733
Joe Perches000d1cc12011-07-25 17:13:25 -07003734 WARN("BRACES",
3735 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003736 }
3737 }
3738
Joe Perches0979ae62012-12-17 16:01:59 -08003739# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003740 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003741 CHK("BRACES",
3742 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3743 }
Joe Perches77b9a532013-07-03 15:05:29 -07003744 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003745 CHK("BRACES",
3746 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3747 }
3748
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003749# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003750 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3751 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003752 WARN("VOLATILE",
3753 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003754 }
3755
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003756# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003757 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003758 CHK("REDUNDANT_CODE",
3759 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003760 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003761 }
3762
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003763# check for needless "if (<foo>) fn(<foo>)" uses
3764 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3765 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3766 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3767 WARN('NEEDLESS_IF',
3768 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003769 }
3770 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003771
Joe Perches8716de32013-09-11 14:24:05 -07003772# check for bad placement of section $InitAttribute (e.g.: __initdata)
3773 if ($line =~ /(\b$InitAttribute\b)/) {
3774 my $attr = $1;
3775 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3776 my $ptr = $1;
3777 my $var = $2;
3778 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3779 ERROR("MISPLACED_INIT",
3780 "$attr should be placed after $var\n" . $herecurr)) ||
3781 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3782 WARN("MISPLACED_INIT",
3783 "$attr should be placed after $var\n" . $herecurr))) &&
3784 $fix) {
3785 $fixed[$linenr - 1] =~ 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;
3786 }
3787 }
3788 }
3789
Joe Perchese970b8842013-11-12 15:10:10 -08003790# check for $InitAttributeData (ie: __initdata) with const
3791 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3792 my $attr = $1;
3793 $attr =~ /($InitAttributePrefix)(.*)/;
3794 my $attr_prefix = $1;
3795 my $attr_type = $2;
3796 if (ERROR("INIT_ATTRIBUTE",
3797 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3798 $fix) {
3799 $fixed[$linenr - 1] =~
3800 s/$InitAttributeData/${attr_prefix}initconst/;
3801 }
3802 }
3803
3804# check for $InitAttributeConst (ie: __initconst) without const
3805 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3806 my $attr = $1;
3807 if (ERROR("INIT_ATTRIBUTE",
3808 "Use of $attr requires a separate use of const\n" . $herecurr) &&
3809 $fix) {
3810 my $lead = $fixed[$linenr - 1] =~
3811 /(^\+\s*(?:static\s+))/;
3812 $lead = rtrim($1);
3813 $lead = "$lead " if ($lead !~ /^\+$/);
3814 $lead = "${lead}const ";
3815 $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3816 }
3817 }
3818
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003819# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003820 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003821 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003822 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003823 CHK("USLEEP_RANGE",
3824 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003825 }
3826 }
3827
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003828# warn about unexpectedly long msleep's
3829 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3830 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003831 WARN("MSLEEP",
3832 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003833 }
3834 }
3835
Joe Perches36ec1932013-07-03 15:05:25 -07003836# check for comparisons of jiffies
3837 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3838 WARN("JIFFIES_COMPARISON",
3839 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3840 }
3841
Joe Perches9d7a34a2013-07-03 15:05:26 -07003842# check for comparisons of get_jiffies_64()
3843 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3844 WARN("JIFFIES_COMPARISON",
3845 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3846 }
3847
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003848# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003849# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003850# print "#ifdef in C files should be avoided\n";
3851# print "$herecurr";
3852# $clean = 0;
3853# }
3854
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003855# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003856 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003857 if (ERROR("SPACING",
3858 "exactly one space required after that #$1\n" . $herecurr) &&
3859 $fix) {
3860 $fixed[$linenr - 1] =~
3861 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3862 }
3863
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003864 }
3865
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003866# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003867 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3868 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003869 my $which = $1;
3870 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003871 CHK("UNCOMMENTED_DEFINITION",
3872 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003873 }
3874 }
3875# check for memory barriers without a comment.
3876 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3877 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08003878 WARN("MEMORY_BARRIER",
3879 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003880 }
3881 }
3882# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003883 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003884 CHK("ARCH_DEFINES",
3885 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003886 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003887
Tobias Klauserd4977c72010-05-24 14:33:30 -07003888# Check that the storage class is at the beginning of a declaration
3889 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003890 WARN("STORAGE_CLASS",
3891 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003892 }
3893
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003894# check the location of the inline attribute, that it is between
3895# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003896 if ($line =~ /\b$Type\s+$Inline\b/ ||
3897 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003898 ERROR("INLINE_LOCATION",
3899 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003900 }
3901
Andy Whitcroft8905a672007-11-28 16:21:06 -08003902# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08003903 if ($realfile !~ m@\binclude/uapi/@ &&
3904 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003905 if (WARN("INLINE",
3906 "plain inline is preferred over $1\n" . $herecurr) &&
3907 $fix) {
3908 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3909
3910 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003911 }
3912
Joe Perches3d130fd2011-01-12 17:00:00 -08003913# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08003914 if ($realfile !~ m@\binclude/uapi/@ &&
3915 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003916 WARN("PREFER_PACKED",
3917 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003918 }
3919
Joe Perches39b7e282011-07-25 17:13:24 -07003920# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08003921 if ($realfile !~ m@\binclude/uapi/@ &&
3922 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003923 WARN("PREFER_ALIGNED",
3924 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003925 }
3926
Joe Perches5f14d3b2012-01-10 15:09:52 -08003927# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08003928 if ($realfile !~ m@\binclude/uapi/@ &&
3929 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003930 if (WARN("PREFER_PRINTF",
3931 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3932 $fix) {
3933 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3934
3935 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003936 }
3937
Joe Perches6061d942012-03-23 15:02:16 -07003938# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08003939 if ($realfile !~ m@\binclude/uapi/@ &&
3940 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003941 if (WARN("PREFER_SCANF",
3942 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3943 $fix) {
3944 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3945 }
Joe Perches6061d942012-03-23 15:02:16 -07003946 }
3947
Joe Perches8f53a9b2010-03-05 13:43:48 -08003948# check for sizeof(&)
3949 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003950 WARN("SIZEOF_ADDRESS",
3951 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003952 }
3953
Joe Perches66c80b62012-07-30 14:41:22 -07003954# check for sizeof without parenthesis
3955 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003956 if (WARN("SIZEOF_PARENTHESIS",
3957 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3958 $fix) {
3959 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3960 }
Joe Perches66c80b62012-07-30 14:41:22 -07003961 }
3962
Joe Perches428e2fd2011-05-24 17:13:39 -07003963# check for line continuations in quoted strings with odd counts of "
3964 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003965 WARN("LINE_CONTINUATIONS",
3966 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003967 }
3968
Joe Perches88982fe2012-12-17 16:02:00 -08003969# check for struct spinlock declarations
3970 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3971 WARN("USE_SPINLOCK_T",
3972 "struct spinlock should be spinlock_t\n" . $herecurr);
3973 }
3974
Joe Perchesa6962d72013-04-29 16:18:13 -07003975# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08003976 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07003977 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08003978 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003979 if (WARN("PREFER_SEQ_PUTS",
3980 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3981 $fix) {
3982 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3983 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003984 }
3985 }
3986
Andy Whitcroft554e1652012-01-10 15:09:57 -08003987# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003988 if ($^V && $^V ge 5.10.0 &&
3989 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003990 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003991
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003992 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003993 my $ms_val = $7;
3994 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003995
Andy Whitcroft554e1652012-01-10 15:09:57 -08003996 if ($ms_size =~ /^(0x|)0$/i) {
3997 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003998 "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 -08003999 } elsif ($ms_size =~ /^(0x|)1$/i) {
4000 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004001 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4002 }
4003 }
4004
4005# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004006 if ($^V && $^V ge 5.10.0 &&
4007 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004008 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004009 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004010 my $call = $1;
4011 my $cast1 = deparenthesize($2);
4012 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004013 my $cast2 = deparenthesize($7);
4014 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004015 my $cast;
4016
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004017 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004018 $cast = "$cast1 or $cast2";
4019 } elsif ($cast1 ne "") {
4020 $cast = $cast1;
4021 } else {
4022 $cast = $cast2;
4023 }
4024 WARN("MINMAX",
4025 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004026 }
4027 }
4028
Joe Perches4a273192012-07-30 14:41:20 -07004029# check usleep_range arguments
4030 if ($^V && $^V ge 5.10.0 &&
4031 defined $stat &&
4032 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4033 my $min = $1;
4034 my $max = $7;
4035 if ($min eq $max) {
4036 WARN("USLEEP_RANGE",
4037 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4038 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4039 $min > $max) {
4040 WARN("USLEEP_RANGE",
4041 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4042 }
4043 }
4044
Joe Perches823b7942013-11-12 15:10:15 -08004045# check for naked sscanf
4046 if ($^V && $^V ge 5.10.0 &&
4047 defined $stat &&
4048 $stat =~ /\bsscanf\b/ &&
4049 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4050 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4051 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4052 my $lc = $stat =~ tr@\n@@;
4053 $lc = $lc + $linenr;
4054 my $stat_real = raw_line($linenr, 0);
4055 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4056 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4057 }
4058 WARN("NAKED_SSCANF",
4059 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4060 }
4061
Joe Perches70dc8a42013-09-11 14:23:58 -07004062# check for new externs in .h files.
4063 if ($realfile =~ /\.h$/ &&
4064 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004065 if (CHK("AVOID_EXTERNS",
4066 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004067 $fix) {
4068 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4069 }
4070 }
4071
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004072# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004073 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004074 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004075 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004076 my $function_name = $1;
4077 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004078
4079 my $s = $stat;
4080 if (defined $cond) {
4081 substr($s, 0, length($cond), '');
4082 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004083 if ($s =~ /^\s*;/ &&
4084 $function_name ne 'uninitialized_var')
4085 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004086 WARN("AVOID_EXTERNS",
4087 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004088 }
4089
4090 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004091 WARN("FUNCTION_ARGUMENTS",
4092 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004093 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004094
4095 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4096 $stat =~ /^.\s*extern\s+/)
4097 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004098 WARN("AVOID_EXTERNS",
4099 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004100 }
4101
4102# checks for new __setup's
4103 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4104 my $name = $1;
4105
4106 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004107 CHK("UNDOCUMENTED_SETUP",
4108 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004109 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004110 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004111
4112# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004113 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004114 WARN("UNNECESSARY_CASTS",
4115 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004116 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004117
Joe Perchesa640d252013-07-03 15:05:21 -07004118# alloc style
4119# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4120 if ($^V && $^V ge 5.10.0 &&
4121 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4122 CHK("ALLOC_SIZEOF_STRUCT",
4123 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4124 }
4125
Joe Perches972fdea2013-04-29 16:18:12 -07004126# check for krealloc arg reuse
4127 if ($^V && $^V ge 5.10.0 &&
4128 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4129 WARN("KREALLOC_ARG_REUSE",
4130 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4131 }
4132
Joe Perches5ce59ae2013-02-21 16:44:18 -08004133# check for alloc argument mismatch
4134 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4135 WARN("ALLOC_ARRAY_ARGS",
4136 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4137 }
4138
David Rientjes7e4915e2014-01-23 15:54:42 -08004139# check for GFP_NOWAIT use
4140 if ($line =~ /\b__GFP_NOFAIL\b/) {
4141 WARN("__GFP_NOFAIL",
4142 "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr);
4143 }
4144
Joe Perchescaf2a542011-01-12 16:59:56 -08004145# check for multiple semicolons
4146 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004147 if (WARN("ONE_SEMICOLON",
4148 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4149 $fix) {
4150 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4151 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004152 }
4153
Joe Perchesc34c09a2014-01-23 15:54:43 -08004154# check for case / default statements not preceeded by break/fallthrough/switch
4155 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4156 my $has_break = 0;
4157 my $has_statement = 0;
4158 my $count = 0;
4159 my $prevline = $linenr;
4160 while ($prevline > 1 && $count < 3 && !$has_break) {
4161 $prevline--;
4162 my $rline = $rawlines[$prevline - 1];
4163 my $fline = $lines[$prevline - 1];
4164 last if ($fline =~ /^\@\@/);
4165 next if ($fline =~ /^\-/);
4166 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4167 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4168 next if ($fline =~ /^.[\s$;]*$/);
4169 $has_statement = 1;
4170 $count++;
4171 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4172 }
4173 if (!$has_break && $has_statement) {
4174 WARN("MISSING_BREAK",
4175 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4176 }
4177 }
4178
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004179# check for switch/default statements without a break;
4180 if ($^V && $^V ge 5.10.0 &&
4181 defined $stat &&
4182 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4183 my $ctx = '';
4184 my $herectx = $here . "\n";
4185 my $cnt = statement_rawlines($stat);
4186 for (my $n = 0; $n < $cnt; $n++) {
4187 $herectx .= raw_line($linenr, $n) . "\n";
4188 }
4189 WARN("DEFAULT_NO_BREAK",
4190 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004191 }
4192
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004193# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004194 if ($line =~ /\b__FUNCTION__\b/) {
4195 if (WARN("USE_FUNC",
4196 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4197 $fix) {
4198 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4199 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004200 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004201
Joe Perches2c924882012-03-23 15:02:20 -07004202# check for use of yield()
4203 if ($line =~ /\byield\s*\(\s*\)/) {
4204 WARN("YIELD",
4205 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4206 }
4207
Joe Perches179f8f42013-07-03 15:05:30 -07004208# check for comparisons against true and false
4209 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4210 my $lead = $1;
4211 my $arg = $2;
4212 my $test = $3;
4213 my $otype = $4;
4214 my $trail = $5;
4215 my $op = "!";
4216
4217 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4218
4219 my $type = lc($otype);
4220 if ($type =~ /^(?:true|false)$/) {
4221 if (("$test" eq "==" && "$type" eq "true") ||
4222 ("$test" eq "!=" && "$type" eq "false")) {
4223 $op = "";
4224 }
4225
4226 CHK("BOOL_COMPARISON",
4227 "Using comparison to $otype is error prone\n" . $herecurr);
4228
4229## maybe suggesting a correct construct would better
4230## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4231
4232 }
4233 }
4234
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004235# check for semaphores initialized locked
4236 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004237 WARN("CONSIDER_COMPLETION",
4238 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004239 }
Joe Perches6712d852012-03-23 15:02:20 -07004240
Joe Perches67d0a072011-10-31 17:13:10 -07004241# recommend kstrto* over simple_strto* and strict_strto*
4242 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004243 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004244 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004245 }
Joe Perches6712d852012-03-23 15:02:20 -07004246
Michael Ellermanf3db6632008-07-23 21:28:57 -07004247# check for __initcall(), use device_initcall() explicitly please
4248 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004249 WARN("USE_DEVICE_INITCALL",
4250 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004251 }
Joe Perches6712d852012-03-23 15:02:20 -07004252
Emese Revfy79404842010-03-05 13:43:53 -08004253# check for various ops structs, ensure they are const.
4254 my $struct_ops = qr{acpi_dock_ops|
4255 address_space_operations|
4256 backlight_ops|
4257 block_device_operations|
4258 dentry_operations|
4259 dev_pm_ops|
4260 dma_map_ops|
4261 extent_io_ops|
4262 file_lock_operations|
4263 file_operations|
4264 hv_ops|
4265 ide_dma_ops|
4266 intel_dvo_dev_ops|
4267 item_operations|
4268 iwl_ops|
4269 kgdb_arch|
4270 kgdb_io|
4271 kset_uevent_ops|
4272 lock_manager_operations|
4273 microcode_ops|
4274 mtrr_ops|
4275 neigh_ops|
4276 nlmsvc_binding|
4277 pci_raw_ops|
4278 pipe_buf_operations|
4279 platform_hibernation_ops|
4280 platform_suspend_ops|
4281 proto_ops|
4282 rpc_pipe_ops|
4283 seq_operations|
4284 snd_ac97_build_ops|
4285 soc_pcmcia_socket_ops|
4286 stacktrace_ops|
4287 sysfs_ops|
4288 tty_operations|
4289 usb_mon_operations|
4290 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004291 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004292 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004293 WARN("CONST_STRUCT",
4294 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004295 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004296 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004297
4298# use of NR_CPUS is usually wrong
4299# ignore definitions of NR_CPUS and usage to define arrays as likely right
4300 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004301 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4302 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004303 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4304 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4305 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004306 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004307 WARN("NR_CPUS",
4308 "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 -07004309 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004310
Joe Perches52ea8502013-11-12 15:10:09 -08004311# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4312 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4313 ERROR("DEFINE_ARCH_HAS",
4314 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4315 }
4316
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004317# check for %L{u,d,i} in strings
4318 my $string;
4319 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4320 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004321 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004322 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004323 WARN("PRINTF_L",
4324 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004325 last;
4326 }
4327 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004328
4329# whine mightly about in_atomic
4330 if ($line =~ /\bin_atomic\s*\(/) {
4331 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004332 ERROR("IN_ATOMIC",
4333 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004334 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004335 WARN("IN_ATOMIC",
4336 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004337 }
4338 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004339
4340# check for lockdep_set_novalidate_class
4341 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4342 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4343 if ($realfile !~ m@^kernel/lockdep@ &&
4344 $realfile !~ m@^include/linux/lockdep@ &&
4345 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004346 ERROR("LOCKDEP",
4347 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004348 }
4349 }
Dave Jones88f88312011-01-12 16:59:59 -08004350
4351 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4352 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004353 WARN("EXPORTED_WORLD_WRITABLE",
4354 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004355 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004356 }
4357
4358 # If we have no input at all, then there is nothing to report on
4359 # so just keep quiet.
4360 if ($#rawlines == -1) {
4361 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004362 }
4363
Andy Whitcroft8905a672007-11-28 16:21:06 -08004364 # In mailback mode only produce a report in the negative, for
4365 # things that appear to be patches.
4366 if ($mailback && ($clean == 1 || !$is_patch)) {
4367 exit(0);
4368 }
4369
4370 # This is not a patch, and we are are in 'no-patch' mode so
4371 # just keep quiet.
4372 if (!$chk_patch && !$is_patch) {
4373 exit(0);
4374 }
4375
4376 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004377 ERROR("NOT_UNIFIED_DIFF",
4378 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004379 }
4380 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004381 ERROR("MISSING_SIGN_OFF",
4382 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004383 }
4384
Andy Whitcroft8905a672007-11-28 16:21:06 -08004385 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004386 if ($summary && !($clean == 1 && $quiet == 1)) {
4387 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004388 print "total: $cnt_error errors, $cnt_warn warnings, " .
4389 (($check)? "$cnt_chk checks, " : "") .
4390 "$cnt_lines lines checked\n";
4391 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004392 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004393
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004394 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004395
4396 if ($^V lt 5.10.0) {
4397 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4398 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4399 }
4400
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004401 # If there were whitespace errors which cleanpatch can fix
4402 # then suggest that.
4403 if ($rpt_cleaners) {
4404 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4405 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004406 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004407 }
4408 }
4409
Joe Perches91bfe482013-09-11 14:23:59 -07004410 hash_show_words(\%use_type, "Used");
4411 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004412
Joe Perches3705ce52013-07-03 15:05:31 -07004413 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
Joe Perches9624b8d2014-01-23 15:54:44 -08004414 my $newfile = $filename;
4415 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07004416 my $linecount = 0;
4417 my $f;
4418
4419 open($f, '>', $newfile)
4420 or die "$P: Can't open $newfile for write\n";
4421 foreach my $fixed_line (@fixed) {
4422 $linecount++;
4423 if ($file) {
4424 if ($linecount > 3) {
4425 $fixed_line =~ s/^\+//;
4426 print $f $fixed_line. "\n";
4427 }
4428 } else {
4429 print $f $fixed_line . "\n";
4430 }
4431 }
4432 close($f);
4433
4434 if (!$quiet) {
4435 print << "EOM";
4436Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4437
4438Do _NOT_ trust the results written to this file.
4439Do _NOT_ submit these changes without inspecting them for correctness.
4440
4441This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4442No warranties, expressed or implied...
4443
4444EOM
4445 }
4446 }
4447
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004448 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004449 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004450 }
4451 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004452 print << "EOM";
4453$vname has style problems, please review.
4454
4455If any of these errors are false positives, please report
4456them to the maintainer, see CHECKPATCH in MAINTAINERS.
4457EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004458 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004459
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004460 return $clean;
4461}