blob: 42567bcd66b2eea52da8b20f6458438d7a2de565 [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 Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy 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 Whitcroft00df3442007-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;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080033my %debug;
Joe Perches34456862013-07-03 15:05:34 -070034my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070035my %use_type = ();
36my @use = ();
37my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070038my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070039my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070040my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080041my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070042my $ignore_perl_version = 0;
43my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070044
45sub help {
46 my ($exitcode) = @_;
47
48 print << "EOM";
49Usage: $P [OPTION]... [FILE]...
50Version: $V
51
52Options:
53 -q, --quiet quiet
54 --no-tree run without a kernel tree
55 --no-signoff do not check for 'Signed-off-by' line
56 --patch treat FILE as patchfile (default)
57 --emacs emacs compile window format
58 --terse one line per report
59 -f, --file treat FILE as regular source file
60 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070061 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070062 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080063 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070064 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070065 --root=PATH PATH to the kernel tree root
66 --no-summary suppress the per-file summary
67 --mailback only produce a report in case of warnings/errors
68 --summary-file include the filename in summary
69 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
70 'values', 'possible', 'type', and 'attr' (default
71 is all off)
72 --test-only=WORD report only warnings/errors containing WORD
73 literally
Joe Perches3705ce52013-07-03 15:05:31 -070074 --fix EXPERIMENTAL - may create horrible results
75 If correctable single-line errors exist, create
76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
77 with potential errors corrected to the preferred
78 checkpatch style
Dave Hansend62a2012013-09-11 14:23:56 -070079 --ignore-perl-version override checking of perl version. expect
80 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070081 -h, --help, --version display this help and exit
82
83When FILE is - read standard input.
84EOM
85
86 exit($exitcode);
87}
88
Joe Perches000d1cc12011-07-25 17:13:25 -070089my $conf = which_conf($configuration_file);
90if (-f $conf) {
91 my @conf_args;
92 open(my $conffile, '<', "$conf")
93 or warn "$P: Can't find a readable $configuration_file file $!\n";
94
95 while (<$conffile>) {
96 my $line = $_;
97
98 $line =~ s/\s*\n?$//g;
99 $line =~ s/^\s*//g;
100 $line =~ s/\s+/ /g;
101
102 next if ($line =~ m/^\s*#/);
103 next if ($line =~ m/^\s*$/);
104
105 my @words = split(" ", $line);
106 foreach my $word (@words) {
107 last if ($word =~ m/^#/);
108 push (@conf_args, $word);
109 }
110 }
111 close($conffile);
112 unshift(@ARGV, @conf_args) if @conf_args;
113}
114
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700115GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700117 'tree!' => \$tree,
118 'signoff!' => \$chk_signoff,
119 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800121 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700122 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700123 'subjective!' => \$check,
124 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700125 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700126 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700127 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800128 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700129 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800130 'summary!' => \$summary,
131 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800132 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700133 'fix!' => \$fix,
Dave Hansend62a2012013-09-11 14:23:56 -0700134 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800135 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700136 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'h|help' => \$help,
138 'version' => \$help
139) or help(1);
140
141help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700142
143my $exit = 0;
144
Dave Hansend62a2012013-09-11 14:23:56 -0700145if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
148 exit(1);
149 }
150}
151
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700152if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700153 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700154 exit(1);
155}
156
Joe Perches91bfe482013-09-11 14:23:59 -0700157sub hash_save_array_words {
158 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700159
Joe Perches91bfe482013-09-11 14:23:59 -0700160 my @array = split(/,/, join(',', @$arrayRef));
161 foreach my $word (@array) {
162 $word =~ s/\s*\n?$//g;
163 $word =~ s/^\s*//g;
164 $word =~ s/\s+/ /g;
165 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700166
Joe Perches91bfe482013-09-11 14:23:59 -0700167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
169
170 $hashRef->{$word}++;
171 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700172}
173
Joe Perches91bfe482013-09-11 14:23:59 -0700174sub hash_show_words {
175 my ($hashRef, $prefix) = @_;
176
Joe Perches58cb3cf2013-09-11 14:24:04 -0700177 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700178 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700179 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700180 print " $word";
181 }
182 print "\n\n";
183 }
184}
185
186hash_save_array_words(\%ignore_type, \@ignore);
187hash_save_array_words(\%use_type, \@use);
188
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800189my $dbg_values = 0;
190my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700191my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700192my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800193for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800194 ## no critic
195 eval "\${dbg_$key} = '$debug{$key}';";
196 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800197}
198
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700199my $rpt_cleaners = 0;
200
Andy Whitcroft8905a672007-11-28 16:21:06 -0800201if ($terse) {
202 $emacs = 1;
203 $quiet++;
204}
205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206if ($tree) {
207 if (defined $root) {
208 if (!top_of_kernel_tree($root)) {
209 die "$P: $root: --root does not point at a valid tree\n";
210 }
211 } else {
212 if (top_of_kernel_tree('.')) {
213 $root = '.';
214 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
215 top_of_kernel_tree($1)) {
216 $root = $1;
217 }
218 }
219
220 if (!defined $root) {
221 print "Must be run from the top-level dir. of a kernel tree\n";
222 exit(2);
223 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700224}
225
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700226my $emitted_corrupt = 0;
227
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700228our $Ident = qr{
229 [A-Za-z_][A-Za-z\d_]*
230 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
231 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700232our $Storage = qr{extern|static|asmlinkage};
233our $Sparse = qr{
234 __user|
235 __kernel|
236 __force|
237 __iomem|
238 __must_check|
239 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800240 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700241 __ref|
242 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700243 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800244
Joe Perches8716de32013-09-11 14:24:05 -0700245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
246
Wolfram Sang52131292010-03-05 13:43:51 -0800247# Notes to $Attribute:
248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700249our $Attribute = qr{
250 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700251 __percpu|
252 __nocast|
253 __safe|
254 __bitwise__|
255 __packed__|
256 __packed2__|
257 __naked|
258 __maybe_unused|
259 __always_unused|
260 __noreturn|
261 __used|
262 __cold|
263 __noclone|
264 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 __read_mostly|
266 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700267 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700268 ____cacheline_aligned|
269 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800270 ____cacheline_internodealigned_in_smp|
271 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700272 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700273our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700274our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700275our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
276our $Lval = qr{$Ident(?:$Member)*};
277
Joe Perches95e2c602013-07-03 15:05:20 -0700278our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
279our $Binary = qr{(?i)0b[01]+$Int_type?};
280our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
281our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800282our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
283our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
284our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800285our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700286our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800287our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800288our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700289our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290our $Operators = qr{
291 <=|>=|==|!=|
292 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700293 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294 }x;
295
Andy Whitcroft8905a672007-11-28 16:21:06 -0800296our $NonptrType;
Joe Perches8716de32013-09-11 14:24:05 -0700297our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800298our $Type;
299our $Declare;
300
Joe Perches15662b32011-10-31 17:13:12 -0700301our $NON_ASCII_UTF8 = qr{
302 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700303 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
304 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
305 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
306 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
307 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
308 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
309}x;
310
Joe Perches15662b32011-10-31 17:13:12 -0700311our $UTF8 = qr{
312 [\x09\x0A\x0D\x20-\x7E] # ASCII
313 | $NON_ASCII_UTF8
314}x;
315
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700316our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700317 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700318 atomic_t
319)};
320
Joe Perches691e6692010-03-05 13:43:51 -0800321our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700322 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700323 (?:[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 -0700324 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700325 panic|
Joe Perches06668722013-11-12 15:10:07 -0800326 MODULE_[A-Z_]+|
327 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800328)};
329
Joe Perches20112472011-07-25 17:13:23 -0700330our $signature_tags = qr{(?xi:
331 Signed-off-by:|
332 Acked-by:|
333 Tested-by:|
334 Reviewed-by:|
335 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700336 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700337 To:|
338 Cc:
339)};
340
Andy Whitcroft8905a672007-11-28 16:21:06 -0800341our @typeList = (
342 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700343 qr{(?:unsigned\s+)?char},
344 qr{(?:unsigned\s+)?short},
345 qr{(?:unsigned\s+)?int},
346 qr{(?:unsigned\s+)?long},
347 qr{(?:unsigned\s+)?long\s+int},
348 qr{(?:unsigned\s+)?long\s+long},
349 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800350 qr{unsigned},
351 qr{float},
352 qr{double},
353 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800354 qr{struct\s+$Ident},
355 qr{union\s+$Ident},
356 qr{enum\s+$Ident},
357 qr{${Ident}_t},
358 qr{${Ident}_handler},
359 qr{${Ident}_handler_fn},
360);
Joe Perches8716de32013-09-11 14:24:05 -0700361our @typeListWithAttr = (
362 @typeList,
363 qr{struct\s+$InitAttribute\s+$Ident},
364 qr{union\s+$InitAttribute\s+$Ident},
365);
366
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700367our @modifierList = (
368 qr{fastcall},
369);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800370
Wolfram Sang7840a942010-08-09 17:20:57 -0700371our $allowed_asm_includes = qr{(?x:
372 irq|
373 memory
374)};
375# memory.h: ARM has a custom one
376
Andy Whitcroft8905a672007-11-28 16:21:06 -0800377sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700378 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
379 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700380 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700381 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800382 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700383 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800384 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800385 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700386 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700387 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800388 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700389 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800390 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700391 $NonptrTypeWithAttr = qr{
392 (?:$Modifier\s+|const\s+)*
393 (?:
394 (?:typeof|__typeof__)\s*\([^\)]*\)|
395 (?:$typeTypedefs\b)|
396 (?:${allWithAttr}\b)
397 )
398 (?:\s+$Modifier|\s+const)*
399 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800400 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700401 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700402 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700403 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800404 }x;
405 $Declare = qr{(?:$Storage\s+)?$Type};
406}
407build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408
Joe Perches7d2367a2011-07-25 17:13:22 -0700409our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700410
411# Using $balanced_parens, $LvalOrFunc, or $FuncArg
412# requires at least perl version v5.10.0
413# Any use must be runtime checked with $^V
414
415our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
416our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800417our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700418
419sub deparenthesize {
420 my ($string) = @_;
421 return "" if (!defined($string));
422 $string =~ s@^\s*\(\s*@@g;
423 $string =~ s@\s*\)\s*$@@g;
424 $string =~ s@\s+@ @g;
425 return $string;
426}
427
Joe Perches34456862013-07-03 15:05:34 -0700428sub seed_camelcase_file {
429 my ($file) = @_;
430
431 return if (!(-f $file));
432
433 local $/;
434
435 open(my $include_file, '<', "$file")
436 or warn "$P: Can't read '$file' $!\n";
437 my $text = <$include_file>;
438 close($include_file);
439
440 my @lines = split('\n', $text);
441
442 foreach my $line (@lines) {
443 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
444 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
445 $camelcase{$1} = 1;
446 }
Joe Perchesd8b07712013-11-12 15:10:06 -0800447 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
Joe Perches34456862013-07-03 15:05:34 -0700448 $camelcase{$1} = 1;
449 }
450 }
451}
452
453my $camelcase_seeded = 0;
454sub seed_camelcase_includes {
455 return if ($camelcase_seeded);
456
457 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700458 my $camelcase_cache = "";
459 my @include_files = ();
460
461 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700462
Joe Perches34456862013-07-03 15:05:34 -0700463 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700464 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
465 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700466 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700467 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700468 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700469 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700470 @include_files = split('\n', $files);
471 foreach my $file (@include_files) {
472 my $date = POSIX::strftime("%Y%m%d%H%M",
473 localtime((stat $file)[9]));
474 $last_mod_date = $date if ($last_mod_date < $date);
475 }
476 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700477 }
Joe Perchesc707a812013-07-08 16:00:43 -0700478
479 if ($camelcase_cache ne "" && -f $camelcase_cache) {
480 open(my $camelcase_file, '<', "$camelcase_cache")
481 or warn "$P: Can't read '$camelcase_cache' $!\n";
482 while (<$camelcase_file>) {
483 chomp;
484 $camelcase{$_} = 1;
485 }
486 close($camelcase_file);
487
488 return;
489 }
490
491 if (-d ".git") {
492 $files = `git ls-files "include/*.h"`;
493 @include_files = split('\n', $files);
494 }
495
Joe Perches34456862013-07-03 15:05:34 -0700496 foreach my $file (@include_files) {
497 seed_camelcase_file($file);
498 }
Joe Perches351b2a12013-07-03 15:05:36 -0700499
Joe Perchesc707a812013-07-08 16:00:43 -0700500 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700501 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700502 open(my $camelcase_file, '>', "$camelcase_cache")
503 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700504 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
505 print $camelcase_file ("$_\n");
506 }
507 close($camelcase_file);
508 }
Joe Perches34456862013-07-03 15:05:34 -0700509}
510
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700511$chk_signoff = 0 if ($file);
512
Andy Whitcroft00df3442007-06-08 13:47:06 -0700513my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800514my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700515my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800516my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700517for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800518 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700519 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800520 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700521 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800522 } elsif ($filename eq '-') {
523 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700524 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800525 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700526 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700527 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800528 if ($filename eq '-') {
529 $vname = 'Your patch';
530 } else {
531 $vname = $filename;
532 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800533 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700534 chomp;
535 push(@rawlines, $_);
536 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800537 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800538 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700539 $exit = 1;
540 }
541 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800542 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700543 @fixed = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700544}
545
546exit($exit);
547
548sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700549 my ($root) = @_;
550
551 my @tree_check = (
552 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
553 "README", "Documentation", "arch", "include", "drivers",
554 "fs", "init", "ipc", "kernel", "lib", "scripts",
555 );
556
557 foreach my $check (@tree_check) {
558 if (! -e $root . '/' . $check) {
559 return 0;
560 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700561 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700562 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700563}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700564
Joe Perches20112472011-07-25 17:13:23 -0700565sub parse_email {
566 my ($formatted_email) = @_;
567
568 my $name = "";
569 my $address = "";
570 my $comment = "";
571
572 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
573 $name = $1;
574 $address = $2;
575 $comment = $3 if defined $3;
576 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
577 $address = $1;
578 $comment = $2 if defined $2;
579 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
580 $address = $1;
581 $comment = $2 if defined $2;
582 $formatted_email =~ s/$address.*$//;
583 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700584 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700585 $name =~ s/^\"|\"$//g;
586 # If there's a name left after stripping spaces and
587 # leading quotes, and the address doesn't have both
588 # leading and trailing angle brackets, the address
589 # is invalid. ie:
590 # "joe smith joe@smith.com" bad
591 # "joe smith <joe@smith.com" bad
592 if ($name ne "" && $address !~ /^<[^>]+>$/) {
593 $name = "";
594 $address = "";
595 $comment = "";
596 }
597 }
598
Joe Perches3705ce52013-07-03 15:05:31 -0700599 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700600 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700601 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700602 $address =~ s/^\<|\>$//g;
603
604 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
605 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
606 $name = "\"$name\"";
607 }
608
609 return ($name, $address, $comment);
610}
611
612sub format_email {
613 my ($name, $address) = @_;
614
615 my $formatted_email;
616
Joe Perches3705ce52013-07-03 15:05:31 -0700617 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700618 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700619 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700620
621 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
622 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
623 $name = "\"$name\"";
624 }
625
626 if ("$name" eq "") {
627 $formatted_email = "$address";
628 } else {
629 $formatted_email = "$name <$address>";
630 }
631
632 return $formatted_email;
633}
634
Joe Perches000d1cc12011-07-25 17:13:25 -0700635sub which_conf {
636 my ($conf) = @_;
637
638 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
639 if (-e "$path/$conf") {
640 return "$path/$conf";
641 }
642 }
643
644 return "";
645}
646
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700647sub expand_tabs {
648 my ($str) = @_;
649
650 my $res = '';
651 my $n = 0;
652 for my $c (split(//, $str)) {
653 if ($c eq "\t") {
654 $res .= ' ';
655 $n++;
656 for (; ($n % 8) != 0; $n++) {
657 $res .= ' ';
658 }
659 next;
660 }
661 $res .= $c;
662 $n++;
663 }
664
665 return $res;
666}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700667sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700668 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700669 return $res;
670}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700671
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700672sub line_stats {
673 my ($line) = @_;
674
675 # Drop the diff line leader and expand tabs
676 $line =~ s/^.//;
677 $line = expand_tabs($line);
678
679 # Pick the indent from the front of the line.
680 my ($white) = ($line =~ /^(\s*)/);
681
682 return (length($line), length($white));
683}
684
Andy Whitcroft773647a2008-03-28 14:15:58 -0700685my $sanitise_quote = '';
686
687sub sanitise_line_reset {
688 my ($in_comment) = @_;
689
690 if ($in_comment) {
691 $sanitise_quote = '*/';
692 } else {
693 $sanitise_quote = '';
694 }
695}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700696sub sanitise_line {
697 my ($line) = @_;
698
699 my $res = '';
700 my $l = '';
701
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800702 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700703 my $off = 0;
704 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700705
Andy Whitcroft773647a2008-03-28 14:15:58 -0700706 # Always copy over the diff marker.
707 $res = substr($line, 0, 1);
708
709 for ($off = 1; $off < length($line); $off++) {
710 $c = substr($line, $off, 1);
711
712 # Comments we are wacking completly including the begin
713 # and end, all to $;.
714 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
715 $sanitise_quote = '*/';
716
717 substr($res, $off, 2, "$;$;");
718 $off++;
719 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700721 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700722 $sanitise_quote = '';
723 substr($res, $off, 2, "$;$;");
724 $off++;
725 next;
726 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700727 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
728 $sanitise_quote = '//';
729
730 substr($res, $off, 2, $sanitise_quote);
731 $off++;
732 next;
733 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700734
735 # A \ in a string means ignore the next character.
736 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
737 $c eq "\\") {
738 substr($res, $off, 2, 'XX');
739 $off++;
740 next;
741 }
742 # Regular quotes.
743 if ($c eq "'" || $c eq '"') {
744 if ($sanitise_quote eq '') {
745 $sanitise_quote = $c;
746
747 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700748 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700749 } elsif ($sanitise_quote eq $c) {
750 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700751 }
752 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700753
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800754 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700755 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
756 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700757 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
758 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700759 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
760 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700761 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700762 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700763 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800764 }
765
Daniel Walker113f04a2009-09-21 17:04:35 -0700766 if ($sanitise_quote eq '//') {
767 $sanitise_quote = '';
768 }
769
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800770 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800772 my $clean = 'X' x length($1);
773 $res =~ s@\<.*\>@<$clean>@;
774
775 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700776 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800777 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700778 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800779 }
780
Andy Whitcroft00df3442007-06-08 13:47:06 -0700781 return $res;
782}
783
Joe Perchesa6962d72013-04-29 16:18:13 -0700784sub get_quoted_string {
785 my ($line, $rawline) = @_;
786
787 return "" if ($line !~ m/(\"[X]+\")/g);
788 return substr($rawline, $-[0], $+[0] - $-[0]);
789}
790
Andy Whitcroft8905a672007-11-28 16:21:06 -0800791sub ctx_statement_block {
792 my ($linenr, $remain, $off) = @_;
793 my $line = $linenr - 1;
794 my $blk = '';
795 my $soff = $off;
796 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700797 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800798
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800799 my $loff = 0;
800
Andy Whitcroft8905a672007-11-28 16:21:06 -0800801 my $type = '';
802 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800803 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800804 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 my $c;
806 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800807
808 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800810 @stack = (['', 0]) if ($#stack == -1);
811
Andy Whitcroft773647a2008-03-28 14:15:58 -0700812 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800813 # If we are about to drop off the end, pull in more
814 # context.
815 if ($off >= $len) {
816 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700817 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800818 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800819 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800820 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800821 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800822 $len = length($blk);
823 $line++;
824 last;
825 }
826 # Bail if there is no further context.
827 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800828 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800829 last;
830 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800831 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
832 $level++;
833 $type = '#';
834 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800835 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800836 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800837 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800838 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800839
Andy Whitcroft773647a2008-03-28 14:15:58 -0700840 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800841
842 # Handle nested #if/#else.
843 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
844 push(@stack, [ $type, $level ]);
845 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
846 ($type, $level) = @{$stack[$#stack - 1]};
847 } elsif ($remainder =~ /^#\s*endif\b/) {
848 ($type, $level) = @{pop(@stack)};
849 }
850
Andy Whitcroft8905a672007-11-28 16:21:06 -0800851 # Statement ends at the ';' or a close '}' at the
852 # outermost level.
853 if ($level == 0 && $c eq ';') {
854 last;
855 }
856
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800857 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700858 if ($level == 0 && $coff_set == 0 &&
859 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
860 $remainder =~ /^(else)(?:\s|{)/ &&
861 $remainder !~ /^else\s+if\b/) {
862 $coff = $off + length($1) - 1;
863 $coff_set = 1;
864 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
865 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800866 }
867
Andy Whitcroft8905a672007-11-28 16:21:06 -0800868 if (($type eq '' || $type eq '(') && $c eq '(') {
869 $level++;
870 $type = '(';
871 }
872 if ($type eq '(' && $c eq ')') {
873 $level--;
874 $type = ($level != 0)? '(' : '';
875
876 if ($level == 0 && $coff < $soff) {
877 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700878 $coff_set = 1;
879 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800880 }
881 }
882 if (($type eq '' || $type eq '{') && $c eq '{') {
883 $level++;
884 $type = '{';
885 }
886 if ($type eq '{' && $c eq '}') {
887 $level--;
888 $type = ($level != 0)? '{' : '';
889
890 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700891 if (substr($blk, $off + 1, 1) eq ';') {
892 $off++;
893 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800894 last;
895 }
896 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800897 # Preprocessor commands end at the newline unless escaped.
898 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
899 $level--;
900 $type = '';
901 $off++;
902 last;
903 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800904 $off++;
905 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700906 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800907 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700908 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800909 $line++;
910 $remain--;
911 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800912
913 my $statement = substr($blk, $soff, $off - $soff + 1);
914 my $condition = substr($blk, $soff, $coff - $soff + 1);
915
916 #warn "STATEMENT<$statement>\n";
917 #warn "CONDITION<$condition>\n";
918
Andy Whitcroft773647a2008-03-28 14:15:58 -0700919 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800920
921 return ($statement, $condition,
922 $line, $remain + 1, $off - $loff + 1, $level);
923}
924
Andy Whitcroftcf655042008-03-04 14:28:20 -0800925sub statement_lines {
926 my ($stmt) = @_;
927
928 # Strip the diff line prefixes and rip blank lines at start and end.
929 $stmt =~ s/(^|\n)./$1/g;
930 $stmt =~ s/^\s*//;
931 $stmt =~ s/\s*$//;
932
933 my @stmt_lines = ($stmt =~ /\n/g);
934
935 return $#stmt_lines + 2;
936}
937
938sub statement_rawlines {
939 my ($stmt) = @_;
940
941 my @stmt_lines = ($stmt =~ /\n/g);
942
943 return $#stmt_lines + 2;
944}
945
946sub statement_block_size {
947 my ($stmt) = @_;
948
949 $stmt =~ s/(^|\n)./$1/g;
950 $stmt =~ s/^\s*{//;
951 $stmt =~ s/}\s*$//;
952 $stmt =~ s/^\s*//;
953 $stmt =~ s/\s*$//;
954
955 my @stmt_lines = ($stmt =~ /\n/g);
956 my @stmt_statements = ($stmt =~ /;/g);
957
958 my $stmt_lines = $#stmt_lines + 2;
959 my $stmt_statements = $#stmt_statements + 1;
960
961 if ($stmt_lines > $stmt_statements) {
962 return $stmt_lines;
963 } else {
964 return $stmt_statements;
965 }
966}
967
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800968sub ctx_statement_full {
969 my ($linenr, $remain, $off) = @_;
970 my ($statement, $condition, $level);
971
972 my (@chunks);
973
Andy Whitcroftcf655042008-03-04 14:28:20 -0800974 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800975 ($statement, $condition, $linenr, $remain, $off, $level) =
976 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700977 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800978 push(@chunks, [ $condition, $statement ]);
979 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
980 return ($level, $linenr, @chunks);
981 }
982
983 # Pull in the following conditional/block pairs and see if they
984 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800985 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800986 ($statement, $condition, $linenr, $remain, $off, $level) =
987 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800988 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700989 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800990 #print "C: push\n";
991 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800992 }
993
994 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800995}
996
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700997sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700998 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700999 my $line;
1000 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001001 my $blk = '';
1002 my @o;
1003 my @c;
1004 my @res = ();
1005
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001006 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001007 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001008 for ($line = $start; $remain > 0; $line++) {
1009 next if ($rawlines[$line] =~ /^-/);
1010 $remain--;
1011
1012 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001013
1014 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001015 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001016 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001017 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001018 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001019 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001020 $level = pop(@stack);
1021 }
1022
Andy Whitcroft01464f32010-10-26 14:23:19 -07001023 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001024 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1025 if ($off > 0) {
1026 $off--;
1027 next;
1028 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001029
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001030 if ($c eq $close && $level > 0) {
1031 $level--;
1032 last if ($level == 0);
1033 } elsif ($c eq $open) {
1034 $level++;
1035 }
1036 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001037
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001038 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001039 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001040 }
1041
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001042 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001043 }
1044
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001045 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001046}
1047sub ctx_block_outer {
1048 my ($linenr, $remain) = @_;
1049
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001050 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1051 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001052}
1053sub ctx_block {
1054 my ($linenr, $remain) = @_;
1055
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001056 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1057 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001058}
1059sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001060 my ($linenr, $remain, $off) = @_;
1061
1062 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1063 return @r;
1064}
1065sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001066 my ($linenr, $remain) = @_;
1067
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001068 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001069}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001070sub ctx_statement_level {
1071 my ($linenr, $remain, $off) = @_;
1072
1073 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1074}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001075
1076sub ctx_locate_comment {
1077 my ($first_line, $end_line) = @_;
1078
1079 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001080 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001081 return $current_comment if (defined $current_comment);
1082
1083 # Look through the context and try and figure out if there is a
1084 # comment.
1085 my $in_comment = 0;
1086 $current_comment = '';
1087 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001088 my $line = $rawlines[$linenr - 1];
1089 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001090 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1091 $in_comment = 1;
1092 }
1093 if ($line =~ m@/\*@) {
1094 $in_comment = 1;
1095 }
1096 if (!$in_comment && $current_comment ne '') {
1097 $current_comment = '';
1098 }
1099 $current_comment .= $line . "\n" if ($in_comment);
1100 if ($line =~ m@\*/@) {
1101 $in_comment = 0;
1102 }
1103 }
1104
1105 chomp($current_comment);
1106 return($current_comment);
1107}
1108sub ctx_has_comment {
1109 my ($first_line, $end_line) = @_;
1110 my $cmt = ctx_locate_comment($first_line, $end_line);
1111
Andy Whitcroft00df3442007-06-08 13:47:06 -07001112 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001113 ##print "CMMT: $cmt\n";
1114
1115 return ($cmt ne '');
1116}
1117
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001118sub raw_line {
1119 my ($linenr, $cnt) = @_;
1120
1121 my $offset = $linenr - 1;
1122 $cnt++;
1123
1124 my $line;
1125 while ($cnt) {
1126 $line = $rawlines[$offset++];
1127 next if (defined($line) && $line =~ /^-/);
1128 $cnt--;
1129 }
1130
1131 return $line;
1132}
1133
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001134sub cat_vet {
1135 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001136 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001137
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001138 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1140 $res .= $1;
1141 if ($2 ne '') {
1142 $coded = sprintf("^%c", unpack('C', $2) + 64);
1143 $res .= $coded;
1144 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001145 }
1146 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001147
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001148 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001149}
1150
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001151my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001152my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001154my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155
1156sub annotate_reset {
1157 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001158 $av_pending = '_';
1159 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001160 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161}
1162
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163sub annotate_values {
1164 my ($stream, $type) = @_;
1165
1166 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001167 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001168 my $cur = $stream;
1169
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001170 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001171
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001172 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001173 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001174 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001175 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001176 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001177 print "WS($1)\n" if ($dbg_values > 1);
1178 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001179 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001180 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001181 }
1182
Florian Micklerc023e4732011-01-12 16:59:58 -08001183 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001184 print "CAST($1)\n" if ($dbg_values > 1);
1185 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001186 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001187
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001188 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001189 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001190 $type = 'T';
1191
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001192 } elsif ($cur =~ /^($Modifier)\s*/) {
1193 print "MODIFIER($1)\n" if ($dbg_values > 1);
1194 $type = 'T';
1195
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001196 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001197 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001198 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001199 push(@av_paren_type, $type);
1200 if ($2 ne '') {
1201 $av_pending = 'N';
1202 }
1203 $type = 'E';
1204
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001205 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001206 print "UNDEF($1)\n" if ($dbg_values > 1);
1207 $av_preprocessor = 1;
1208 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001209
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001210 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001211 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001212 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001213
1214 push(@av_paren_type, $type);
1215 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001216 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001217
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001218 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001219 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1220 $av_preprocessor = 1;
1221
1222 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1223
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001224 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001225
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001226 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001227 print "PRE_END($1)\n" if ($dbg_values > 1);
1228
1229 $av_preprocessor = 1;
1230
1231 # Assume all arms of the conditional end as this
1232 # one does, and continue as if the #endif was not here.
1233 pop(@av_paren_type);
1234 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001235 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001236
1237 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001238 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001239
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001240 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1241 print "ATTR($1)\n" if ($dbg_values > 1);
1242 $av_pending = $type;
1243 $type = 'N';
1244
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001245 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001246 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001247 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001248 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001249 }
1250 $type = 'N';
1251
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001252 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001253 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001254 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 $type = 'N';
1256
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001257 } elsif ($cur =~/^(case)/o) {
1258 print "CASE($1)\n" if ($dbg_values > 1);
1259 $av_pend_colon = 'C';
1260 $type = 'N';
1261
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001262 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001263 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001264 $type = 'N';
1265
1266 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001267 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001268 push(@av_paren_type, $av_pending);
1269 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001270 $type = 'N';
1271
1272 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001273 my $new_type = pop(@av_paren_type);
1274 if ($new_type ne '_') {
1275 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001276 print "PAREN('$1') -> $type\n"
1277 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001278 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001279 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001280 }
1281
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001282 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001284 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001285 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001286
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001287 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1288 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001289 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001290 } elsif ($type eq 'E') {
1291 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001292 }
1293 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1294 $type = 'V';
1295
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001296 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298 $type = 'V';
1299
1300 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001301 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001302 $type = 'N';
1303
Andy Whitcroftcf655042008-03-04 14:28:20 -08001304 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001305 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001306 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001307 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001308
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001309 } elsif ($cur =~/^(,)/) {
1310 print "COMMA($1)\n" if ($dbg_values > 1);
1311 $type = 'C';
1312
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001313 } elsif ($cur =~ /^(\?)/o) {
1314 print "QUESTION($1)\n" if ($dbg_values > 1);
1315 $type = 'N';
1316
1317 } elsif ($cur =~ /^(:)/o) {
1318 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1319
1320 substr($var, length($res), 1, $av_pend_colon);
1321 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1322 $type = 'E';
1323 } else {
1324 $type = 'N';
1325 }
1326 $av_pend_colon = 'O';
1327
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001328 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001329 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001330 $type = 'N';
1331
Andy Whitcroft0d413862008-10-15 22:02:16 -07001332 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001333 my $variant;
1334
1335 print "OPV($1)\n" if ($dbg_values > 1);
1336 if ($type eq 'V') {
1337 $variant = 'B';
1338 } else {
1339 $variant = 'U';
1340 }
1341
1342 substr($var, length($res), 1, $variant);
1343 $type = 'N';
1344
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001345 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001346 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001347 if ($1 ne '++' && $1 ne '--') {
1348 $type = 'N';
1349 }
1350
1351 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001353 }
1354 if (defined $1) {
1355 $cur = substr($cur, length($1));
1356 $res .= $type x length($1);
1357 }
1358 }
1359
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001360 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001361}
1362
Andy Whitcroft8905a672007-11-28 16:21:06 -08001363sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001364 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001365 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001366 ^(?:
1367 $Modifier|
1368 $Storage|
1369 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001370 DEFINE_\S+
1371 )$|
1372 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001373 goto|
1374 return|
1375 case|
1376 else|
1377 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001378 do|
1379 \#|
1380 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001381 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001382 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001383 )}x;
1384 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1385 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001386 # Check for modifiers.
1387 $possible =~ s/\s*$Storage\s*//g;
1388 $possible =~ s/\s*$Sparse\s*//g;
1389 if ($possible =~ /^\s*$/) {
1390
1391 } elsif ($possible =~ /\s/) {
1392 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001393 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001394 if ($modifier !~ $notPermitted) {
1395 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1396 push(@modifierList, $modifier);
1397 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001398 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001399
1400 } else {
1401 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1402 push(@typeList, $possible);
1403 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001404 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001405 } else {
1406 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001407 }
1408}
1409
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001410my $prefix = '';
1411
Joe Perches000d1cc12011-07-25 17:13:25 -07001412sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001413 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1414
1415 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001416}
1417
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001418sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001419 if (!show_type($_[1]) ||
1420 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 return 0;
1422 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001423 my $line;
1424 if ($show_types) {
1425 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1426 } else {
1427 $line = "$prefix$_[0]: $_[2]\n";
1428 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429 $line = (split('\n', $line))[0] . "\n" if ($terse);
1430
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432
1433 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001434}
1435sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001436 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001437}
Joe Perches000d1cc12011-07-25 17:13:25 -07001438
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001439sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001440 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001441 our $clean = 0;
1442 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001443 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 }
Joe Perches3705ce52013-07-03 15:05:31 -07001445 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001446}
1447sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001448 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001449 our $clean = 0;
1450 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001451 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001452 }
Joe Perches3705ce52013-07-03 15:05:31 -07001453 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001454}
1455sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001456 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001457 our $clean = 0;
1458 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001459 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 }
Joe Perches3705ce52013-07-03 15:05:31 -07001461 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001462}
1463
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001464sub check_absolute_file {
1465 my ($absolute, $herecurr) = @_;
1466 my $file = $absolute;
1467
1468 ##print "absolute<$absolute>\n";
1469
1470 # See if any suffix of this path is a path within the tree.
1471 while ($file =~ s@^[^/]*/@@) {
1472 if (-f "$root/$file") {
1473 ##print "file<$file>\n";
1474 last;
1475 }
1476 }
1477 if (! -f _) {
1478 return 0;
1479 }
1480
1481 # It is, so see if the prefix is acceptable.
1482 my $prefix = $absolute;
1483 substr($prefix, -length($file)) = '';
1484
1485 ##print "prefix<$prefix>\n";
1486 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001487 WARN("USE_RELATIVE_PATH",
1488 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001489 }
1490}
1491
Joe Perches3705ce52013-07-03 15:05:31 -07001492sub trim {
1493 my ($string) = @_;
1494
Joe Perchesb34c6482013-09-11 14:24:01 -07001495 $string =~ s/^\s+|\s+$//g;
1496
1497 return $string;
1498}
1499
1500sub ltrim {
1501 my ($string) = @_;
1502
1503 $string =~ s/^\s+//;
1504
1505 return $string;
1506}
1507
1508sub rtrim {
1509 my ($string) = @_;
1510
1511 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001512
1513 return $string;
1514}
1515
1516sub tabify {
1517 my ($leading) = @_;
1518
1519 my $source_indent = 8;
1520 my $max_spaces_before_tab = $source_indent - 1;
1521 my $spaces_to_tab = " " x $source_indent;
1522
1523 #convert leading spaces to tabs
1524 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1525 #Remove spaces before a tab
1526 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1527
1528 return "$leading";
1529}
1530
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001531sub pos_last_openparen {
1532 my ($line) = @_;
1533
1534 my $pos = 0;
1535
1536 my $opens = $line =~ tr/\(/\(/;
1537 my $closes = $line =~ tr/\)/\)/;
1538
1539 my $last_openparen = 0;
1540
1541 if (($opens == 0) || ($closes >= $opens)) {
1542 return -1;
1543 }
1544
1545 my $len = length($line);
1546
1547 for ($pos = 0; $pos < $len; $pos++) {
1548 my $string = substr($line, $pos);
1549 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1550 $pos += length($1) - 1;
1551 } elsif (substr($line, $pos, 1) eq '(') {
1552 $last_openparen = $pos;
1553 } elsif (index($string, '(') == -1) {
1554 last;
1555 }
1556 }
1557
1558 return $last_openparen + 1;
1559}
1560
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001561sub process {
1562 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001563
1564 my $linenr=0;
1565 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001566 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001567 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001568 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001569
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001570 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001571 my $indent;
1572 my $previndent=0;
1573 my $stashindent=0;
1574
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001575 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001576 my $signoff = 0;
1577 my $is_patch = 0;
1578
Joe Perches15662b32011-10-31 17:13:12 -07001579 my $in_header_lines = 1;
1580 my $in_commit_log = 0; #Scanning lines before patch
1581
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001582 my $non_utf8_charset = 0;
1583
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001584 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001585 our $cnt_lines = 0;
1586 our $cnt_error = 0;
1587 our $cnt_warn = 0;
1588 our $cnt_chk = 0;
1589
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001590 # Trace the real file/line as we go.
1591 my $realfile = '';
1592 my $realline = 0;
1593 my $realcnt = 0;
1594 my $here = '';
1595 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001596 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001597 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001598 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001599
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001600 my $prev_values = 'E';
1601
1602 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001603 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001604 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001605 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001606 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001607
Joe Perches7e51f192013-09-11 14:23:57 -07001608 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001609
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001610 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001611 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001612 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001613 my @setup_docs = ();
1614 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001615
Joe Perchesd8b07712013-11-12 15:10:06 -08001616 my $camelcase_file_seeded = 0;
1617
Andy Whitcroft773647a2008-03-28 14:15:58 -07001618 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001619 my $line;
1620 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 $linenr++;
1622 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001623
Joe Perches3705ce52013-07-03 15:05:31 -07001624 push(@fixed, $rawline) if ($fix);
1625
Andy Whitcroft773647a2008-03-28 14:15:58 -07001626 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001627 $setup_docs = 0;
1628 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1629 $setup_docs = 1;
1630 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001632 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001633 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1634 $realline=$1-1;
1635 if (defined $2) {
1636 $realcnt=$3+1;
1637 } else {
1638 $realcnt=1+1;
1639 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001640 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001641
1642 # Guestimate if this is a continuing comment. Run
1643 # the context looking for a comment "edge". If this
1644 # edge is a close comment then we must be in a comment
1645 # at context start.
1646 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001647 my $cnt = $realcnt;
1648 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1649 next if (defined $rawlines[$ln - 1] &&
1650 $rawlines[$ln - 1] =~ /^-/);
1651 $cnt--;
1652 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001653 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001654 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1655 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1656 ($edge) = $1;
1657 last;
1658 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001659 }
1660 if (defined $edge && $edge eq '*/') {
1661 $in_comment = 1;
1662 }
1663
1664 # Guestimate if this is a continuing comment. If this
1665 # is the start of a diff block and this line starts
1666 # ' *' then it is very likely a comment.
1667 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001668 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001669 {
1670 $in_comment = 1;
1671 }
1672
1673 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1674 sanitise_line_reset($in_comment);
1675
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001676 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001677 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001678 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001679 $line = sanitise_line($rawline);
1680 }
1681 push(@lines, $line);
1682
1683 if ($realcnt > 1) {
1684 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1685 } else {
1686 $realcnt = 0;
1687 }
1688
1689 #print "==>$rawline\n";
1690 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001691
1692 if ($setup_docs && $line =~ /^\+/) {
1693 push(@setup_docs, $line);
1694 }
1695 }
1696
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001697 $prefix = '';
1698
Andy Whitcroft773647a2008-03-28 14:15:58 -07001699 $realcnt = 0;
1700 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001701 foreach my $line (@lines) {
1702 $linenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001703 my $sline = $line; #copy of $line
1704 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001705
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001706 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001707
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001708#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001710 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001711 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001712 $realline=$1-1;
1713 if (defined $2) {
1714 $realcnt=$3+1;
1715 } else {
1716 $realcnt=1+1;
1717 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001719 $prev_values = 'E';
1720
Andy Whitcroft773647a2008-03-28 14:15:58 -07001721 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001722 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001723 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001724 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001725 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001726
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001727# track the line number as we move through the hunk, note that
1728# new versions of GNU diff omit the leading space on completely
1729# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001730 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001731 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001732 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001733
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001734 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001735 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001736
1737 # Track the previous line.
1738 ($prevline, $stashline) = ($stashline, $line);
1739 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001740 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1741
Andy Whitcroft773647a2008-03-28 14:15:58 -07001742 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001743
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001744 } elsif ($realcnt == 1) {
1745 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001746 }
1747
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001748 my $hunk_line = ($realcnt != 0);
1749
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001750#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001751 $prefix = "$filename:$realline: " if ($emacs && $file);
1752 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1753
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001754 $here = "#$linenr: " if (!$file);
1755 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001756
1757 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001758 if ($line =~ /^diff --git.*?(\S+)$/) {
1759 $realfile = $1;
1760 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001761 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001762 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001763 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001764 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001765 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001766
1767 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001768 if (!$file && $tree && $p1_prefix ne '' &&
1769 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001770 WARN("PATCH_PREFIX",
1771 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001772 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001773
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001774 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001775 ERROR("MODIFIED_INCLUDE_ASM",
1776 "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 -07001777 }
1778 next;
1779 }
1780
Randy Dunlap389834b2007-06-08 13:47:03 -07001781 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001782
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001783 my $hereline = "$here\n$rawline\n";
1784 my $herecurr = "$here\n$rawline\n";
1785 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001786
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001787 $cnt_lines++ if ($realcnt != 0);
1788
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001789# Check for incorrect file permissions
1790 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1791 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001792 if ($realfile !~ m@scripts/@ &&
1793 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001794 ERROR("EXECUTE_PERMISSIONS",
1795 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001796 }
1797 }
1798
Joe Perches20112472011-07-25 17:13:23 -07001799# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001800 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001801 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001802 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001803 }
1804
1805# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001806 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001807 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001808 my $space_before = $1;
1809 my $sign_off = $2;
1810 my $space_after = $3;
1811 my $email = $4;
1812 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1813
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001814 if ($sign_off !~ /$signature_tags/) {
1815 WARN("BAD_SIGN_OFF",
1816 "Non-standard signature: $sign_off\n" . $herecurr);
1817 }
Joe Perches20112472011-07-25 17:13:23 -07001818 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001819 if (WARN("BAD_SIGN_OFF",
1820 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1821 $fix) {
1822 $fixed[$linenr - 1] =
1823 "$ucfirst_sign_off $email";
1824 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001825 }
Joe Perches20112472011-07-25 17:13:23 -07001826 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001827 if (WARN("BAD_SIGN_OFF",
1828 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1829 $fix) {
1830 $fixed[$linenr - 1] =
1831 "$ucfirst_sign_off $email";
1832 }
1833
Joe Perches20112472011-07-25 17:13:23 -07001834 }
1835 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001836 if (WARN("BAD_SIGN_OFF",
1837 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1838 $fix) {
1839 $fixed[$linenr - 1] =
1840 "$ucfirst_sign_off $email";
1841 }
Joe Perches20112472011-07-25 17:13:23 -07001842 }
1843
1844 my ($email_name, $email_address, $comment) = parse_email($email);
1845 my $suggested_email = format_email(($email_name, $email_address));
1846 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001847 ERROR("BAD_SIGN_OFF",
1848 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001849 } else {
1850 my $dequoted = $suggested_email;
1851 $dequoted =~ s/^"//;
1852 $dequoted =~ s/" </ </;
1853 # Don't force email to have quotes
1854 # Allow just an angle bracketed address
1855 if ("$dequoted$comment" ne $email &&
1856 "<$email_address>$comment" ne $email &&
1857 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001858 WARN("BAD_SIGN_OFF",
1859 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001860 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001861 }
Joe Perches7e51f192013-09-11 14:23:57 -07001862
1863# Check for duplicate signatures
1864 my $sig_nospace = $line;
1865 $sig_nospace =~ s/\s//g;
1866 $sig_nospace = lc($sig_nospace);
1867 if (defined $signatures{$sig_nospace}) {
1868 WARN("BAD_SIGN_OFF",
1869 "Duplicate signature\n" . $herecurr);
1870 } else {
1871 $signatures{$sig_nospace} = 1;
1872 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001873 }
1874
Andy Whitcroft00df3442007-06-08 13:47:06 -07001875# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001876 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001877 ERROR("CORRUPTED_PATCH",
1878 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001879 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001880 }
1881
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001882# Check for absolute kernel paths.
1883 if ($tree) {
1884 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1885 my $file = $1;
1886
1887 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1888 check_absolute_file($1, $herecurr)) {
1889 #
1890 } else {
1891 check_absolute_file($file, $herecurr);
1892 }
1893 }
1894 }
1895
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001896# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1897 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001898 $rawline !~ m/^$UTF8*$/) {
1899 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1900
1901 my $blank = copy_spacing($rawline);
1902 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1903 my $hereptr = "$hereline$ptr\n";
1904
Joe Perches34d99212011-07-25 17:13:26 -07001905 CHK("INVALID_UTF8",
1906 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001907 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001908
Joe Perches15662b32011-10-31 17:13:12 -07001909# Check if it's the start of a commit log
1910# (not a header line and we haven't seen the patch filename)
1911 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001912 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001913 $in_header_lines = 0;
1914 $in_commit_log = 1;
1915 }
1916
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001917# Check if there is UTF-8 in a commit log when a mail header has explicitly
1918# declined it, i.e defined some charset where it is missing.
1919 if ($in_header_lines &&
1920 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1921 $1 !~ /utf-8/i) {
1922 $non_utf8_charset = 1;
1923 }
1924
1925 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001926 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001927 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001928 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1929 }
1930
Andy Whitcroft306708542008-10-15 22:02:28 -07001931# ignore non-hunk lines and lines being removed
1932 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001933
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001934#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001935 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001936 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001937 if (ERROR("DOS_LINE_ENDINGS",
1938 "DOS line endings\n" . $herevet) &&
1939 $fix) {
1940 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1941 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001942 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1943 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001944 if (ERROR("TRAILING_WHITESPACE",
1945 "trailing whitespace\n" . $herevet) &&
1946 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001947 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001948 }
1949
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001950 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001951 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001952
Andi Kleen33549572010-05-24 14:33:29 -07001953# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001954# Only applies when adding the entry originally, after that we do not have
1955# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001956 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001957 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001958 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001959 my $cnt = $realcnt;
1960 my $ln = $linenr + 1;
1961 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001962 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001963 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001964 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001965 $f = $lines[$ln - 1];
1966 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1967 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001968
1969 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001970
1971 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1972 $is_start = 1;
1973 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1974 $length = -1;
1975 }
1976
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001977 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001978 $f =~ s/#.*//;
1979 $f =~ s/^\s+//;
1980 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001981 if ($f =~ /^\s*config\s/) {
1982 $is_end = 1;
1983 last;
1984 }
Andi Kleen33549572010-05-24 14:33:29 -07001985 $length++;
1986 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001987 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001988 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1989 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001990 }
1991
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001992# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1993 if ($realfile =~ /Kconfig/ &&
1994 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1995 WARN("CONFIG_EXPERIMENTAL",
1996 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1997 }
1998
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001999 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2000 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2001 my $flag = $1;
2002 my $replacement = {
2003 'EXTRA_AFLAGS' => 'asflags-y',
2004 'EXTRA_CFLAGS' => 'ccflags-y',
2005 'EXTRA_CPPFLAGS' => 'cppflags-y',
2006 'EXTRA_LDFLAGS' => 'ldflags-y',
2007 };
2008
2009 WARN("DEPRECATED_VARIABLE",
2010 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2011 }
2012
Andy Whitcroft5368df202008-10-15 22:02:27 -07002013# check we are in a valid source file if not then ignore this hunk
2014 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2015
Joe Perches6cd7f382012-12-17 16:01:54 -08002016#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002017 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002018 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002019 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002020 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002021 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002022 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002023 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002024 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002025 }
2026
Josh Triplettca56dc02012-03-23 15:02:21 -07002027# Check for user-visible strings broken across lines, which breaks the ability
2028# to grep for the string. Limited to strings used as parameters (those
2029# following an open parenthesis), which almost completely eliminates false
2030# positives, as well as warning only once per parameter rather than once per
2031# line of the string. Make an exception when the previous string ends in a
2032# newline (multiple lines in one string constant) or \n\t (common in inline
2033# assembly to indent the instruction on the following line).
2034 if ($line =~ /^\+\s*"/ &&
2035 $prevline =~ /"\s*$/ &&
2036 $prevline =~ /\(/ &&
2037 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2038 WARN("SPLIT_STRING",
2039 "quoted string split across lines\n" . $hereprev);
2040 }
2041
Joe Perches5e79d962010-03-05 13:43:55 -08002042# check for spaces before a quoted newline
2043 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002044 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2045 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2046 $fix) {
2047 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2048 }
2049
Joe Perches5e79d962010-03-05 13:43:55 -08002050 }
2051
Andy Whitcroft8905a672007-11-28 16:21:06 -08002052# check for adding lines without a newline.
2053 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002054 WARN("MISSING_EOF_NEWLINE",
2055 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002056 }
2057
Mike Frysinger42e41c52009-09-21 17:04:40 -07002058# Blackfin: use hi/lo macros
2059 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2060 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2061 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002062 ERROR("LO_MACRO",
2063 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002064 }
2065 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2066 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002067 ERROR("HI_MACRO",
2068 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002069 }
2070 }
2071
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002072# check we are in a valid source file C or perl if not then ignore this hunk
2073 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002074
2075# at the beginning of a line any tabs must come first and anything
2076# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002077 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2078 $rawline =~ /^\+\s* \s*/) {
2079 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002080 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002081 if (ERROR("CODE_INDENT",
2082 "code indent should use tabs where possible\n" . $herevet) &&
2083 $fix) {
2084 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2085 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002086 }
2087
Alberto Panizzo08e44362010-03-05 13:43:54 -08002088# check for space before tabs.
2089 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2090 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002091 if (WARN("SPACE_BEFORE_TAB",
2092 "please, no space before tabs\n" . $herevet) &&
2093 $fix) {
2094 $fixed[$linenr - 1] =~
2095 s/(^\+.*) +\t/$1\t/;
2096 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002097 }
2098
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002099# check for && or || at the start of a line
2100 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2101 CHK("LOGICAL_CONTINUATIONS",
2102 "Logical continuations should be on the previous line\n" . $hereprev);
2103 }
2104
2105# check multi-line statement indentation matches previous line
2106 if ($^V && $^V ge 5.10.0 &&
2107 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2108 $prevline =~ /^\+(\t*)(.*)$/;
2109 my $oldindent = $1;
2110 my $rest = $2;
2111
2112 my $pos = pos_last_openparen($rest);
2113 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002114 $line =~ /^(\+| )([ \t]*)/;
2115 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002116
2117 my $goodtabindent = $oldindent .
2118 "\t" x ($pos / 8) .
2119 " " x ($pos % 8);
2120 my $goodspaceindent = $oldindent . " " x $pos;
2121
2122 if ($newindent ne $goodtabindent &&
2123 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002124
2125 if (CHK("PARENTHESIS_ALIGNMENT",
2126 "Alignment should match open parenthesis\n" . $hereprev) &&
2127 $fix && $line =~ /^\+/) {
2128 $fixed[$linenr - 1] =~
2129 s/^\+[ \t]*/\+$goodtabindent/;
2130 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002131 }
2132 }
2133 }
2134
Joe Perches23f780c2013-07-03 15:05:31 -07002135 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002136 if (CHK("SPACING",
2137 "No space is necessary after a cast\n" . $hereprev) &&
2138 $fix) {
2139 $fixed[$linenr - 1] =~
2140 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2141 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002142 }
2143
Joe Perches05880602012-10-04 17:13:35 -07002144 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002145 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2146 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002147 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2148 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2149 }
2150
2151 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002152 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2153 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002154 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002155 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2156 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2157 "networking block comments start with * on subsequent lines\n" . $hereprev);
2158 }
2159
2160 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002161 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2162 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2163 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2164 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002165 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2166 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2167 }
2168
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002169# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002170# Exceptions:
2171# 1) within comments
2172# 2) indented preprocessor commands
2173# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002174 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002175 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002176 if (WARN("LEADING_SPACE",
2177 "please, no spaces at the start of a line\n" . $herevet) &&
2178 $fix) {
2179 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2180 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002181 }
2182
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002183# check we are in a valid C source file if not then ignore this hunk
2184 next if ($realfile !~ /\.(h|c)$/);
2185
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002186# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2187 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2188 WARN("CONFIG_EXPERIMENTAL",
2189 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2190 }
2191
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002192# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002193 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002194 WARN("CVS_KEYWORD",
2195 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002196 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002197
Mike Frysinger42e41c52009-09-21 17:04:40 -07002198# Blackfin: don't use __builtin_bfin_[cs]sync
2199 if ($line =~ /__builtin_bfin_csync/) {
2200 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002201 ERROR("CSYNC",
2202 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002203 }
2204 if ($line =~ /__builtin_bfin_ssync/) {
2205 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002206 ERROR("SSYNC",
2207 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002208 }
2209
Joe Perches56e77d72013-02-21 16:44:14 -08002210# check for old HOTPLUG __dev<foo> section markings
2211 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2212 WARN("HOTPLUG_SECTION",
2213 "Using $1 is unnecessary\n" . $herecurr);
2214 }
2215
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002216# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002217 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2218 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002219#print "LINE<$line>\n";
2220 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002221 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002222 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002223 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002224 $stat =~ s/\n./\n /g;
2225 $cond =~ s/\n./\n /g;
2226
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002227#print "linenr<$linenr> <$stat>\n";
2228 # If this statement has no statement boundaries within
2229 # it there is no point in retrying a statement scan
2230 # until we hit end of it.
2231 my $frag = $stat; $frag =~ s/;+\s*$//;
2232 if ($frag !~ /(?:{|;)/) {
2233#print "skip<$line_nr_next>\n";
2234 $suppress_statement = $line_nr_next;
2235 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002236
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002237 # Find the real next line.
2238 $realline_next = $line_nr_next;
2239 if (defined $realline_next &&
2240 (!defined $lines[$realline_next - 1] ||
2241 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2242 $realline_next++;
2243 }
2244
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002245 my $s = $stat;
2246 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002247
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002248 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002249 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002250
2251 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002252 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002253
Andy Whitcroft463f2862009-09-21 17:04:34 -07002254 } elsif ($s =~ /^.\s*else\b/s) {
2255
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002256 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002257 } 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 -07002258 my $type = $1;
2259 $type =~ s/\s+/ /g;
2260 possible($type, "A:" . $s);
2261
Andy Whitcroft8905a672007-11-28 16:21:06 -08002262 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002263 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002264 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002265 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002266
2267 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002268 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002269 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002270 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002271
2272 # Check for any sort of function declaration.
2273 # int foo(something bar, other baz);
2274 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002275 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 -08002276 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002277
Andy Whitcroftcf655042008-03-04 14:28:20 -08002278 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002279 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002280 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002281
Andy Whitcroft8905a672007-11-28 16:21:06 -08002282 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002283 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002284
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002285 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002286 }
2287 }
2288 }
2289
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002290 }
2291
Andy Whitcroft653d4872007-06-23 17:16:34 -07002292#
2293# Checks which may be anchored in the context.
2294#
2295
2296# Check for switch () and associated case and default
2297# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002298 if ($line=~/\bswitch\s*\(.*\)/) {
2299 my $err = '';
2300 my $sep = '';
2301 my @ctx = ctx_block_outer($linenr, $realcnt);
2302 shift(@ctx);
2303 for my $ctx (@ctx) {
2304 my ($clen, $cindent) = line_stats($ctx);
2305 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2306 $indent != $cindent) {
2307 $err .= "$sep$ctx\n";
2308 $sep = '';
2309 } else {
2310 $sep = "[...]\n";
2311 }
2312 }
2313 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002314 ERROR("SWITCH_CASE_INDENT_LEVEL",
2315 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002316 }
2317 }
2318
2319# if/while/etc brace do not go on next line, unless defining a do while loop,
2320# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002321 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002322 my $pre_ctx = "$1$2";
2323
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002324 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002325
2326 if ($line =~ /^\+\t{6,}/) {
2327 WARN("DEEP_INDENTATION",
2328 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2329 }
2330
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002331 my $ctx_cnt = $realcnt - $#ctx - 1;
2332 my $ctx = join("\n", @ctx);
2333
Andy Whitcroft548596d2008-07-23 21:29:01 -07002334 my $ctx_ln = $linenr;
2335 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002336
Andy Whitcroft548596d2008-07-23 21:29:01 -07002337 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2338 defined $lines[$ctx_ln - 1] &&
2339 $lines[$ctx_ln - 1] =~ /^-/)) {
2340 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2341 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002342 $ctx_ln++;
2343 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002344
Andy Whitcroft53210162008-07-23 21:29:03 -07002345 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2346 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002347
2348 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002349 ERROR("OPEN_BRACE",
2350 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002351 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002352 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2354 $ctx =~ /\)\s*\;\s*$/ &&
2355 defined $lines[$ctx_ln - 1])
2356 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002357 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2358 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002359 WARN("TRAILING_SEMICOLON",
2360 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002361 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002362 }
2363 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002364 }
2365
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002366# Check relative indent for conditionals and blocks.
2367 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002368 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2369 ctx_statement_block($linenr, $realcnt, 0)
2370 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002371 my ($s, $c) = ($stat, $cond);
2372
2373 substr($s, 0, length($c), '');
2374
2375 # Make sure we remove the line prefixes as we have
2376 # none on the first line, and are going to readd them
2377 # where necessary.
2378 $s =~ s/\n./\n/gs;
2379
2380 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002381 my @newlines = ($c =~ /\n/gs);
2382 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002383
2384 # We want to check the first line inside the block
2385 # starting at the end of the conditional, so remove:
2386 # 1) any blank line termination
2387 # 2) any opening brace { on end of the line
2388 # 3) any do (...) {
2389 my $continuation = 0;
2390 my $check = 0;
2391 $s =~ s/^.*\bdo\b//;
2392 $s =~ s/^\s*{//;
2393 if ($s =~ s/^\s*\\//) {
2394 $continuation = 1;
2395 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002396 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002397 $check = 1;
2398 $cond_lines++;
2399 }
2400
2401 # Also ignore a loop construct at the end of a
2402 # preprocessor statement.
2403 if (($prevline =~ /^.\s*#\s*define\s/ ||
2404 $prevline =~ /\\\s*$/) && $continuation == 0) {
2405 $check = 0;
2406 }
2407
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002408 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002409 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002410 while ($cond_ptr != $cond_lines) {
2411 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002412
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002413 # If we see an #else/#elif then the code
2414 # is not linear.
2415 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2416 $check = 0;
2417 }
2418
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002419 # Ignore:
2420 # 1) blank lines, they should be at 0,
2421 # 2) preprocessor lines, and
2422 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002423 if ($continuation ||
2424 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002425 $s =~ /^\s*#\s*?/ ||
2426 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002427 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002428 if ($s =~ s/^.*?\n//) {
2429 $cond_lines++;
2430 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002431 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002432 }
2433
2434 my (undef, $sindent) = line_stats("+" . $s);
2435 my $stat_real = raw_line($linenr, $cond_lines);
2436
2437 # Check if either of these lines are modified, else
2438 # this is not this patch's fault.
2439 if (!defined($stat_real) ||
2440 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2441 $check = 0;
2442 }
2443 if (defined($stat_real) && $cond_lines > 1) {
2444 $stat_real = "[...]\n$stat_real";
2445 }
2446
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002447 #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 -07002448
2449 if ($check && (($sindent % 8) != 0 ||
2450 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002451 WARN("SUSPECT_CODE_INDENT",
2452 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002453 }
2454 }
2455
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002456 # Track the 'values' across context and added lines.
2457 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002458 my ($curr_values, $curr_vars) =
2459 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002460 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002461 if ($dbg_values) {
2462 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002463 print "$linenr > .$outline\n";
2464 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002465 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002466 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002467 $prev_values = substr($curr_values, -1);
2468
Andy Whitcroft00df3442007-06-08 13:47:06 -07002469#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002470 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002471
Andy Whitcroft653d4872007-06-23 17:16:34 -07002472# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002473 if ($dbg_type) {
2474 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002475 ERROR("TEST_TYPE",
2476 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002477 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002478 ERROR("TEST_NOT_TYPE",
2479 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002480 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002481 next;
2482 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002483# TEST: allow direct testing of the attribute matcher.
2484 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002485 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002486 ERROR("TEST_ATTR",
2487 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002488 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002489 ERROR("TEST_NOT_ATTR",
2490 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002491 }
2492 next;
2493 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002494
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002495# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002496 if ($line =~ /^.\s*{/ &&
2497 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002498 ERROR("OPEN_BRACE",
2499 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002500 }
2501
Andy Whitcroft653d4872007-06-23 17:16:34 -07002502#
2503# Checks which are anchored on the added line.
2504#
2505
2506# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002507 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002508 my $path = $1;
2509 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002510 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002511 "malformed #include filename\n" . $herecurr);
2512 }
2513 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2514 ERROR("UAPI_INCLUDE",
2515 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002516 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002517 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002518
2519# no C99 // comments
2520 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002521 if (ERROR("C99_COMMENTS",
2522 "do not use C99 // comments\n" . $herecurr) &&
2523 $fix) {
2524 my $line = $fixed[$linenr - 1];
2525 if ($line =~ /\/\/(.*)$/) {
2526 my $comment = trim($1);
2527 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2528 }
2529 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002530 }
2531 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002532 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002533 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002534
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002535# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2536# the whole statement.
2537#print "APW <$lines[$realline_next - 1]>\n";
2538 if (defined $realline_next &&
2539 exists $lines[$realline_next - 1] &&
2540 !defined $suppress_export{$realline_next} &&
2541 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2542 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002543 # Handle definitions which produce identifiers with
2544 # a prefix:
2545 # XXX(foo);
2546 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002547 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002548 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002549 $name =~ /^${Ident}_$2/) {
2550#print "FOO C name<$name>\n";
2551 $suppress_export{$realline_next} = 1;
2552
2553 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002554 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002555 ^.DEFINE_$Ident\(\Q$name\E\)|
2556 ^.DECLARE_$Ident\(\Q$name\E\)|
2557 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002558 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2559 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002560 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002561#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2562 $suppress_export{$realline_next} = 2;
2563 } else {
2564 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002565 }
2566 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002567 if (!defined $suppress_export{$linenr} &&
2568 $prevline =~ /^.\s*$/ &&
2569 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2570 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2571#print "FOO B <$lines[$linenr - 1]>\n";
2572 $suppress_export{$linenr} = 2;
2573 }
2574 if (defined $suppress_export{$linenr} &&
2575 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002576 WARN("EXPORT_SYMBOL",
2577 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002578 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002579
Joe Eloff5150bda2010-08-09 17:21:00 -07002580# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002581 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2582 if (ERROR("GLOBAL_INITIALISERS",
2583 "do not initialise globals to 0 or NULL\n" .
2584 $herecurr) &&
2585 $fix) {
2586 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2587 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002588 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002589# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002590 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2591 if (ERROR("INITIALISED_STATIC",
2592 "do not initialise statics to 0 or NULL\n" .
2593 $herecurr) &&
2594 $fix) {
2595 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2596 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002597 }
2598
Joe Perchescb710ec2010-10-26 14:23:20 -07002599# check for static const char * arrays.
2600 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002601 WARN("STATIC_CONST_CHAR_ARRAY",
2602 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002603 $herecurr);
2604 }
2605
2606# check for static char foo[] = "bar" declarations.
2607 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002608 WARN("STATIC_CONST_CHAR_ARRAY",
2609 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002610 $herecurr);
2611 }
2612
Joe Perches93ed0e22010-10-26 14:23:21 -07002613# check for declarations of struct pci_device_id
2614 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002615 WARN("DEFINE_PCI_DEVICE_TABLE",
2616 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002617 }
2618
Andy Whitcroft653d4872007-06-23 17:16:34 -07002619# check for new typedefs, only function parameters and sparse annotations
2620# make sense.
2621 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002622 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002623 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002624 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002625 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002626 WARN("NEW_TYPEDEFS",
2627 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002628 }
2629
2630# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002631 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002632 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2633 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002634 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002635
Andy Whitcroft65863862009-01-06 14:41:21 -08002636 # Should start with a space.
2637 $to =~ s/^(\S)/ $1/;
2638 # Should not end with a space.
2639 $to =~ s/\s+$//;
2640 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002641 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002642 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002643
Joe Perches3705ce52013-07-03 15:05:31 -07002644## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002645 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002646 if (ERROR("POINTER_LOCATION",
2647 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2648 $fix) {
2649 my $sub_from = $ident;
2650 my $sub_to = $ident;
2651 $sub_to =~ s/\Q$from\E/$to/;
2652 $fixed[$linenr - 1] =~
2653 s@\Q$sub_from\E@$sub_to@;
2654 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002655 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002656 }
2657 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2658 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002659 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002660
Andy Whitcroft65863862009-01-06 14:41:21 -08002661 # Should start with a space.
2662 $to =~ s/^(\S)/ $1/;
2663 # Should not end with a space.
2664 $to =~ s/\s+$//;
2665 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002666 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002667 }
2668 # Modifiers should have spaces.
2669 $to =~ s/(\b$Modifier$)/$1 /;
2670
Joe Perches3705ce52013-07-03 15:05:31 -07002671## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002672 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002673 if (ERROR("POINTER_LOCATION",
2674 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2675 $fix) {
2676
2677 my $sub_from = $match;
2678 my $sub_to = $match;
2679 $sub_to =~ s/\Q$from\E/$to/;
2680 $fixed[$linenr - 1] =~
2681 s@\Q$sub_from\E@$sub_to@;
2682 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002683 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002684 }
2685
2686# # no BUG() or BUG_ON()
2687# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2688# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2689# print "$herecurr";
2690# $clean = 0;
2691# }
2692
Andy Whitcroft8905a672007-11-28 16:21:06 -08002693 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002694 WARN("LINUX_VERSION_CODE",
2695 "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 -08002696 }
2697
Joe Perches17441222011-06-15 15:08:17 -07002698# check for uses of printk_ratelimit
2699 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002700 WARN("PRINTK_RATELIMITED",
2701"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002702 }
2703
Andy Whitcroft00df3442007-06-08 13:47:06 -07002704# printk should use KERN_* levels. Note that follow on printk's on the
2705# same line do not need a level, so we use the current block context
2706# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002707# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002708# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002709 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002710 my $ok = 0;
2711 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2712 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002713 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002714 # with "\n" ignore it, else it is to blame
2715 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2716 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2717 $ok = 1;
2718 }
2719 last;
2720 }
2721 }
2722 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002723 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2724 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002725 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002726 }
2727
Joe Perches243f3802012-05-31 16:26:09 -07002728 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2729 my $orig = $1;
2730 my $level = lc($orig);
2731 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002732 my $level2 = $level;
2733 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002734 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002735 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002736 }
2737
2738 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002739 if (WARN("PREFER_PR_LEVEL",
2740 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2741 $fix) {
2742 $fixed[$linenr - 1] =~
2743 s/\bpr_warning\b/pr_warn/;
2744 }
Joe Perches243f3802012-05-31 16:26:09 -07002745 }
2746
Joe Perchesdc139312013-02-21 16:44:13 -08002747 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2748 my $orig = $1;
2749 my $level = lc($orig);
2750 $level = "warn" if ($level eq "warning");
2751 $level = "dbg" if ($level eq "debug");
2752 WARN("PREFER_DEV_LEVEL",
2753 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2754 }
2755
Andy Whitcroft653d4872007-06-23 17:16:34 -07002756# function brace can't be on same line, except for #defines of do while,
2757# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002758 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2759 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002760 ERROR("OPEN_BRACE",
2761 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002762 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002763
Andy Whitcroft8905a672007-11-28 16:21:06 -08002764# open braces for enum, union and struct go on the same line.
2765 if ($line =~ /^.\s*{/ &&
2766 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002767 ERROR("OPEN_BRACE",
2768 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002769 }
2770
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002771# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002772 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2773 if (WARN("SPACING",
2774 "missing space after $1 definition\n" . $herecurr) &&
2775 $fix) {
2776 $fixed[$linenr - 1] =~
2777 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2778 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002779 }
2780
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002781# check for spacing round square brackets; allowed:
2782# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002783# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2784# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002785 while ($line =~ /(.*?\s)\[/g) {
2786 my ($where, $prefix) = ($-[1], $1);
2787 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002788 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002789 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002790 if (ERROR("BRACKET_SPACE",
2791 "space prohibited before open square bracket '['\n" . $herecurr) &&
2792 $fix) {
2793 $fixed[$linenr - 1] =~
2794 s/^(\+.*?)\s+\[/$1\[/;
2795 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002796 }
2797 }
2798
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002799# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002800 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002801 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002802 my $ctx_before = substr($line, 0, $-[1]);
2803 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002804
2805 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002806 if ($name =~ /^(?:
2807 if|for|while|switch|return|case|
2808 volatile|__volatile__|
2809 __attribute__|format|__extension__|
2810 asm|__asm__)$/x)
2811 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002812 # cpp #define statements have non-optional spaces, ie
2813 # if there is a space between the name and the open
2814 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002815 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002816
2817 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002818 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002819
2820 # If this whole things ends with a type its most
2821 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002822 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002823
2824 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002825 if (WARN("SPACING",
2826 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2827 $fix) {
2828 $fixed[$linenr - 1] =~
2829 s/\b$name\s+\(/$name\(/;
2830 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002831 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002832 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002833
Andy Whitcroft653d4872007-06-23 17:16:34 -07002834# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002835 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002836 my $fixed_line = "";
2837 my $line_fixed = 0;
2838
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002839 my $ops = qr{
2840 <<=|>>=|<=|>=|==|!=|
2841 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2842 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002843 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08002844 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002845 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002846 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002847
2848## print("element count: <" . $#elements . ">\n");
2849## foreach my $el (@elements) {
2850## print("el: <$el>\n");
2851## }
2852
2853 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002854 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002855
Joe Perches3705ce52013-07-03 15:05:31 -07002856 foreach my $el (@elements) {
2857 push(@fix_elements, substr($rawline, $off, length($el)));
2858 $off += length($el);
2859 }
2860
2861 $off = 0;
2862
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002863 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07002864 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002865
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002866 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002867
2868 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2869
2870## print("n: <$n> good: <$good>\n");
2871
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002872 $off += length($elements[$n]);
2873
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002874 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002875 my $ca = substr($opline, 0, $off);
2876 my $cc = '';
2877 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2878 $cc = substr($opline, $off + length($elements[$n + 1]));
2879 }
2880 my $cb = "$ca$;$cc";
2881
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002882 my $a = '';
2883 $a = 'V' if ($elements[$n] ne '');
2884 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002885 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002886 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2887 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002888 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002889
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002890 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002891
2892 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002893 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002894 $c = 'V' if ($elements[$n + 2] ne '');
2895 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002896 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002897 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2898 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002899 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002900 } else {
2901 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002902 }
2903
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002904 my $ctx = "${a}x${c}";
2905
2906 my $at = "(ctx:$ctx)";
2907
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002908 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002909 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002910
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002911 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002912 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002913
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002914 # Get the full operator variant.
2915 my $opv = $op . substr($curr_vars, $off, 1);
2916
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002917 # Ignore operators passed as parameters.
2918 if ($op_type ne 'V' &&
2919 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2920
Andy Whitcroftcf655042008-03-04 14:28:20 -08002921# # Ignore comments
2922# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002923
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002924 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002925 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002926 if ($ctx !~ /.x[WEBC]/ &&
2927 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002928 if (ERROR("SPACING",
2929 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002930 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002931 $line_fixed = 1;
2932 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002933 }
2934
2935 # // is a comment
2936 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002937
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002938 # No spaces for:
2939 # ->
2940 # : when part of a bitfield
2941 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002942 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002943 if (ERROR("SPACING",
2944 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002945 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002946 if (defined $fix_elements[$n + 2]) {
2947 $fix_elements[$n + 2] =~ s/^\s+//;
2948 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002949 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002950 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002951 }
2952
2953 # , must have a space on the right.
2954 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002955 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002956 if (ERROR("SPACING",
2957 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002958 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002959 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07002960 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07002961 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002962 }
2963
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002964 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002965 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002966 #warn "'*' is part of type\n";
2967
2968 # unary operators should have a space before and
2969 # none after. May be left adjacent to another
2970 # unary operator, or a cast
2971 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002972 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002973 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002974 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002975 if (ERROR("SPACING",
2976 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002977 if ($n != $last_after + 2) {
2978 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
2979 $line_fixed = 1;
2980 }
Joe Perches3705ce52013-07-03 15:05:31 -07002981 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002982 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002983 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002984 # A unary '*' may be const
2985
2986 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002987 if (ERROR("SPACING",
2988 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002989 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002990 if (defined $fix_elements[$n + 2]) {
2991 $fix_elements[$n + 2] =~ s/^\s+//;
2992 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002993 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002994 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002995 }
2996
2997 # unary ++ and unary -- are allowed no space on one side.
2998 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002999 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003000 if (ERROR("SPACING",
3001 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003002 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003003 $line_fixed = 1;
3004 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003005 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003006 if ($ctx =~ /Wx[BE]/ ||
3007 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003008 if (ERROR("SPACING",
3009 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003010 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003011 $line_fixed = 1;
3012 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003013 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003014 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003015 if (ERROR("SPACING",
3016 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003017 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003018 if (defined $fix_elements[$n + 2]) {
3019 $fix_elements[$n + 2] =~ s/^\s+//;
3020 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003021 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003022 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003023 }
3024
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003025 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003026 } elsif ($op eq '<<' or $op eq '>>' or
3027 $op eq '&' or $op eq '^' or $op eq '|' or
3028 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003029 $op eq '*' or $op eq '/' or
3030 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003031 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003032 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003033 if (ERROR("SPACING",
3034 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003035 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3036 if (defined $fix_elements[$n + 2]) {
3037 $fix_elements[$n + 2] =~ s/^\s+//;
3038 }
Joe Perches3705ce52013-07-03 15:05:31 -07003039 $line_fixed = 1;
3040 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003041 }
3042
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003043 # A colon needs no spaces before when it is
3044 # terminating a case value or a label.
3045 } elsif ($opv eq ':C' || $opv eq ':L') {
3046 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003047 if (ERROR("SPACING",
3048 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003049 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003050 $line_fixed = 1;
3051 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003052 }
3053
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003054 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003055 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003056 my $ok = 0;
3057
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003058 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003059 if (($op eq '<' &&
3060 $cc =~ /^\S+\@\S+>/) ||
3061 ($op eq '>' &&
3062 $ca =~ /<\S+\@\S+$/))
3063 {
3064 $ok = 1;
3065 }
3066
Joe Perches84731622013-11-12 15:10:05 -08003067 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003068 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003069 my $msg_type = \&ERROR;
3070 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3071
3072 if (&{$msg_type}("SPACING",
3073 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003074 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3075 if (defined $fix_elements[$n + 2]) {
3076 $fix_elements[$n + 2] =~ s/^\s+//;
3077 }
Joe Perches3705ce52013-07-03 15:05:31 -07003078 $line_fixed = 1;
3079 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003080 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003081 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003082 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003083
3084## print("n: <$n> GOOD: <$good>\n");
3085
3086 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003087 }
Joe Perches3705ce52013-07-03 15:05:31 -07003088
3089 if (($#elements % 2) == 0) {
3090 $fixed_line = $fixed_line . $fix_elements[$#elements];
3091 }
3092
3093 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3094 $fixed[$linenr - 1] = $fixed_line;
3095 }
3096
3097
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003098 }
3099
Joe Perches786b6322013-07-03 15:05:32 -07003100# check for whitespace before a non-naked semicolon
3101 if ($line =~ /^\+.*\S\s+;/) {
3102 if (WARN("SPACING",
3103 "space prohibited before semicolon\n" . $herecurr) &&
3104 $fix) {
3105 1 while $fixed[$linenr - 1] =~
3106 s/^(\+.*\S)\s+;/$1;/;
3107 }
3108 }
3109
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003110# check for multiple assignments
3111 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003112 CHK("MULTIPLE_ASSIGNMENTS",
3113 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003114 }
3115
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003116## # check for multiple declarations, allowing for a function declaration
3117## # continuation.
3118## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3119## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3120##
3121## # Remove any bracketed sections to ensure we do not
3122## # falsly report the parameters of functions.
3123## my $ln = $line;
3124## while ($ln =~ s/\([^\(\)]*\)//g) {
3125## }
3126## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003127## WARN("MULTIPLE_DECLARATION",
3128## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003129## }
3130## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003131
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003132#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003133 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3134 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003135 if (ERROR("SPACING",
3136 "space required before the open brace '{'\n" . $herecurr) &&
3137 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003138 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003139 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003140 }
3141
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003142## # check for blank lines before declarations
3143## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3144## $prevrawline =~ /^.\s*$/) {
3145## WARN("SPACING",
3146## "No blank lines before declarations\n" . $hereprev);
3147## }
3148##
3149
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003150# closing brace should have a space following it when it has anything
3151# on the line
3152 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003153 if (ERROR("SPACING",
3154 "space required after that close brace '}'\n" . $herecurr) &&
3155 $fix) {
3156 $fixed[$linenr - 1] =~
3157 s/}((?!(?:,|;|\)))\S)/} $1/;
3158 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003159 }
3160
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003161# check spacing on square brackets
3162 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003163 if (ERROR("SPACING",
3164 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3165 $fix) {
3166 $fixed[$linenr - 1] =~
3167 s/\[\s+/\[/;
3168 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003169 }
3170 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003171 if (ERROR("SPACING",
3172 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3173 $fix) {
3174 $fixed[$linenr - 1] =~
3175 s/\s+\]/\]/;
3176 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003177 }
3178
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003179# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003180 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3181 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003182 if (ERROR("SPACING",
3183 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3184 $fix) {
3185 $fixed[$linenr - 1] =~
3186 s/\(\s+/\(/;
3187 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003188 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003189 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003190 $line !~ /for\s*\(.*;\s+\)/ &&
3191 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003192 if (ERROR("SPACING",
3193 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3194 $fix) {
3195 $fixed[$linenr - 1] =~
3196 s/\s+\)/\)/;
3197 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003198 }
3199
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003200#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003201 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003202 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003203 if (WARN("INDENTED_LABEL",
3204 "labels should not be indented\n" . $herecurr) &&
3205 $fix) {
3206 $fixed[$linenr - 1] =~
3207 s/^(.)\s+/$1/;
3208 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003209 }
3210
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003211# Return is not a function.
3212 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3213 my $spacing = $1;
3214 my $value = $2;
3215
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003216 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003217 $value =~ s/\(/ \(/g;
3218 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003219 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003220 $value !~ /(?:$Ident|-?$Constant)\s*
3221 $Compare\s*
3222 (?:$Ident|-?$Constant)/x &&
3223 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003224 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003225#print "value<$value>\n";
3226 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003227 ERROR("RETURN_PARENTHESES",
3228 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003229
3230 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003231 ERROR("SPACING",
3232 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003233 }
3234 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003235# Return of what appears to be an errno should normally be -'ve
3236 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3237 my $name = $1;
3238 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003239 WARN("USE_NEGATIVE_ERRNO",
3240 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003241 }
3242 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003243
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003244# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003245 if ($line =~ /\b(if|while|for|switch)\(/) {
3246 if (ERROR("SPACING",
3247 "space required before the open parenthesis '('\n" . $herecurr) &&
3248 $fix) {
3249 $fixed[$linenr - 1] =~
3250 s/\b(if|while|for|switch)\(/$1 \(/;
3251 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003252 }
3253
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003254# Check for illegal assignment in if conditional -- and check for trailing
3255# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003256 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003257 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3258 ctx_statement_block($linenr, $realcnt, 0)
3259 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003260 my ($stat_next) = ctx_statement_block($line_nr_next,
3261 $remain_next, $off_next);
3262 $stat_next =~ s/\n./\n /g;
3263 ##print "stat<$stat> stat_next<$stat_next>\n";
3264
3265 if ($stat_next =~ /^\s*while\b/) {
3266 # If the statement carries leading newlines,
3267 # then count those as offsets.
3268 my ($whitespace) =
3269 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3270 my $offset =
3271 statement_rawlines($whitespace) - 1;
3272
3273 $suppress_whiletrailers{$line_nr_next +
3274 $offset} = 1;
3275 }
3276 }
3277 if (!defined $suppress_whiletrailers{$linenr} &&
3278 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003279 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003280
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003281 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003282 ERROR("ASSIGN_IN_IF",
3283 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003284 }
3285
3286 # Find out what is on the end of the line after the
3287 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003288 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003289 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003290 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003291 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3292 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003293 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003294 # Find out how long the conditional actually is.
3295 my @newlines = ($c =~ /\n/gs);
3296 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003297 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003298
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003299 $stat_real = raw_line($linenr, $cond_lines)
3300 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003301 if (defined($stat_real) && $cond_lines > 1) {
3302 $stat_real = "[...]\n$stat_real";
3303 }
3304
Joe Perches000d1cc12011-07-25 17:13:25 -07003305 ERROR("TRAILING_STATEMENTS",
3306 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003307 }
3308 }
3309
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003310# Check for bitwise tests written as boolean
3311 if ($line =~ /
3312 (?:
3313 (?:\[|\(|\&\&|\|\|)
3314 \s*0[xX][0-9]+\s*
3315 (?:\&\&|\|\|)
3316 |
3317 (?:\&\&|\|\|)
3318 \s*0[xX][0-9]+\s*
3319 (?:\&\&|\|\||\)|\])
3320 )/x)
3321 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003322 WARN("HEXADECIMAL_BOOLEAN_TEST",
3323 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003324 }
3325
Andy Whitcroft8905a672007-11-28 16:21:06 -08003326# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003327 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3328 my $s = $1;
3329 $s =~ s/$;//g; # Remove any comments
3330 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003331 ERROR("TRAILING_STATEMENTS",
3332 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003333 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003334 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003335# if should not continue a brace
3336 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003337 ERROR("TRAILING_STATEMENTS",
3338 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003339 $herecurr);
3340 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003341# case and default should not have general statements after them
3342 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3343 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003344 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003345 \s*return\s+
3346 )/xg)
3347 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003348 ERROR("TRAILING_STATEMENTS",
3349 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003350 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003351
3352 # Check for }<nl>else {, these must be at the same
3353 # indent level to be relevant to each other.
3354 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3355 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003356 ERROR("ELSE_AFTER_BRACE",
3357 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003358 }
3359
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003360 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3361 $previndent == $indent) {
3362 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3363
3364 # Find out what is on the end of the line after the
3365 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003366 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003367 $s =~ s/\n.*//g;
3368
3369 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003370 ERROR("WHILE_AFTER_BRACE",
3371 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003372 }
3373 }
3374
Joe Perches95e2c602013-07-03 15:05:20 -07003375#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003376 while ($line =~ m{($Constant|$Lval)}g) {
3377 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003378
3379#gcc binary extension
3380 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003381 if (WARN("GCC_BINARY_CONSTANT",
3382 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3383 $fix) {
3384 my $hexval = sprintf("0x%x", oct($var));
3385 $fixed[$linenr - 1] =~
3386 s/\b$var\b/$hexval/;
3387 }
Joe Perches95e2c602013-07-03 15:05:20 -07003388 }
3389
3390#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003391 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003392 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003393#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003394 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003395#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003396 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003397 while ($var =~ m{($Ident)}g) {
3398 my $word = $1;
3399 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003400 if ($check) {
3401 seed_camelcase_includes();
3402 if (!$file && !$camelcase_file_seeded) {
3403 seed_camelcase_file($realfile);
3404 $camelcase_file_seeded = 1;
3405 }
3406 }
Joe Perches7e781f62013-09-11 14:23:55 -07003407 if (!defined $camelcase{$word}) {
3408 $camelcase{$word} = 1;
3409 CHK("CAMELCASE",
3410 "Avoid CamelCase: <$word>\n" . $herecurr);
3411 }
Joe Perches34456862013-07-03 15:05:34 -07003412 }
Joe Perches323c1262012-12-17 16:02:07 -08003413 }
3414 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003415
3416#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003417 if ($line =~ /\#\s*define.*\\\s+$/) {
3418 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3419 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3420 $fix) {
3421 $fixed[$linenr - 1] =~ s/\s+$//;
3422 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003423 }
3424
Andy Whitcroft653d4872007-06-23 17:16:34 -07003425#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003426 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003427 my $file = "$1.h";
3428 my $checkfile = "include/linux/$file";
3429 if (-f "$root/$checkfile" &&
3430 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003431 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003432 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003433 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003434 CHK("ARCH_INCLUDE_LINUX",
3435 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003436 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003437 WARN("INCLUDE_LINUX",
3438 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003439 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003440 }
3441 }
3442
Andy Whitcroft653d4872007-06-23 17:16:34 -07003443# multi-statement macros should be enclosed in a do while loop, grab the
3444# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003445# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003446 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3447 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003448 my $ln = $linenr;
3449 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003450 my ($off, $dstat, $dcond, $rest);
3451 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003452 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003453 ctx_statement_block($linenr, $realcnt, 0);
3454 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003455 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003456 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003457
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003458 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003459 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003460 $dstat =~ s/\\\n.//g;
3461 $dstat =~ s/^\s*//s;
3462 $dstat =~ s/\s*$//s;
3463
3464 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003465 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3466 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003467 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003468 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003469 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003470
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003471 # Flatten any obvious string concatentation.
3472 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3473 $dstat =~ s/$Ident\s*("X*")/$1/)
3474 {
3475 }
3476
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003477 my $exceptions = qr{
3478 $Declare|
3479 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003480 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003481 DECLARE_PER_CPU|
3482 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003483 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003484 union|
3485 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003486 \.$Ident\s*=\s*|
3487 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003488 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003489 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003490 if ($dstat ne '' &&
3491 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3492 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003493 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003494 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003495 $dstat !~ /$exceptions/ &&
3496 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003497 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003498 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003499 $dstat !~ /^for\s*$Constant$/ && # for (...)
3500 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3501 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003502 $dstat !~ /^\({/ && # ({...
3503 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003504 {
3505 $ctx =~ s/\n*$//;
3506 my $herectx = $here . "\n";
3507 my $cnt = statement_rawlines($ctx);
3508
3509 for (my $n = 0; $n < $cnt; $n++) {
3510 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003511 }
3512
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003513 if ($dstat =~ /;/) {
3514 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3515 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3516 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003517 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003518 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003519 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003520 }
Joe Perches5023d342012-12-17 16:01:47 -08003521
Joe Perches481eb482012-12-17 16:01:56 -08003522# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003523
3524 } else {
3525 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003526 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3527 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003528 $line =~ /^\+.*\\$/) {
3529 WARN("LINE_CONTINUATIONS",
3530 "Avoid unnecessary line continuations\n" . $herecurr);
3531 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003532 }
3533
Joe Perchesb13edf72012-07-30 14:41:24 -07003534# do {} while (0) macro tests:
3535# single-statement macros do not need to be enclosed in do while (0) loop,
3536# macro should not end with a semicolon
3537 if ($^V && $^V ge 5.10.0 &&
3538 $realfile !~ m@/vmlinux.lds.h$@ &&
3539 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3540 my $ln = $linenr;
3541 my $cnt = $realcnt;
3542 my ($off, $dstat, $dcond, $rest);
3543 my $ctx = '';
3544 ($dstat, $dcond, $ln, $cnt, $off) =
3545 ctx_statement_block($linenr, $realcnt, 0);
3546 $ctx = $dstat;
3547
3548 $dstat =~ s/\\\n.//g;
3549
3550 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3551 my $stmts = $2;
3552 my $semis = $3;
3553
3554 $ctx =~ s/\n*$//;
3555 my $cnt = statement_rawlines($ctx);
3556 my $herectx = $here . "\n";
3557
3558 for (my $n = 0; $n < $cnt; $n++) {
3559 $herectx .= raw_line($linenr, $n) . "\n";
3560 }
3561
Joe Perchesac8e97f2012-08-21 16:15:53 -07003562 if (($stmts =~ tr/;/;/) == 1 &&
3563 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003564 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3565 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3566 }
3567 if (defined $semis && $semis ne "") {
3568 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3569 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3570 }
3571 }
3572 }
3573
Mike Frysinger080ba922009-01-06 14:41:25 -08003574# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3575# all assignments may have only one of the following with an assignment:
3576# .
3577# ALIGN(...)
3578# VMLINUX_SYMBOL(...)
3579 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003580 WARN("MISSING_VMLINUX_SYMBOL",
3581 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003582 }
3583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003584# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003585 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3586 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003587 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003588 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003589 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3590 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003591 my @allowed = ();
3592 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003593 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003594 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003595 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003596 for my $chunk (@chunks) {
3597 my ($cond, $block) = @{$chunk};
3598
Andy Whitcroft773647a2008-03-28 14:15:58 -07003599 # If the condition carries leading newlines, then count those as offsets.
3600 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3601 my $offset = statement_rawlines($whitespace) - 1;
3602
Joe Perchesaad4f612012-03-23 15:02:19 -07003603 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003604 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3605
3606 # We have looked at and allowed this specific line.
3607 $suppress_ifbraces{$ln + $offset} = 1;
3608
3609 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003610 $ln += statement_rawlines($block) - 1;
3611
Andy Whitcroft773647a2008-03-28 14:15:58 -07003612 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003613
3614 $seen++ if ($block =~ /^\s*{/);
3615
Joe Perchesaad4f612012-03-23 15:02:19 -07003616 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003617 if (statement_lines($cond) > 1) {
3618 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003619 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003620 }
3621 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003622 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003623 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003624 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003625 if (statement_block_size($block) > 1) {
3626 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003627 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003628 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003629 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003630 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003631 if ($seen) {
3632 my $sum_allowed = 0;
3633 foreach (@allowed) {
3634 $sum_allowed += $_;
3635 }
3636 if ($sum_allowed == 0) {
3637 WARN("BRACES",
3638 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3639 } elsif ($sum_allowed != $allow &&
3640 $seen != $allow) {
3641 CHK("BRACES",
3642 "braces {} should be used on all arms of this statement\n" . $herectx);
3643 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003644 }
3645 }
3646 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003647 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003648 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003649 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003650
Andy Whitcroftcf655042008-03-04 14:28:20 -08003651 # Check the pre-context.
3652 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3653 #print "APW: ALLOWED: pre<$1>\n";
3654 $allowed = 1;
3655 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003656
3657 my ($level, $endln, @chunks) =
3658 ctx_statement_full($linenr, $realcnt, $-[0]);
3659
Andy Whitcroftcf655042008-03-04 14:28:20 -08003660 # Check the condition.
3661 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003662 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003663 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003664 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003665 }
3666 if (statement_lines($cond) > 1) {
3667 #print "APW: ALLOWED: cond<$cond>\n";
3668 $allowed = 1;
3669 }
3670 if ($block =~/\b(?:if|for|while)\b/) {
3671 #print "APW: ALLOWED: block<$block>\n";
3672 $allowed = 1;
3673 }
3674 if (statement_block_size($block) > 1) {
3675 #print "APW: ALLOWED: lines block<$block>\n";
3676 $allowed = 1;
3677 }
3678 # Check the post-context.
3679 if (defined $chunks[1]) {
3680 my ($cond, $block) = @{$chunks[1]};
3681 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003682 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003683 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003684 if ($block =~ /^\s*\{/) {
3685 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3686 $allowed = 1;
3687 }
3688 }
3689 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003690 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003691 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003692
Andy Whitcroftf0556632008-10-15 22:02:23 -07003693 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003694 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003695 }
3696
Joe Perches000d1cc12011-07-25 17:13:25 -07003697 WARN("BRACES",
3698 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003699 }
3700 }
3701
Joe Perches0979ae62012-12-17 16:01:59 -08003702# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003703 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003704 CHK("BRACES",
3705 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3706 }
Joe Perches77b9a532013-07-03 15:05:29 -07003707 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003708 CHK("BRACES",
3709 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3710 }
3711
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003712# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003713 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3714 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003715 WARN("VOLATILE",
3716 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003717 }
3718
Andy Whitcroft00df3442007-06-08 13:47:06 -07003719# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003720 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003721 CHK("REDUNDANT_CODE",
3722 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003723 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003724 }
3725
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003726# check for needless "if (<foo>) fn(<foo>)" uses
3727 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3728 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3729 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3730 WARN('NEEDLESS_IF',
3731 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003732 }
3733 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003734
Joe Perches8716de32013-09-11 14:24:05 -07003735sub string_find_replace {
3736 my ($string, $find, $replace) = @_;
3737
3738 $string =~ s/$find/$replace/g;
3739
3740 return $string;
3741}
3742
3743# check for bad placement of section $InitAttribute (e.g.: __initdata)
3744 if ($line =~ /(\b$InitAttribute\b)/) {
3745 my $attr = $1;
3746 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3747 my $ptr = $1;
3748 my $var = $2;
3749 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3750 ERROR("MISPLACED_INIT",
3751 "$attr should be placed after $var\n" . $herecurr)) ||
3752 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3753 WARN("MISPLACED_INIT",
3754 "$attr should be placed after $var\n" . $herecurr))) &&
3755 $fix) {
3756 $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;
3757 }
3758 }
3759 }
3760
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003761# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003762 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003763 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003764 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003765 CHK("USLEEP_RANGE",
3766 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003767 }
3768 }
3769
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003770# warn about unexpectedly long msleep's
3771 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3772 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003773 WARN("MSLEEP",
3774 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003775 }
3776 }
3777
Joe Perches36ec1932013-07-03 15:05:25 -07003778# check for comparisons of jiffies
3779 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3780 WARN("JIFFIES_COMPARISON",
3781 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3782 }
3783
Joe Perches9d7a34a2013-07-03 15:05:26 -07003784# check for comparisons of get_jiffies_64()
3785 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3786 WARN("JIFFIES_COMPARISON",
3787 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3788 }
3789
Andy Whitcroft00df3442007-06-08 13:47:06 -07003790# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003791# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003792# print "#ifdef in C files should be avoided\n";
3793# print "$herecurr";
3794# $clean = 0;
3795# }
3796
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003797# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003798 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003799 if (ERROR("SPACING",
3800 "exactly one space required after that #$1\n" . $herecurr) &&
3801 $fix) {
3802 $fixed[$linenr - 1] =~
3803 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3804 }
3805
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003806 }
3807
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003808# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003809 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3810 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003811 my $which = $1;
3812 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003813 CHK("UNCOMMENTED_DEFINITION",
3814 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003815 }
3816 }
3817# check for memory barriers without a comment.
3818 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3819 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003820 CHK("MEMORY_BARRIER",
3821 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003822 }
3823 }
3824# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003825 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003826 CHK("ARCH_DEFINES",
3827 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003828 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003829
Tobias Klauserd4977c72010-05-24 14:33:30 -07003830# Check that the storage class is at the beginning of a declaration
3831 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003832 WARN("STORAGE_CLASS",
3833 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003834 }
3835
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003836# check the location of the inline attribute, that it is between
3837# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003838 if ($line =~ /\b$Type\s+$Inline\b/ ||
3839 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003840 ERROR("INLINE_LOCATION",
3841 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003842 }
3843
Andy Whitcroft8905a672007-11-28 16:21:06 -08003844# Check for __inline__ and __inline, prefer inline
3845 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003846 if (WARN("INLINE",
3847 "plain inline is preferred over $1\n" . $herecurr) &&
3848 $fix) {
3849 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3850
3851 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003852 }
3853
Joe Perches3d130fd2011-01-12 17:00:00 -08003854# Check for __attribute__ packed, prefer __packed
3855 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003856 WARN("PREFER_PACKED",
3857 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003858 }
3859
Joe Perches39b7e282011-07-25 17:13:24 -07003860# Check for __attribute__ aligned, prefer __aligned
3861 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003862 WARN("PREFER_ALIGNED",
3863 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003864 }
3865
Joe Perches5f14d3b2012-01-10 15:09:52 -08003866# Check for __attribute__ format(printf, prefer __printf
3867 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003868 if (WARN("PREFER_PRINTF",
3869 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3870 $fix) {
3871 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3872
3873 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003874 }
3875
Joe Perches6061d942012-03-23 15:02:16 -07003876# Check for __attribute__ format(scanf, prefer __scanf
3877 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003878 if (WARN("PREFER_SCANF",
3879 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3880 $fix) {
3881 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3882 }
Joe Perches6061d942012-03-23 15:02:16 -07003883 }
3884
Joe Perches8f53a9b2010-03-05 13:43:48 -08003885# check for sizeof(&)
3886 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003887 WARN("SIZEOF_ADDRESS",
3888 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003889 }
3890
Joe Perches66c80b62012-07-30 14:41:22 -07003891# check for sizeof without parenthesis
3892 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003893 if (WARN("SIZEOF_PARENTHESIS",
3894 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3895 $fix) {
3896 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3897 }
Joe Perches66c80b62012-07-30 14:41:22 -07003898 }
3899
Joe Perches428e2fd2011-05-24 17:13:39 -07003900# check for line continuations in quoted strings with odd counts of "
3901 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 WARN("LINE_CONTINUATIONS",
3903 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003904 }
3905
Joe Perches88982fe2012-12-17 16:02:00 -08003906# check for struct spinlock declarations
3907 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3908 WARN("USE_SPINLOCK_T",
3909 "struct spinlock should be spinlock_t\n" . $herecurr);
3910 }
3911
Joe Perchesa6962d72013-04-29 16:18:13 -07003912# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08003913 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07003914 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08003915 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003916 if (WARN("PREFER_SEQ_PUTS",
3917 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3918 $fix) {
3919 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3920 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003921 }
3922 }
3923
Andy Whitcroft554e1652012-01-10 15:09:57 -08003924# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003925 if ($^V && $^V ge 5.10.0 &&
3926 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003927 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003928
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003929 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003930 my $ms_val = $7;
3931 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003932
Andy Whitcroft554e1652012-01-10 15:09:57 -08003933 if ($ms_size =~ /^(0x|)0$/i) {
3934 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003935 "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 -08003936 } elsif ($ms_size =~ /^(0x|)1$/i) {
3937 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003938 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3939 }
3940 }
3941
3942# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003943 if ($^V && $^V ge 5.10.0 &&
3944 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003945 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003946 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003947 my $call = $1;
3948 my $cast1 = deparenthesize($2);
3949 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003950 my $cast2 = deparenthesize($7);
3951 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003952 my $cast;
3953
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003954 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003955 $cast = "$cast1 or $cast2";
3956 } elsif ($cast1 ne "") {
3957 $cast = $cast1;
3958 } else {
3959 $cast = $cast2;
3960 }
3961 WARN("MINMAX",
3962 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003963 }
3964 }
3965
Joe Perches4a273192012-07-30 14:41:20 -07003966# check usleep_range arguments
3967 if ($^V && $^V ge 5.10.0 &&
3968 defined $stat &&
3969 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3970 my $min = $1;
3971 my $max = $7;
3972 if ($min eq $max) {
3973 WARN("USLEEP_RANGE",
3974 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3975 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3976 $min > $max) {
3977 WARN("USLEEP_RANGE",
3978 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3979 }
3980 }
3981
Joe Perches70dc8a42013-09-11 14:23:58 -07003982# check for new externs in .h files.
3983 if ($realfile =~ /\.h$/ &&
3984 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07003985 if (CHK("AVOID_EXTERNS",
3986 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07003987 $fix) {
3988 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3989 }
3990 }
3991
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003992# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003993 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003994 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003995 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003996 my $function_name = $1;
3997 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003998
3999 my $s = $stat;
4000 if (defined $cond) {
4001 substr($s, 0, length($cond), '');
4002 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004003 if ($s =~ /^\s*;/ &&
4004 $function_name ne 'uninitialized_var')
4005 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004006 WARN("AVOID_EXTERNS",
4007 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004008 }
4009
4010 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004011 WARN("FUNCTION_ARGUMENTS",
4012 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004013 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004014
4015 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4016 $stat =~ /^.\s*extern\s+/)
4017 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004018 WARN("AVOID_EXTERNS",
4019 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004020 }
4021
4022# checks for new __setup's
4023 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4024 my $name = $1;
4025
4026 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004027 CHK("UNDOCUMENTED_SETUP",
4028 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004029 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004030 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004031
4032# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004033 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004034 WARN("UNNECESSARY_CASTS",
4035 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004036 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004037
Joe Perchesa640d252013-07-03 15:05:21 -07004038# alloc style
4039# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4040 if ($^V && $^V ge 5.10.0 &&
4041 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4042 CHK("ALLOC_SIZEOF_STRUCT",
4043 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4044 }
4045
Joe Perches972fdea2013-04-29 16:18:12 -07004046# check for krealloc arg reuse
4047 if ($^V && $^V ge 5.10.0 &&
4048 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4049 WARN("KREALLOC_ARG_REUSE",
4050 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4051 }
4052
Joe Perches5ce59ae2013-02-21 16:44:18 -08004053# check for alloc argument mismatch
4054 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4055 WARN("ALLOC_ARRAY_ARGS",
4056 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4057 }
4058
Joe Perchescaf2a542011-01-12 16:59:56 -08004059# check for multiple semicolons
4060 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004061 if (WARN("ONE_SEMICOLON",
4062 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4063 $fix) {
4064 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4065 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004066 }
4067
4068# check for switch/default statements without a break;
4069 if ($^V && $^V ge 5.10.0 &&
4070 defined $stat &&
4071 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4072 my $ctx = '';
4073 my $herectx = $here . "\n";
4074 my $cnt = statement_rawlines($stat);
4075 for (my $n = 0; $n < $cnt; $n++) {
4076 $herectx .= raw_line($linenr, $n) . "\n";
4077 }
4078 WARN("DEFAULT_NO_BREAK",
4079 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004080 }
4081
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004082# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004083 if ($line =~ /\b__FUNCTION__\b/) {
4084 if (WARN("USE_FUNC",
4085 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4086 $fix) {
4087 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4088 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004089 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004090
Joe Perches2c924882012-03-23 15:02:20 -07004091# check for use of yield()
4092 if ($line =~ /\byield\s*\(\s*\)/) {
4093 WARN("YIELD",
4094 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4095 }
4096
Joe Perches179f8f42013-07-03 15:05:30 -07004097# check for comparisons against true and false
4098 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4099 my $lead = $1;
4100 my $arg = $2;
4101 my $test = $3;
4102 my $otype = $4;
4103 my $trail = $5;
4104 my $op = "!";
4105
4106 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4107
4108 my $type = lc($otype);
4109 if ($type =~ /^(?:true|false)$/) {
4110 if (("$test" eq "==" && "$type" eq "true") ||
4111 ("$test" eq "!=" && "$type" eq "false")) {
4112 $op = "";
4113 }
4114
4115 CHK("BOOL_COMPARISON",
4116 "Using comparison to $otype is error prone\n" . $herecurr);
4117
4118## maybe suggesting a correct construct would better
4119## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4120
4121 }
4122 }
4123
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004124# check for semaphores initialized locked
4125 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004126 WARN("CONSIDER_COMPLETION",
4127 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004128 }
Joe Perches6712d852012-03-23 15:02:20 -07004129
Joe Perches67d0a072011-10-31 17:13:10 -07004130# recommend kstrto* over simple_strto* and strict_strto*
4131 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004132 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004133 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004134 }
Joe Perches6712d852012-03-23 15:02:20 -07004135
Michael Ellermanf3db6632008-07-23 21:28:57 -07004136# check for __initcall(), use device_initcall() explicitly please
4137 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004138 WARN("USE_DEVICE_INITCALL",
4139 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004140 }
Joe Perches6712d852012-03-23 15:02:20 -07004141
Emese Revfy79404842010-03-05 13:43:53 -08004142# check for various ops structs, ensure they are const.
4143 my $struct_ops = qr{acpi_dock_ops|
4144 address_space_operations|
4145 backlight_ops|
4146 block_device_operations|
4147 dentry_operations|
4148 dev_pm_ops|
4149 dma_map_ops|
4150 extent_io_ops|
4151 file_lock_operations|
4152 file_operations|
4153 hv_ops|
4154 ide_dma_ops|
4155 intel_dvo_dev_ops|
4156 item_operations|
4157 iwl_ops|
4158 kgdb_arch|
4159 kgdb_io|
4160 kset_uevent_ops|
4161 lock_manager_operations|
4162 microcode_ops|
4163 mtrr_ops|
4164 neigh_ops|
4165 nlmsvc_binding|
4166 pci_raw_ops|
4167 pipe_buf_operations|
4168 platform_hibernation_ops|
4169 platform_suspend_ops|
4170 proto_ops|
4171 rpc_pipe_ops|
4172 seq_operations|
4173 snd_ac97_build_ops|
4174 soc_pcmcia_socket_ops|
4175 stacktrace_ops|
4176 sysfs_ops|
4177 tty_operations|
4178 usb_mon_operations|
4179 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004180 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004181 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004182 WARN("CONST_STRUCT",
4183 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004184 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004185 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004186
4187# use of NR_CPUS is usually wrong
4188# ignore definitions of NR_CPUS and usage to define arrays as likely right
4189 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004190 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4191 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004192 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4193 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4194 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004195 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004196 WARN("NR_CPUS",
4197 "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 -07004198 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004199
4200# check for %L{u,d,i} in strings
4201 my $string;
4202 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4203 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004204 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004205 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004206 WARN("PRINTF_L",
4207 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004208 last;
4209 }
4210 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004211
4212# whine mightly about in_atomic
4213 if ($line =~ /\bin_atomic\s*\(/) {
4214 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004215 ERROR("IN_ATOMIC",
4216 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004217 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004218 WARN("IN_ATOMIC",
4219 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004220 }
4221 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004222
4223# check for lockdep_set_novalidate_class
4224 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4225 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4226 if ($realfile !~ m@^kernel/lockdep@ &&
4227 $realfile !~ m@^include/linux/lockdep@ &&
4228 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004229 ERROR("LOCKDEP",
4230 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004231 }
4232 }
Dave Jones88f88312011-01-12 16:59:59 -08004233
4234 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4235 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004236 WARN("EXPORTED_WORLD_WRITABLE",
4237 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004238 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004239 }
4240
4241 # If we have no input at all, then there is nothing to report on
4242 # so just keep quiet.
4243 if ($#rawlines == -1) {
4244 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004245 }
4246
Andy Whitcroft8905a672007-11-28 16:21:06 -08004247 # In mailback mode only produce a report in the negative, for
4248 # things that appear to be patches.
4249 if ($mailback && ($clean == 1 || !$is_patch)) {
4250 exit(0);
4251 }
4252
4253 # This is not a patch, and we are are in 'no-patch' mode so
4254 # just keep quiet.
4255 if (!$chk_patch && !$is_patch) {
4256 exit(0);
4257 }
4258
4259 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004260 ERROR("NOT_UNIFIED_DIFF",
4261 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004262 }
4263 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004264 ERROR("MISSING_SIGN_OFF",
4265 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004266 }
4267
Andy Whitcroft8905a672007-11-28 16:21:06 -08004268 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004269 if ($summary && !($clean == 1 && $quiet == 1)) {
4270 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004271 print "total: $cnt_error errors, $cnt_warn warnings, " .
4272 (($check)? "$cnt_chk checks, " : "") .
4273 "$cnt_lines lines checked\n";
4274 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004275 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004276
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004277 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004278
4279 if ($^V lt 5.10.0) {
4280 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4281 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4282 }
4283
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004284 # If there were whitespace errors which cleanpatch can fix
4285 # then suggest that.
4286 if ($rpt_cleaners) {
4287 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4288 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004289 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004290 }
4291 }
4292
Joe Perches91bfe482013-09-11 14:23:59 -07004293 hash_show_words(\%use_type, "Used");
4294 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004295
Joe Perches3705ce52013-07-03 15:05:31 -07004296 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4297 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4298 my $linecount = 0;
4299 my $f;
4300
4301 open($f, '>', $newfile)
4302 or die "$P: Can't open $newfile for write\n";
4303 foreach my $fixed_line (@fixed) {
4304 $linecount++;
4305 if ($file) {
4306 if ($linecount > 3) {
4307 $fixed_line =~ s/^\+//;
4308 print $f $fixed_line. "\n";
4309 }
4310 } else {
4311 print $f $fixed_line . "\n";
4312 }
4313 }
4314 close($f);
4315
4316 if (!$quiet) {
4317 print << "EOM";
4318Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4319
4320Do _NOT_ trust the results written to this file.
4321Do _NOT_ submit these changes without inspecting them for correctness.
4322
4323This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4324No warranties, expressed or implied...
4325
4326EOM
4327 }
4328 }
4329
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004330 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004331 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004332 }
4333 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004334 print << "EOM";
4335$vname has style problems, please review.
4336
4337If any of these errors are false positives, please report
4338them to the maintainer, see CHECKPATCH in MAINTAINERS.
4339EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004340 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004341
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004342 return $clean;
4343}