blob: b953c76be3691c0f9c136f16fc7da77a17497f5d [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesf4432c52008-10-20 13:31:45 -04002# (c) 2001, Dave Jones. <davej@redhat.com> (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)
5# (c) 2008, 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;
9
10my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070012
Andy Whitcroft50a7dcf2009-01-06 14:41:21 -080013my $V = '0.25';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080030my %debug;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070031GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070033 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080037 'terse!' => \$terse,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070038 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080042 'summary!' => \$summary,
43 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044 'summary-file!' => \$summary_file,
45
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070047 'test-only=s' => \$tst_only,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070048) or exit;
49
50my $exit = 0;
51
52if ($#ARGV < 0) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070053 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070054 print "version: $V\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -080055 print "options: -q => quiet\n";
56 print " --no-tree => run without a kernel tree\n";
57 print " --terse => one line per report\n";
58 print " --emacs => emacs compile window format\n";
59 print " --file => check a source file\n";
60 print " --strict => enable more subjective tests\n";
61 print " --root => path to the kernel tree root\n";
62 print " --no-summary => suppress the per-file summary\n";
63 print " --summary-file => include the filename in summary\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070064 exit(1);
65}
66
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080067my $dbg_values = 0;
68my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -070069my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -070070my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080071for my $key (keys %debug) {
72 eval "\${dbg_$key} = '$debug{$key}';"
73}
74
Andy Whitcroft8905a672007-11-28 16:21:06 -080075if ($terse) {
76 $emacs = 1;
77 $quiet++;
78}
79
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070080if ($tree) {
81 if (defined $root) {
82 if (!top_of_kernel_tree($root)) {
83 die "$P: $root: --root does not point at a valid tree\n";
84 }
85 } else {
86 if (top_of_kernel_tree('.')) {
87 $root = '.';
88 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
89 top_of_kernel_tree($1)) {
90 $root = $1;
91 }
92 }
93
94 if (!defined $root) {
95 print "Must be run from the top-level dir. of a kernel tree\n";
96 exit(2);
97 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -070098}
99
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700100my $emitted_corrupt = 0;
101
102our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
103our $Storage = qr{extern|static|asmlinkage};
104our $Sparse = qr{
105 __user|
106 __kernel|
107 __force|
108 __iomem|
109 __must_check|
110 __init_refok|
Andy Whitcroftcf655042008-03-04 14:28:20 -0800111 __kprobes
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700112 }x;
113our $Attribute = qr{
114 const|
115 __read_mostly|
116 __kprobes|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700117 __(?:mem|cpu|dev|)(?:initdata|init)|
118 ____cacheline_aligned|
119 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800120 ____cacheline_internodealigned_in_smp|
121 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700122 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700123our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700124our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
126our $Lval = qr{$Ident(?:$Member)*};
127
128our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
129our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
130our $Operators = qr{
131 <=|>=|==|!=|
132 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800133 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700134 }x;
135
Andy Whitcroft8905a672007-11-28 16:21:06 -0800136our $NonptrType;
137our $Type;
138our $Declare;
139
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700140our $UTF8 = qr {
141 [\x09\x0A\x0D\x20-\x7E] # ASCII
142 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
143 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
144 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
145 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
146 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
147 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
148 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
149}x;
150
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700151our $typeTypedefs = qr{(?x:
152 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
153 atomic_t
154)};
155
Andy Whitcroft8905a672007-11-28 16:21:06 -0800156our @typeList = (
157 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700158 qr{(?:unsigned\s+)?char},
159 qr{(?:unsigned\s+)?short},
160 qr{(?:unsigned\s+)?int},
161 qr{(?:unsigned\s+)?long},
162 qr{(?:unsigned\s+)?long\s+int},
163 qr{(?:unsigned\s+)?long\s+long},
164 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800165 qr{unsigned},
166 qr{float},
167 qr{double},
168 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800169 qr{struct\s+$Ident},
170 qr{union\s+$Ident},
171 qr{enum\s+$Ident},
172 qr{${Ident}_t},
173 qr{${Ident}_handler},
174 qr{${Ident}_handler_fn},
175);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700176our @modifierList = (
177 qr{fastcall},
178);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800179
180sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700181 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
182 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700183 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800184 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700185 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800186 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700187 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700188 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700189 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800190 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700191 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800192 }x;
193 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700194 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800195 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700196 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800197 }x;
198 $Declare = qr{(?:$Storage\s+)?$Type};
199}
200build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700201
202$chk_signoff = 0 if ($file);
203
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700204my @dep_includes = ();
205my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700206my $removal = "Documentation/feature-removal-schedule.txt";
207if ($tree && -f "$root/$removal") {
208 open(REMOVE, "<$root/$removal") ||
209 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700210 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700211 if (/^Check:\s+(.*\S)/) {
212 for my $entry (split(/[, ]+/, $1)) {
213 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700214 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700215
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700216 } elsif ($entry !~ m@/@) {
217 push(@dep_functions, $entry);
218 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700219 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700220 }
221 }
222}
223
Andy Whitcroft00df3442007-06-08 13:47:06 -0700224my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800225my @lines = ();
226my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700227for my $filename (@ARGV) {
228 if ($file) {
229 open(FILE, "diff -u /dev/null $filename|") ||
230 die "$P: $filename: diff failed - $!\n";
231 } else {
232 open(FILE, "<$filename") ||
233 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700234 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800235 if ($filename eq '-') {
236 $vname = 'Your patch';
237 } else {
238 $vname = $filename;
239 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240 while (<FILE>) {
241 chomp;
242 push(@rawlines, $_);
243 }
244 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800245 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700246 $exit = 1;
247 }
248 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800249 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700250}
251
252exit($exit);
253
254sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700255 my ($root) = @_;
256
257 my @tree_check = (
258 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
259 "README", "Documentation", "arch", "include", "drivers",
260 "fs", "init", "ipc", "kernel", "lib", "scripts",
261 );
262
263 foreach my $check (@tree_check) {
264 if (! -e $root . '/' . $check) {
265 return 0;
266 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700267 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700268 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700269}
270
271sub expand_tabs {
272 my ($str) = @_;
273
274 my $res = '';
275 my $n = 0;
276 for my $c (split(//, $str)) {
277 if ($c eq "\t") {
278 $res .= ' ';
279 $n++;
280 for (; ($n % 8) != 0; $n++) {
281 $res .= ' ';
282 }
283 next;
284 }
285 $res .= $c;
286 $n++;
287 }
288
289 return $res;
290}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700292 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700293 return $res;
294}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700295
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700296sub line_stats {
297 my ($line) = @_;
298
299 # Drop the diff line leader and expand tabs
300 $line =~ s/^.//;
301 $line = expand_tabs($line);
302
303 # Pick the indent from the front of the line.
304 my ($white) = ($line =~ /^(\s*)/);
305
306 return (length($line), length($white));
307}
308
Andy Whitcroft773647a2008-03-28 14:15:58 -0700309my $sanitise_quote = '';
310
311sub sanitise_line_reset {
312 my ($in_comment) = @_;
313
314 if ($in_comment) {
315 $sanitise_quote = '*/';
316 } else {
317 $sanitise_quote = '';
318 }
319}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700320sub sanitise_line {
321 my ($line) = @_;
322
323 my $res = '';
324 my $l = '';
325
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800326 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700327 my $off = 0;
328 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700329
Andy Whitcroft773647a2008-03-28 14:15:58 -0700330 # Always copy over the diff marker.
331 $res = substr($line, 0, 1);
332
333 for ($off = 1; $off < length($line); $off++) {
334 $c = substr($line, $off, 1);
335
336 # Comments we are wacking completly including the begin
337 # and end, all to $;.
338 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
339 $sanitise_quote = '*/';
340
341 substr($res, $off, 2, "$;$;");
342 $off++;
343 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800344 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700345 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700346 $sanitise_quote = '';
347 substr($res, $off, 2, "$;$;");
348 $off++;
349 next;
350 }
351
352 # A \ in a string means ignore the next character.
353 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
354 $c eq "\\") {
355 substr($res, $off, 2, 'XX');
356 $off++;
357 next;
358 }
359 # Regular quotes.
360 if ($c eq "'" || $c eq '"') {
361 if ($sanitise_quote eq '') {
362 $sanitise_quote = $c;
363
364 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700365 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700366 } elsif ($sanitise_quote eq $c) {
367 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700368 }
369 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700370
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800371 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700372 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
373 substr($res, $off, 1, $;);
374 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
375 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700376 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700377 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700378 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800379 }
380
381 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700382 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800383 my $clean = 'X' x length($1);
384 $res =~ s@\<.*\>@<$clean>@;
385
386 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700387 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800388 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700389 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800390 }
391
Andy Whitcroft00df3442007-06-08 13:47:06 -0700392 return $res;
393}
394
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395sub ctx_statement_block {
396 my ($linenr, $remain, $off) = @_;
397 my $line = $linenr - 1;
398 my $blk = '';
399 my $soff = $off;
400 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700401 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800402
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800403 my $loff = 0;
404
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405 my $type = '';
406 my $level = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800407 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800408 my $c;
409 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800410
411 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800412 while (1) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700413 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800414 # If we are about to drop off the end, pull in more
415 # context.
416 if ($off >= $len) {
417 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700418 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800419 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800420 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800421 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800422 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800423 $len = length($blk);
424 $line++;
425 last;
426 }
427 # Bail if there is no further context.
428 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800429 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800430 last;
431 }
432 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800433 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800434 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800435 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436
Andy Whitcroft773647a2008-03-28 14:15:58 -0700437 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438 # Statement ends at the ';' or a close '}' at the
439 # outermost level.
440 if ($level == 0 && $c eq ';') {
441 last;
442 }
443
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800444 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700445 if ($level == 0 && $coff_set == 0 &&
446 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
447 $remainder =~ /^(else)(?:\s|{)/ &&
448 $remainder !~ /^else\s+if\b/) {
449 $coff = $off + length($1) - 1;
450 $coff_set = 1;
451 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
452 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800453 }
454
Andy Whitcroft8905a672007-11-28 16:21:06 -0800455 if (($type eq '' || $type eq '(') && $c eq '(') {
456 $level++;
457 $type = '(';
458 }
459 if ($type eq '(' && $c eq ')') {
460 $level--;
461 $type = ($level != 0)? '(' : '';
462
463 if ($level == 0 && $coff < $soff) {
464 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700465 $coff_set = 1;
466 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467 }
468 }
469 if (($type eq '' || $type eq '{') && $c eq '{') {
470 $level++;
471 $type = '{';
472 }
473 if ($type eq '{' && $c eq '}') {
474 $level--;
475 $type = ($level != 0)? '{' : '';
476
477 if ($level == 0) {
478 last;
479 }
480 }
481 $off++;
482 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700483 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800484 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700485 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800486 $line++;
487 $remain--;
488 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489
490 my $statement = substr($blk, $soff, $off - $soff + 1);
491 my $condition = substr($blk, $soff, $coff - $soff + 1);
492
493 #warn "STATEMENT<$statement>\n";
494 #warn "CONDITION<$condition>\n";
495
Andy Whitcroft773647a2008-03-28 14:15:58 -0700496 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800497
498 return ($statement, $condition,
499 $line, $remain + 1, $off - $loff + 1, $level);
500}
501
Andy Whitcroftcf655042008-03-04 14:28:20 -0800502sub statement_lines {
503 my ($stmt) = @_;
504
505 # Strip the diff line prefixes and rip blank lines at start and end.
506 $stmt =~ s/(^|\n)./$1/g;
507 $stmt =~ s/^\s*//;
508 $stmt =~ s/\s*$//;
509
510 my @stmt_lines = ($stmt =~ /\n/g);
511
512 return $#stmt_lines + 2;
513}
514
515sub statement_rawlines {
516 my ($stmt) = @_;
517
518 my @stmt_lines = ($stmt =~ /\n/g);
519
520 return $#stmt_lines + 2;
521}
522
523sub statement_block_size {
524 my ($stmt) = @_;
525
526 $stmt =~ s/(^|\n)./$1/g;
527 $stmt =~ s/^\s*{//;
528 $stmt =~ s/}\s*$//;
529 $stmt =~ s/^\s*//;
530 $stmt =~ s/\s*$//;
531
532 my @stmt_lines = ($stmt =~ /\n/g);
533 my @stmt_statements = ($stmt =~ /;/g);
534
535 my $stmt_lines = $#stmt_lines + 2;
536 my $stmt_statements = $#stmt_statements + 1;
537
538 if ($stmt_lines > $stmt_statements) {
539 return $stmt_lines;
540 } else {
541 return $stmt_statements;
542 }
543}
544
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800545sub ctx_statement_full {
546 my ($linenr, $remain, $off) = @_;
547 my ($statement, $condition, $level);
548
549 my (@chunks);
550
Andy Whitcroftcf655042008-03-04 14:28:20 -0800551 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800552 ($statement, $condition, $linenr, $remain, $off, $level) =
553 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700554 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800555 push(@chunks, [ $condition, $statement ]);
556 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
557 return ($level, $linenr, @chunks);
558 }
559
560 # Pull in the following conditional/block pairs and see if they
561 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800562 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800563 ($statement, $condition, $linenr, $remain, $off, $level) =
564 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800565 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700566 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800567 #print "C: push\n";
568 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800569 }
570
571 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800572}
573
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700574sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700575 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700576 my $line;
577 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700578 my $blk = '';
579 my @o;
580 my @c;
581 my @res = ();
582
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700583 my $level = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700584 for ($line = $start; $remain > 0; $line++) {
585 next if ($rawlines[$line] =~ /^-/);
586 $remain--;
587
588 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700589 foreach my $c (split(//, $rawlines[$line])) {
590 ##print "C<$c>L<$level><$open$close>O<$off>\n";
591 if ($off > 0) {
592 $off--;
593 next;
594 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700595
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700596 if ($c eq $close && $level > 0) {
597 $level--;
598 last if ($level == 0);
599 } elsif ($c eq $open) {
600 $level++;
601 }
602 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700603
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700604 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700605 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700606 }
607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700608 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700609 }
610
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700611 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700612}
613sub ctx_block_outer {
614 my ($linenr, $remain) = @_;
615
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700616 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
617 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700618}
619sub ctx_block {
620 my ($linenr, $remain) = @_;
621
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700622 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
623 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700624}
625sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700626 my ($linenr, $remain, $off) = @_;
627
628 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
629 return @r;
630}
631sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700632 my ($linenr, $remain) = @_;
633
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700634 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700635}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700636sub ctx_statement_level {
637 my ($linenr, $remain, $off) = @_;
638
639 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
640}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700641
642sub ctx_locate_comment {
643 my ($first_line, $end_line) = @_;
644
645 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700646 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700647 return $current_comment if (defined $current_comment);
648
649 # Look through the context and try and figure out if there is a
650 # comment.
651 my $in_comment = 0;
652 $current_comment = '';
653 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700654 my $line = $rawlines[$linenr - 1];
655 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700656 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
657 $in_comment = 1;
658 }
659 if ($line =~ m@/\*@) {
660 $in_comment = 1;
661 }
662 if (!$in_comment && $current_comment ne '') {
663 $current_comment = '';
664 }
665 $current_comment .= $line . "\n" if ($in_comment);
666 if ($line =~ m@\*/@) {
667 $in_comment = 0;
668 }
669 }
670
671 chomp($current_comment);
672 return($current_comment);
673}
674sub ctx_has_comment {
675 my ($first_line, $end_line) = @_;
676 my $cmt = ctx_locate_comment($first_line, $end_line);
677
Andy Whitcroft00df3442007-06-08 13:47:06 -0700678 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700679 ##print "CMMT: $cmt\n";
680
681 return ($cmt ne '');
682}
683
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700684sub raw_line {
685 my ($linenr, $cnt) = @_;
686
687 my $offset = $linenr - 1;
688 $cnt++;
689
690 my $line;
691 while ($cnt) {
692 $line = $rawlines[$offset++];
693 next if (defined($line) && $line =~ /^-/);
694 $cnt--;
695 }
696
697 return $line;
698}
699
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700700sub cat_vet {
701 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700702 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700703
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700704 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700705 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
706 $res .= $1;
707 if ($2 ne '') {
708 $coded = sprintf("^%c", unpack('C', $2) + 64);
709 $res .= $coded;
710 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700711 }
712 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700713
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700714 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700715}
716
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800717my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800718my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800719my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700720my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800721
722sub annotate_reset {
723 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800724 $av_pending = '_';
725 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700726 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800727}
728
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700729sub annotate_values {
730 my ($stream, $type) = @_;
731
732 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700733 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700734 my $cur = $stream;
735
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800736 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700737
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700738 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700739 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800740 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700741 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700742 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800743 print "WS($1)\n" if ($dbg_values > 1);
744 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800745 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800746 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700747 }
748
Andy Whitcroft8ea3eb92008-07-23 21:29:08 -0700749 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800750 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700751 $type = 'T';
752
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700753 } elsif ($cur =~ /^($Modifier)\s*/) {
754 print "MODIFIER($1)\n" if ($dbg_values > 1);
755 $type = 'T';
756
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700757 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700758 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800759 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700760 push(@av_paren_type, $type);
761 if ($2 ne '') {
762 $av_pending = 'N';
763 }
764 $type = 'E';
765
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700766 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700767 print "UNDEF($1)\n" if ($dbg_values > 1);
768 $av_preprocessor = 1;
769 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700770
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800772 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800773 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800774
775 push(@av_paren_type, $type);
776 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700777 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800778
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700779 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800780 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
781 $av_preprocessor = 1;
782
783 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
784
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700785 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700787 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800788 print "PRE_END($1)\n" if ($dbg_values > 1);
789
790 $av_preprocessor = 1;
791
792 # Assume all arms of the conditional end as this
793 # one does, and continue as if the #endif was not here.
794 pop(@av_paren_type);
795 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700796 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700797
798 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800799 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700800
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700801 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
802 print "ATTR($1)\n" if ($dbg_values > 1);
803 $av_pending = $type;
804 $type = 'N';
805
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700806 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800807 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700808 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800809 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700810 }
811 $type = 'N';
812
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700813 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800814 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700815 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700816 $type = 'N';
817
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700818 } elsif ($cur =~/^(case)/o) {
819 print "CASE($1)\n" if ($dbg_values > 1);
820 $av_pend_colon = 'C';
821 $type = 'N';
822
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700823 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800824 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700825 $type = 'N';
826
827 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800828 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800829 push(@av_paren_type, $av_pending);
830 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831 $type = 'N';
832
833 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800834 my $new_type = pop(@av_paren_type);
835 if ($new_type ne '_') {
836 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800837 print "PAREN('$1') -> $type\n"
838 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700839 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800840 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700841 }
842
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700843 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800844 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700845 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800846 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700847
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800848 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
849 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700850 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800851 } elsif ($type eq 'E') {
852 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700853 }
854 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
855 $type = 'V';
856
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700857 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800858 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700859 $type = 'V';
860
861 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800862 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700863 $type = 'N';
864
Andy Whitcroftcf655042008-03-04 14:28:20 -0800865 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800866 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800867 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700868 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800869
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800870 } elsif ($cur =~/^(,)/) {
871 print "COMMA($1)\n" if ($dbg_values > 1);
872 $type = 'C';
873
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700874 } elsif ($cur =~ /^(\?)/o) {
875 print "QUESTION($1)\n" if ($dbg_values > 1);
876 $type = 'N';
877
878 } elsif ($cur =~ /^(:)/o) {
879 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
880
881 substr($var, length($res), 1, $av_pend_colon);
882 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
883 $type = 'E';
884 } else {
885 $type = 'N';
886 }
887 $av_pend_colon = 'O';
888
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800889 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800890 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700891 $type = 'N';
892
Andy Whitcroft0d413862008-10-15 22:02:16 -0700893 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700894 my $variant;
895
896 print "OPV($1)\n" if ($dbg_values > 1);
897 if ($type eq 'V') {
898 $variant = 'B';
899 } else {
900 $variant = 'U';
901 }
902
903 substr($var, length($res), 1, $variant);
904 $type = 'N';
905
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700906 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800907 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700908 if ($1 ne '++' && $1 ne '--') {
909 $type = 'N';
910 }
911
912 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800913 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700914 }
915 if (defined $1) {
916 $cur = substr($cur, length($1));
917 $res .= $type x length($1);
918 }
919 }
920
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700921 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922}
923
Andy Whitcroft8905a672007-11-28 16:21:06 -0800924sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800925 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800926
Andy Whitcroft0776e592008-10-15 22:02:29 -0700927 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
928 if ($possible !~ /(?:
929 ^(?:
930 $Modifier|
931 $Storage|
932 $Type|
933 DEFINE_\S+|
934 goto|
935 return|
936 case|
937 else|
938 asm|__asm__|
939 do
940 )$|
941 ^(?:typedef|struct|enum)\b
942 )/x) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700943 # Check for modifiers.
944 $possible =~ s/\s*$Storage\s*//g;
945 $possible =~ s/\s*$Sparse\s*//g;
946 if ($possible =~ /^\s*$/) {
947
948 } elsif ($possible =~ /\s/) {
949 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -0700950 for my $modifier (split(' ', $possible)) {
951 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
952 push(@modifierList, $modifier);
953 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700954
955 } else {
956 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
957 push(@typeList, $possible);
958 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800959 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -0700960 } else {
961 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800962 }
963}
964
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700965my $prefix = '';
966
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700967sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700968 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
969 return 0;
970 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800971 my $line = $prefix . $_[0];
972
973 $line = (split('\n', $line))[0] . "\n" if ($terse);
974
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800975 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700976
977 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700978}
979sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800980 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700981}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700982sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700983 if (report("ERROR: $_[0]\n")) {
984 our $clean = 0;
985 our $cnt_error++;
986 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700987}
988sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700989 if (report("WARNING: $_[0]\n")) {
990 our $clean = 0;
991 our $cnt_warn++;
992 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700993}
994sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700995 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700996 our $clean = 0;
997 our $cnt_chk++;
998 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700999}
1000
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001001sub check_absolute_file {
1002 my ($absolute, $herecurr) = @_;
1003 my $file = $absolute;
1004
1005 ##print "absolute<$absolute>\n";
1006
1007 # See if any suffix of this path is a path within the tree.
1008 while ($file =~ s@^[^/]*/@@) {
1009 if (-f "$root/$file") {
1010 ##print "file<$file>\n";
1011 last;
1012 }
1013 }
1014 if (! -f _) {
1015 return 0;
1016 }
1017
1018 # It is, so see if the prefix is acceptable.
1019 my $prefix = $absolute;
1020 substr($prefix, -length($file)) = '';
1021
1022 ##print "prefix<$prefix>\n";
1023 if ($prefix ne ".../") {
1024 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1025 }
1026}
1027
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001028sub process {
1029 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001030
1031 my $linenr=0;
1032 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001033 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001034 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001035 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001036
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001037 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001038 my $indent;
1039 my $previndent=0;
1040 my $stashindent=0;
1041
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001042 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001043 my $signoff = 0;
1044 my $is_patch = 0;
1045
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001046 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001047 our $cnt_lines = 0;
1048 our $cnt_error = 0;
1049 our $cnt_warn = 0;
1050 our $cnt_chk = 0;
1051
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001052 # Trace the real file/line as we go.
1053 my $realfile = '';
1054 my $realline = 0;
1055 my $realcnt = 0;
1056 my $here = '';
1057 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001059 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001060 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001061
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001062 my $prev_values = 'E';
1063
1064 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001065 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001066 my %suppress_whiletrailers;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001067
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001068 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001069 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001070 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001071 my @setup_docs = ();
1072 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001073
1074 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001075 my $line;
1076 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001077 $linenr++;
1078 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001079
Andy Whitcroft773647a2008-03-28 14:15:58 -07001080 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001081 $setup_docs = 0;
1082 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1083 $setup_docs = 1;
1084 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001085 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001086 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001087 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1088 $realline=$1-1;
1089 if (defined $2) {
1090 $realcnt=$3+1;
1091 } else {
1092 $realcnt=1+1;
1093 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001094 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001095
1096 # Guestimate if this is a continuing comment. Run
1097 # the context looking for a comment "edge". If this
1098 # edge is a close comment then we must be in a comment
1099 # at context start.
1100 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001101 my $cnt = $realcnt;
1102 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1103 next if (defined $rawlines[$ln - 1] &&
1104 $rawlines[$ln - 1] =~ /^-/);
1105 $cnt--;
1106 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001107 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001108 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1109 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1110 ($edge) = $1;
1111 last;
1112 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001113 }
1114 if (defined $edge && $edge eq '*/') {
1115 $in_comment = 1;
1116 }
1117
1118 # Guestimate if this is a continuing comment. If this
1119 # is the start of a diff block and this line starts
1120 # ' *' then it is very likely a comment.
1121 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001122 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001123 {
1124 $in_comment = 1;
1125 }
1126
1127 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1128 sanitise_line_reset($in_comment);
1129
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001130 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001131 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001132 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001133 $line = sanitise_line($rawline);
1134 }
1135 push(@lines, $line);
1136
1137 if ($realcnt > 1) {
1138 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1139 } else {
1140 $realcnt = 0;
1141 }
1142
1143 #print "==>$rawline\n";
1144 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001145
1146 if ($setup_docs && $line =~ /^\+/) {
1147 push(@setup_docs, $line);
1148 }
1149 }
1150
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001151 $prefix = '';
1152
Andy Whitcroft773647a2008-03-28 14:15:58 -07001153 $realcnt = 0;
1154 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001155 foreach my $line (@lines) {
1156 $linenr++;
1157
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft306708542008-10-15 22:02:28 -07001159 my $hunk_line = ($realcnt != 0);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001160
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001161#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001162 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001163 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001164 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001165 $realline=$1-1;
1166 if (defined $2) {
1167 $realcnt=$3+1;
1168 } else {
1169 $realcnt=1+1;
1170 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001171 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001172 $prev_values = 'E';
1173
Andy Whitcroft773647a2008-03-28 14:15:58 -07001174 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001175 %suppress_whiletrailers = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001176 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001177
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001178# track the line number as we move through the hunk, note that
1179# new versions of GNU diff omit the leading space on completely
1180# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001181 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001182 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001183 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001184
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001185 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001186 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001187
1188 # Track the previous line.
1189 ($prevline, $stashline) = ($stashline, $line);
1190 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001191 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1192
Andy Whitcroft773647a2008-03-28 14:15:58 -07001193 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001194
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001195 } elsif ($realcnt == 1) {
1196 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001197 }
1198
1199#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 $prefix = "$filename:$realline: " if ($emacs && $file);
1201 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1202
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001203 $here = "#$linenr: " if (!$file);
1204 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001205
1206 # extract the filename as it passes
1207 if ($line=~/^\+\+\+\s+(\S+)/) {
1208 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001209 $realfile =~ s@^([^/]*)/@@;
1210
1211 $p1_prefix = $1;
1212 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1213 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1214 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001215
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001216 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001217 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1218 }
1219 next;
1220 }
1221
Randy Dunlap389834b2007-06-08 13:47:03 -07001222 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001223
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001224 my $hereline = "$here\n$rawline\n";
1225 my $herecurr = "$here\n$rawline\n";
1226 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001227
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001228 $cnt_lines++ if ($realcnt != 0);
1229
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001230#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001231 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001232 # This is a signoff, if ugly, so do not double report.
1233 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001234 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001235 WARN("Signed-off-by: is the preferred form\n" .
1236 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001237 }
1238 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001240 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001241 }
1242 }
1243
Andy Whitcroft00df3442007-06-08 13:47:06 -07001244# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001245 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001246 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001247 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001248 }
1249
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001250# Check for absolute kernel paths.
1251 if ($tree) {
1252 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1253 my $file = $1;
1254
1255 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1256 check_absolute_file($1, $herecurr)) {
1257 #
1258 } else {
1259 check_absolute_file($file, $herecurr);
1260 }
1261 }
1262 }
1263
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001264# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1265 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001266 $rawline !~ m/^$UTF8*$/) {
1267 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1268
1269 my $blank = copy_spacing($rawline);
1270 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1271 my $hereptr = "$hereline$ptr\n";
1272
1273 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001274 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001275
Andy Whitcroft306708542008-10-15 22:02:28 -07001276# ignore non-hunk lines and lines being removed
1277 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001278
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001279#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001280 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001282 ERROR("DOS line endings\n" . $herevet);
1283
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001284 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1285 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001286 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001287 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001288
1289# check we are in a valid source file if not then ignore this hunk
1290 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1291
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001292#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001293 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001294 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1295 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1296 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001297 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001298 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001299 }
1300
Andy Whitcroft8905a672007-11-28 16:21:06 -08001301# check for adding lines without a newline.
1302 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1303 WARN("adding a line without newline at end of file\n" . $herecurr);
1304 }
1305
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001306# check we are in a valid source file C or perl if not then ignore this hunk
1307 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001308
1309# at the beginning of a line any tabs must come first and anything
1310# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001311 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1312 $rawline =~ /^\+\s* \s*/) {
1313 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001314 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001315 }
1316
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001317# check we are in a valid C source file if not then ignore this hunk
1318 next if ($realfile !~ /\.(h|c)$/);
1319
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001320# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001321 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001322 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1323 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001324
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001325# Check for potential 'bare' types
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001326 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001327 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001328 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001329 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001330 $stat =~ s/\n./\n /g;
1331 $cond =~ s/\n./\n /g;
1332
1333 my $s = $stat;
1334 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001335
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001337 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001338
1339 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001340 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001341
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001342 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001343 } 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 -07001344 my $type = $1;
1345 $type =~ s/\s+/ /g;
1346 possible($type, "A:" . $s);
1347
Andy Whitcroft8905a672007-11-28 16:21:06 -08001348 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001349 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001350 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001351 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352
1353 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001354 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001355 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001356 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001357
1358 # Check for any sort of function declaration.
1359 # int foo(something bar, other baz);
1360 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001361 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 -08001362 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001363
Andy Whitcroftcf655042008-03-04 14:28:20 -08001364 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001365 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001366 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001367
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001369 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001370
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001371 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001372 }
1373 }
1374 }
1375
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001376 }
1377
Andy Whitcroft653d4872007-06-23 17:16:34 -07001378#
1379# Checks which may be anchored in the context.
1380#
1381
1382# Check for switch () and associated case and default
1383# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001384 if ($line=~/\bswitch\s*\(.*\)/) {
1385 my $err = '';
1386 my $sep = '';
1387 my @ctx = ctx_block_outer($linenr, $realcnt);
1388 shift(@ctx);
1389 for my $ctx (@ctx) {
1390 my ($clen, $cindent) = line_stats($ctx);
1391 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1392 $indent != $cindent) {
1393 $err .= "$sep$ctx\n";
1394 $sep = '';
1395 } else {
1396 $sep = "[...]\n";
1397 }
1398 }
1399 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001400 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001401 }
1402 }
1403
1404# if/while/etc brace do not go on next line, unless defining a do while loop,
1405# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001406 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001407 my $pre_ctx = "$1$2";
1408
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001409 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001410 my $ctx_cnt = $realcnt - $#ctx - 1;
1411 my $ctx = join("\n", @ctx);
1412
Andy Whitcroft548596d2008-07-23 21:29:01 -07001413 my $ctx_ln = $linenr;
1414 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001415
Andy Whitcroft548596d2008-07-23 21:29:01 -07001416 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1417 defined $lines[$ctx_ln - 1] &&
1418 $lines[$ctx_ln - 1] =~ /^-/)) {
1419 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1420 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 $ctx_ln++;
1422 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001423
Andy Whitcroft53210162008-07-23 21:29:03 -07001424 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1425 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001426
1427 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1428 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001429 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001430 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001431 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1432 $ctx =~ /\)\s*\;\s*$/ &&
1433 defined $lines[$ctx_ln - 1])
1434 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001435 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1436 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001438 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001439 }
1440 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001441 }
1442
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001443# Check relative indent for conditionals and blocks.
1444 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1445 my ($s, $c) = ($stat, $cond);
1446
1447 substr($s, 0, length($c), '');
1448
1449 # Make sure we remove the line prefixes as we have
1450 # none on the first line, and are going to readd them
1451 # where necessary.
1452 $s =~ s/\n./\n/gs;
1453
1454 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001455 my @newlines = ($c =~ /\n/gs);
1456 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001457
1458 # We want to check the first line inside the block
1459 # starting at the end of the conditional, so remove:
1460 # 1) any blank line termination
1461 # 2) any opening brace { on end of the line
1462 # 3) any do (...) {
1463 my $continuation = 0;
1464 my $check = 0;
1465 $s =~ s/^.*\bdo\b//;
1466 $s =~ s/^\s*{//;
1467 if ($s =~ s/^\s*\\//) {
1468 $continuation = 1;
1469 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001470 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001471 $check = 1;
1472 $cond_lines++;
1473 }
1474
1475 # Also ignore a loop construct at the end of a
1476 # preprocessor statement.
1477 if (($prevline =~ /^.\s*#\s*define\s/ ||
1478 $prevline =~ /\\\s*$/) && $continuation == 0) {
1479 $check = 0;
1480 }
1481
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001482 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001483 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001484 while ($cond_ptr != $cond_lines) {
1485 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001486
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001487 # If we see an #else/#elif then the code
1488 # is not linear.
1489 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1490 $check = 0;
1491 }
1492
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001493 # Ignore:
1494 # 1) blank lines, they should be at 0,
1495 # 2) preprocessor lines, and
1496 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001497 if ($continuation ||
1498 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001499 $s =~ /^\s*#\s*?/ ||
1500 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001501 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001502 $s =~ s/^.*?\n//;
1503 $cond_lines++;
1504 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001505 }
1506
1507 my (undef, $sindent) = line_stats("+" . $s);
1508 my $stat_real = raw_line($linenr, $cond_lines);
1509
1510 # Check if either of these lines are modified, else
1511 # this is not this patch's fault.
1512 if (!defined($stat_real) ||
1513 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1514 $check = 0;
1515 }
1516 if (defined($stat_real) && $cond_lines > 1) {
1517 $stat_real = "[...]\n$stat_real";
1518 }
1519
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001520 #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 -07001521
1522 if ($check && (($sindent % 8) != 0 ||
1523 ($sindent <= $indent && $s ne ''))) {
1524 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1525 }
1526 }
1527
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001528 # Track the 'values' across context and added lines.
1529 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001530 my ($curr_values, $curr_vars) =
1531 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001532 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001533 if ($dbg_values) {
1534 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001535 print "$linenr > .$outline\n";
1536 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001537 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001538 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001539 $prev_values = substr($curr_values, -1);
1540
Andy Whitcroft00df3442007-06-08 13:47:06 -07001541#ignore lines not being added
1542 if ($line=~/^[^\+]/) {next;}
1543
Andy Whitcroft653d4872007-06-23 17:16:34 -07001544# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001545 if ($dbg_type) {
1546 if ($line =~ /^.\s*$Declare\s*$/) {
1547 ERROR("TEST: is type\n" . $herecurr);
1548 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1549 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1550 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001551 next;
1552 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001553# TEST: allow direct testing of the attribute matcher.
1554 if ($dbg_attr) {
1555 if ($line =~ /^.\s*$Attribute\s*$/) {
1556 ERROR("TEST: is attr\n" . $herecurr);
1557 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1558 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1559 }
1560 next;
1561 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001562
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001563# check for initialisation to aggregates open brace on the next line
1564 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1565 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001566 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001567 }
1568
Andy Whitcroft653d4872007-06-23 17:16:34 -07001569#
1570# Checks which are anchored on the added line.
1571#
1572
1573# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001574 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001575 my $path = $1;
1576 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001577 ERROR("malformed #include filename\n" .
1578 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001579 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001580 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001581
1582# no C99 // comments
1583 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001584 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001585 }
1586 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001587 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001588 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001589
1590#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001591 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1592 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1593 my $name = $1;
Andy Whitcroft48012052008-10-15 22:02:34 -07001594 if ($prevline !~ /(?:
1595 ^.}|
1596 ^.DEFINE_$Ident\(\Q$name\E\)|
1597 ^.DECLARE_$Ident\(\Q$name\E\)|
1598 ^.LIST_HEAD\(\Q$name\E\)|
1599 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1600 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1601 )/x) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001602 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001603 }
1604 }
1605
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001606# check for external initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001607 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608 ERROR("do not initialise externals to 0 or NULL\n" .
1609 $herecurr);
1610 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001611# check for static initialisers.
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001612 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001613 ERROR("do not initialise statics to 0 or NULL\n" .
1614 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001615 }
1616
Andy Whitcroft653d4872007-06-23 17:16:34 -07001617# check for new typedefs, only function parameters and sparse annotations
1618# make sense.
1619 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001620 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001621 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001622 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001623 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001624 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001625 }
1626
1627# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001628 # (char*[ const])
1629 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1630 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001631
Andy Whitcroft65863862009-01-06 14:41:21 -08001632 # Should start with a space.
1633 $to =~ s/^(\S)/ $1/;
1634 # Should not end with a space.
1635 $to =~ s/\s+$//;
1636 # '*'s should not have spaces between.
1637 while ($to =~ s/(.)\s\*/$1\*/) {
1638 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001639
Andy Whitcroft65863862009-01-06 14:41:21 -08001640 #print "from<$from> to<$to>\n";
1641 if ($from ne $to) {
1642 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1643 }
1644 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1645 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001646
Andy Whitcroft65863862009-01-06 14:41:21 -08001647 # Should start with a space.
1648 $to =~ s/^(\S)/ $1/;
1649 # Should not end with a space.
1650 $to =~ s/\s+$//;
1651 # '*'s should not have spaces between.
1652 while ($to =~ s/(.)\s\*/$1\*/) {
1653 }
1654 # Modifiers should have spaces.
1655 $to =~ s/(\b$Modifier$)/$1 /;
1656
1657 #print "from<$from> to<$to>\n";
1658 if ($from ne $to) {
1659 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1660 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001661 }
1662
1663# # no BUG() or BUG_ON()
1664# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1665# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1666# print "$herecurr";
1667# $clean = 0;
1668# }
1669
Andy Whitcroft8905a672007-11-28 16:21:06 -08001670 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001671 WARN("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 -08001672 }
1673
Andy Whitcroft00df3442007-06-08 13:47:06 -07001674# printk should use KERN_* levels. Note that follow on printk's on the
1675# same line do not need a level, so we use the current block context
1676# to try and find and validate the current printk. In summary the current
1677# printk includes all preceeding printk's which have no newline on the end.
1678# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001679 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001680 my $ok = 0;
1681 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1682 #print "CHECK<$lines[$ln - 1]\n";
1683 # we have a preceeding printk if it ends
1684 # with "\n" ignore it, else it is to blame
1685 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1686 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1687 $ok = 1;
1688 }
1689 last;
1690 }
1691 }
1692 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001693 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001694 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001695 }
1696
Andy Whitcroft653d4872007-06-23 17:16:34 -07001697# function brace can't be on same line, except for #defines of do while,
1698# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001699 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1700 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001701 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001702 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001703
Andy Whitcroft8905a672007-11-28 16:21:06 -08001704# open braces for enum, union and struct go on the same line.
1705 if ($line =~ /^.\s*{/ &&
1706 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1707 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1708 }
1709
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001710# check for spacing round square brackets; allowed:
1711# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001712# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1713# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001714 while ($line =~ /(.*?\s)\[/g) {
1715 my ($where, $prefix) = ($-[1], $1);
1716 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001717 ($where != 0 || $prefix !~ /^.\s+$/) &&
1718 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001719 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1720 }
1721 }
1722
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001723# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001726 my $ctx_before = substr($line, 0, $-[1]);
1727 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001728
1729 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001730 if ($name =~ /^(?:
1731 if|for|while|switch|return|case|
1732 volatile|__volatile__|
1733 __attribute__|format|__extension__|
1734 asm|__asm__)$/x)
1735 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001736
1737 # cpp #define statements have non-optional spaces, ie
1738 # if there is a space between the name and the open
1739 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001740 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001741
1742 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001743 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001744
1745 # If this whole things ends with a type its most
1746 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001747 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001748
1749 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001750 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001751 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001752 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001753# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001754 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001755 my $ops = qr{
1756 <<=|>>=|<=|>=|==|!=|
1757 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1758 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001759 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1760 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001761 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001762 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001763 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001764
1765 my $blank = copy_spacing($opline);
1766
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001767 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001768 $off += length($elements[$n]);
1769
Andy Whitcroft773647a2008-03-28 14:15:58 -07001770 # Pick up the preceeding and succeeding characters.
1771 my $ca = substr($opline, 0, $off);
1772 my $cc = '';
1773 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1774 $cc = substr($opline, $off + length($elements[$n + 1]));
1775 }
1776 my $cb = "$ca$;$cc";
1777
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001778 my $a = '';
1779 $a = 'V' if ($elements[$n] ne '');
1780 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001781 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001782 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1783 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001784 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001785
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001786 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001787
1788 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001789 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001790 $c = 'V' if ($elements[$n + 2] ne '');
1791 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001792 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001793 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1794 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001795 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001796 } else {
1797 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001798 }
1799
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001800 my $ctx = "${a}x${c}";
1801
1802 my $at = "(ctx:$ctx)";
1803
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001805 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001806
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001807 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001808 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001809
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001810 # Get the full operator variant.
1811 my $opv = $op . substr($curr_vars, $off, 1);
1812
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001813 # Ignore operators passed as parameters.
1814 if ($op_type ne 'V' &&
1815 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1816
Andy Whitcroftcf655042008-03-04 14:28:20 -08001817# # Ignore comments
1818# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001819
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001820 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001821 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001822 if ($ctx !~ /.x[WEBC]/ &&
1823 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001824 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001825 }
1826
1827 # // is a comment
1828 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001829
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001830 # No spaces for:
1831 # ->
1832 # : when part of a bitfield
1833 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001834 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001835 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001836 }
1837
1838 # , must have a space on the right.
1839 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001840 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001841 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001842 }
1843
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001844 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001845 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001846 #warn "'*' is part of type\n";
1847
1848 # unary operators should have a space before and
1849 # none after. May be left adjacent to another
1850 # unary operator, or a cast
1851 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001852 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07001853 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001854 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001855 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001856 }
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001857 if ($op eq '*' && $cc =~/\s*const\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001858 # A unary '*' may be const
1859
1860 } elsif ($ctx =~ /.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001861 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001862 }
1863
1864 # unary ++ and unary -- are allowed no space on one side.
1865 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001866 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1867 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001868 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001869 if ($ctx =~ /Wx[BE]/ ||
1870 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1871 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001872 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 if ($ctx =~ /ExW/) {
1874 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1875 }
1876
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001877
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001878 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001879 } elsif ($op eq '<<' or $op eq '>>' or
1880 $op eq '&' or $op eq '^' or $op eq '|' or
1881 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 $op eq '*' or $op eq '/' or
1883 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001884 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001885 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001886 ERROR("need consistent spacing around '$op' $at\n" .
1887 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001888 }
1889
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001890 # A colon needs no spaces before when it is
1891 # terminating a case value or a label.
1892 } elsif ($opv eq ':C' || $opv eq ':L') {
1893 if ($ctx =~ /Wx./) {
1894 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1895 }
1896
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001897 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001898 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001899 my $ok = 0;
1900
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001901 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001902 if (($op eq '<' &&
1903 $cc =~ /^\S+\@\S+>/) ||
1904 ($op eq '>' &&
1905 $ca =~ /<\S+\@\S+$/))
1906 {
1907 $ok = 1;
1908 }
1909
1910 # Ignore ?:
1911 if (($opv eq ':O' && $ca =~ /\?$/) ||
1912 ($op eq '?' && $cc =~ /^:/)) {
1913 $ok = 1;
1914 }
1915
1916 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001917 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001918 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001919 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001920 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001921 }
1922 }
1923
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001924# check for multiple assignments
1925 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001926 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001927 }
1928
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001929## # check for multiple declarations, allowing for a function declaration
1930## # continuation.
1931## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1932## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1933##
1934## # Remove any bracketed sections to ensure we do not
1935## # falsly report the parameters of functions.
1936## my $ln = $line;
1937## while ($ln =~ s/\([^\(\)]*\)//g) {
1938## }
1939## if ($ln =~ /,/) {
1940## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1941## }
1942## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001943
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001944#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001945 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1946 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001947 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001948 }
1949
1950# closing brace should have a space following it when it has anything
1951# on the line
1952 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001953 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001954 }
1955
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001956# check spacing on square brackets
1957 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001958 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001959 }
1960 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001961 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001962 }
1963
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001964# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001965 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1966 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001967 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001968 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001969 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001970 $line !~ /for\s*\(.*;\s+\)/ &&
1971 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001972 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001973 }
1974
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001975#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001976 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001977 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001978 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001979 }
1980
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001981# Return is not a function.
1982 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1983 my $spacing = $1;
1984 my $value = $2;
1985
1986 # Flatten any parentheses and braces
Andy Whitcroftfee61c42008-07-23 21:28:56 -07001987 $value =~ s/\)\(/\) \(/g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001988 while ($value =~ s/\([^\(\)]*\)/1/) {
1989 }
1990
1991 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1992 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1993
1994 } elsif ($spacing !~ /\s+/) {
1995 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1996 }
1997 }
1998
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001999# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002000 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002002 }
2003
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002004# Check for illegal assignment in if conditional -- and check for trailing
2005# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002006 if ($line =~ /do\s*(?!{)/) {
2007 my ($stat_next) = ctx_statement_block($line_nr_next,
2008 $remain_next, $off_next);
2009 $stat_next =~ s/\n./\n /g;
2010 ##print "stat<$stat> stat_next<$stat_next>\n";
2011
2012 if ($stat_next =~ /^\s*while\b/) {
2013 # If the statement carries leading newlines,
2014 # then count those as offsets.
2015 my ($whitespace) =
2016 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2017 my $offset =
2018 statement_rawlines($whitespace) - 1;
2019
2020 $suppress_whiletrailers{$line_nr_next +
2021 $offset} = 1;
2022 }
2023 }
2024 if (!defined $suppress_whiletrailers{$linenr} &&
2025 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002026 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002027
2028 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002029 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002030 }
2031
2032 # Find out what is on the end of the line after the
2033 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002034 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002035 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002036 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002037 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2038 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002039 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002040 # Find out how long the conditional actually is.
2041 my @newlines = ($c =~ /\n/gs);
2042 my $cond_lines = 1 + $#newlines;
2043
2044 my $stat_real = raw_line($linenr, $cond_lines);
2045 if (defined($stat_real) && $cond_lines > 1) {
2046 $stat_real = "[...]\n$stat_real";
2047 }
2048
2049 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002050 }
2051 }
2052
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002053# Check for bitwise tests written as boolean
2054 if ($line =~ /
2055 (?:
2056 (?:\[|\(|\&\&|\|\|)
2057 \s*0[xX][0-9]+\s*
2058 (?:\&\&|\|\|)
2059 |
2060 (?:\&\&|\|\|)
2061 \s*0[xX][0-9]+\s*
2062 (?:\&\&|\|\||\)|\])
2063 )/x)
2064 {
2065 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2066 }
2067
Andy Whitcroft8905a672007-11-28 16:21:06 -08002068# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002069 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2070 my $s = $1;
2071 $s =~ s/$;//g; # Remove any comments
2072 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2073 ERROR("trailing statements should be on next line\n" . $herecurr);
2074 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002075 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002076# case and default should not have general statements after them
2077 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2078 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002079 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002080 \s*return\s+
2081 )/xg)
2082 {
2083 ERROR("trailing statements should be on next line\n" . $herecurr);
2084 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002085
2086 # Check for }<nl>else {, these must be at the same
2087 # indent level to be relevant to each other.
2088 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2089 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002090 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091 }
2092
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002093 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2094 $previndent == $indent) {
2095 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2096
2097 # Find out what is on the end of the line after the
2098 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002099 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002100 $s =~ s/\n.*//g;
2101
2102 if ($s =~ /^\s*;/) {
2103 ERROR("while should follow close brace '}'\n" . $hereprev);
2104 }
2105 }
2106
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002107#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2108# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2109# print "No studly caps, use _\n";
2110# print "$herecurr";
2111# $clean = 0;
2112# }
2113
2114#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002115 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002116 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002117 }
2118
Andy Whitcroft653d4872007-06-23 17:16:34 -07002119#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002120 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002121 my $file = "$1.h";
2122 my $checkfile = "include/linux/$file";
2123 if (-f "$root/$checkfile" &&
2124 $realfile ne $checkfile &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002125 $1 ne 'irq')
2126 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002127 if ($realfile =~ m{^arch/}) {
2128 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2129 } else {
2130 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2131 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002132 }
2133 }
2134
Andy Whitcroft653d4872007-06-23 17:16:34 -07002135# multi-statement macros should be enclosed in a do while loop, grab the
2136# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002137# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002138 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2139 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002140 my $ln = $linenr;
2141 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002142 my ($off, $dstat, $dcond, $rest);
2143 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002144
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002145 my $args = defined($1);
2146
2147 # Find the end of the macro and limit our statement
2148 # search to that.
2149 while ($cnt > 0 && defined $lines[$ln - 1] &&
2150 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2151 {
2152 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002153 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002154 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002155 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002156 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002157
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002158 ($dstat, $dcond, $ln, $cnt, $off) =
2159 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2160 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002161 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002162
2163 # Extract the remainder of the define (if any) and
2164 # rip off surrounding spaces, and trailing \'s.
2165 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002166 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2167 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002168 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2169 $rest .= substr($lines[$ln - 1], $off) . "\n";
2170 $cnt--;
2171 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002172 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002173 $off = 0;
2174 }
2175 $rest =~ s/\\\n.//g;
2176 $rest =~ s/^\s*//s;
2177 $rest =~ s/\s*$//s;
2178
2179 # Clean up the original statement.
2180 if ($args) {
2181 substr($dstat, 0, length($dcond), '');
2182 } else {
2183 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2184 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002185 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002186 $dstat =~ s/\\\n.//g;
2187 $dstat =~ s/^\s*//s;
2188 $dstat =~ s/\s*$//s;
2189
2190 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002191 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2192 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2193 $dstat =~ s/\[[^\{\}]*\]/1/)
2194 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002195 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002196
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002197 my $exceptions = qr{
2198 $Declare|
2199 module_param_named|
2200 MODULE_PARAM_DESC|
2201 DECLARE_PER_CPU|
2202 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002203 __typeof__\(|
2204 \.$Ident\s*=\s*
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002205 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002206 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002207 if ($rest ne '') {
2208 if ($rest !~ /while\s*\(/ &&
2209 $dstat !~ /$exceptions/)
2210 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002211 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002212 }
2213
2214 } elsif ($ctx !~ /;/) {
2215 if ($dstat ne '' &&
2216 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2217 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002218 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002219 $dstat =~ /$Operators/)
2220 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002221 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002222 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002223 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002224 }
2225
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002226# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002227 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2228 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002229 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002230 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002231 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2232 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002233 my $allowed = 0;
2234 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002235 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002236 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002237 for my $chunk (@chunks) {
2238 my ($cond, $block) = @{$chunk};
2239
Andy Whitcroft773647a2008-03-28 14:15:58 -07002240 # If the condition carries leading newlines, then count those as offsets.
2241 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2242 my $offset = statement_rawlines($whitespace) - 1;
2243
2244 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2245
2246 # We have looked at and allowed this specific line.
2247 $suppress_ifbraces{$ln + $offset} = 1;
2248
2249 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002250 $ln += statement_rawlines($block) - 1;
2251
Andy Whitcroft773647a2008-03-28 14:15:58 -07002252 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002253
2254 $seen++ if ($block =~ /^\s*{/);
2255
Andy Whitcroftcf655042008-03-04 14:28:20 -08002256 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2257 if (statement_lines($cond) > 1) {
2258 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002259 $allowed = 1;
2260 }
2261 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002262 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002263 $allowed = 1;
2264 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002265 if (statement_block_size($block) > 1) {
2266 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002267 $allowed = 1;
2268 }
2269 }
2270 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002271 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002272 }
2273 }
2274 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002275 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002276 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002277 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002278
Andy Whitcroftcf655042008-03-04 14:28:20 -08002279 # Check the pre-context.
2280 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2281 #print "APW: ALLOWED: pre<$1>\n";
2282 $allowed = 1;
2283 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002284
2285 my ($level, $endln, @chunks) =
2286 ctx_statement_full($linenr, $realcnt, $-[0]);
2287
Andy Whitcroftcf655042008-03-04 14:28:20 -08002288 # Check the condition.
2289 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002290 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002291 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002292 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002293 }
2294 if (statement_lines($cond) > 1) {
2295 #print "APW: ALLOWED: cond<$cond>\n";
2296 $allowed = 1;
2297 }
2298 if ($block =~/\b(?:if|for|while)\b/) {
2299 #print "APW: ALLOWED: block<$block>\n";
2300 $allowed = 1;
2301 }
2302 if (statement_block_size($block) > 1) {
2303 #print "APW: ALLOWED: lines block<$block>\n";
2304 $allowed = 1;
2305 }
2306 # Check the post-context.
2307 if (defined $chunks[1]) {
2308 my ($cond, $block) = @{$chunks[1]};
2309 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002310 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002311 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002312 if ($block =~ /^\s*\{/) {
2313 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2314 $allowed = 1;
2315 }
2316 }
2317 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2318 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002319 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002320
Andy Whitcroftf0556632008-10-15 22:02:23 -07002321 for (my $n = 0; $n < $cnt; $n++) {
2322 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002323 }
2324
2325 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002326 }
2327 }
2328
Andy Whitcroft653d4872007-06-23 17:16:34 -07002329# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002330 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002331 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002332 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002333 }
2334 }
2335
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002336# don't use deprecated functions
2337 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002338 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002339 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002340 }
2341 }
2342
2343# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002344 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2345 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002346 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002347 }
2348
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002349# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2350 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2351 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2352 }
2353
Andy Whitcroft00df3442007-06-08 13:47:06 -07002354# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002355 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002356 CHK("if this code is redundant consider removing it\n" .
2357 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002358 }
2359
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002360# check for needless kfree() checks
2361 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2362 my $expr = $1;
2363 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002364 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002365 }
2366 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002367# check for needless usb_free_urb() checks
2368 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2369 my $expr = $1;
2370 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2371 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2372 }
2373 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002374
Andy Whitcroft00df3442007-06-08 13:47:06 -07002375# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002376# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002377# print "#ifdef in C files should be avoided\n";
2378# print "$herecurr";
2379# $clean = 0;
2380# }
2381
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002382# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002383 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002384 ERROR("exactly one space required after that #$1\n" . $herecurr);
2385 }
2386
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002387# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002388 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2389 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002390 my $which = $1;
2391 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002392 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002393 }
2394 }
2395# check for memory barriers without a comment.
2396 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2397 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002398 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002399 }
2400 }
2401# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002402 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002403 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002404 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002405
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002406# check the location of the inline attribute, that it is between
2407# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002408 if ($line =~ /\b$Type\s+$Inline\b/ ||
2409 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002410 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2411 }
2412
Andy Whitcroft8905a672007-11-28 16:21:06 -08002413# Check for __inline__ and __inline, prefer inline
2414 if ($line =~ /\b(__inline__|__inline)\b/) {
2415 WARN("plain inline is preferred over $1\n" . $herecurr);
2416 }
2417
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002418# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002419 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002420 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002421 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002422 my $function_name = $1;
2423 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002424
2425 my $s = $stat;
2426 if (defined $cond) {
2427 substr($s, 0, length($cond), '');
2428 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002429 if ($s =~ /^\s*;/ &&
2430 $function_name ne 'uninitialized_var')
2431 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002432 WARN("externs should be avoided in .c files\n" . $herecurr);
2433 }
2434
2435 if ($paren_space =~ /\n/) {
2436 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2437 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002438
2439 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2440 $stat =~ /^.\s*extern\s+/)
2441 {
2442 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002443 }
2444
2445# checks for new __setup's
2446 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2447 my $name = $1;
2448
2449 if (!grep(/$name/, @setup_docs)) {
2450 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2451 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002452 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002453
2454# check for pointless casting of kmalloc return
2455 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2456 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2457 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002458
2459# check for gcc specific __FUNCTION__
2460 if ($line =~ /__FUNCTION__/) {
2461 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2462 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002463
2464# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002465 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002466 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2467 }
2468# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002469 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002470 WARN("consider using a completion\n" . $herecurr);
2471 }
2472# recommend strict_strto* over simple_strto*
2473 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2474 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2475 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002476# check for __initcall(), use device_initcall() explicitly please
2477 if ($line =~ /^.\s*__initcall\s*\(/) {
2478 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2479 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480
2481# use of NR_CPUS is usually wrong
2482# ignore definitions of NR_CPUS and usage to define arrays as likely right
2483 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002484 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2485 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002486 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2487 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2488 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002489 {
2490 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2491 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002492
2493# check for %L{u,d,i} in strings
2494 my $string;
2495 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2496 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002497 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002498 if ($string =~ /(?<!%)%L[udi]/) {
2499 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2500 last;
2501 }
2502 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002503
2504# whine mightly about in_atomic
2505 if ($line =~ /\bin_atomic\s*\(/) {
2506 if ($realfile =~ m@^drivers/@) {
2507 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2508 } else {
2509 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2510 }
2511 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002512 }
2513
2514 # If we have no input at all, then there is nothing to report on
2515 # so just keep quiet.
2516 if ($#rawlines == -1) {
2517 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002518 }
2519
Andy Whitcroft8905a672007-11-28 16:21:06 -08002520 # In mailback mode only produce a report in the negative, for
2521 # things that appear to be patches.
2522 if ($mailback && ($clean == 1 || !$is_patch)) {
2523 exit(0);
2524 }
2525
2526 # This is not a patch, and we are are in 'no-patch' mode so
2527 # just keep quiet.
2528 if (!$chk_patch && !$is_patch) {
2529 exit(0);
2530 }
2531
2532 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002533 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002534 }
2535 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002536 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002537 }
2538
Andy Whitcroft8905a672007-11-28 16:21:06 -08002539 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002540 if ($summary && !($clean == 1 && $quiet == 1)) {
2541 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002542 print "total: $cnt_error errors, $cnt_warn warnings, " .
2543 (($check)? "$cnt_chk checks, " : "") .
2544 "$cnt_lines lines checked\n";
2545 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002546 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002547
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002548 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002549 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002550 }
2551 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002552 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002553 print "are false positives report them to the maintainer, see\n";
2554 print "CHECKPATCH in MAINTAINERS.\n";
2555 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002556
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002557 return $clean;
2558}