blob: a521d493b0c8600790d20b9574fc445d98f18d4a [file] [log] [blame]
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -070012
Andy Whitcroft50a7dcf2009-01-06 14:41:21 -080013my $V = '0.25';
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-06-01 00:46:48 -070031GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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 Whitcroft0a920b5b2007-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{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800130our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131our $Operators = qr{
132 <=|>=|==|!=|
133 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700135 }x;
136
Andy Whitcroft8905a672007-11-28 16:21:06 -0800137our $NonptrType;
138our $Type;
139our $Declare;
140
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700141our $UTF8 = qr {
142 [\x09\x0A\x0D\x20-\x7E] # ASCII
143 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
144 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
145 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
146 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
147 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
148 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
149 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
150}x;
151
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700152our $typeTypedefs = qr{(?x:
153 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
154 atomic_t
155)};
156
Andy Whitcroft8905a672007-11-28 16:21:06 -0800157our @typeList = (
158 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700159 qr{(?:unsigned\s+)?char},
160 qr{(?:unsigned\s+)?short},
161 qr{(?:unsigned\s+)?int},
162 qr{(?:unsigned\s+)?long},
163 qr{(?:unsigned\s+)?long\s+int},
164 qr{(?:unsigned\s+)?long\s+long},
165 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800166 qr{unsigned},
167 qr{float},
168 qr{double},
169 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800170 qr{struct\s+$Ident},
171 qr{union\s+$Ident},
172 qr{enum\s+$Ident},
173 qr{${Ident}_t},
174 qr{${Ident}_handler},
175 qr{${Ident}_handler_fn},
176);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700177our @modifierList = (
178 qr{fastcall},
179);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800180
181sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700182 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
183 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700184 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800185 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700186 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800187 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700188 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700189 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700190 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800191 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700192 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800193 }x;
194 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700195 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800196 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700197 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800198 }x;
199 $Declare = qr{(?:$Storage\s+)?$Type};
200}
201build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202
203$chk_signoff = 0 if ($file);
204
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700205my @dep_includes = ();
206my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207my $removal = "Documentation/feature-removal-schedule.txt";
208if ($tree && -f "$root/$removal") {
209 open(REMOVE, "<$root/$removal") ||
210 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700212 if (/^Check:\s+(.*\S)/) {
213 for my $entry (split(/[, ]+/, $1)) {
214 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700215 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700216
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700217 } elsif ($entry !~ m@/@) {
218 push(@dep_functions, $entry);
219 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700220 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700221 }
222 }
223}
224
Andy Whitcroft00df3442007-06-08 13:47:06 -0700225my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800226my @lines = ();
227my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228for my $filename (@ARGV) {
229 if ($file) {
230 open(FILE, "diff -u /dev/null $filename|") ||
231 die "$P: $filename: diff failed - $!\n";
232 } else {
233 open(FILE, "<$filename") ||
234 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700235 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800236 if ($filename eq '-') {
237 $vname = 'Your patch';
238 } else {
239 $vname = $filename;
240 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241 while (<FILE>) {
242 chomp;
243 push(@rawlines, $_);
244 }
245 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800246 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247 $exit = 1;
248 }
249 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800250 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700251}
252
253exit($exit);
254
255sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700256 my ($root) = @_;
257
258 my @tree_check = (
259 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
260 "README", "Documentation", "arch", "include", "drivers",
261 "fs", "init", "ipc", "kernel", "lib", "scripts",
262 );
263
264 foreach my $check (@tree_check) {
265 if (! -e $root . '/' . $check) {
266 return 0;
267 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700268 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700269 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700270}
271
272sub expand_tabs {
273 my ($str) = @_;
274
275 my $res = '';
276 my $n = 0;
277 for my $c (split(//, $str)) {
278 if ($c eq "\t") {
279 $res .= ' ';
280 $n++;
281 for (; ($n % 8) != 0; $n++) {
282 $res .= ' ';
283 }
284 next;
285 }
286 $res .= $c;
287 $n++;
288 }
289
290 return $res;
291}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700293 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294 return $res;
295}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700296
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700297sub line_stats {
298 my ($line) = @_;
299
300 # Drop the diff line leader and expand tabs
301 $line =~ s/^.//;
302 $line = expand_tabs($line);
303
304 # Pick the indent from the front of the line.
305 my ($white) = ($line =~ /^(\s*)/);
306
307 return (length($line), length($white));
308}
309
Andy Whitcroft773647a2008-03-28 14:15:58 -0700310my $sanitise_quote = '';
311
312sub sanitise_line_reset {
313 my ($in_comment) = @_;
314
315 if ($in_comment) {
316 $sanitise_quote = '*/';
317 } else {
318 $sanitise_quote = '';
319 }
320}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700321sub sanitise_line {
322 my ($line) = @_;
323
324 my $res = '';
325 my $l = '';
326
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800327 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700328 my $off = 0;
329 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700330
Andy Whitcroft773647a2008-03-28 14:15:58 -0700331 # Always copy over the diff marker.
332 $res = substr($line, 0, 1);
333
334 for ($off = 1; $off < length($line); $off++) {
335 $c = substr($line, $off, 1);
336
337 # Comments we are wacking completly including the begin
338 # and end, all to $;.
339 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
340 $sanitise_quote = '*/';
341
342 substr($res, $off, 2, "$;$;");
343 $off++;
344 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800345 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700346 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700347 $sanitise_quote = '';
348 substr($res, $off, 2, "$;$;");
349 $off++;
350 next;
351 }
352
353 # A \ in a string means ignore the next character.
354 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
355 $c eq "\\") {
356 substr($res, $off, 2, 'XX');
357 $off++;
358 next;
359 }
360 # Regular quotes.
361 if ($c eq "'" || $c eq '"') {
362 if ($sanitise_quote eq '') {
363 $sanitise_quote = $c;
364
365 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700366 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700367 } elsif ($sanitise_quote eq $c) {
368 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700369 }
370 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700371
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800372 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700373 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
374 substr($res, $off, 1, $;);
375 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
376 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700377 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700378 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700379 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800380 }
381
382 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700383 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800384 my $clean = 'X' x length($1);
385 $res =~ s@\<.*\>@<$clean>@;
386
387 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700388 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800389 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800391 }
392
Andy Whitcroft00df3442007-06-08 13:47:06 -0700393 return $res;
394}
395
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396sub ctx_statement_block {
397 my ($linenr, $remain, $off) = @_;
398 my $line = $linenr - 1;
399 my $blk = '';
400 my $soff = $off;
401 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700402 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800404 my $loff = 0;
405
Andy Whitcroft8905a672007-11-28 16:21:06 -0800406 my $type = '';
407 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800408 my @stack = ([$type, $level]);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800409 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800410 my $c;
411 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800412
413 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800414 while (1) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700415 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800416 # If we are about to drop off the end, pull in more
417 # context.
418 if ($off >= $len) {
419 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700420 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800421 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800423 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800424 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425 $len = length($blk);
426 $line++;
427 last;
428 }
429 # Bail if there is no further context.
430 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800431 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800432 last;
433 }
434 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800435 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800436 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800437 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438
Andy Whitcroft773647a2008-03-28 14:15:58 -0700439 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800440
441 # Handle nested #if/#else.
442 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
443 push(@stack, [ $type, $level ]);
444 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
445 ($type, $level) = @{$stack[$#stack - 1]};
446 } elsif ($remainder =~ /^#\s*endif\b/) {
447 ($type, $level) = @{pop(@stack)};
448 }
449
Andy Whitcroft8905a672007-11-28 16:21:06 -0800450 # Statement ends at the ';' or a close '}' at the
451 # outermost level.
452 if ($level == 0 && $c eq ';') {
453 last;
454 }
455
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800456 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700457 if ($level == 0 && $coff_set == 0 &&
458 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
459 $remainder =~ /^(else)(?:\s|{)/ &&
460 $remainder !~ /^else\s+if\b/) {
461 $coff = $off + length($1) - 1;
462 $coff_set = 1;
463 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
464 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800465 }
466
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467 if (($type eq '' || $type eq '(') && $c eq '(') {
468 $level++;
469 $type = '(';
470 }
471 if ($type eq '(' && $c eq ')') {
472 $level--;
473 $type = ($level != 0)? '(' : '';
474
475 if ($level == 0 && $coff < $soff) {
476 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700477 $coff_set = 1;
478 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800479 }
480 }
481 if (($type eq '' || $type eq '{') && $c eq '{') {
482 $level++;
483 $type = '{';
484 }
485 if ($type eq '{' && $c eq '}') {
486 $level--;
487 $type = ($level != 0)? '{' : '';
488
489 if ($level == 0) {
490 last;
491 }
492 }
493 $off++;
494 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700495 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800496 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700497 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800498 $line++;
499 $remain--;
500 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800501
502 my $statement = substr($blk, $soff, $off - $soff + 1);
503 my $condition = substr($blk, $soff, $coff - $soff + 1);
504
505 #warn "STATEMENT<$statement>\n";
506 #warn "CONDITION<$condition>\n";
507
Andy Whitcroft773647a2008-03-28 14:15:58 -0700508 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800509
510 return ($statement, $condition,
511 $line, $remain + 1, $off - $loff + 1, $level);
512}
513
Andy Whitcroftcf655042008-03-04 14:28:20 -0800514sub statement_lines {
515 my ($stmt) = @_;
516
517 # Strip the diff line prefixes and rip blank lines at start and end.
518 $stmt =~ s/(^|\n)./$1/g;
519 $stmt =~ s/^\s*//;
520 $stmt =~ s/\s*$//;
521
522 my @stmt_lines = ($stmt =~ /\n/g);
523
524 return $#stmt_lines + 2;
525}
526
527sub statement_rawlines {
528 my ($stmt) = @_;
529
530 my @stmt_lines = ($stmt =~ /\n/g);
531
532 return $#stmt_lines + 2;
533}
534
535sub statement_block_size {
536 my ($stmt) = @_;
537
538 $stmt =~ s/(^|\n)./$1/g;
539 $stmt =~ s/^\s*{//;
540 $stmt =~ s/}\s*$//;
541 $stmt =~ s/^\s*//;
542 $stmt =~ s/\s*$//;
543
544 my @stmt_lines = ($stmt =~ /\n/g);
545 my @stmt_statements = ($stmt =~ /;/g);
546
547 my $stmt_lines = $#stmt_lines + 2;
548 my $stmt_statements = $#stmt_statements + 1;
549
550 if ($stmt_lines > $stmt_statements) {
551 return $stmt_lines;
552 } else {
553 return $stmt_statements;
554 }
555}
556
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800557sub ctx_statement_full {
558 my ($linenr, $remain, $off) = @_;
559 my ($statement, $condition, $level);
560
561 my (@chunks);
562
Andy Whitcroftcf655042008-03-04 14:28:20 -0800563 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800564 ($statement, $condition, $linenr, $remain, $off, $level) =
565 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700566 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800567 push(@chunks, [ $condition, $statement ]);
568 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
569 return ($level, $linenr, @chunks);
570 }
571
572 # Pull in the following conditional/block pairs and see if they
573 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800574 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800575 ($statement, $condition, $linenr, $remain, $off, $level) =
576 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800577 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700578 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800579 #print "C: push\n";
580 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800581 }
582
583 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800584}
585
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700586sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700587 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700588 my $line;
589 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700590 my $blk = '';
591 my @o;
592 my @c;
593 my @res = ();
594
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700595 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800596 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700597 for ($line = $start; $remain > 0; $line++) {
598 next if ($rawlines[$line] =~ /^-/);
599 $remain--;
600
601 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800602
603 # Handle nested #if/#else.
604 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
605 push(@stack, $level);
606 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
607 $level = $stack[$#stack - 1];
608 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
609 $level = pop(@stack);
610 }
611
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700612 foreach my $c (split(//, $rawlines[$line])) {
613 ##print "C<$c>L<$level><$open$close>O<$off>\n";
614 if ($off > 0) {
615 $off--;
616 next;
617 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700618
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700619 if ($c eq $close && $level > 0) {
620 $level--;
621 last if ($level == 0);
622 } elsif ($c eq $open) {
623 $level++;
624 }
625 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700626
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700627 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700628 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700629 }
630
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700631 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700632 }
633
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700634 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700635}
636sub ctx_block_outer {
637 my ($linenr, $remain) = @_;
638
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700639 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
640 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700641}
642sub ctx_block {
643 my ($linenr, $remain) = @_;
644
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700645 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
646 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700647}
648sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700649 my ($linenr, $remain, $off) = @_;
650
651 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
652 return @r;
653}
654sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700655 my ($linenr, $remain) = @_;
656
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700657 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700658}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700659sub ctx_statement_level {
660 my ($linenr, $remain, $off) = @_;
661
662 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
663}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700664
665sub ctx_locate_comment {
666 my ($first_line, $end_line) = @_;
667
668 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700669 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700670 return $current_comment if (defined $current_comment);
671
672 # Look through the context and try and figure out if there is a
673 # comment.
674 my $in_comment = 0;
675 $current_comment = '';
676 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700677 my $line = $rawlines[$linenr - 1];
678 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700679 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
680 $in_comment = 1;
681 }
682 if ($line =~ m@/\*@) {
683 $in_comment = 1;
684 }
685 if (!$in_comment && $current_comment ne '') {
686 $current_comment = '';
687 }
688 $current_comment .= $line . "\n" if ($in_comment);
689 if ($line =~ m@\*/@) {
690 $in_comment = 0;
691 }
692 }
693
694 chomp($current_comment);
695 return($current_comment);
696}
697sub ctx_has_comment {
698 my ($first_line, $end_line) = @_;
699 my $cmt = ctx_locate_comment($first_line, $end_line);
700
Andy Whitcroft00df3442007-06-08 13:47:06 -0700701 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700702 ##print "CMMT: $cmt\n";
703
704 return ($cmt ne '');
705}
706
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700707sub raw_line {
708 my ($linenr, $cnt) = @_;
709
710 my $offset = $linenr - 1;
711 $cnt++;
712
713 my $line;
714 while ($cnt) {
715 $line = $rawlines[$offset++];
716 next if (defined($line) && $line =~ /^-/);
717 $cnt--;
718 }
719
720 return $line;
721}
722
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700723sub cat_vet {
724 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700725 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700726
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700727 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700728 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
729 $res .= $1;
730 if ($2 ne '') {
731 $coded = sprintf("^%c", unpack('C', $2) + 64);
732 $res .= $coded;
733 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700734 }
735 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700736
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700737 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700738}
739
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800740my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800741my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800742my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700743my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800744
745sub annotate_reset {
746 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800747 $av_pending = '_';
748 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700749 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800750}
751
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700752sub annotate_values {
753 my ($stream, $type) = @_;
754
755 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700756 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700757 my $cur = $stream;
758
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800759 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700760
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700761 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700762 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800763 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700764 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700765 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800766 print "WS($1)\n" if ($dbg_values > 1);
767 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800768 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800769 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700770 }
771
Andy Whitcroft8ea3eb92008-07-23 21:29:08 -0700772 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800773 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700774 $type = 'T';
775
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700776 } elsif ($cur =~ /^($Modifier)\s*/) {
777 print "MODIFIER($1)\n" if ($dbg_values > 1);
778 $type = 'T';
779
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700780 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700781 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800782 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700783 push(@av_paren_type, $type);
784 if ($2 ne '') {
785 $av_pending = 'N';
786 }
787 $type = 'E';
788
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700789 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700790 print "UNDEF($1)\n" if ($dbg_values > 1);
791 $av_preprocessor = 1;
792 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700793
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700794 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800795 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800796 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800797
798 push(@av_paren_type, $type);
799 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700800 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800801
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700802 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800803 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
804 $av_preprocessor = 1;
805
806 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
807
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700808 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800809
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700810 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800811 print "PRE_END($1)\n" if ($dbg_values > 1);
812
813 $av_preprocessor = 1;
814
815 # Assume all arms of the conditional end as this
816 # one does, and continue as if the #endif was not here.
817 pop(@av_paren_type);
818 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700819 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700820
821 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800822 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700823
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700824 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
825 print "ATTR($1)\n" if ($dbg_values > 1);
826 $av_pending = $type;
827 $type = 'N';
828
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700829 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800830 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800832 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700833 }
834 $type = 'N';
835
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700836 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800837 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700838 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700839 $type = 'N';
840
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700841 } elsif ($cur =~/^(case)/o) {
842 print "CASE($1)\n" if ($dbg_values > 1);
843 $av_pend_colon = 'C';
844 $type = 'N';
845
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700846 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800847 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700848 $type = 'N';
849
850 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800851 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800852 push(@av_paren_type, $av_pending);
853 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700854 $type = 'N';
855
856 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800857 my $new_type = pop(@av_paren_type);
858 if ($new_type ne '_') {
859 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800860 print "PAREN('$1') -> $type\n"
861 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700862 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800863 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700864 }
865
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700866 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800867 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700868 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800869 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700870
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800871 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
872 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700873 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800874 } elsif ($type eq 'E') {
875 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700876 }
877 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
878 $type = 'V';
879
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700880 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800881 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700882 $type = 'V';
883
884 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800885 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700886 $type = 'N';
887
Andy Whitcroftcf655042008-03-04 14:28:20 -0800888 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800889 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800890 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700891 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800892
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800893 } elsif ($cur =~/^(,)/) {
894 print "COMMA($1)\n" if ($dbg_values > 1);
895 $type = 'C';
896
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700897 } elsif ($cur =~ /^(\?)/o) {
898 print "QUESTION($1)\n" if ($dbg_values > 1);
899 $type = 'N';
900
901 } elsif ($cur =~ /^(:)/o) {
902 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
903
904 substr($var, length($res), 1, $av_pend_colon);
905 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
906 $type = 'E';
907 } else {
908 $type = 'N';
909 }
910 $av_pend_colon = 'O';
911
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800912 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800913 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700914 $type = 'N';
915
Andy Whitcroft0d413862008-10-15 22:02:16 -0700916 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700917 my $variant;
918
919 print "OPV($1)\n" if ($dbg_values > 1);
920 if ($type eq 'V') {
921 $variant = 'B';
922 } else {
923 $variant = 'U';
924 }
925
926 substr($var, length($res), 1, $variant);
927 $type = 'N';
928
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700929 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800930 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700931 if ($1 ne '++' && $1 ne '--') {
932 $type = 'N';
933 }
934
935 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700937 }
938 if (defined $1) {
939 $cur = substr($cur, length($1));
940 $res .= $type x length($1);
941 }
942 }
943
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700944 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700945}
946
Andy Whitcroft8905a672007-11-28 16:21:06 -0800947sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800948 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800949
Andy Whitcroft0776e592008-10-15 22:02:29 -0700950 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
951 if ($possible !~ /(?:
952 ^(?:
953 $Modifier|
954 $Storage|
955 $Type|
956 DEFINE_\S+|
957 goto|
958 return|
959 case|
960 else|
961 asm|__asm__|
962 do
963 )$|
964 ^(?:typedef|struct|enum)\b
965 )/x) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700966 # Check for modifiers.
967 $possible =~ s/\s*$Storage\s*//g;
968 $possible =~ s/\s*$Sparse\s*//g;
969 if ($possible =~ /^\s*$/) {
970
971 } elsif ($possible =~ /\s/) {
972 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -0700973 for my $modifier (split(' ', $possible)) {
974 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
975 push(@modifierList, $modifier);
976 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700977
978 } else {
979 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
980 push(@typeList, $possible);
981 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800982 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -0700983 } else {
984 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800985 }
986}
987
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700988my $prefix = '';
989
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700990sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700991 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
992 return 0;
993 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800994 my $line = $prefix . $_[0];
995
996 $line = (split('\n', $line))[0] . "\n" if ($terse);
997
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800998 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700999
1000 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001001}
1002sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001003 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001004}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001005sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001006 if (report("ERROR: $_[0]\n")) {
1007 our $clean = 0;
1008 our $cnt_error++;
1009 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001010}
1011sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001012 if (report("WARNING: $_[0]\n")) {
1013 our $clean = 0;
1014 our $cnt_warn++;
1015 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001016}
1017sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001018 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001019 our $clean = 0;
1020 our $cnt_chk++;
1021 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001022}
1023
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001024sub check_absolute_file {
1025 my ($absolute, $herecurr) = @_;
1026 my $file = $absolute;
1027
1028 ##print "absolute<$absolute>\n";
1029
1030 # See if any suffix of this path is a path within the tree.
1031 while ($file =~ s@^[^/]*/@@) {
1032 if (-f "$root/$file") {
1033 ##print "file<$file>\n";
1034 last;
1035 }
1036 }
1037 if (! -f _) {
1038 return 0;
1039 }
1040
1041 # It is, so see if the prefix is acceptable.
1042 my $prefix = $absolute;
1043 substr($prefix, -length($file)) = '';
1044
1045 ##print "prefix<$prefix>\n";
1046 if ($prefix ne ".../") {
1047 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1048 }
1049}
1050
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001051sub process {
1052 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001053
1054 my $linenr=0;
1055 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001056 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001057 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001059
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001060 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001061 my $indent;
1062 my $previndent=0;
1063 my $stashindent=0;
1064
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001065 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001066 my $signoff = 0;
1067 my $is_patch = 0;
1068
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001069 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070 our $cnt_lines = 0;
1071 our $cnt_error = 0;
1072 our $cnt_warn = 0;
1073 our $cnt_chk = 0;
1074
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001075 # Trace the real file/line as we go.
1076 my $realfile = '';
1077 my $realline = 0;
1078 my $realcnt = 0;
1079 my $here = '';
1080 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001081 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001082 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001083 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001084
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001085 my $prev_values = 'E';
1086
1087 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001088 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001089 my %suppress_whiletrailers;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001090
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001091 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001092 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001093 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001094 my @setup_docs = ();
1095 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001096
1097 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001098 my $line;
1099 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001100 $linenr++;
1101 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001102
Andy Whitcroft773647a2008-03-28 14:15:58 -07001103 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001104 $setup_docs = 0;
1105 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1106 $setup_docs = 1;
1107 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001108 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001109 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001110 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1111 $realline=$1-1;
1112 if (defined $2) {
1113 $realcnt=$3+1;
1114 } else {
1115 $realcnt=1+1;
1116 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001117 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001118
1119 # Guestimate if this is a continuing comment. Run
1120 # the context looking for a comment "edge". If this
1121 # edge is a close comment then we must be in a comment
1122 # at context start.
1123 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001124 my $cnt = $realcnt;
1125 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1126 next if (defined $rawlines[$ln - 1] &&
1127 $rawlines[$ln - 1] =~ /^-/);
1128 $cnt--;
1129 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001130 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001131 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1132 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1133 ($edge) = $1;
1134 last;
1135 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001136 }
1137 if (defined $edge && $edge eq '*/') {
1138 $in_comment = 1;
1139 }
1140
1141 # Guestimate if this is a continuing comment. If this
1142 # is the start of a diff block and this line starts
1143 # ' *' then it is very likely a comment.
1144 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001145 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001146 {
1147 $in_comment = 1;
1148 }
1149
1150 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1151 sanitise_line_reset($in_comment);
1152
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001153 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001154 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001155 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001156 $line = sanitise_line($rawline);
1157 }
1158 push(@lines, $line);
1159
1160 if ($realcnt > 1) {
1161 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1162 } else {
1163 $realcnt = 0;
1164 }
1165
1166 #print "==>$rawline\n";
1167 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001168
1169 if ($setup_docs && $line =~ /^\+/) {
1170 push(@setup_docs, $line);
1171 }
1172 }
1173
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001174 $prefix = '';
1175
Andy Whitcroft773647a2008-03-28 14:15:58 -07001176 $realcnt = 0;
1177 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001178 foreach my $line (@lines) {
1179 $linenr++;
1180
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001181 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft306708542008-10-15 22:02:28 -07001182 my $hunk_line = ($realcnt != 0);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001184#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001185 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001186 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001187 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001188 $realline=$1-1;
1189 if (defined $2) {
1190 $realcnt=$3+1;
1191 } else {
1192 $realcnt=1+1;
1193 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001194 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001195 $prev_values = 'E';
1196
Andy Whitcroft773647a2008-03-28 14:15:58 -07001197 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001198 %suppress_whiletrailers = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001199 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001200
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001201# track the line number as we move through the hunk, note that
1202# new versions of GNU diff omit the leading space on completely
1203# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001204 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001205 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001206 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001207
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001208 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001209 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001210
1211 # Track the previous line.
1212 ($prevline, $stashline) = ($stashline, $line);
1213 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001214 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1215
Andy Whitcroft773647a2008-03-28 14:15:58 -07001216 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001217
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001218 } elsif ($realcnt == 1) {
1219 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001220 }
1221
1222#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001223 $prefix = "$filename:$realline: " if ($emacs && $file);
1224 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1225
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001226 $here = "#$linenr: " if (!$file);
1227 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001228
1229 # extract the filename as it passes
1230 if ($line=~/^\+\+\+\s+(\S+)/) {
1231 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001232 $realfile =~ s@^([^/]*)/@@;
1233
1234 $p1_prefix = $1;
1235 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1236 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1237 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001238
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001239 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001240 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1241 }
1242 next;
1243 }
1244
Randy Dunlap389834b2007-06-08 13:47:03 -07001245 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001246
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001247 my $hereline = "$here\n$rawline\n";
1248 my $herecurr = "$here\n$rawline\n";
1249 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001250
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001251 $cnt_lines++ if ($realcnt != 0);
1252
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001253#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001254 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001255 # This is a signoff, if ugly, so do not double report.
1256 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001257 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001258 WARN("Signed-off-by: is the preferred form\n" .
1259 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001260 }
1261 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001262 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001263 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001264 }
1265 }
1266
Andy Whitcroft00df3442007-06-08 13:47:06 -07001267# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001269 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001270 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001271 }
1272
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001273# Check for absolute kernel paths.
1274 if ($tree) {
1275 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1276 my $file = $1;
1277
1278 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1279 check_absolute_file($1, $herecurr)) {
1280 #
1281 } else {
1282 check_absolute_file($file, $herecurr);
1283 }
1284 }
1285 }
1286
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001287# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1288 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001289 $rawline !~ m/^$UTF8*$/) {
1290 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1291
1292 my $blank = copy_spacing($rawline);
1293 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1294 my $hereptr = "$hereline$ptr\n";
1295
1296 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001297 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001298
Andy Whitcroft306708542008-10-15 22:02:28 -07001299# ignore non-hunk lines and lines being removed
1300 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001301
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001302#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001303 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001304 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001305 ERROR("DOS line endings\n" . $herevet);
1306
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001307 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1308 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001309 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001310 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001311
1312# check we are in a valid source file if not then ignore this hunk
1313 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1314
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001315#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001316 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001317 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1318 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1319 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001320 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001321 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001322 }
1323
Andy Whitcroft8905a672007-11-28 16:21:06 -08001324# check for adding lines without a newline.
1325 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1326 WARN("adding a line without newline at end of file\n" . $herecurr);
1327 }
1328
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001329# check we are in a valid source file C or perl if not then ignore this hunk
1330 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001331
1332# at the beginning of a line any tabs must come first and anything
1333# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001334 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1335 $rawline =~ /^\+\s* \s*/) {
1336 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001337 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001338 }
1339
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001340# check we are in a valid C source file if not then ignore this hunk
1341 next if ($realfile !~ /\.(h|c)$/);
1342
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001344 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001345 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1346 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001347
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001348# Check for potential 'bare' types
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001349 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001350 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001351 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001352 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001353 $stat =~ s/\n./\n /g;
1354 $cond =~ s/\n./\n /g;
1355
1356 my $s = $stat;
1357 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001358
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001359 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001360 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001361
1362 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001363 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001365 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001366 } 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 -07001367 my $type = $1;
1368 $type =~ s/\s+/ /g;
1369 possible($type, "A:" . $s);
1370
Andy Whitcroft8905a672007-11-28 16:21:06 -08001371 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001372 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001373 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001375
1376 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001377 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001378 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001379 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001380
1381 # Check for any sort of function declaration.
1382 # int foo(something bar, other baz);
1383 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001384 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 -08001385 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001386
Andy Whitcroftcf655042008-03-04 14:28:20 -08001387 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001388 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001389 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001390
Andy Whitcroft8905a672007-11-28 16:21:06 -08001391 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001392 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001393
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001394 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001395 }
1396 }
1397 }
1398
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001399 }
1400
Andy Whitcroft653d4872007-06-23 17:16:34 -07001401#
1402# Checks which may be anchored in the context.
1403#
1404
1405# Check for switch () and associated case and default
1406# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001407 if ($line=~/\bswitch\s*\(.*\)/) {
1408 my $err = '';
1409 my $sep = '';
1410 my @ctx = ctx_block_outer($linenr, $realcnt);
1411 shift(@ctx);
1412 for my $ctx (@ctx) {
1413 my ($clen, $cindent) = line_stats($ctx);
1414 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1415 $indent != $cindent) {
1416 $err .= "$sep$ctx\n";
1417 $sep = '';
1418 } else {
1419 $sep = "[...]\n";
1420 }
1421 }
1422 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001423 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001424 }
1425 }
1426
1427# if/while/etc brace do not go on next line, unless defining a do while loop,
1428# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001429 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001430 my $pre_ctx = "$1$2";
1431
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001432 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001433 my $ctx_cnt = $realcnt - $#ctx - 1;
1434 my $ctx = join("\n", @ctx);
1435
Andy Whitcroft548596d2008-07-23 21:29:01 -07001436 my $ctx_ln = $linenr;
1437 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001438
Andy Whitcroft548596d2008-07-23 21:29:01 -07001439 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1440 defined $lines[$ctx_ln - 1] &&
1441 $lines[$ctx_ln - 1] =~ /^-/)) {
1442 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1443 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 $ctx_ln++;
1445 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001446
Andy Whitcroft53210162008-07-23 21:29:03 -07001447 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1448 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001449
1450 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1451 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001452 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001453 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001454 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1455 $ctx =~ /\)\s*\;\s*$/ &&
1456 defined $lines[$ctx_ln - 1])
1457 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001458 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1459 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001461 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001462 }
1463 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001464 }
1465
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001466# Check relative indent for conditionals and blocks.
1467 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1468 my ($s, $c) = ($stat, $cond);
1469
1470 substr($s, 0, length($c), '');
1471
1472 # Make sure we remove the line prefixes as we have
1473 # none on the first line, and are going to readd them
1474 # where necessary.
1475 $s =~ s/\n./\n/gs;
1476
1477 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001478 my @newlines = ($c =~ /\n/gs);
1479 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001480
1481 # We want to check the first line inside the block
1482 # starting at the end of the conditional, so remove:
1483 # 1) any blank line termination
1484 # 2) any opening brace { on end of the line
1485 # 3) any do (...) {
1486 my $continuation = 0;
1487 my $check = 0;
1488 $s =~ s/^.*\bdo\b//;
1489 $s =~ s/^\s*{//;
1490 if ($s =~ s/^\s*\\//) {
1491 $continuation = 1;
1492 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001493 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001494 $check = 1;
1495 $cond_lines++;
1496 }
1497
1498 # Also ignore a loop construct at the end of a
1499 # preprocessor statement.
1500 if (($prevline =~ /^.\s*#\s*define\s/ ||
1501 $prevline =~ /\\\s*$/) && $continuation == 0) {
1502 $check = 0;
1503 }
1504
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001505 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001506 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001507 while ($cond_ptr != $cond_lines) {
1508 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001509
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001510 # If we see an #else/#elif then the code
1511 # is not linear.
1512 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1513 $check = 0;
1514 }
1515
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001516 # Ignore:
1517 # 1) blank lines, they should be at 0,
1518 # 2) preprocessor lines, and
1519 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001520 if ($continuation ||
1521 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001522 $s =~ /^\s*#\s*?/ ||
1523 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001524 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001525 $s =~ s/^.*?\n//;
1526 $cond_lines++;
1527 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001528 }
1529
1530 my (undef, $sindent) = line_stats("+" . $s);
1531 my $stat_real = raw_line($linenr, $cond_lines);
1532
1533 # Check if either of these lines are modified, else
1534 # this is not this patch's fault.
1535 if (!defined($stat_real) ||
1536 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1537 $check = 0;
1538 }
1539 if (defined($stat_real) && $cond_lines > 1) {
1540 $stat_real = "[...]\n$stat_real";
1541 }
1542
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001543 #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 -07001544
1545 if ($check && (($sindent % 8) != 0 ||
1546 ($sindent <= $indent && $s ne ''))) {
1547 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1548 }
1549 }
1550
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001551 # Track the 'values' across context and added lines.
1552 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001553 my ($curr_values, $curr_vars) =
1554 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001555 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001556 if ($dbg_values) {
1557 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001558 print "$linenr > .$outline\n";
1559 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001560 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001561 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001562 $prev_values = substr($curr_values, -1);
1563
Andy Whitcroft00df3442007-06-08 13:47:06 -07001564#ignore lines not being added
1565 if ($line=~/^[^\+]/) {next;}
1566
Andy Whitcroft653d4872007-06-23 17:16:34 -07001567# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001568 if ($dbg_type) {
1569 if ($line =~ /^.\s*$Declare\s*$/) {
1570 ERROR("TEST: is type\n" . $herecurr);
1571 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1572 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1573 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001574 next;
1575 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001576# TEST: allow direct testing of the attribute matcher.
1577 if ($dbg_attr) {
1578 if ($line =~ /^.\s*$Attribute\s*$/) {
1579 ERROR("TEST: is attr\n" . $herecurr);
1580 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1581 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1582 }
1583 next;
1584 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001585
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001586# check for initialisation to aggregates open brace on the next line
1587 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1588 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001589 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001590 }
1591
Andy Whitcroft653d4872007-06-23 17:16:34 -07001592#
1593# Checks which are anchored on the added line.
1594#
1595
1596# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001597 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001598 my $path = $1;
1599 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001600 ERROR("malformed #include filename\n" .
1601 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001602 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001603 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001604
1605# no C99 // comments
1606 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001607 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001608 }
1609 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001610 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001611 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001612
1613#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001614 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1615 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1616 my $name = $1;
Andy Whitcroft48012052008-10-15 22:02:34 -07001617 if ($prevline !~ /(?:
1618 ^.}|
1619 ^.DEFINE_$Ident\(\Q$name\E\)|
1620 ^.DECLARE_$Ident\(\Q$name\E\)|
1621 ^.LIST_HEAD\(\Q$name\E\)|
1622 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1623 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1624 )/x) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001625 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001626 }
1627 }
1628
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001629# check for external initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001630 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001631 ERROR("do not initialise externals to 0 or NULL\n" .
1632 $herecurr);
1633 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001634# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001635 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001636 ERROR("do not initialise statics to 0 or NULL\n" .
1637 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001638 }
1639
Andy Whitcroft653d4872007-06-23 17:16:34 -07001640# check for new typedefs, only function parameters and sparse annotations
1641# make sense.
1642 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001643 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001644 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001645 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001646 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001647 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001648 }
1649
1650# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001651 # (char*[ const])
1652 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1653 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001654
Andy Whitcroft65863862009-01-06 14:41:21 -08001655 # Should start with a space.
1656 $to =~ s/^(\S)/ $1/;
1657 # Should not end with a space.
1658 $to =~ s/\s+$//;
1659 # '*'s should not have spaces between.
1660 while ($to =~ s/(.)\s\*/$1\*/) {
1661 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001662
Andy Whitcroft65863862009-01-06 14:41:21 -08001663 #print "from<$from> to<$to>\n";
1664 if ($from ne $to) {
1665 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1666 }
1667 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1668 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001669
Andy Whitcroft65863862009-01-06 14:41:21 -08001670 # Should start with a space.
1671 $to =~ s/^(\S)/ $1/;
1672 # Should not end with a space.
1673 $to =~ s/\s+$//;
1674 # '*'s should not have spaces between.
1675 while ($to =~ s/(.)\s\*/$1\*/) {
1676 }
1677 # Modifiers should have spaces.
1678 $to =~ s/(\b$Modifier$)/$1 /;
1679
1680 #print "from<$from> to<$to>\n";
1681 if ($from ne $to) {
1682 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1683 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001684 }
1685
1686# # no BUG() or BUG_ON()
1687# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1688# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1689# print "$herecurr";
1690# $clean = 0;
1691# }
1692
Andy Whitcroft8905a672007-11-28 16:21:06 -08001693 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694 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 -08001695 }
1696
Andy Whitcroft00df3442007-06-08 13:47:06 -07001697# printk should use KERN_* levels. Note that follow on printk's on the
1698# same line do not need a level, so we use the current block context
1699# to try and find and validate the current printk. In summary the current
1700# printk includes all preceeding printk's which have no newline on the end.
1701# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001702 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001703 my $ok = 0;
1704 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1705 #print "CHECK<$lines[$ln - 1]\n";
1706 # we have a preceeding printk if it ends
1707 # with "\n" ignore it, else it is to blame
1708 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1709 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1710 $ok = 1;
1711 }
1712 last;
1713 }
1714 }
1715 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001716 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001717 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001718 }
1719
Andy Whitcroft653d4872007-06-23 17:16:34 -07001720# function brace can't be on same line, except for #defines of do while,
1721# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001722 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1723 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001724 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001725 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001726
Andy Whitcroft8905a672007-11-28 16:21:06 -08001727# open braces for enum, union and struct go on the same line.
1728 if ($line =~ /^.\s*{/ &&
1729 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1730 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1731 }
1732
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001733# check for spacing round square brackets; allowed:
1734# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001735# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1736# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001737 while ($line =~ /(.*?\s)\[/g) {
1738 my ($where, $prefix) = ($-[1], $1);
1739 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001740 ($where != 0 || $prefix !~ /^.\s+$/) &&
1741 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001742 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1743 }
1744 }
1745
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001746# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001747 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001748 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001749 my $ctx_before = substr($line, 0, $-[1]);
1750 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001751
1752 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001753 if ($name =~ /^(?:
1754 if|for|while|switch|return|case|
1755 volatile|__volatile__|
1756 __attribute__|format|__extension__|
1757 asm|__asm__)$/x)
1758 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001759
1760 # cpp #define statements have non-optional spaces, ie
1761 # if there is a space between the name and the open
1762 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001763 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001764
1765 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001766 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001767
1768 # If this whole things ends with a type its most
1769 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001770 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001771
1772 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001773 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001774 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001775 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001776# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001777 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001778 my $ops = qr{
1779 <<=|>>=|<=|>=|==|!=|
1780 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1781 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001782 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1783 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001784 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001785 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001786 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001787
1788 my $blank = copy_spacing($opline);
1789
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001790 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001791 $off += length($elements[$n]);
1792
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 # Pick up the preceeding and succeeding characters.
1794 my $ca = substr($opline, 0, $off);
1795 my $cc = '';
1796 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1797 $cc = substr($opline, $off + length($elements[$n + 1]));
1798 }
1799 my $cb = "$ca$;$cc";
1800
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001801 my $a = '';
1802 $a = 'V' if ($elements[$n] ne '');
1803 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001804 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001805 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1806 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001807 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001808
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001809 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001810
1811 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001812 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001813 $c = 'V' if ($elements[$n + 2] ne '');
1814 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001815 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001816 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1817 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08001818 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001819 } else {
1820 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001821 }
1822
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001823 my $ctx = "${a}x${c}";
1824
1825 my $at = "(ctx:$ctx)";
1826
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001827 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001828 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001829
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001830 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001831 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001832
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001833 # Get the full operator variant.
1834 my $opv = $op . substr($curr_vars, $off, 1);
1835
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001836 # Ignore operators passed as parameters.
1837 if ($op_type ne 'V' &&
1838 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1839
Andy Whitcroftcf655042008-03-04 14:28:20 -08001840# # Ignore comments
1841# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001842
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001843 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001844 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845 if ($ctx !~ /.x[WEBC]/ &&
1846 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001847 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001848 }
1849
1850 # // is a comment
1851 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001852
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001853 # No spaces for:
1854 # ->
1855 # : when part of a bitfield
1856 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001857 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001858 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001859 }
1860
1861 # , must have a space on the right.
1862 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001863 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001864 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001865 }
1866
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001867 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001868 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001869 #warn "'*' is part of type\n";
1870
1871 # unary operators should have a space before and
1872 # none after. May be left adjacent to another
1873 # unary operator, or a cast
1874 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001875 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07001876 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001877 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001878 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001879 }
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001880 if ($op eq '*' && $cc =~/\s*const\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001881 # A unary '*' may be const
1882
1883 } elsif ($ctx =~ /.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001884 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001885 }
1886
1887 # unary ++ and unary -- are allowed no space on one side.
1888 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001889 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1890 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001891 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001892 if ($ctx =~ /Wx[BE]/ ||
1893 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1894 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001895 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001896 if ($ctx =~ /ExW/) {
1897 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1898 }
1899
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001900
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001901 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001902 } elsif ($op eq '<<' or $op eq '>>' or
1903 $op eq '&' or $op eq '^' or $op eq '|' or
1904 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001905 $op eq '*' or $op eq '/' or
1906 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001907 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001908 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001909 ERROR("need consistent spacing around '$op' $at\n" .
1910 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001911 }
1912
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001913 # A colon needs no spaces before when it is
1914 # terminating a case value or a label.
1915 } elsif ($opv eq ':C' || $opv eq ':L') {
1916 if ($ctx =~ /Wx./) {
1917 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1918 }
1919
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001920 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001921 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001922 my $ok = 0;
1923
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001924 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001925 if (($op eq '<' &&
1926 $cc =~ /^\S+\@\S+>/) ||
1927 ($op eq '>' &&
1928 $ca =~ /<\S+\@\S+$/))
1929 {
1930 $ok = 1;
1931 }
1932
1933 # Ignore ?:
1934 if (($opv eq ':O' && $ca =~ /\?$/) ||
1935 ($op eq '?' && $cc =~ /^:/)) {
1936 $ok = 1;
1937 }
1938
1939 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001941 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001942 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001943 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001944 }
1945 }
1946
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001947# check for multiple assignments
1948 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001949 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001950 }
1951
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001952## # check for multiple declarations, allowing for a function declaration
1953## # continuation.
1954## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1955## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1956##
1957## # Remove any bracketed sections to ensure we do not
1958## # falsly report the parameters of functions.
1959## my $ln = $line;
1960## while ($ln =~ s/\([^\(\)]*\)//g) {
1961## }
1962## if ($ln =~ /,/) {
1963## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1964## }
1965## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001966
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001967#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001968 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1969 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001970 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001971 }
1972
1973# closing brace should have a space following it when it has anything
1974# on the line
1975 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001976 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001977 }
1978
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001979# check spacing on square brackets
1980 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001981 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001982 }
1983 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001984 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001985 }
1986
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001987# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001988 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1989 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001990 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001991 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001992 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001993 $line !~ /for\s*\(.*;\s+\)/ &&
1994 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001996 }
1997
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001998#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001999 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002000 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002001 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002002 }
2003
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002004# Return is not a function.
2005 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2006 my $spacing = $1;
2007 my $value = $2;
2008
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002009 # Flatten any parentheses
Andy Whitcroftfee61c42008-07-23 21:28:56 -07002010 $value =~ s/\)\(/\) \(/g;
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002011 while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002012 }
2013
2014 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2015 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2016
2017 } elsif ($spacing !~ /\s+/) {
2018 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2019 }
2020 }
2021
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002022# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002023 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002024 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002025 }
2026
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002027# Check for illegal assignment in if conditional -- and check for trailing
2028# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002029 if ($line =~ /do\s*(?!{)/) {
2030 my ($stat_next) = ctx_statement_block($line_nr_next,
2031 $remain_next, $off_next);
2032 $stat_next =~ s/\n./\n /g;
2033 ##print "stat<$stat> stat_next<$stat_next>\n";
2034
2035 if ($stat_next =~ /^\s*while\b/) {
2036 # If the statement carries leading newlines,
2037 # then count those as offsets.
2038 my ($whitespace) =
2039 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2040 my $offset =
2041 statement_rawlines($whitespace) - 1;
2042
2043 $suppress_whiletrailers{$line_nr_next +
2044 $offset} = 1;
2045 }
2046 }
2047 if (!defined $suppress_whiletrailers{$linenr} &&
2048 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002049 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002050
2051 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002052 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002053 }
2054
2055 # Find out what is on the end of the line after the
2056 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002057 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002058 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002059 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002060 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2061 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002062 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002063 # Find out how long the conditional actually is.
2064 my @newlines = ($c =~ /\n/gs);
2065 my $cond_lines = 1 + $#newlines;
2066
2067 my $stat_real = raw_line($linenr, $cond_lines);
2068 if (defined($stat_real) && $cond_lines > 1) {
2069 $stat_real = "[...]\n$stat_real";
2070 }
2071
2072 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002073 }
2074 }
2075
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002076# Check for bitwise tests written as boolean
2077 if ($line =~ /
2078 (?:
2079 (?:\[|\(|\&\&|\|\|)
2080 \s*0[xX][0-9]+\s*
2081 (?:\&\&|\|\|)
2082 |
2083 (?:\&\&|\|\|)
2084 \s*0[xX][0-9]+\s*
2085 (?:\&\&|\|\||\)|\])
2086 )/x)
2087 {
2088 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2089 }
2090
Andy Whitcroft8905a672007-11-28 16:21:06 -08002091# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002092 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2093 my $s = $1;
2094 $s =~ s/$;//g; # Remove any comments
2095 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2096 ERROR("trailing statements should be on next line\n" . $herecurr);
2097 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002098 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002099# case and default should not have general statements after them
2100 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2101 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002102 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002103 \s*return\s+
2104 )/xg)
2105 {
2106 ERROR("trailing statements should be on next line\n" . $herecurr);
2107 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002108
2109 # Check for }<nl>else {, these must be at the same
2110 # indent level to be relevant to each other.
2111 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2112 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002113 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002114 }
2115
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002116 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2117 $previndent == $indent) {
2118 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2119
2120 # Find out what is on the end of the line after the
2121 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002122 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002123 $s =~ s/\n.*//g;
2124
2125 if ($s =~ /^\s*;/) {
2126 ERROR("while should follow close brace '}'\n" . $hereprev);
2127 }
2128 }
2129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002130#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2131# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2132# print "No studly caps, use _\n";
2133# print "$herecurr";
2134# $clean = 0;
2135# }
2136
2137#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002138 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002139 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002140 }
2141
Andy Whitcroft653d4872007-06-23 17:16:34 -07002142#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002143 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002144 my $file = "$1.h";
2145 my $checkfile = "include/linux/$file";
2146 if (-f "$root/$checkfile" &&
2147 $realfile ne $checkfile &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002148 $1 ne 'irq')
2149 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002150 if ($realfile =~ m{^arch/}) {
2151 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2152 } else {
2153 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2154 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002155 }
2156 }
2157
Andy Whitcroft653d4872007-06-23 17:16:34 -07002158# multi-statement macros should be enclosed in a do while loop, grab the
2159# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002160# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002161 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2162 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002163 my $ln = $linenr;
2164 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002165 my ($off, $dstat, $dcond, $rest);
2166 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002167
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002168 my $args = defined($1);
2169
2170 # Find the end of the macro and limit our statement
2171 # search to that.
2172 while ($cnt > 0 && defined $lines[$ln - 1] &&
2173 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2174 {
2175 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002176 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002177 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002178 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002179 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002180
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002181 ($dstat, $dcond, $ln, $cnt, $off) =
2182 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2183 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002184 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002185
2186 # Extract the remainder of the define (if any) and
2187 # rip off surrounding spaces, and trailing \'s.
2188 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002189 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2190 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002191 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2192 $rest .= substr($lines[$ln - 1], $off) . "\n";
2193 $cnt--;
2194 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002195 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002196 $off = 0;
2197 }
2198 $rest =~ s/\\\n.//g;
2199 $rest =~ s/^\s*//s;
2200 $rest =~ s/\s*$//s;
2201
2202 # Clean up the original statement.
2203 if ($args) {
2204 substr($dstat, 0, length($dcond), '');
2205 } else {
2206 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2207 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002208 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002209 $dstat =~ s/\\\n.//g;
2210 $dstat =~ s/^\s*//s;
2211 $dstat =~ s/\s*$//s;
2212
2213 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002214 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2215 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2216 $dstat =~ s/\[[^\{\}]*\]/1/)
2217 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002218 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002219
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002220 my $exceptions = qr{
2221 $Declare|
2222 module_param_named|
2223 MODULE_PARAM_DESC|
2224 DECLARE_PER_CPU|
2225 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002226 __typeof__\(|
2227 \.$Ident\s*=\s*
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002228 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002229 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002230 if ($rest ne '') {
2231 if ($rest !~ /while\s*\(/ &&
2232 $dstat !~ /$exceptions/)
2233 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002234 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 -07002235 }
2236
2237 } elsif ($ctx !~ /;/) {
2238 if ($dstat ne '' &&
2239 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2240 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002241 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002242 $dstat =~ /$Operators/)
2243 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002244 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002245 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002246 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002247 }
2248
Mike Frysinger080ba922009-01-06 14:41:25 -08002249# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2250# all assignments may have only one of the following with an assignment:
2251# .
2252# ALIGN(...)
2253# VMLINUX_SYMBOL(...)
2254 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2255 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2256 }
2257
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002258# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002259 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2260 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002261 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002262 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002263 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2264 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002265 my $allowed = 0;
2266 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002268 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002269 for my $chunk (@chunks) {
2270 my ($cond, $block) = @{$chunk};
2271
Andy Whitcroft773647a2008-03-28 14:15:58 -07002272 # If the condition carries leading newlines, then count those as offsets.
2273 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2274 my $offset = statement_rawlines($whitespace) - 1;
2275
2276 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2277
2278 # We have looked at and allowed this specific line.
2279 $suppress_ifbraces{$ln + $offset} = 1;
2280
2281 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002282 $ln += statement_rawlines($block) - 1;
2283
Andy Whitcroft773647a2008-03-28 14:15:58 -07002284 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002285
2286 $seen++ if ($block =~ /^\s*{/);
2287
Andy Whitcroftcf655042008-03-04 14:28:20 -08002288 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2289 if (statement_lines($cond) > 1) {
2290 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002291 $allowed = 1;
2292 }
2293 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002294 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002295 $allowed = 1;
2296 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002297 if (statement_block_size($block) > 1) {
2298 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002299 $allowed = 1;
2300 }
2301 }
2302 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002303 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002304 }
2305 }
2306 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002308 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002309 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002310
Andy Whitcroftcf655042008-03-04 14:28:20 -08002311 # Check the pre-context.
2312 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2313 #print "APW: ALLOWED: pre<$1>\n";
2314 $allowed = 1;
2315 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002316
2317 my ($level, $endln, @chunks) =
2318 ctx_statement_full($linenr, $realcnt, $-[0]);
2319
Andy Whitcroftcf655042008-03-04 14:28:20 -08002320 # Check the condition.
2321 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002322 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002323 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002324 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002325 }
2326 if (statement_lines($cond) > 1) {
2327 #print "APW: ALLOWED: cond<$cond>\n";
2328 $allowed = 1;
2329 }
2330 if ($block =~/\b(?:if|for|while)\b/) {
2331 #print "APW: ALLOWED: block<$block>\n";
2332 $allowed = 1;
2333 }
2334 if (statement_block_size($block) > 1) {
2335 #print "APW: ALLOWED: lines block<$block>\n";
2336 $allowed = 1;
2337 }
2338 # Check the post-context.
2339 if (defined $chunks[1]) {
2340 my ($cond, $block) = @{$chunks[1]};
2341 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002342 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002343 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002344 if ($block =~ /^\s*\{/) {
2345 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2346 $allowed = 1;
2347 }
2348 }
2349 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2350 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002351 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002352
Andy Whitcroftf0556632008-10-15 22:02:23 -07002353 for (my $n = 0; $n < $cnt; $n++) {
2354 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002355 }
2356
2357 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002358 }
2359 }
2360
Andy Whitcroft653d4872007-06-23 17:16:34 -07002361# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002362 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002363 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002364 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002365 }
2366 }
2367
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002368# don't use deprecated functions
2369 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002370 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002371 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002372 }
2373 }
2374
2375# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002376 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2377 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002378 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002379 }
2380
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002381# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2382 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2383 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2384 }
2385
Andy Whitcroft00df3442007-06-08 13:47:06 -07002386# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002387 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002388 CHK("if this code is redundant consider removing it\n" .
2389 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002390 }
2391
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002392# check for needless kfree() checks
2393 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2394 my $expr = $1;
2395 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002396 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002397 }
2398 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002399# check for needless usb_free_urb() checks
2400 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2401 my $expr = $1;
2402 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2403 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2404 }
2405 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002406
Andy Whitcroft00df3442007-06-08 13:47:06 -07002407# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002408# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002409# print "#ifdef in C files should be avoided\n";
2410# print "$herecurr";
2411# $clean = 0;
2412# }
2413
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002414# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002415 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002416 ERROR("exactly one space required after that #$1\n" . $herecurr);
2417 }
2418
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002419# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002420 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2421 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002422 my $which = $1;
2423 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002424 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002425 }
2426 }
2427# check for memory barriers without a comment.
2428 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2429 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002430 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002431 }
2432 }
2433# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002434 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002435 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002436 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002437
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002438# check the location of the inline attribute, that it is between
2439# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002440 if ($line =~ /\b$Type\s+$Inline\b/ ||
2441 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002442 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2443 }
2444
Andy Whitcroft8905a672007-11-28 16:21:06 -08002445# Check for __inline__ and __inline, prefer inline
2446 if ($line =~ /\b(__inline__|__inline)\b/) {
2447 WARN("plain inline is preferred over $1\n" . $herecurr);
2448 }
2449
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002450# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002451 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002452 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002453 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002454 my $function_name = $1;
2455 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002456
2457 my $s = $stat;
2458 if (defined $cond) {
2459 substr($s, 0, length($cond), '');
2460 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002461 if ($s =~ /^\s*;/ &&
2462 $function_name ne 'uninitialized_var')
2463 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002464 WARN("externs should be avoided in .c files\n" . $herecurr);
2465 }
2466
2467 if ($paren_space =~ /\n/) {
2468 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2469 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002470
2471 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2472 $stat =~ /^.\s*extern\s+/)
2473 {
2474 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002475 }
2476
2477# checks for new __setup's
2478 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2479 my $name = $1;
2480
2481 if (!grep(/$name/, @setup_docs)) {
2482 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2483 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002484 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002485
2486# check for pointless casting of kmalloc return
2487 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2488 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2489 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002490
2491# check for gcc specific __FUNCTION__
2492 if ($line =~ /__FUNCTION__/) {
2493 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2494 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002495
2496# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002497 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2499 }
2500# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002501 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002502 WARN("consider using a completion\n" . $herecurr);
2503 }
2504# recommend strict_strto* over simple_strto*
2505 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2506 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2507 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002508# check for __initcall(), use device_initcall() explicitly please
2509 if ($line =~ /^.\s*__initcall\s*\(/) {
2510 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2511 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002512
2513# use of NR_CPUS is usually wrong
2514# ignore definitions of NR_CPUS and usage to define arrays as likely right
2515 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002516 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2517 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002518 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2519 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2520 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002521 {
2522 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2523 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002524
2525# check for %L{u,d,i} in strings
2526 my $string;
2527 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2528 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002529 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002530 if ($string =~ /(?<!%)%L[udi]/) {
2531 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2532 last;
2533 }
2534 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002535
2536# whine mightly about in_atomic
2537 if ($line =~ /\bin_atomic\s*\(/) {
2538 if ($realfile =~ m@^drivers/@) {
2539 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2540 } else {
2541 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2542 }
2543 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002544 }
2545
2546 # If we have no input at all, then there is nothing to report on
2547 # so just keep quiet.
2548 if ($#rawlines == -1) {
2549 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002550 }
2551
Andy Whitcroft8905a672007-11-28 16:21:06 -08002552 # In mailback mode only produce a report in the negative, for
2553 # things that appear to be patches.
2554 if ($mailback && ($clean == 1 || !$is_patch)) {
2555 exit(0);
2556 }
2557
2558 # This is not a patch, and we are are in 'no-patch' mode so
2559 # just keep quiet.
2560 if (!$chk_patch && !$is_patch) {
2561 exit(0);
2562 }
2563
2564 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002565 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002566 }
2567 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002568 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002569 }
2570
Andy Whitcroft8905a672007-11-28 16:21:06 -08002571 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002572 if ($summary && !($clean == 1 && $quiet == 1)) {
2573 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002574 print "total: $cnt_error errors, $cnt_warn warnings, " .
2575 (($check)? "$cnt_chk checks, " : "") .
2576 "$cnt_lines lines checked\n";
2577 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002578 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002579
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002580 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002581 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002582 }
2583 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002584 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002585 print "are false positives report them to the maintainer, see\n";
2586 print "CHECKPATCH in MAINTAINERS.\n";
2587 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002588
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002589 return $clean;
2590}