blob: 8489c35799e8b8858348a830564a28c94ab7b69d [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
21my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070022my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080024my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $file = 0;
26my $check = 0;
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 Whitcroft0a920b52007-06-01 00:46:48 -0700115GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-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 Whitcroft0a920b52007-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 Whitcroft0a920b52007-06-01 00:46:48 -0700152if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700153 print "$P: no input files\n";
Andy Whitcroft0a920b52007-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 Whitcroft0a920b52007-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;
Joe Perches11ea5162013-11-12 15:10:08 -0800446 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
447 $camelcase{$1} = 1;
448 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700449 $camelcase{$1} = 1;
450 }
451 }
452}
453
454my $camelcase_seeded = 0;
455sub seed_camelcase_includes {
456 return if ($camelcase_seeded);
457
458 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700459 my $camelcase_cache = "";
460 my @include_files = ();
461
462 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700463
Joe Perches34456862013-07-03 15:05:34 -0700464 if (-d ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700465 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
466 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700467 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700468 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700469 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700470 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700471 @include_files = split('\n', $files);
472 foreach my $file (@include_files) {
473 my $date = POSIX::strftime("%Y%m%d%H%M",
474 localtime((stat $file)[9]));
475 $last_mod_date = $date if ($last_mod_date < $date);
476 }
477 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700478 }
Joe Perchesc707a812013-07-08 16:00:43 -0700479
480 if ($camelcase_cache ne "" && -f $camelcase_cache) {
481 open(my $camelcase_file, '<', "$camelcase_cache")
482 or warn "$P: Can't read '$camelcase_cache' $!\n";
483 while (<$camelcase_file>) {
484 chomp;
485 $camelcase{$_} = 1;
486 }
487 close($camelcase_file);
488
489 return;
490 }
491
492 if (-d ".git") {
493 $files = `git ls-files "include/*.h"`;
494 @include_files = split('\n', $files);
495 }
496
Joe Perches34456862013-07-03 15:05:34 -0700497 foreach my $file (@include_files) {
498 seed_camelcase_file($file);
499 }
Joe Perches351b2a12013-07-03 15:05:36 -0700500
Joe Perchesc707a812013-07-08 16:00:43 -0700501 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700502 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700503 open(my $camelcase_file, '>', "$camelcase_cache")
504 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700505 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
506 print $camelcase_file ("$_\n");
507 }
508 close($camelcase_file);
509 }
Joe Perches34456862013-07-03 15:05:34 -0700510}
511
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700512$chk_signoff = 0 if ($file);
513
Andy Whitcroft00df3442007-06-08 13:47:06 -0700514my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800515my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700516my @fixed = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800517my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700518for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800519 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700520 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800521 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700522 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800523 } elsif ($filename eq '-') {
524 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700525 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800526 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700527 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700528 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800529 if ($filename eq '-') {
530 $vname = 'Your patch';
531 } else {
532 $vname = $filename;
533 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800534 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700535 chomp;
536 push(@rawlines, $_);
537 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800538 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800539 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700540 $exit = 1;
541 }
542 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800543 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700544 @fixed = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700545}
546
547exit($exit);
548
549sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700550 my ($root) = @_;
551
552 my @tree_check = (
553 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
554 "README", "Documentation", "arch", "include", "drivers",
555 "fs", "init", "ipc", "kernel", "lib", "scripts",
556 );
557
558 foreach my $check (@tree_check) {
559 if (! -e $root . '/' . $check) {
560 return 0;
561 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700562 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700563 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700564}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700565
Joe Perches20112472011-07-25 17:13:23 -0700566sub parse_email {
567 my ($formatted_email) = @_;
568
569 my $name = "";
570 my $address = "";
571 my $comment = "";
572
573 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
574 $name = $1;
575 $address = $2;
576 $comment = $3 if defined $3;
577 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
578 $address = $1;
579 $comment = $2 if defined $2;
580 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
581 $address = $1;
582 $comment = $2 if defined $2;
583 $formatted_email =~ s/$address.*$//;
584 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700585 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700586 $name =~ s/^\"|\"$//g;
587 # If there's a name left after stripping spaces and
588 # leading quotes, and the address doesn't have both
589 # leading and trailing angle brackets, the address
590 # is invalid. ie:
591 # "joe smith joe@smith.com" bad
592 # "joe smith <joe@smith.com" bad
593 if ($name ne "" && $address !~ /^<[^>]+>$/) {
594 $name = "";
595 $address = "";
596 $comment = "";
597 }
598 }
599
Joe Perches3705ce52013-07-03 15:05:31 -0700600 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700601 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700602 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700603 $address =~ s/^\<|\>$//g;
604
605 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
606 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
607 $name = "\"$name\"";
608 }
609
610 return ($name, $address, $comment);
611}
612
613sub format_email {
614 my ($name, $address) = @_;
615
616 my $formatted_email;
617
Joe Perches3705ce52013-07-03 15:05:31 -0700618 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700619 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700620 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700621
622 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
623 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
624 $name = "\"$name\"";
625 }
626
627 if ("$name" eq "") {
628 $formatted_email = "$address";
629 } else {
630 $formatted_email = "$name <$address>";
631 }
632
633 return $formatted_email;
634}
635
Joe Perches000d1cc12011-07-25 17:13:25 -0700636sub which_conf {
637 my ($conf) = @_;
638
639 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
640 if (-e "$path/$conf") {
641 return "$path/$conf";
642 }
643 }
644
645 return "";
646}
647
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700648sub expand_tabs {
649 my ($str) = @_;
650
651 my $res = '';
652 my $n = 0;
653 for my $c (split(//, $str)) {
654 if ($c eq "\t") {
655 $res .= ' ';
656 $n++;
657 for (; ($n % 8) != 0; $n++) {
658 $res .= ' ';
659 }
660 next;
661 }
662 $res .= $c;
663 $n++;
664 }
665
666 return $res;
667}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700668sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700669 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700670 return $res;
671}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700672
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700673sub line_stats {
674 my ($line) = @_;
675
676 # Drop the diff line leader and expand tabs
677 $line =~ s/^.//;
678 $line = expand_tabs($line);
679
680 # Pick the indent from the front of the line.
681 my ($white) = ($line =~ /^(\s*)/);
682
683 return (length($line), length($white));
684}
685
Andy Whitcroft773647a2008-03-28 14:15:58 -0700686my $sanitise_quote = '';
687
688sub sanitise_line_reset {
689 my ($in_comment) = @_;
690
691 if ($in_comment) {
692 $sanitise_quote = '*/';
693 } else {
694 $sanitise_quote = '';
695 }
696}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700697sub sanitise_line {
698 my ($line) = @_;
699
700 my $res = '';
701 my $l = '';
702
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800703 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700704 my $off = 0;
705 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700706
Andy Whitcroft773647a2008-03-28 14:15:58 -0700707 # Always copy over the diff marker.
708 $res = substr($line, 0, 1);
709
710 for ($off = 1; $off < length($line); $off++) {
711 $c = substr($line, $off, 1);
712
713 # Comments we are wacking completly including the begin
714 # and end, all to $;.
715 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
716 $sanitise_quote = '*/';
717
718 substr($res, $off, 2, "$;$;");
719 $off++;
720 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800721 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700722 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700723 $sanitise_quote = '';
724 substr($res, $off, 2, "$;$;");
725 $off++;
726 next;
727 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700728 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
729 $sanitise_quote = '//';
730
731 substr($res, $off, 2, $sanitise_quote);
732 $off++;
733 next;
734 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700735
736 # A \ in a string means ignore the next character.
737 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
738 $c eq "\\") {
739 substr($res, $off, 2, 'XX');
740 $off++;
741 next;
742 }
743 # Regular quotes.
744 if ($c eq "'" || $c eq '"') {
745 if ($sanitise_quote eq '') {
746 $sanitise_quote = $c;
747
748 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700749 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700750 } elsif ($sanitise_quote eq $c) {
751 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700752 }
753 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700754
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800755 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700756 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
757 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700758 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
759 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700760 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
761 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700762 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700763 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700764 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800765 }
766
Daniel Walker113f04a2009-09-21 17:04:35 -0700767 if ($sanitise_quote eq '//') {
768 $sanitise_quote = '';
769 }
770
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800771 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700772 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800773 my $clean = 'X' x length($1);
774 $res =~ s@\<.*\>@<$clean>@;
775
776 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700777 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800778 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700779 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800780 }
781
Andy Whitcroft00df3442007-06-08 13:47:06 -0700782 return $res;
783}
784
Joe Perchesa6962d72013-04-29 16:18:13 -0700785sub get_quoted_string {
786 my ($line, $rawline) = @_;
787
788 return "" if ($line !~ m/(\"[X]+\")/g);
789 return substr($rawline, $-[0], $+[0] - $-[0]);
790}
791
Andy Whitcroft8905a672007-11-28 16:21:06 -0800792sub ctx_statement_block {
793 my ($linenr, $remain, $off) = @_;
794 my $line = $linenr - 1;
795 my $blk = '';
796 my $soff = $off;
797 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700798 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800799
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800800 my $loff = 0;
801
Andy Whitcroft8905a672007-11-28 16:21:06 -0800802 my $type = '';
803 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800804 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800805 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800806 my $c;
807 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800808
809 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800810 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800811 @stack = (['', 0]) if ($#stack == -1);
812
Andy Whitcroft773647a2008-03-28 14:15:58 -0700813 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800814 # If we are about to drop off the end, pull in more
815 # context.
816 if ($off >= $len) {
817 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700818 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800819 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800820 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800821 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800822 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800823 $len = length($blk);
824 $line++;
825 last;
826 }
827 # Bail if there is no further context.
828 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800829 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800830 last;
831 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800832 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
833 $level++;
834 $type = '#';
835 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800836 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800837 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800838 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800839 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800840
Andy Whitcroft773647a2008-03-28 14:15:58 -0700841 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800842
843 # Handle nested #if/#else.
844 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
845 push(@stack, [ $type, $level ]);
846 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
847 ($type, $level) = @{$stack[$#stack - 1]};
848 } elsif ($remainder =~ /^#\s*endif\b/) {
849 ($type, $level) = @{pop(@stack)};
850 }
851
Andy Whitcroft8905a672007-11-28 16:21:06 -0800852 # Statement ends at the ';' or a close '}' at the
853 # outermost level.
854 if ($level == 0 && $c eq ';') {
855 last;
856 }
857
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800858 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700859 if ($level == 0 && $coff_set == 0 &&
860 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
861 $remainder =~ /^(else)(?:\s|{)/ &&
862 $remainder !~ /^else\s+if\b/) {
863 $coff = $off + length($1) - 1;
864 $coff_set = 1;
865 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
866 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800867 }
868
Andy Whitcroft8905a672007-11-28 16:21:06 -0800869 if (($type eq '' || $type eq '(') && $c eq '(') {
870 $level++;
871 $type = '(';
872 }
873 if ($type eq '(' && $c eq ')') {
874 $level--;
875 $type = ($level != 0)? '(' : '';
876
877 if ($level == 0 && $coff < $soff) {
878 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700879 $coff_set = 1;
880 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800881 }
882 }
883 if (($type eq '' || $type eq '{') && $c eq '{') {
884 $level++;
885 $type = '{';
886 }
887 if ($type eq '{' && $c eq '}') {
888 $level--;
889 $type = ($level != 0)? '{' : '';
890
891 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700892 if (substr($blk, $off + 1, 1) eq ';') {
893 $off++;
894 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800895 last;
896 }
897 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800898 # Preprocessor commands end at the newline unless escaped.
899 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
900 $level--;
901 $type = '';
902 $off++;
903 last;
904 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800905 $off++;
906 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700907 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800908 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700909 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800910 $line++;
911 $remain--;
912 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800913
914 my $statement = substr($blk, $soff, $off - $soff + 1);
915 my $condition = substr($blk, $soff, $coff - $soff + 1);
916
917 #warn "STATEMENT<$statement>\n";
918 #warn "CONDITION<$condition>\n";
919
Andy Whitcroft773647a2008-03-28 14:15:58 -0700920 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800921
922 return ($statement, $condition,
923 $line, $remain + 1, $off - $loff + 1, $level);
924}
925
Andy Whitcroftcf655042008-03-04 14:28:20 -0800926sub statement_lines {
927 my ($stmt) = @_;
928
929 # Strip the diff line prefixes and rip blank lines at start and end.
930 $stmt =~ s/(^|\n)./$1/g;
931 $stmt =~ s/^\s*//;
932 $stmt =~ s/\s*$//;
933
934 my @stmt_lines = ($stmt =~ /\n/g);
935
936 return $#stmt_lines + 2;
937}
938
939sub statement_rawlines {
940 my ($stmt) = @_;
941
942 my @stmt_lines = ($stmt =~ /\n/g);
943
944 return $#stmt_lines + 2;
945}
946
947sub statement_block_size {
948 my ($stmt) = @_;
949
950 $stmt =~ s/(^|\n)./$1/g;
951 $stmt =~ s/^\s*{//;
952 $stmt =~ s/}\s*$//;
953 $stmt =~ s/^\s*//;
954 $stmt =~ s/\s*$//;
955
956 my @stmt_lines = ($stmt =~ /\n/g);
957 my @stmt_statements = ($stmt =~ /;/g);
958
959 my $stmt_lines = $#stmt_lines + 2;
960 my $stmt_statements = $#stmt_statements + 1;
961
962 if ($stmt_lines > $stmt_statements) {
963 return $stmt_lines;
964 } else {
965 return $stmt_statements;
966 }
967}
968
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800969sub ctx_statement_full {
970 my ($linenr, $remain, $off) = @_;
971 my ($statement, $condition, $level);
972
973 my (@chunks);
974
Andy Whitcroftcf655042008-03-04 14:28:20 -0800975 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800976 ($statement, $condition, $linenr, $remain, $off, $level) =
977 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700978 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800979 push(@chunks, [ $condition, $statement ]);
980 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
981 return ($level, $linenr, @chunks);
982 }
983
984 # Pull in the following conditional/block pairs and see if they
985 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800986 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800987 ($statement, $condition, $linenr, $remain, $off, $level) =
988 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800989 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700990 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800991 #print "C: push\n";
992 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800993 }
994
995 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800996}
997
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700998sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700999 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001000 my $line;
1001 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001002 my $blk = '';
1003 my @o;
1004 my @c;
1005 my @res = ();
1006
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001007 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001008 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001009 for ($line = $start; $remain > 0; $line++) {
1010 next if ($rawlines[$line] =~ /^-/);
1011 $remain--;
1012
1013 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001014
1015 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001016 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001017 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001018 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001019 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001020 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001021 $level = pop(@stack);
1022 }
1023
Andy Whitcroft01464f32010-10-26 14:23:19 -07001024 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001025 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1026 if ($off > 0) {
1027 $off--;
1028 next;
1029 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001030
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001031 if ($c eq $close && $level > 0) {
1032 $level--;
1033 last if ($level == 0);
1034 } elsif ($c eq $open) {
1035 $level++;
1036 }
1037 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001038
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001039 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001040 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001041 }
1042
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001043 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001044 }
1045
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001046 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001047}
1048sub ctx_block_outer {
1049 my ($linenr, $remain) = @_;
1050
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001051 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1052 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001053}
1054sub ctx_block {
1055 my ($linenr, $remain) = @_;
1056
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001057 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1058 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001059}
1060sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001061 my ($linenr, $remain, $off) = @_;
1062
1063 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1064 return @r;
1065}
1066sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001067 my ($linenr, $remain) = @_;
1068
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001069 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001070}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001071sub ctx_statement_level {
1072 my ($linenr, $remain, $off) = @_;
1073
1074 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1075}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001076
1077sub ctx_locate_comment {
1078 my ($first_line, $end_line) = @_;
1079
1080 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001081 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001082 return $current_comment if (defined $current_comment);
1083
1084 # Look through the context and try and figure out if there is a
1085 # comment.
1086 my $in_comment = 0;
1087 $current_comment = '';
1088 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001089 my $line = $rawlines[$linenr - 1];
1090 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001091 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1092 $in_comment = 1;
1093 }
1094 if ($line =~ m@/\*@) {
1095 $in_comment = 1;
1096 }
1097 if (!$in_comment && $current_comment ne '') {
1098 $current_comment = '';
1099 }
1100 $current_comment .= $line . "\n" if ($in_comment);
1101 if ($line =~ m@\*/@) {
1102 $in_comment = 0;
1103 }
1104 }
1105
1106 chomp($current_comment);
1107 return($current_comment);
1108}
1109sub ctx_has_comment {
1110 my ($first_line, $end_line) = @_;
1111 my $cmt = ctx_locate_comment($first_line, $end_line);
1112
Andy Whitcroft00df3442007-06-08 13:47:06 -07001113 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001114 ##print "CMMT: $cmt\n";
1115
1116 return ($cmt ne '');
1117}
1118
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001119sub raw_line {
1120 my ($linenr, $cnt) = @_;
1121
1122 my $offset = $linenr - 1;
1123 $cnt++;
1124
1125 my $line;
1126 while ($cnt) {
1127 $line = $rawlines[$offset++];
1128 next if (defined($line) && $line =~ /^-/);
1129 $cnt--;
1130 }
1131
1132 return $line;
1133}
1134
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001135sub cat_vet {
1136 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001137 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001138
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001139 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001140 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1141 $res .= $1;
1142 if ($2 ne '') {
1143 $coded = sprintf("^%c", unpack('C', $2) + 64);
1144 $res .= $coded;
1145 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001146 }
1147 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001148
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001149 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001150}
1151
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001152my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001153my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001154my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001155my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001156
1157sub annotate_reset {
1158 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001159 $av_pending = '_';
1160 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001161 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162}
1163
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001164sub annotate_values {
1165 my ($stream, $type) = @_;
1166
1167 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001168 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001169 my $cur = $stream;
1170
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001171 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001172
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001173 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001174 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001175 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001176 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001177 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001178 print "WS($1)\n" if ($dbg_values > 1);
1179 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001180 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001181 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001182 }
1183
Florian Micklerc023e4732011-01-12 16:59:58 -08001184 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001185 print "CAST($1)\n" if ($dbg_values > 1);
1186 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001187 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001188
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001189 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001190 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001191 $type = 'T';
1192
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001193 } elsif ($cur =~ /^($Modifier)\s*/) {
1194 print "MODIFIER($1)\n" if ($dbg_values > 1);
1195 $type = 'T';
1196
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001197 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001198 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001200 push(@av_paren_type, $type);
1201 if ($2 ne '') {
1202 $av_pending = 'N';
1203 }
1204 $type = 'E';
1205
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001206 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001207 print "UNDEF($1)\n" if ($dbg_values > 1);
1208 $av_preprocessor = 1;
1209 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001210
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001211 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001212 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001214
1215 push(@av_paren_type, $type);
1216 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001217 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001218
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001219 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001220 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1221 $av_preprocessor = 1;
1222
1223 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1224
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001225 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001226
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001227 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001228 print "PRE_END($1)\n" if ($dbg_values > 1);
1229
1230 $av_preprocessor = 1;
1231
1232 # Assume all arms of the conditional end as this
1233 # one does, and continue as if the #endif was not here.
1234 pop(@av_paren_type);
1235 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001236 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001237
1238 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001239 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001240
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001241 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1242 print "ATTR($1)\n" if ($dbg_values > 1);
1243 $av_pending = $type;
1244 $type = 'N';
1245
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001246 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001247 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001248 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001249 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001250 }
1251 $type = 'N';
1252
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001253 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001254 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001255 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001256 $type = 'N';
1257
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001258 } elsif ($cur =~/^(case)/o) {
1259 print "CASE($1)\n" if ($dbg_values > 1);
1260 $av_pend_colon = 'C';
1261 $type = 'N';
1262
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001263 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001264 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001265 $type = 'N';
1266
1267 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001268 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001269 push(@av_paren_type, $av_pending);
1270 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001271 $type = 'N';
1272
1273 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001274 my $new_type = pop(@av_paren_type);
1275 if ($new_type ne '_') {
1276 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 print "PAREN('$1') -> $type\n"
1278 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001279 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001280 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001281 }
1282
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001283 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001284 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001285 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001286 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001287
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001288 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1289 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001290 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001291 } elsif ($type eq 'E') {
1292 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001293 }
1294 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1295 $type = 'V';
1296
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001297 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001298 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001299 $type = 'V';
1300
1301 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001302 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001303 $type = 'N';
1304
Andy Whitcroftcf655042008-03-04 14:28:20 -08001305 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001306 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001307 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001308 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001309
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001310 } elsif ($cur =~/^(,)/) {
1311 print "COMMA($1)\n" if ($dbg_values > 1);
1312 $type = 'C';
1313
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001314 } elsif ($cur =~ /^(\?)/o) {
1315 print "QUESTION($1)\n" if ($dbg_values > 1);
1316 $type = 'N';
1317
1318 } elsif ($cur =~ /^(:)/o) {
1319 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1320
1321 substr($var, length($res), 1, $av_pend_colon);
1322 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1323 $type = 'E';
1324 } else {
1325 $type = 'N';
1326 }
1327 $av_pend_colon = 'O';
1328
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001329 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001330 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001331 $type = 'N';
1332
Andy Whitcroft0d413862008-10-15 22:02:16 -07001333 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001334 my $variant;
1335
1336 print "OPV($1)\n" if ($dbg_values > 1);
1337 if ($type eq 'V') {
1338 $variant = 'B';
1339 } else {
1340 $variant = 'U';
1341 }
1342
1343 substr($var, length($res), 1, $variant);
1344 $type = 'N';
1345
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001346 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001347 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001348 if ($1 ne '++' && $1 ne '--') {
1349 $type = 'N';
1350 }
1351
1352 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001353 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001354 }
1355 if (defined $1) {
1356 $cur = substr($cur, length($1));
1357 $res .= $type x length($1);
1358 }
1359 }
1360
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001361 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001362}
1363
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001365 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001366 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001367 ^(?:
1368 $Modifier|
1369 $Storage|
1370 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001371 DEFINE_\S+
1372 )$|
1373 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001374 goto|
1375 return|
1376 case|
1377 else|
1378 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001379 do|
1380 \#|
1381 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001382 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001383 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001384 )}x;
1385 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1386 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001387 # Check for modifiers.
1388 $possible =~ s/\s*$Storage\s*//g;
1389 $possible =~ s/\s*$Sparse\s*//g;
1390 if ($possible =~ /^\s*$/) {
1391
1392 } elsif ($possible =~ /\s/) {
1393 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001394 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001395 if ($modifier !~ $notPermitted) {
1396 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1397 push(@modifierList, $modifier);
1398 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001399 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001400
1401 } else {
1402 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1403 push(@typeList, $possible);
1404 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001406 } else {
1407 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001408 }
1409}
1410
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411my $prefix = '';
1412
Joe Perches000d1cc12011-07-25 17:13:25 -07001413sub show_type {
Joe Perches91bfe482013-09-11 14:23:59 -07001414 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1415
1416 return !defined $ignore_type{$_[0]};
Joe Perches000d1cc12011-07-25 17:13:25 -07001417}
1418
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001419sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001420 if (!show_type($_[1]) ||
1421 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001422 return 0;
1423 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001424 my $line;
1425 if ($show_types) {
1426 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1427 } else {
1428 $line = "$prefix$_[0]: $_[2]\n";
1429 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001430 $line = (split('\n', $line))[0] . "\n" if ($terse);
1431
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001432 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001433
1434 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001435}
1436sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001437 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001438}
Joe Perches000d1cc12011-07-25 17:13:25 -07001439
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001440sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001441 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001442 our $clean = 0;
1443 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001444 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001445 }
Joe Perches3705ce52013-07-03 15:05:31 -07001446 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001447}
1448sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001449 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001450 our $clean = 0;
1451 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001452 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001453 }
Joe Perches3705ce52013-07-03 15:05:31 -07001454 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001455}
1456sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001457 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458 our $clean = 0;
1459 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001460 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001461 }
Joe Perches3705ce52013-07-03 15:05:31 -07001462 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001463}
1464
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001465sub check_absolute_file {
1466 my ($absolute, $herecurr) = @_;
1467 my $file = $absolute;
1468
1469 ##print "absolute<$absolute>\n";
1470
1471 # See if any suffix of this path is a path within the tree.
1472 while ($file =~ s@^[^/]*/@@) {
1473 if (-f "$root/$file") {
1474 ##print "file<$file>\n";
1475 last;
1476 }
1477 }
1478 if (! -f _) {
1479 return 0;
1480 }
1481
1482 # It is, so see if the prefix is acceptable.
1483 my $prefix = $absolute;
1484 substr($prefix, -length($file)) = '';
1485
1486 ##print "prefix<$prefix>\n";
1487 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001488 WARN("USE_RELATIVE_PATH",
1489 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001490 }
1491}
1492
Joe Perches3705ce52013-07-03 15:05:31 -07001493sub trim {
1494 my ($string) = @_;
1495
Joe Perchesb34c6482013-09-11 14:24:01 -07001496 $string =~ s/^\s+|\s+$//g;
1497
1498 return $string;
1499}
1500
1501sub ltrim {
1502 my ($string) = @_;
1503
1504 $string =~ s/^\s+//;
1505
1506 return $string;
1507}
1508
1509sub rtrim {
1510 my ($string) = @_;
1511
1512 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001513
1514 return $string;
1515}
1516
Joe Perches52ea8502013-11-12 15:10:09 -08001517sub string_find_replace {
1518 my ($string, $find, $replace) = @_;
1519
1520 $string =~ s/$find/$replace/g;
1521
1522 return $string;
1523}
1524
Joe Perches3705ce52013-07-03 15:05:31 -07001525sub tabify {
1526 my ($leading) = @_;
1527
1528 my $source_indent = 8;
1529 my $max_spaces_before_tab = $source_indent - 1;
1530 my $spaces_to_tab = " " x $source_indent;
1531
1532 #convert leading spaces to tabs
1533 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1534 #Remove spaces before a tab
1535 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1536
1537 return "$leading";
1538}
1539
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001540sub pos_last_openparen {
1541 my ($line) = @_;
1542
1543 my $pos = 0;
1544
1545 my $opens = $line =~ tr/\(/\(/;
1546 my $closes = $line =~ tr/\)/\)/;
1547
1548 my $last_openparen = 0;
1549
1550 if (($opens == 0) || ($closes >= $opens)) {
1551 return -1;
1552 }
1553
1554 my $len = length($line);
1555
1556 for ($pos = 0; $pos < $len; $pos++) {
1557 my $string = substr($line, $pos);
1558 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1559 $pos += length($1) - 1;
1560 } elsif (substr($line, $pos, 1) eq '(') {
1561 $last_openparen = $pos;
1562 } elsif (index($string, '(') == -1) {
1563 last;
1564 }
1565 }
1566
1567 return $last_openparen + 1;
1568}
1569
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001570sub process {
1571 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001572
1573 my $linenr=0;
1574 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001575 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001576 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001577 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001578
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001579 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001580 my $indent;
1581 my $previndent=0;
1582 my $stashindent=0;
1583
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001584 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001585 my $signoff = 0;
1586 my $is_patch = 0;
1587
Joe Perches15662b32011-10-31 17:13:12 -07001588 my $in_header_lines = 1;
1589 my $in_commit_log = 0; #Scanning lines before patch
1590
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001591 my $non_utf8_charset = 0;
1592
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001593 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001594 our $cnt_lines = 0;
1595 our $cnt_error = 0;
1596 our $cnt_warn = 0;
1597 our $cnt_chk = 0;
1598
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001599 # Trace the real file/line as we go.
1600 my $realfile = '';
1601 my $realline = 0;
1602 my $realcnt = 0;
1603 my $here = '';
1604 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001605 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001606 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001607 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001608
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001609 my $prev_values = 'E';
1610
1611 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001612 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001613 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001614 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001615 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001616
Joe Perches7e51f192013-09-11 14:23:57 -07001617 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001618
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001619 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001620 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001621 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001622 my @setup_docs = ();
1623 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001624
Joe Perchesd8b07712013-11-12 15:10:06 -08001625 my $camelcase_file_seeded = 0;
1626
Andy Whitcroft773647a2008-03-28 14:15:58 -07001627 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 my $line;
1629 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001630 $linenr++;
1631 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001632
Joe Perches3705ce52013-07-03 15:05:31 -07001633 push(@fixed, $rawline) if ($fix);
1634
Andy Whitcroft773647a2008-03-28 14:15:58 -07001635 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001636 $setup_docs = 0;
1637 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1638 $setup_docs = 1;
1639 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001640 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001641 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001642 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1643 $realline=$1-1;
1644 if (defined $2) {
1645 $realcnt=$3+1;
1646 } else {
1647 $realcnt=1+1;
1648 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001649 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001650
1651 # Guestimate if this is a continuing comment. Run
1652 # the context looking for a comment "edge". If this
1653 # edge is a close comment then we must be in a comment
1654 # at context start.
1655 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001656 my $cnt = $realcnt;
1657 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1658 next if (defined $rawlines[$ln - 1] &&
1659 $rawlines[$ln - 1] =~ /^-/);
1660 $cnt--;
1661 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001662 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001663 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1664 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1665 ($edge) = $1;
1666 last;
1667 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001668 }
1669 if (defined $edge && $edge eq '*/') {
1670 $in_comment = 1;
1671 }
1672
1673 # Guestimate if this is a continuing comment. If this
1674 # is the start of a diff block and this line starts
1675 # ' *' then it is very likely a comment.
1676 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001677 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001678 {
1679 $in_comment = 1;
1680 }
1681
1682 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1683 sanitise_line_reset($in_comment);
1684
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001685 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001686 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001687 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001688 $line = sanitise_line($rawline);
1689 }
1690 push(@lines, $line);
1691
1692 if ($realcnt > 1) {
1693 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1694 } else {
1695 $realcnt = 0;
1696 }
1697
1698 #print "==>$rawline\n";
1699 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001700
1701 if ($setup_docs && $line =~ /^\+/) {
1702 push(@setup_docs, $line);
1703 }
1704 }
1705
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001706 $prefix = '';
1707
Andy Whitcroft773647a2008-03-28 14:15:58 -07001708 $realcnt = 0;
1709 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001710 foreach my $line (@lines) {
1711 $linenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001712 my $sline = $line; #copy of $line
1713 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001714
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001715 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001716
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001717#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001719 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001720 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001721 $realline=$1-1;
1722 if (defined $2) {
1723 $realcnt=$3+1;
1724 } else {
1725 $realcnt=1+1;
1726 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001728 $prev_values = 'E';
1729
Andy Whitcroft773647a2008-03-28 14:15:58 -07001730 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001731 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001732 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001733 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001734 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001735
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001736# track the line number as we move through the hunk, note that
1737# new versions of GNU diff omit the leading space on completely
1738# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001739 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001740 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001741 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001742
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001743 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001744 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001745
1746 # Track the previous line.
1747 ($prevline, $stashline) = ($stashline, $line);
1748 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1750
Andy Whitcroft773647a2008-03-28 14:15:58 -07001751 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001752
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001753 } elsif ($realcnt == 1) {
1754 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001755 }
1756
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001757 my $hunk_line = ($realcnt != 0);
1758
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001759#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001760 $prefix = "$filename:$realline: " if ($emacs && $file);
1761 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1762
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001763 $here = "#$linenr: " if (!$file);
1764 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001765
1766 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001767 if ($line =~ /^diff --git.*?(\S+)$/) {
1768 $realfile = $1;
1769 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001770 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001771 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001772 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001773 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001774 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001775
1776 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001777 if (!$file && $tree && $p1_prefix ne '' &&
1778 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001779 WARN("PATCH_PREFIX",
1780 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001781 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001782
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001783 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001784 ERROR("MODIFIED_INCLUDE_ASM",
1785 "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 -07001786 }
1787 next;
1788 }
1789
Randy Dunlap389834b2007-06-08 13:47:03 -07001790 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001791
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001792 my $hereline = "$here\n$rawline\n";
1793 my $herecurr = "$here\n$rawline\n";
1794 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001795
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 $cnt_lines++ if ($realcnt != 0);
1797
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001798# Check for incorrect file permissions
1799 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1800 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001801 if ($realfile !~ m@scripts/@ &&
1802 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001803 ERROR("EXECUTE_PERMISSIONS",
1804 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001805 }
1806 }
1807
Joe Perches20112472011-07-25 17:13:23 -07001808# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001809 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001810 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001811 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001812 }
1813
1814# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001815 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001816 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001817 my $space_before = $1;
1818 my $sign_off = $2;
1819 my $space_after = $3;
1820 my $email = $4;
1821 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1822
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001823 if ($sign_off !~ /$signature_tags/) {
1824 WARN("BAD_SIGN_OFF",
1825 "Non-standard signature: $sign_off\n" . $herecurr);
1826 }
Joe Perches20112472011-07-25 17:13:23 -07001827 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07001828 if (WARN("BAD_SIGN_OFF",
1829 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1830 $fix) {
1831 $fixed[$linenr - 1] =
1832 "$ucfirst_sign_off $email";
1833 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001834 }
Joe Perches20112472011-07-25 17:13:23 -07001835 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07001836 if (WARN("BAD_SIGN_OFF",
1837 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1838 $fix) {
1839 $fixed[$linenr - 1] =
1840 "$ucfirst_sign_off $email";
1841 }
1842
Joe Perches20112472011-07-25 17:13:23 -07001843 }
1844 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07001845 if (WARN("BAD_SIGN_OFF",
1846 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1847 $fix) {
1848 $fixed[$linenr - 1] =
1849 "$ucfirst_sign_off $email";
1850 }
Joe Perches20112472011-07-25 17:13:23 -07001851 }
1852
1853 my ($email_name, $email_address, $comment) = parse_email($email);
1854 my $suggested_email = format_email(($email_name, $email_address));
1855 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001856 ERROR("BAD_SIGN_OFF",
1857 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001858 } else {
1859 my $dequoted = $suggested_email;
1860 $dequoted =~ s/^"//;
1861 $dequoted =~ s/" </ </;
1862 # Don't force email to have quotes
1863 # Allow just an angle bracketed address
1864 if ("$dequoted$comment" ne $email &&
1865 "<$email_address>$comment" ne $email &&
1866 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001867 WARN("BAD_SIGN_OFF",
1868 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001869 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001870 }
Joe Perches7e51f192013-09-11 14:23:57 -07001871
1872# Check for duplicate signatures
1873 my $sig_nospace = $line;
1874 $sig_nospace =~ s/\s//g;
1875 $sig_nospace = lc($sig_nospace);
1876 if (defined $signatures{$sig_nospace}) {
1877 WARN("BAD_SIGN_OFF",
1878 "Duplicate signature\n" . $herecurr);
1879 } else {
1880 $signatures{$sig_nospace} = 1;
1881 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001882 }
1883
Andy Whitcroft00df3442007-06-08 13:47:06 -07001884# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001885 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001886 ERROR("CORRUPTED_PATCH",
1887 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001888 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001889 }
1890
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001891# Check for absolute kernel paths.
1892 if ($tree) {
1893 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1894 my $file = $1;
1895
1896 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1897 check_absolute_file($1, $herecurr)) {
1898 #
1899 } else {
1900 check_absolute_file($file, $herecurr);
1901 }
1902 }
1903 }
1904
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001905# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1906 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001907 $rawline !~ m/^$UTF8*$/) {
1908 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1909
1910 my $blank = copy_spacing($rawline);
1911 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1912 my $hereptr = "$hereline$ptr\n";
1913
Joe Perches34d99212011-07-25 17:13:26 -07001914 CHK("INVALID_UTF8",
1915 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001916 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001917
Joe Perches15662b32011-10-31 17:13:12 -07001918# Check if it's the start of a commit log
1919# (not a header line and we haven't seen the patch filename)
1920 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001921 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001922 $in_header_lines = 0;
1923 $in_commit_log = 1;
1924 }
1925
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001926# Check if there is UTF-8 in a commit log when a mail header has explicitly
1927# declined it, i.e defined some charset where it is missing.
1928 if ($in_header_lines &&
1929 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1930 $1 !~ /utf-8/i) {
1931 $non_utf8_charset = 1;
1932 }
1933
1934 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001935 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001936 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001937 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1938 }
1939
Andy Whitcroft306708542008-10-15 22:02:28 -07001940# ignore non-hunk lines and lines being removed
1941 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001942
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001943#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001944 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001945 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07001946 if (ERROR("DOS_LINE_ENDINGS",
1947 "DOS line endings\n" . $herevet) &&
1948 $fix) {
1949 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1950 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001951 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1952 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07001953 if (ERROR("TRAILING_WHITESPACE",
1954 "trailing whitespace\n" . $herevet) &&
1955 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07001956 $fixed[$linenr - 1] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001957 }
1958
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001959 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001960 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001961
Andi Kleen33549572010-05-24 14:33:29 -07001962# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001963# Only applies when adding the entry originally, after that we do not have
1964# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001965 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001966 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001967 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001968 my $cnt = $realcnt;
1969 my $ln = $linenr + 1;
1970 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001971 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001972 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001973 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001974 $f = $lines[$ln - 1];
1975 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1976 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001977
1978 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001979
1980 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1981 $is_start = 1;
1982 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1983 $length = -1;
1984 }
1985
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001986 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001987 $f =~ s/#.*//;
1988 $f =~ s/^\s+//;
1989 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001990 if ($f =~ /^\s*config\s/) {
1991 $is_end = 1;
1992 last;
1993 }
Andi Kleen33549572010-05-24 14:33:29 -07001994 $length++;
1995 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001996 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001997 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1998 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001999 }
2000
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002001# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2002 if ($realfile =~ /Kconfig/ &&
2003 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2004 WARN("CONFIG_EXPERIMENTAL",
2005 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2006 }
2007
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002008 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2009 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2010 my $flag = $1;
2011 my $replacement = {
2012 'EXTRA_AFLAGS' => 'asflags-y',
2013 'EXTRA_CFLAGS' => 'ccflags-y',
2014 'EXTRA_CPPFLAGS' => 'cppflags-y',
2015 'EXTRA_LDFLAGS' => 'ldflags-y',
2016 };
2017
2018 WARN("DEPRECATED_VARIABLE",
2019 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2020 }
2021
Andy Whitcroft5368df22008-10-15 22:02:27 -07002022# check we are in a valid source file if not then ignore this hunk
2023 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2024
Joe Perches6cd7f382012-12-17 16:01:54 -08002025#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002026 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002027 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002028 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002029 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002030 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002031 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002032 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002033 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002034 }
2035
Josh Triplettca56dc02012-03-23 15:02:21 -07002036# Check for user-visible strings broken across lines, which breaks the ability
2037# to grep for the string. Limited to strings used as parameters (those
2038# following an open parenthesis), which almost completely eliminates false
2039# positives, as well as warning only once per parameter rather than once per
2040# line of the string. Make an exception when the previous string ends in a
2041# newline (multiple lines in one string constant) or \n\t (common in inline
2042# assembly to indent the instruction on the following line).
2043 if ($line =~ /^\+\s*"/ &&
2044 $prevline =~ /"\s*$/ &&
2045 $prevline =~ /\(/ &&
2046 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
2047 WARN("SPLIT_STRING",
2048 "quoted string split across lines\n" . $hereprev);
2049 }
2050
Joe Perches5e79d962010-03-05 13:43:55 -08002051# check for spaces before a quoted newline
2052 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002053 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2054 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2055 $fix) {
2056 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2057 }
2058
Joe Perches5e79d962010-03-05 13:43:55 -08002059 }
2060
Andy Whitcroft8905a672007-11-28 16:21:06 -08002061# check for adding lines without a newline.
2062 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002063 WARN("MISSING_EOF_NEWLINE",
2064 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002065 }
2066
Mike Frysinger42e41c52009-09-21 17:04:40 -07002067# Blackfin: use hi/lo macros
2068 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2069 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2070 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002071 ERROR("LO_MACRO",
2072 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002073 }
2074 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2075 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002076 ERROR("HI_MACRO",
2077 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002078 }
2079 }
2080
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002081# check we are in a valid source file C or perl if not then ignore this hunk
2082 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002083
2084# at the beginning of a line any tabs must come first and anything
2085# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002086 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2087 $rawline =~ /^\+\s* \s*/) {
2088 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002089 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002090 if (ERROR("CODE_INDENT",
2091 "code indent should use tabs where possible\n" . $herevet) &&
2092 $fix) {
2093 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2094 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002095 }
2096
Alberto Panizzo08e44362010-03-05 13:43:54 -08002097# check for space before tabs.
2098 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2099 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002100 if (WARN("SPACE_BEFORE_TAB",
2101 "please, no space before tabs\n" . $herevet) &&
2102 $fix) {
2103 $fixed[$linenr - 1] =~
2104 s/(^\+.*) +\t/$1\t/;
2105 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002106 }
2107
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002108# check for && or || at the start of a line
2109 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2110 CHK("LOGICAL_CONTINUATIONS",
2111 "Logical continuations should be on the previous line\n" . $hereprev);
2112 }
2113
2114# check multi-line statement indentation matches previous line
2115 if ($^V && $^V ge 5.10.0 &&
2116 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2117 $prevline =~ /^\+(\t*)(.*)$/;
2118 my $oldindent = $1;
2119 my $rest = $2;
2120
2121 my $pos = pos_last_openparen($rest);
2122 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002123 $line =~ /^(\+| )([ \t]*)/;
2124 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002125
2126 my $goodtabindent = $oldindent .
2127 "\t" x ($pos / 8) .
2128 " " x ($pos % 8);
2129 my $goodspaceindent = $oldindent . " " x $pos;
2130
2131 if ($newindent ne $goodtabindent &&
2132 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002133
2134 if (CHK("PARENTHESIS_ALIGNMENT",
2135 "Alignment should match open parenthesis\n" . $hereprev) &&
2136 $fix && $line =~ /^\+/) {
2137 $fixed[$linenr - 1] =~
2138 s/^\+[ \t]*/\+$goodtabindent/;
2139 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002140 }
2141 }
2142 }
2143
Joe Perches23f780c2013-07-03 15:05:31 -07002144 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002145 if (CHK("SPACING",
2146 "No space is necessary after a cast\n" . $hereprev) &&
2147 $fix) {
2148 $fixed[$linenr - 1] =~
2149 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2150 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002151 }
2152
Joe Perches05880602012-10-04 17:13:35 -07002153 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002154 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2155 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07002156 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2157 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2158 }
2159
2160 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002161 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2162 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002163 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002164 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2165 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2166 "networking block comments start with * on subsequent lines\n" . $hereprev);
2167 }
2168
2169 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002170 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2171 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2172 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2173 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002174 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2175 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2176 }
2177
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002178# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002179# Exceptions:
2180# 1) within comments
2181# 2) indented preprocessor commands
2182# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002183 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002184 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002185 if (WARN("LEADING_SPACE",
2186 "please, no spaces at the start of a line\n" . $herevet) &&
2187 $fix) {
2188 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2189 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002190 }
2191
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002192# check we are in a valid C source file if not then ignore this hunk
2193 next if ($realfile !~ /\.(h|c)$/);
2194
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002195# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2196 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2197 WARN("CONFIG_EXPERIMENTAL",
2198 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2199 }
2200
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002201# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002202 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002203 WARN("CVS_KEYWORD",
2204 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002205 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002206
Mike Frysinger42e41c52009-09-21 17:04:40 -07002207# Blackfin: don't use __builtin_bfin_[cs]sync
2208 if ($line =~ /__builtin_bfin_csync/) {
2209 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002210 ERROR("CSYNC",
2211 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002212 }
2213 if ($line =~ /__builtin_bfin_ssync/) {
2214 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002215 ERROR("SSYNC",
2216 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002217 }
2218
Joe Perches56e77d72013-02-21 16:44:14 -08002219# check for old HOTPLUG __dev<foo> section markings
2220 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2221 WARN("HOTPLUG_SECTION",
2222 "Using $1 is unnecessary\n" . $herecurr);
2223 }
2224
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002225# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002226 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2227 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002228#print "LINE<$line>\n";
2229 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002230 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002231 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002232 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002233 $stat =~ s/\n./\n /g;
2234 $cond =~ s/\n./\n /g;
2235
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002236#print "linenr<$linenr> <$stat>\n";
2237 # If this statement has no statement boundaries within
2238 # it there is no point in retrying a statement scan
2239 # until we hit end of it.
2240 my $frag = $stat; $frag =~ s/;+\s*$//;
2241 if ($frag !~ /(?:{|;)/) {
2242#print "skip<$line_nr_next>\n";
2243 $suppress_statement = $line_nr_next;
2244 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002245
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002246 # Find the real next line.
2247 $realline_next = $line_nr_next;
2248 if (defined $realline_next &&
2249 (!defined $lines[$realline_next - 1] ||
2250 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2251 $realline_next++;
2252 }
2253
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002254 my $s = $stat;
2255 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002256
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002257 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002258 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002259
2260 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002261 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002262
Andy Whitcroft463f2862009-09-21 17:04:34 -07002263 } elsif ($s =~ /^.\s*else\b/s) {
2264
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002265 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002266 } 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 -07002267 my $type = $1;
2268 $type =~ s/\s+/ /g;
2269 possible($type, "A:" . $s);
2270
Andy Whitcroft8905a672007-11-28 16:21:06 -08002271 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002272 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002273 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002274 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002275
2276 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002277 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002278 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002279 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002280
2281 # Check for any sort of function declaration.
2282 # int foo(something bar, other baz);
2283 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 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 -08002285 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002286
Andy Whitcroftcf655042008-03-04 14:28:20 -08002287 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002288 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002289 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002290
Andy Whitcroft8905a672007-11-28 16:21:06 -08002291 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002292 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002293
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002294 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002295 }
2296 }
2297 }
2298
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002299 }
2300
Andy Whitcroft653d4872007-06-23 17:16:34 -07002301#
2302# Checks which may be anchored in the context.
2303#
2304
2305# Check for switch () and associated case and default
2306# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002307 if ($line=~/\bswitch\s*\(.*\)/) {
2308 my $err = '';
2309 my $sep = '';
2310 my @ctx = ctx_block_outer($linenr, $realcnt);
2311 shift(@ctx);
2312 for my $ctx (@ctx) {
2313 my ($clen, $cindent) = line_stats($ctx);
2314 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2315 $indent != $cindent) {
2316 $err .= "$sep$ctx\n";
2317 $sep = '';
2318 } else {
2319 $sep = "[...]\n";
2320 }
2321 }
2322 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002323 ERROR("SWITCH_CASE_INDENT_LEVEL",
2324 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002325 }
2326 }
2327
2328# if/while/etc brace do not go on next line, unless defining a do while loop,
2329# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002330 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002331 my $pre_ctx = "$1$2";
2332
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002333 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002334
2335 if ($line =~ /^\+\t{6,}/) {
2336 WARN("DEEP_INDENTATION",
2337 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2338 }
2339
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002340 my $ctx_cnt = $realcnt - $#ctx - 1;
2341 my $ctx = join("\n", @ctx);
2342
Andy Whitcroft548596d2008-07-23 21:29:01 -07002343 my $ctx_ln = $linenr;
2344 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002345
Andy Whitcroft548596d2008-07-23 21:29:01 -07002346 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2347 defined $lines[$ctx_ln - 1] &&
2348 $lines[$ctx_ln - 1] =~ /^-/)) {
2349 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2350 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 $ctx_ln++;
2352 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002353
Andy Whitcroft53210162008-07-23 21:29:03 -07002354 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2355 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002356
2357 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002358 ERROR("OPEN_BRACE",
2359 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002360 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002361 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002362 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2363 $ctx =~ /\)\s*\;\s*$/ &&
2364 defined $lines[$ctx_ln - 1])
2365 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002366 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2367 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002368 WARN("TRAILING_SEMICOLON",
2369 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002370 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002371 }
2372 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002373 }
2374
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002375# Check relative indent for conditionals and blocks.
2376 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002377 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2378 ctx_statement_block($linenr, $realcnt, 0)
2379 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002380 my ($s, $c) = ($stat, $cond);
2381
2382 substr($s, 0, length($c), '');
2383
2384 # Make sure we remove the line prefixes as we have
2385 # none on the first line, and are going to readd them
2386 # where necessary.
2387 $s =~ s/\n./\n/gs;
2388
2389 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002390 my @newlines = ($c =~ /\n/gs);
2391 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002392
2393 # We want to check the first line inside the block
2394 # starting at the end of the conditional, so remove:
2395 # 1) any blank line termination
2396 # 2) any opening brace { on end of the line
2397 # 3) any do (...) {
2398 my $continuation = 0;
2399 my $check = 0;
2400 $s =~ s/^.*\bdo\b//;
2401 $s =~ s/^\s*{//;
2402 if ($s =~ s/^\s*\\//) {
2403 $continuation = 1;
2404 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002405 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002406 $check = 1;
2407 $cond_lines++;
2408 }
2409
2410 # Also ignore a loop construct at the end of a
2411 # preprocessor statement.
2412 if (($prevline =~ /^.\s*#\s*define\s/ ||
2413 $prevline =~ /\\\s*$/) && $continuation == 0) {
2414 $check = 0;
2415 }
2416
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002417 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002418 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002419 while ($cond_ptr != $cond_lines) {
2420 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002421
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002422 # If we see an #else/#elif then the code
2423 # is not linear.
2424 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2425 $check = 0;
2426 }
2427
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002428 # Ignore:
2429 # 1) blank lines, they should be at 0,
2430 # 2) preprocessor lines, and
2431 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002432 if ($continuation ||
2433 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002434 $s =~ /^\s*#\s*?/ ||
2435 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002436 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002437 if ($s =~ s/^.*?\n//) {
2438 $cond_lines++;
2439 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002440 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002441 }
2442
2443 my (undef, $sindent) = line_stats("+" . $s);
2444 my $stat_real = raw_line($linenr, $cond_lines);
2445
2446 # Check if either of these lines are modified, else
2447 # this is not this patch's fault.
2448 if (!defined($stat_real) ||
2449 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2450 $check = 0;
2451 }
2452 if (defined($stat_real) && $cond_lines > 1) {
2453 $stat_real = "[...]\n$stat_real";
2454 }
2455
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002456 #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 -07002457
2458 if ($check && (($sindent % 8) != 0 ||
2459 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002460 WARN("SUSPECT_CODE_INDENT",
2461 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002462 }
2463 }
2464
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002465 # Track the 'values' across context and added lines.
2466 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002467 my ($curr_values, $curr_vars) =
2468 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002469 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002470 if ($dbg_values) {
2471 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002472 print "$linenr > .$outline\n";
2473 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002474 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002475 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002476 $prev_values = substr($curr_values, -1);
2477
Andy Whitcroft00df3442007-06-08 13:47:06 -07002478#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002479 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002480
Andy Whitcroft653d4872007-06-23 17:16:34 -07002481# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002482 if ($dbg_type) {
2483 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002484 ERROR("TEST_TYPE",
2485 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002486 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002487 ERROR("TEST_NOT_TYPE",
2488 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002489 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002490 next;
2491 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002492# TEST: allow direct testing of the attribute matcher.
2493 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002494 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002495 ERROR("TEST_ATTR",
2496 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002497 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002498 ERROR("TEST_NOT_ATTR",
2499 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002500 }
2501 next;
2502 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002503
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002504# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002505 if ($line =~ /^.\s*{/ &&
2506 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002507 ERROR("OPEN_BRACE",
2508 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002509 }
2510
Andy Whitcroft653d4872007-06-23 17:16:34 -07002511#
2512# Checks which are anchored on the added line.
2513#
2514
2515# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002516 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002517 my $path = $1;
2518 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002519 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002520 "malformed #include filename\n" . $herecurr);
2521 }
2522 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2523 ERROR("UAPI_INCLUDE",
2524 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002525 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002526 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002527
2528# no C99 // comments
2529 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002530 if (ERROR("C99_COMMENTS",
2531 "do not use C99 // comments\n" . $herecurr) &&
2532 $fix) {
2533 my $line = $fixed[$linenr - 1];
2534 if ($line =~ /\/\/(.*)$/) {
2535 my $comment = trim($1);
2536 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2537 }
2538 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002539 }
2540 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002541 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002542 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002543
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002544# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2545# the whole statement.
2546#print "APW <$lines[$realline_next - 1]>\n";
2547 if (defined $realline_next &&
2548 exists $lines[$realline_next - 1] &&
2549 !defined $suppress_export{$realline_next} &&
2550 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2551 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002552 # Handle definitions which produce identifiers with
2553 # a prefix:
2554 # XXX(foo);
2555 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002556 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002557 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002558 $name =~ /^${Ident}_$2/) {
2559#print "FOO C name<$name>\n";
2560 $suppress_export{$realline_next} = 1;
2561
2562 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002563 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002564 ^.DEFINE_$Ident\(\Q$name\E\)|
2565 ^.DECLARE_$Ident\(\Q$name\E\)|
2566 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002567 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2568 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002569 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002570#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2571 $suppress_export{$realline_next} = 2;
2572 } else {
2573 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002574 }
2575 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002576 if (!defined $suppress_export{$linenr} &&
2577 $prevline =~ /^.\s*$/ &&
2578 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2579 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2580#print "FOO B <$lines[$linenr - 1]>\n";
2581 $suppress_export{$linenr} = 2;
2582 }
2583 if (defined $suppress_export{$linenr} &&
2584 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002585 WARN("EXPORT_SYMBOL",
2586 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002587 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002588
Joe Eloff5150bda2010-08-09 17:21:00 -07002589# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002590 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2591 if (ERROR("GLOBAL_INITIALISERS",
2592 "do not initialise globals to 0 or NULL\n" .
2593 $herecurr) &&
2594 $fix) {
2595 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2596 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002597 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002598# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07002599 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2600 if (ERROR("INITIALISED_STATIC",
2601 "do not initialise statics to 0 or NULL\n" .
2602 $herecurr) &&
2603 $fix) {
2604 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2605 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002606 }
2607
Joe Perchescb710ec2010-10-26 14:23:20 -07002608# check for static const char * arrays.
2609 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002610 WARN("STATIC_CONST_CHAR_ARRAY",
2611 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002612 $herecurr);
2613 }
2614
2615# check for static char foo[] = "bar" declarations.
2616 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002617 WARN("STATIC_CONST_CHAR_ARRAY",
2618 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002619 $herecurr);
2620 }
2621
Joe Perches93ed0e22010-10-26 14:23:21 -07002622# check for declarations of struct pci_device_id
2623 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002624 WARN("DEFINE_PCI_DEVICE_TABLE",
2625 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002626 }
2627
Andy Whitcroft653d4872007-06-23 17:16:34 -07002628# check for new typedefs, only function parameters and sparse annotations
2629# make sense.
2630 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002631 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002632 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002633 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002634 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002635 WARN("NEW_TYPEDEFS",
2636 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002637 }
2638
2639# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002640 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002641 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2642 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002643 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002644
Andy Whitcroft65863862009-01-06 14:41:21 -08002645 # Should start with a space.
2646 $to =~ s/^(\S)/ $1/;
2647 # Should not end with a space.
2648 $to =~ s/\s+$//;
2649 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002650 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002651 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002652
Joe Perches3705ce52013-07-03 15:05:31 -07002653## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08002654 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07002655 if (ERROR("POINTER_LOCATION",
2656 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2657 $fix) {
2658 my $sub_from = $ident;
2659 my $sub_to = $ident;
2660 $sub_to =~ s/\Q$from\E/$to/;
2661 $fixed[$linenr - 1] =~
2662 s@\Q$sub_from\E@$sub_to@;
2663 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002664 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002665 }
2666 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2667 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002668 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002669
Andy Whitcroft65863862009-01-06 14:41:21 -08002670 # Should start with a space.
2671 $to =~ s/^(\S)/ $1/;
2672 # Should not end with a space.
2673 $to =~ s/\s+$//;
2674 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002675 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002676 }
2677 # Modifiers should have spaces.
2678 $to =~ s/(\b$Modifier$)/$1 /;
2679
Joe Perches3705ce52013-07-03 15:05:31 -07002680## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08002681 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002682 if (ERROR("POINTER_LOCATION",
2683 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2684 $fix) {
2685
2686 my $sub_from = $match;
2687 my $sub_to = $match;
2688 $sub_to =~ s/\Q$from\E/$to/;
2689 $fixed[$linenr - 1] =~
2690 s@\Q$sub_from\E@$sub_to@;
2691 }
Andy Whitcroft65863862009-01-06 14:41:21 -08002692 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002693 }
2694
2695# # no BUG() or BUG_ON()
2696# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2697# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2698# print "$herecurr";
2699# $clean = 0;
2700# }
2701
Andy Whitcroft8905a672007-11-28 16:21:06 -08002702 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002703 WARN("LINUX_VERSION_CODE",
2704 "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 -08002705 }
2706
Joe Perches17441222011-06-15 15:08:17 -07002707# check for uses of printk_ratelimit
2708 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002709 WARN("PRINTK_RATELIMITED",
2710"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002711 }
2712
Andy Whitcroft00df3442007-06-08 13:47:06 -07002713# printk should use KERN_* levels. Note that follow on printk's on the
2714# same line do not need a level, so we use the current block context
2715# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002716# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002717# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002718 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002719 my $ok = 0;
2720 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2721 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002722 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002723 # with "\n" ignore it, else it is to blame
2724 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2725 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2726 $ok = 1;
2727 }
2728 last;
2729 }
2730 }
2731 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002732 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2733 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002734 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002735 }
2736
Joe Perches243f3802012-05-31 16:26:09 -07002737 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2738 my $orig = $1;
2739 my $level = lc($orig);
2740 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002741 my $level2 = $level;
2742 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002743 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002744 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002745 }
2746
2747 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07002748 if (WARN("PREFER_PR_LEVEL",
2749 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2750 $fix) {
2751 $fixed[$linenr - 1] =~
2752 s/\bpr_warning\b/pr_warn/;
2753 }
Joe Perches243f3802012-05-31 16:26:09 -07002754 }
2755
Joe Perchesdc139312013-02-21 16:44:13 -08002756 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2757 my $orig = $1;
2758 my $level = lc($orig);
2759 $level = "warn" if ($level eq "warning");
2760 $level = "dbg" if ($level eq "debug");
2761 WARN("PREFER_DEV_LEVEL",
2762 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2763 }
2764
Andy Whitcroft653d4872007-06-23 17:16:34 -07002765# function brace can't be on same line, except for #defines of do while,
2766# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002767 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2768 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002769 ERROR("OPEN_BRACE",
2770 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002771 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002772
Andy Whitcroft8905a672007-11-28 16:21:06 -08002773# open braces for enum, union and struct go on the same line.
2774 if ($line =~ /^.\s*{/ &&
2775 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002776 ERROR("OPEN_BRACE",
2777 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002778 }
2779
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002780# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07002781 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2782 if (WARN("SPACING",
2783 "missing space after $1 definition\n" . $herecurr) &&
2784 $fix) {
2785 $fixed[$linenr - 1] =~
2786 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2787 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002788 }
2789
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002790# check for spacing round square brackets; allowed:
2791# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002792# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2793# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002794 while ($line =~ /(.*?\s)\[/g) {
2795 my ($where, $prefix) = ($-[1], $1);
2796 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002797 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002798 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002799 if (ERROR("BRACKET_SPACE",
2800 "space prohibited before open square bracket '['\n" . $herecurr) &&
2801 $fix) {
2802 $fixed[$linenr - 1] =~
2803 s/^(\+.*?)\s+\[/$1\[/;
2804 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002805 }
2806 }
2807
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002808# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002809 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002810 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002811 my $ctx_before = substr($line, 0, $-[1]);
2812 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002813
2814 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002815 if ($name =~ /^(?:
2816 if|for|while|switch|return|case|
2817 volatile|__volatile__|
2818 __attribute__|format|__extension__|
2819 asm|__asm__)$/x)
2820 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002821 # cpp #define statements have non-optional spaces, ie
2822 # if there is a space between the name and the open
2823 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002824 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002825
2826 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002827 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002828
2829 # If this whole things ends with a type its most
2830 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002831 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002832
2833 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07002834 if (WARN("SPACING",
2835 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2836 $fix) {
2837 $fixed[$linenr - 1] =~
2838 s/\b$name\s+\(/$name\(/;
2839 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002840 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002841 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002842
Andy Whitcroft653d4872007-06-23 17:16:34 -07002843# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002844 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002845 my $fixed_line = "";
2846 my $line_fixed = 0;
2847
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002848 my $ops = qr{
2849 <<=|>>=|<=|>=|==|!=|
2850 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2851 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002852 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08002853 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002854 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002855 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07002856
2857## print("element count: <" . $#elements . ">\n");
2858## foreach my $el (@elements) {
2859## print("el: <$el>\n");
2860## }
2861
2862 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07002863 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002864
Joe Perches3705ce52013-07-03 15:05:31 -07002865 foreach my $el (@elements) {
2866 push(@fix_elements, substr($rawline, $off, length($el)));
2867 $off += length($el);
2868 }
2869
2870 $off = 0;
2871
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002872 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07002873 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002874
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002875 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07002876
2877 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2878
2879## print("n: <$n> good: <$good>\n");
2880
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002881 $off += length($elements[$n]);
2882
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002883 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002884 my $ca = substr($opline, 0, $off);
2885 my $cc = '';
2886 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2887 $cc = substr($opline, $off + length($elements[$n + 1]));
2888 }
2889 my $cb = "$ca$;$cc";
2890
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002891 my $a = '';
2892 $a = 'V' if ($elements[$n] ne '');
2893 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002894 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002895 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2896 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002897 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002898
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002899 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002900
2901 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002902 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002903 $c = 'V' if ($elements[$n + 2] ne '');
2904 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002905 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002906 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2907 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002908 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002909 } else {
2910 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002911 }
2912
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002913 my $ctx = "${a}x${c}";
2914
2915 my $at = "(ctx:$ctx)";
2916
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002917 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002918 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002919
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002920 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002921 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002922
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002923 # Get the full operator variant.
2924 my $opv = $op . substr($curr_vars, $off, 1);
2925
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002926 # Ignore operators passed as parameters.
2927 if ($op_type ne 'V' &&
2928 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2929
Andy Whitcroftcf655042008-03-04 14:28:20 -08002930# # Ignore comments
2931# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002932
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002933 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002934 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002935 if ($ctx !~ /.x[WEBC]/ &&
2936 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002937 if (ERROR("SPACING",
2938 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002939 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002940 $line_fixed = 1;
2941 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002942 }
2943
2944 # // is a comment
2945 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002946
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002947 # No spaces for:
2948 # ->
2949 # : when part of a bitfield
2950 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002951 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002952 if (ERROR("SPACING",
2953 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002954 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002955 if (defined $fix_elements[$n + 2]) {
2956 $fix_elements[$n + 2] =~ s/^\s+//;
2957 }
Joe Perchesb34c6482013-09-11 14:24:01 -07002958 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002959 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002960 }
2961
2962 # , must have a space on the right.
2963 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002964 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002965 if (ERROR("SPACING",
2966 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002967 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07002968 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07002969 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07002970 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002971 }
2972
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002973 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002974 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002975 #warn "'*' is part of type\n";
2976
2977 # unary operators should have a space before and
2978 # none after. May be left adjacent to another
2979 # unary operator, or a cast
2980 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002981 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002982 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002983 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002984 if (ERROR("SPACING",
2985 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002986 if ($n != $last_after + 2) {
2987 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
2988 $line_fixed = 1;
2989 }
Joe Perches3705ce52013-07-03 15:05:31 -07002990 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002991 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002992 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002993 # A unary '*' may be const
2994
2995 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002996 if (ERROR("SPACING",
2997 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07002998 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07002999 if (defined $fix_elements[$n + 2]) {
3000 $fix_elements[$n + 2] =~ s/^\s+//;
3001 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003002 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003003 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003004 }
3005
3006 # unary ++ and unary -- are allowed no space on one side.
3007 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003008 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003009 if (ERROR("SPACING",
3010 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003011 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003012 $line_fixed = 1;
3013 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003014 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003015 if ($ctx =~ /Wx[BE]/ ||
3016 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003017 if (ERROR("SPACING",
3018 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003019 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003020 $line_fixed = 1;
3021 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003022 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003023 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003024 if (ERROR("SPACING",
3025 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003026 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003027 if (defined $fix_elements[$n + 2]) {
3028 $fix_elements[$n + 2] =~ s/^\s+//;
3029 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003030 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003031 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003032 }
3033
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003034 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003035 } elsif ($op eq '<<' or $op eq '>>' or
3036 $op eq '&' or $op eq '^' or $op eq '|' or
3037 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003038 $op eq '*' or $op eq '/' or
3039 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003040 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003041 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003042 if (ERROR("SPACING",
3043 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003044 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3045 if (defined $fix_elements[$n + 2]) {
3046 $fix_elements[$n + 2] =~ s/^\s+//;
3047 }
Joe Perches3705ce52013-07-03 15:05:31 -07003048 $line_fixed = 1;
3049 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003050 }
3051
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003052 # A colon needs no spaces before when it is
3053 # terminating a case value or a label.
3054 } elsif ($opv eq ':C' || $opv eq ':L') {
3055 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003056 if (ERROR("SPACING",
3057 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003058 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003059 $line_fixed = 1;
3060 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003061 }
3062
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003063 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003064 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003065 my $ok = 0;
3066
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003067 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003068 if (($op eq '<' &&
3069 $cc =~ /^\S+\@\S+>/) ||
3070 ($op eq '>' &&
3071 $ca =~ /<\S+\@\S+$/))
3072 {
3073 $ok = 1;
3074 }
3075
Joe Perches84731622013-11-12 15:10:05 -08003076 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003077 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003078 my $msg_type = \&ERROR;
3079 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3080
3081 if (&{$msg_type}("SPACING",
3082 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003083 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3084 if (defined $fix_elements[$n + 2]) {
3085 $fix_elements[$n + 2] =~ s/^\s+//;
3086 }
Joe Perches3705ce52013-07-03 15:05:31 -07003087 $line_fixed = 1;
3088 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003089 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003090 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003091 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003092
3093## print("n: <$n> GOOD: <$good>\n");
3094
3095 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003096 }
Joe Perches3705ce52013-07-03 15:05:31 -07003097
3098 if (($#elements % 2) == 0) {
3099 $fixed_line = $fixed_line . $fix_elements[$#elements];
3100 }
3101
3102 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3103 $fixed[$linenr - 1] = $fixed_line;
3104 }
3105
3106
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003107 }
3108
Joe Perches786b6322013-07-03 15:05:32 -07003109# check for whitespace before a non-naked semicolon
3110 if ($line =~ /^\+.*\S\s+;/) {
3111 if (WARN("SPACING",
3112 "space prohibited before semicolon\n" . $herecurr) &&
3113 $fix) {
3114 1 while $fixed[$linenr - 1] =~
3115 s/^(\+.*\S)\s+;/$1;/;
3116 }
3117 }
3118
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003119# check for multiple assignments
3120 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003121 CHK("MULTIPLE_ASSIGNMENTS",
3122 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003123 }
3124
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003125## # check for multiple declarations, allowing for a function declaration
3126## # continuation.
3127## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3128## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3129##
3130## # Remove any bracketed sections to ensure we do not
3131## # falsly report the parameters of functions.
3132## my $ln = $line;
3133## while ($ln =~ s/\([^\(\)]*\)//g) {
3134## }
3135## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003136## WARN("MULTIPLE_DECLARATION",
3137## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003138## }
3139## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003140
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003141#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003142 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3143 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003144 if (ERROR("SPACING",
3145 "space required before the open brace '{'\n" . $herecurr) &&
3146 $fix) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003147 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003148 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003149 }
3150
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003151## # check for blank lines before declarations
3152## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3153## $prevrawline =~ /^.\s*$/) {
3154## WARN("SPACING",
3155## "No blank lines before declarations\n" . $hereprev);
3156## }
3157##
3158
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003159# closing brace should have a space following it when it has anything
3160# on the line
3161 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003162 if (ERROR("SPACING",
3163 "space required after that close brace '}'\n" . $herecurr) &&
3164 $fix) {
3165 $fixed[$linenr - 1] =~
3166 s/}((?!(?:,|;|\)))\S)/} $1/;
3167 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003168 }
3169
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003170# check spacing on square brackets
3171 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003172 if (ERROR("SPACING",
3173 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3174 $fix) {
3175 $fixed[$linenr - 1] =~
3176 s/\[\s+/\[/;
3177 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003178 }
3179 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003180 if (ERROR("SPACING",
3181 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3182 $fix) {
3183 $fixed[$linenr - 1] =~
3184 s/\s+\]/\]/;
3185 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003186 }
3187
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003188# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003189 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3190 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003191 if (ERROR("SPACING",
3192 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3193 $fix) {
3194 $fixed[$linenr - 1] =~
3195 s/\(\s+/\(/;
3196 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003197 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003198 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003199 $line !~ /for\s*\(.*;\s+\)/ &&
3200 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003201 if (ERROR("SPACING",
3202 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3203 $fix) {
3204 $fixed[$linenr - 1] =~
3205 s/\s+\)/\)/;
3206 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003207 }
3208
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003209#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003210 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003211 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003212 if (WARN("INDENTED_LABEL",
3213 "labels should not be indented\n" . $herecurr) &&
3214 $fix) {
3215 $fixed[$linenr - 1] =~
3216 s/^(.)\s+/$1/;
3217 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003218 }
3219
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003220# Return is not a function.
3221 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3222 my $spacing = $1;
3223 my $value = $2;
3224
Andy Whitcroft86f9d052009-01-06 14:41:24 -08003225 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003226 $value =~ s/\(/ \(/g;
3227 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08003228 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08003229 $value !~ /(?:$Ident|-?$Constant)\s*
3230 $Compare\s*
3231 (?:$Ident|-?$Constant)/x &&
3232 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003233 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07003234#print "value<$value>\n";
3235 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003236 ERROR("RETURN_PARENTHESES",
3237 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003238
3239 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003240 ERROR("SPACING",
3241 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242 }
3243 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003244# Return of what appears to be an errno should normally be -'ve
3245 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3246 my $name = $1;
3247 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003248 WARN("USE_NEGATIVE_ERRNO",
3249 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003250 }
3251 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003252
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003253# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003254 if ($line =~ /\b(if|while|for|switch)\(/) {
3255 if (ERROR("SPACING",
3256 "space required before the open parenthesis '('\n" . $herecurr) &&
3257 $fix) {
3258 $fixed[$linenr - 1] =~
3259 s/\b(if|while|for|switch)\(/$1 \(/;
3260 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003261 }
3262
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003263# Check for illegal assignment in if conditional -- and check for trailing
3264# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003265 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003266 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3267 ctx_statement_block($linenr, $realcnt, 0)
3268 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003269 my ($stat_next) = ctx_statement_block($line_nr_next,
3270 $remain_next, $off_next);
3271 $stat_next =~ s/\n./\n /g;
3272 ##print "stat<$stat> stat_next<$stat_next>\n";
3273
3274 if ($stat_next =~ /^\s*while\b/) {
3275 # If the statement carries leading newlines,
3276 # then count those as offsets.
3277 my ($whitespace) =
3278 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3279 my $offset =
3280 statement_rawlines($whitespace) - 1;
3281
3282 $suppress_whiletrailers{$line_nr_next +
3283 $offset} = 1;
3284 }
3285 }
3286 if (!defined $suppress_whiletrailers{$linenr} &&
3287 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003288 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003289
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003290 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003291 ERROR("ASSIGN_IN_IF",
3292 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003293 }
3294
3295 # Find out what is on the end of the line after the
3296 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003297 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003298 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003299 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003300 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3301 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003302 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003303 # Find out how long the conditional actually is.
3304 my @newlines = ($c =~ /\n/gs);
3305 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003306 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003307
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003308 $stat_real = raw_line($linenr, $cond_lines)
3309 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003310 if (defined($stat_real) && $cond_lines > 1) {
3311 $stat_real = "[...]\n$stat_real";
3312 }
3313
Joe Perches000d1cc12011-07-25 17:13:25 -07003314 ERROR("TRAILING_STATEMENTS",
3315 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003316 }
3317 }
3318
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003319# Check for bitwise tests written as boolean
3320 if ($line =~ /
3321 (?:
3322 (?:\[|\(|\&\&|\|\|)
3323 \s*0[xX][0-9]+\s*
3324 (?:\&\&|\|\|)
3325 |
3326 (?:\&\&|\|\|)
3327 \s*0[xX][0-9]+\s*
3328 (?:\&\&|\|\||\)|\])
3329 )/x)
3330 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003331 WARN("HEXADECIMAL_BOOLEAN_TEST",
3332 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003333 }
3334
Andy Whitcroft8905a672007-11-28 16:21:06 -08003335# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003336 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3337 my $s = $1;
3338 $s =~ s/$;//g; # Remove any comments
3339 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003340 ERROR("TRAILING_STATEMENTS",
3341 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003342 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003343 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003344# if should not continue a brace
3345 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003346 ERROR("TRAILING_STATEMENTS",
3347 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003348 $herecurr);
3349 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003350# case and default should not have general statements after them
3351 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3352 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003353 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003354 \s*return\s+
3355 )/xg)
3356 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003357 ERROR("TRAILING_STATEMENTS",
3358 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003359 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003360
3361 # Check for }<nl>else {, these must be at the same
3362 # indent level to be relevant to each other.
3363 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3364 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003365 ERROR("ELSE_AFTER_BRACE",
3366 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003367 }
3368
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003369 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3370 $previndent == $indent) {
3371 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3372
3373 # Find out what is on the end of the line after the
3374 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003375 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003376 $s =~ s/\n.*//g;
3377
3378 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003379 ERROR("WHILE_AFTER_BRACE",
3380 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003381 }
3382 }
3383
Joe Perches95e2c602013-07-03 15:05:20 -07003384#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003385 while ($line =~ m{($Constant|$Lval)}g) {
3386 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003387
3388#gcc binary extension
3389 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003390 if (WARN("GCC_BINARY_CONSTANT",
3391 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3392 $fix) {
3393 my $hexval = sprintf("0x%x", oct($var));
3394 $fixed[$linenr - 1] =~
3395 s/\b$var\b/$hexval/;
3396 }
Joe Perches95e2c602013-07-03 15:05:20 -07003397 }
3398
3399#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003400 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07003401 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003402#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07003403 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07003404#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07003405 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07003406 while ($var =~ m{($Ident)}g) {
3407 my $word = $1;
3408 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08003409 if ($check) {
3410 seed_camelcase_includes();
3411 if (!$file && !$camelcase_file_seeded) {
3412 seed_camelcase_file($realfile);
3413 $camelcase_file_seeded = 1;
3414 }
3415 }
Joe Perches7e781f62013-09-11 14:23:55 -07003416 if (!defined $camelcase{$word}) {
3417 $camelcase{$word} = 1;
3418 CHK("CAMELCASE",
3419 "Avoid CamelCase: <$word>\n" . $herecurr);
3420 }
Joe Perches34456862013-07-03 15:05:34 -07003421 }
Joe Perches323c1262012-12-17 16:02:07 -08003422 }
3423 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003424
3425#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07003426 if ($line =~ /\#\s*define.*\\\s+$/) {
3427 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3428 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3429 $fix) {
3430 $fixed[$linenr - 1] =~ s/\s+$//;
3431 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003432 }
3433
Andy Whitcroft653d4872007-06-23 17:16:34 -07003434#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003435 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003436 my $file = "$1.h";
3437 my $checkfile = "include/linux/$file";
3438 if (-f "$root/$checkfile" &&
3439 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003440 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003441 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003442 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003443 CHK("ARCH_INCLUDE_LINUX",
3444 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003445 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003446 WARN("INCLUDE_LINUX",
3447 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003448 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003449 }
3450 }
3451
Andy Whitcroft653d4872007-06-23 17:16:34 -07003452# multi-statement macros should be enclosed in a do while loop, grab the
3453# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003454# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003455 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3456 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003457 my $ln = $linenr;
3458 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003459 my ($off, $dstat, $dcond, $rest);
3460 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003461 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003462 ctx_statement_block($linenr, $realcnt, 0);
3463 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003464 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003465 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003466
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003467 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003468 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003469 $dstat =~ s/\\\n.//g;
3470 $dstat =~ s/^\s*//s;
3471 $dstat =~ s/\s*$//s;
3472
3473 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003474 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3475 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003476 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003477 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003478 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003479
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003480 # Flatten any obvious string concatentation.
3481 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3482 $dstat =~ s/$Ident\s*("X*")/$1/)
3483 {
3484 }
3485
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003486 my $exceptions = qr{
3487 $Declare|
3488 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003489 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003490 DECLARE_PER_CPU|
3491 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003492 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003493 union|
3494 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003495 \.$Ident\s*=\s*|
3496 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003497 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003498 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003499 if ($dstat ne '' &&
3500 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3501 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003502 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003503 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003504 $dstat !~ /$exceptions/ &&
3505 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003506 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003507 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003508 $dstat !~ /^for\s*$Constant$/ && # for (...)
3509 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3510 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07003511 $dstat !~ /^\({/ && # ({...
3512 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003513 {
3514 $ctx =~ s/\n*$//;
3515 my $herectx = $here . "\n";
3516 my $cnt = statement_rawlines($ctx);
3517
3518 for (my $n = 0; $n < $cnt; $n++) {
3519 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003520 }
3521
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003522 if ($dstat =~ /;/) {
3523 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3524 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3525 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003526 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003527 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003528 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003529 }
Joe Perches5023d342012-12-17 16:01:47 -08003530
Joe Perches481eb482012-12-17 16:01:56 -08003531# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003532
3533 } else {
3534 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003535 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3536 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003537 $line =~ /^\+.*\\$/) {
3538 WARN("LINE_CONTINUATIONS",
3539 "Avoid unnecessary line continuations\n" . $herecurr);
3540 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003541 }
3542
Joe Perchesb13edf72012-07-30 14:41:24 -07003543# do {} while (0) macro tests:
3544# single-statement macros do not need to be enclosed in do while (0) loop,
3545# macro should not end with a semicolon
3546 if ($^V && $^V ge 5.10.0 &&
3547 $realfile !~ m@/vmlinux.lds.h$@ &&
3548 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3549 my $ln = $linenr;
3550 my $cnt = $realcnt;
3551 my ($off, $dstat, $dcond, $rest);
3552 my $ctx = '';
3553 ($dstat, $dcond, $ln, $cnt, $off) =
3554 ctx_statement_block($linenr, $realcnt, 0);
3555 $ctx = $dstat;
3556
3557 $dstat =~ s/\\\n.//g;
3558
3559 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3560 my $stmts = $2;
3561 my $semis = $3;
3562
3563 $ctx =~ s/\n*$//;
3564 my $cnt = statement_rawlines($ctx);
3565 my $herectx = $here . "\n";
3566
3567 for (my $n = 0; $n < $cnt; $n++) {
3568 $herectx .= raw_line($linenr, $n) . "\n";
3569 }
3570
Joe Perchesac8e97f2012-08-21 16:15:53 -07003571 if (($stmts =~ tr/;/;/) == 1 &&
3572 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003573 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3574 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3575 }
3576 if (defined $semis && $semis ne "") {
3577 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3578 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3579 }
3580 }
3581 }
3582
Mike Frysinger080ba922009-01-06 14:41:25 -08003583# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3584# all assignments may have only one of the following with an assignment:
3585# .
3586# ALIGN(...)
3587# VMLINUX_SYMBOL(...)
3588 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003589 WARN("MISSING_VMLINUX_SYMBOL",
3590 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003591 }
3592
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003593# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003594 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3595 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003596 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003597 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003598 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3599 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003600 my @allowed = ();
3601 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003602 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003603 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003604 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003605 for my $chunk (@chunks) {
3606 my ($cond, $block) = @{$chunk};
3607
Andy Whitcroft773647a2008-03-28 14:15:58 -07003608 # If the condition carries leading newlines, then count those as offsets.
3609 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3610 my $offset = statement_rawlines($whitespace) - 1;
3611
Joe Perchesaad4f612012-03-23 15:02:19 -07003612 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003613 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3614
3615 # We have looked at and allowed this specific line.
3616 $suppress_ifbraces{$ln + $offset} = 1;
3617
3618 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003619 $ln += statement_rawlines($block) - 1;
3620
Andy Whitcroft773647a2008-03-28 14:15:58 -07003621 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003622
3623 $seen++ if ($block =~ /^\s*{/);
3624
Joe Perchesaad4f612012-03-23 15:02:19 -07003625 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003626 if (statement_lines($cond) > 1) {
3627 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003628 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003629 }
3630 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003631 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003632 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003633 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003634 if (statement_block_size($block) > 1) {
3635 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003636 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003637 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003638 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003639 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003640 if ($seen) {
3641 my $sum_allowed = 0;
3642 foreach (@allowed) {
3643 $sum_allowed += $_;
3644 }
3645 if ($sum_allowed == 0) {
3646 WARN("BRACES",
3647 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3648 } elsif ($sum_allowed != $allow &&
3649 $seen != $allow) {
3650 CHK("BRACES",
3651 "braces {} should be used on all arms of this statement\n" . $herectx);
3652 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003653 }
3654 }
3655 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003656 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003657 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003658 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003659
Andy Whitcroftcf655042008-03-04 14:28:20 -08003660 # Check the pre-context.
3661 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3662 #print "APW: ALLOWED: pre<$1>\n";
3663 $allowed = 1;
3664 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003665
3666 my ($level, $endln, @chunks) =
3667 ctx_statement_full($linenr, $realcnt, $-[0]);
3668
Andy Whitcroftcf655042008-03-04 14:28:20 -08003669 # Check the condition.
3670 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003671 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003672 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003673 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003674 }
3675 if (statement_lines($cond) > 1) {
3676 #print "APW: ALLOWED: cond<$cond>\n";
3677 $allowed = 1;
3678 }
3679 if ($block =~/\b(?:if|for|while)\b/) {
3680 #print "APW: ALLOWED: block<$block>\n";
3681 $allowed = 1;
3682 }
3683 if (statement_block_size($block) > 1) {
3684 #print "APW: ALLOWED: lines block<$block>\n";
3685 $allowed = 1;
3686 }
3687 # Check the post-context.
3688 if (defined $chunks[1]) {
3689 my ($cond, $block) = @{$chunks[1]};
3690 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003691 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003692 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003693 if ($block =~ /^\s*\{/) {
3694 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3695 $allowed = 1;
3696 }
3697 }
3698 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003699 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003700 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003701
Andy Whitcroftf0556632008-10-15 22:02:23 -07003702 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003703 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003704 }
3705
Joe Perches000d1cc12011-07-25 17:13:25 -07003706 WARN("BRACES",
3707 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003708 }
3709 }
3710
Joe Perches0979ae62012-12-17 16:01:59 -08003711# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003712 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003713 CHK("BRACES",
3714 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3715 }
Joe Perches77b9a532013-07-03 15:05:29 -07003716 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003717 CHK("BRACES",
3718 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3719 }
3720
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003721# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003722 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3723 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003724 WARN("VOLATILE",
3725 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003726 }
3727
Andy Whitcroft00df3442007-06-08 13:47:06 -07003728# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003729 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003730 CHK("REDUNDANT_CODE",
3731 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003732 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003733 }
3734
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003735# check for needless "if (<foo>) fn(<foo>)" uses
3736 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3737 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3738 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3739 WARN('NEEDLESS_IF',
3740 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003741 }
3742 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003743
Joe Perches8716de32013-09-11 14:24:05 -07003744# check for bad placement of section $InitAttribute (e.g.: __initdata)
3745 if ($line =~ /(\b$InitAttribute\b)/) {
3746 my $attr = $1;
3747 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3748 my $ptr = $1;
3749 my $var = $2;
3750 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3751 ERROR("MISPLACED_INIT",
3752 "$attr should be placed after $var\n" . $herecurr)) ||
3753 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3754 WARN("MISPLACED_INIT",
3755 "$attr should be placed after $var\n" . $herecurr))) &&
3756 $fix) {
3757 $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;
3758 }
3759 }
3760 }
3761
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003762# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003763 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003764 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003765 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003766 CHK("USLEEP_RANGE",
3767 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003768 }
3769 }
3770
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003771# warn about unexpectedly long msleep's
3772 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3773 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003774 WARN("MSLEEP",
3775 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003776 }
3777 }
3778
Joe Perches36ec1932013-07-03 15:05:25 -07003779# check for comparisons of jiffies
3780 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3781 WARN("JIFFIES_COMPARISON",
3782 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3783 }
3784
Joe Perches9d7a34a2013-07-03 15:05:26 -07003785# check for comparisons of get_jiffies_64()
3786 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3787 WARN("JIFFIES_COMPARISON",
3788 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3789 }
3790
Andy Whitcroft00df3442007-06-08 13:47:06 -07003791# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003792# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003793# print "#ifdef in C files should be avoided\n";
3794# print "$herecurr";
3795# $clean = 0;
3796# }
3797
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003798# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003799 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003800 if (ERROR("SPACING",
3801 "exactly one space required after that #$1\n" . $herecurr) &&
3802 $fix) {
3803 $fixed[$linenr - 1] =~
3804 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3805 }
3806
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003807 }
3808
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003809# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003810 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3811 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003812 my $which = $1;
3813 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003814 CHK("UNCOMMENTED_DEFINITION",
3815 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003816 }
3817 }
3818# check for memory barriers without a comment.
3819 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3820 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003821 CHK("MEMORY_BARRIER",
3822 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003823 }
3824 }
3825# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003826 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 CHK("ARCH_DEFINES",
3828 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003829 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003830
Tobias Klauserd4977c72010-05-24 14:33:30 -07003831# Check that the storage class is at the beginning of a declaration
3832 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003833 WARN("STORAGE_CLASS",
3834 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003835 }
3836
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003837# check the location of the inline attribute, that it is between
3838# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003839 if ($line =~ /\b$Type\s+$Inline\b/ ||
3840 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003841 ERROR("INLINE_LOCATION",
3842 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003843 }
3844
Andy Whitcroft8905a672007-11-28 16:21:06 -08003845# Check for __inline__ and __inline, prefer inline
3846 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003847 if (WARN("INLINE",
3848 "plain inline is preferred over $1\n" . $herecurr) &&
3849 $fix) {
3850 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3851
3852 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003853 }
3854
Joe Perches3d130fd2011-01-12 17:00:00 -08003855# Check for __attribute__ packed, prefer __packed
3856 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003857 WARN("PREFER_PACKED",
3858 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003859 }
3860
Joe Perches39b7e282011-07-25 17:13:24 -07003861# Check for __attribute__ aligned, prefer __aligned
3862 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003863 WARN("PREFER_ALIGNED",
3864 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003865 }
3866
Joe Perches5f14d3b2012-01-10 15:09:52 -08003867# Check for __attribute__ format(printf, prefer __printf
3868 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003869 if (WARN("PREFER_PRINTF",
3870 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3871 $fix) {
3872 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3873
3874 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08003875 }
3876
Joe Perches6061d942012-03-23 15:02:16 -07003877# Check for __attribute__ format(scanf, prefer __scanf
3878 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003879 if (WARN("PREFER_SCANF",
3880 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3881 $fix) {
3882 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3883 }
Joe Perches6061d942012-03-23 15:02:16 -07003884 }
3885
Joe Perches8f53a9b2010-03-05 13:43:48 -08003886# check for sizeof(&)
3887 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003888 WARN("SIZEOF_ADDRESS",
3889 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003890 }
3891
Joe Perches66c80b62012-07-30 14:41:22 -07003892# check for sizeof without parenthesis
3893 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003894 if (WARN("SIZEOF_PARENTHESIS",
3895 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3896 $fix) {
3897 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3898 }
Joe Perches66c80b62012-07-30 14:41:22 -07003899 }
3900
Joe Perches428e2fd2011-05-24 17:13:39 -07003901# check for line continuations in quoted strings with odd counts of "
3902 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003903 WARN("LINE_CONTINUATIONS",
3904 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003905 }
3906
Joe Perches88982fe2012-12-17 16:02:00 -08003907# check for struct spinlock declarations
3908 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3909 WARN("USE_SPINLOCK_T",
3910 "struct spinlock should be spinlock_t\n" . $herecurr);
3911 }
3912
Joe Perchesa6962d72013-04-29 16:18:13 -07003913# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08003914 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07003915 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08003916 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003917 if (WARN("PREFER_SEQ_PUTS",
3918 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3919 $fix) {
3920 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3921 }
Joe Perchesa6962d72013-04-29 16:18:13 -07003922 }
3923 }
3924
Andy Whitcroft554e1652012-01-10 15:09:57 -08003925# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003926 if ($^V && $^V ge 5.10.0 &&
3927 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003928 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003929
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003930 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003931 my $ms_val = $7;
3932 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003933
Andy Whitcroft554e1652012-01-10 15:09:57 -08003934 if ($ms_size =~ /^(0x|)0$/i) {
3935 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003936 "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 -08003937 } elsif ($ms_size =~ /^(0x|)1$/i) {
3938 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003939 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3940 }
3941 }
3942
3943# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003944 if ($^V && $^V ge 5.10.0 &&
3945 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003946 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003947 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003948 my $call = $1;
3949 my $cast1 = deparenthesize($2);
3950 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003951 my $cast2 = deparenthesize($7);
3952 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003953 my $cast;
3954
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003955 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003956 $cast = "$cast1 or $cast2";
3957 } elsif ($cast1 ne "") {
3958 $cast = $cast1;
3959 } else {
3960 $cast = $cast2;
3961 }
3962 WARN("MINMAX",
3963 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003964 }
3965 }
3966
Joe Perches4a273192012-07-30 14:41:20 -07003967# check usleep_range arguments
3968 if ($^V && $^V ge 5.10.0 &&
3969 defined $stat &&
3970 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3971 my $min = $1;
3972 my $max = $7;
3973 if ($min eq $max) {
3974 WARN("USLEEP_RANGE",
3975 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3976 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3977 $min > $max) {
3978 WARN("USLEEP_RANGE",
3979 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3980 }
3981 }
3982
Joe Perches70dc8a42013-09-11 14:23:58 -07003983# check for new externs in .h files.
3984 if ($realfile =~ /\.h$/ &&
3985 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07003986 if (CHK("AVOID_EXTERNS",
3987 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07003988 $fix) {
3989 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3990 }
3991 }
3992
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003993# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003994 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003995 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003996 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003997 my $function_name = $1;
3998 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003999
4000 my $s = $stat;
4001 if (defined $cond) {
4002 substr($s, 0, length($cond), '');
4003 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004004 if ($s =~ /^\s*;/ &&
4005 $function_name ne 'uninitialized_var')
4006 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004007 WARN("AVOID_EXTERNS",
4008 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004009 }
4010
4011 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004012 WARN("FUNCTION_ARGUMENTS",
4013 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004014 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004015
4016 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4017 $stat =~ /^.\s*extern\s+/)
4018 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004019 WARN("AVOID_EXTERNS",
4020 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004021 }
4022
4023# checks for new __setup's
4024 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4025 my $name = $1;
4026
4027 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004028 CHK("UNDOCUMENTED_SETUP",
4029 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004030 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004031 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004032
4033# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004034 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004035 WARN("UNNECESSARY_CASTS",
4036 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004037 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004038
Joe Perchesa640d252013-07-03 15:05:21 -07004039# alloc style
4040# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4041 if ($^V && $^V ge 5.10.0 &&
4042 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4043 CHK("ALLOC_SIZEOF_STRUCT",
4044 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4045 }
4046
Joe Perches972fdea2013-04-29 16:18:12 -07004047# check for krealloc arg reuse
4048 if ($^V && $^V ge 5.10.0 &&
4049 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4050 WARN("KREALLOC_ARG_REUSE",
4051 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4052 }
4053
Joe Perches5ce59ae2013-02-21 16:44:18 -08004054# check for alloc argument mismatch
4055 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4056 WARN("ALLOC_ARRAY_ARGS",
4057 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4058 }
4059
Joe Perchescaf2a542011-01-12 16:59:56 -08004060# check for multiple semicolons
4061 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004062 if (WARN("ONE_SEMICOLON",
4063 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4064 $fix) {
4065 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4066 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004067 }
4068
4069# check for switch/default statements without a break;
4070 if ($^V && $^V ge 5.10.0 &&
4071 defined $stat &&
4072 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4073 my $ctx = '';
4074 my $herectx = $here . "\n";
4075 my $cnt = statement_rawlines($stat);
4076 for (my $n = 0; $n < $cnt; $n++) {
4077 $herectx .= raw_line($linenr, $n) . "\n";
4078 }
4079 WARN("DEFAULT_NO_BREAK",
4080 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004081 }
4082
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004083# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004084 if ($line =~ /\b__FUNCTION__\b/) {
4085 if (WARN("USE_FUNC",
4086 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4087 $fix) {
4088 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4089 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004090 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004091
Joe Perches2c924882012-03-23 15:02:20 -07004092# check for use of yield()
4093 if ($line =~ /\byield\s*\(\s*\)/) {
4094 WARN("YIELD",
4095 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4096 }
4097
Joe Perches179f8f42013-07-03 15:05:30 -07004098# check for comparisons against true and false
4099 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4100 my $lead = $1;
4101 my $arg = $2;
4102 my $test = $3;
4103 my $otype = $4;
4104 my $trail = $5;
4105 my $op = "!";
4106
4107 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4108
4109 my $type = lc($otype);
4110 if ($type =~ /^(?:true|false)$/) {
4111 if (("$test" eq "==" && "$type" eq "true") ||
4112 ("$test" eq "!=" && "$type" eq "false")) {
4113 $op = "";
4114 }
4115
4116 CHK("BOOL_COMPARISON",
4117 "Using comparison to $otype is error prone\n" . $herecurr);
4118
4119## maybe suggesting a correct construct would better
4120## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4121
4122 }
4123 }
4124
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004125# check for semaphores initialized locked
4126 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004127 WARN("CONSIDER_COMPLETION",
4128 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004129 }
Joe Perches6712d852012-03-23 15:02:20 -07004130
Joe Perches67d0a072011-10-31 17:13:10 -07004131# recommend kstrto* over simple_strto* and strict_strto*
4132 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004133 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004134 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004135 }
Joe Perches6712d852012-03-23 15:02:20 -07004136
Michael Ellermanf3db6632008-07-23 21:28:57 -07004137# check for __initcall(), use device_initcall() explicitly please
4138 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004139 WARN("USE_DEVICE_INITCALL",
4140 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07004141 }
Joe Perches6712d852012-03-23 15:02:20 -07004142
Emese Revfy79404842010-03-05 13:43:53 -08004143# check for various ops structs, ensure they are const.
4144 my $struct_ops = qr{acpi_dock_ops|
4145 address_space_operations|
4146 backlight_ops|
4147 block_device_operations|
4148 dentry_operations|
4149 dev_pm_ops|
4150 dma_map_ops|
4151 extent_io_ops|
4152 file_lock_operations|
4153 file_operations|
4154 hv_ops|
4155 ide_dma_ops|
4156 intel_dvo_dev_ops|
4157 item_operations|
4158 iwl_ops|
4159 kgdb_arch|
4160 kgdb_io|
4161 kset_uevent_ops|
4162 lock_manager_operations|
4163 microcode_ops|
4164 mtrr_ops|
4165 neigh_ops|
4166 nlmsvc_binding|
4167 pci_raw_ops|
4168 pipe_buf_operations|
4169 platform_hibernation_ops|
4170 platform_suspend_ops|
4171 proto_ops|
4172 rpc_pipe_ops|
4173 seq_operations|
4174 snd_ac97_build_ops|
4175 soc_pcmcia_socket_ops|
4176 stacktrace_ops|
4177 sysfs_ops|
4178 tty_operations|
4179 usb_mon_operations|
4180 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004181 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004182 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004183 WARN("CONST_STRUCT",
4184 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004185 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004186 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004187
4188# use of NR_CPUS is usually wrong
4189# ignore definitions of NR_CPUS and usage to define arrays as likely right
4190 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004191 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4192 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004193 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4194 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4195 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004196 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004197 WARN("NR_CPUS",
4198 "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 -07004199 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004200
Joe Perches52ea8502013-11-12 15:10:09 -08004201# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4202 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4203 ERROR("DEFINE_ARCH_HAS",
4204 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4205 }
4206
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004207# check for %L{u,d,i} in strings
4208 my $string;
4209 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4210 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004211 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004212 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004213 WARN("PRINTF_L",
4214 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004215 last;
4216 }
4217 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004218
4219# whine mightly about in_atomic
4220 if ($line =~ /\bin_atomic\s*\(/) {
4221 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004222 ERROR("IN_ATOMIC",
4223 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08004224 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004225 WARN("IN_ATOMIC",
4226 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004227 }
4228 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01004229
4230# check for lockdep_set_novalidate_class
4231 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
4232 $line =~ /__lockdep_no_validate__\s*\)/ ) {
4233 if ($realfile !~ m@^kernel/lockdep@ &&
4234 $realfile !~ m@^include/linux/lockdep@ &&
4235 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004236 ERROR("LOCKDEP",
4237 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004238 }
4239 }
Dave Jones88f88312011-01-12 16:59:59 -08004240
4241 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
4242 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004243 WARN("EXPORTED_WORLD_WRITABLE",
4244 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08004245 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004246 }
4247
4248 # If we have no input at all, then there is nothing to report on
4249 # so just keep quiet.
4250 if ($#rawlines == -1) {
4251 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004252 }
4253
Andy Whitcroft8905a672007-11-28 16:21:06 -08004254 # In mailback mode only produce a report in the negative, for
4255 # things that appear to be patches.
4256 if ($mailback && ($clean == 1 || !$is_patch)) {
4257 exit(0);
4258 }
4259
4260 # This is not a patch, and we are are in 'no-patch' mode so
4261 # just keep quiet.
4262 if (!$chk_patch && !$is_patch) {
4263 exit(0);
4264 }
4265
4266 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004267 ERROR("NOT_UNIFIED_DIFF",
4268 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004269 }
4270 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004271 ERROR("MISSING_SIGN_OFF",
4272 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004273 }
4274
Andy Whitcroft8905a672007-11-28 16:21:06 -08004275 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004276 if ($summary && !($clean == 1 && $quiet == 1)) {
4277 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004278 print "total: $cnt_error errors, $cnt_warn warnings, " .
4279 (($check)? "$cnt_chk checks, " : "") .
4280 "$cnt_lines lines checked\n";
4281 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004282 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004283
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004284 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004285
4286 if ($^V lt 5.10.0) {
4287 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4288 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4289 }
4290
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004291 # If there were whitespace errors which cleanpatch can fix
4292 # then suggest that.
4293 if ($rpt_cleaners) {
4294 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4295 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07004296 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07004297 }
4298 }
4299
Joe Perches91bfe482013-09-11 14:23:59 -07004300 hash_show_words(\%use_type, "Used");
4301 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07004302
Joe Perches3705ce52013-07-03 15:05:31 -07004303 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4304 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4305 my $linecount = 0;
4306 my $f;
4307
4308 open($f, '>', $newfile)
4309 or die "$P: Can't open $newfile for write\n";
4310 foreach my $fixed_line (@fixed) {
4311 $linecount++;
4312 if ($file) {
4313 if ($linecount > 3) {
4314 $fixed_line =~ s/^\+//;
4315 print $f $fixed_line. "\n";
4316 }
4317 } else {
4318 print $f $fixed_line . "\n";
4319 }
4320 }
4321 close($f);
4322
4323 if (!$quiet) {
4324 print << "EOM";
4325Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4326
4327Do _NOT_ trust the results written to this file.
4328Do _NOT_ submit these changes without inspecting them for correctness.
4329
4330This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4331No warranties, expressed or implied...
4332
4333EOM
4334 }
4335 }
4336
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004337 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004338 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004339 }
4340 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004341 print << "EOM";
4342$vname has style problems, please review.
4343
4344If any of these errors are false positives, please report
4345them to the maintainer, see CHECKPATCH in MAINTAINERS.
4346EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004347 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004348
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004349 return $clean;
4350}