blob: 2018f4349b477ba3eac5d370c1efd5d99c39a1df [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft131edb32009-10-26 16:50:14 -07005# (c) 2008,2009, Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070012
Andy Whitcroft5e8d8f62009-10-26 16:50:17 -070013my $V = '0.30';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080030my %debug;
Hannes Eder77f5b102009-09-21 17:04:37 -070031my $help = 0;
32
33sub help {
34 my ($exitcode) = @_;
35
36 print << "EOM";
37Usage: $P [OPTION]... [FILE]...
38Version: $V
39
40Options:
41 -q, --quiet quiet
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
55 is all off)
56 --test-only=WORD report only warnings/errors containing WORD
57 literally
58 -h, --help, --version display this help and exit
59
60When FILE is - read standard input.
61EOM
62
63 exit($exitcode);
64}
65
Andy Whitcroft0a920b52007-06-01 00:46:48 -070066GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070067 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070068 'tree!' => \$tree,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070071 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080072 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070073 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070074 'subjective!' => \$check,
75 'strict!' => \$check,
76 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080077 'summary!' => \$summary,
78 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080079 'summary-file!' => \$summary_file,
80
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080081 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070082 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070083 'h|help' => \$help,
84 'version' => \$help
85) or help(1);
86
87help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070088
89my $exit = 0;
90
91if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070092 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070093 exit(1);
94}
95
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080096my $dbg_values = 0;
97my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -070098my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -070099my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800100for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800101 ## no critic
102 eval "\${dbg_$key} = '$debug{$key}';";
103 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800104}
105
Andy Whitcroft8905a672007-11-28 16:21:06 -0800106if ($terse) {
107 $emacs = 1;
108 $quiet++;
109}
110
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700111if ($tree) {
112 if (defined $root) {
113 if (!top_of_kernel_tree($root)) {
114 die "$P: $root: --root does not point at a valid tree\n";
115 }
116 } else {
117 if (top_of_kernel_tree('.')) {
118 $root = '.';
119 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
120 top_of_kernel_tree($1)) {
121 $root = $1;
122 }
123 }
124
125 if (!defined $root) {
126 print "Must be run from the top-level dir. of a kernel tree\n";
127 exit(2);
128 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700129}
130
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131my $emitted_corrupt = 0;
132
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700133our $Ident = qr{
134 [A-Za-z_][A-Za-z\d_]*
135 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
136 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700137our $Storage = qr{extern|static|asmlinkage};
138our $Sparse = qr{
139 __user|
140 __kernel|
141 __force|
142 __iomem|
143 __must_check|
144 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800145 __kprobes|
146 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700147 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800148
149# Notes to $Attribute:
150# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700151our $Attribute = qr{
152 const|
153 __read_mostly|
154 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800155 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700156 ____cacheline_aligned|
157 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800158 ____cacheline_internodealigned_in_smp|
159 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700160 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700161our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700162our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700163our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
164our $Lval = qr{$Ident(?:$Member)*};
165
166our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
167our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800168our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700169our $Operators = qr{
170 <=|>=|==|!=|
171 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800172 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700173 }x;
174
Andy Whitcroft8905a672007-11-28 16:21:06 -0800175our $NonptrType;
176our $Type;
177our $Declare;
178
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700179our $UTF8 = qr {
180 [\x09\x0A\x0D\x20-\x7E] # ASCII
181 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
182 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
183 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
184 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
185 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
186 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
187 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
188}x;
189
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700190our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700191 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700192 atomic_t
193)};
194
Joe Perches691e6692010-03-05 13:43:51 -0800195our $logFunctions = qr{(?x:
196 printk|
197 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
Joe Perches8bbea962010-08-09 17:21:01 -0700198 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800199 WARN|
200 panic
201)};
202
Andy Whitcroft8905a672007-11-28 16:21:06 -0800203our @typeList = (
204 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700205 qr{(?:unsigned\s+)?char},
206 qr{(?:unsigned\s+)?short},
207 qr{(?:unsigned\s+)?int},
208 qr{(?:unsigned\s+)?long},
209 qr{(?:unsigned\s+)?long\s+int},
210 qr{(?:unsigned\s+)?long\s+long},
211 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800212 qr{unsigned},
213 qr{float},
214 qr{double},
215 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800216 qr{struct\s+$Ident},
217 qr{union\s+$Ident},
218 qr{enum\s+$Ident},
219 qr{${Ident}_t},
220 qr{${Ident}_handler},
221 qr{${Ident}_handler_fn},
222);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700223our @modifierList = (
224 qr{fastcall},
225);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800226
Wolfram Sang7840a942010-08-09 17:20:57 -0700227our $allowed_asm_includes = qr{(?x:
228 irq|
229 memory
230)};
231# memory.h: ARM has a custom one
232
Andy Whitcroft8905a672007-11-28 16:21:06 -0800233sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700234 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
235 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700236 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800237 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700238 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800239 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700240 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700241 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700242 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800243 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700244 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800245 }x;
246 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700247 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800248 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700249 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800250 }x;
251 $Declare = qr{(?:$Storage\s+)?$Type};
252}
253build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700254
255$chk_signoff = 0 if ($file);
256
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700257my @dep_includes = ();
258my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700259my $removal = "Documentation/feature-removal-schedule.txt";
260if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800261 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700262 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800263 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700264 if (/^Check:\s+(.*\S)/) {
265 for my $entry (split(/[, ]+/, $1)) {
266 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700267 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700268
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700269 } elsif ($entry !~ m@/@) {
270 push(@dep_functions, $entry);
271 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700272 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700273 }
274 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800275 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700276}
277
Andy Whitcroft00df3442007-06-08 13:47:06 -0700278my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800279my @lines = ();
280my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700281for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800282 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700283 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800284 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700285 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 } elsif ($filename eq '-') {
287 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700288 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800289 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700291 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800292 if ($filename eq '-') {
293 $vname = 'Your patch';
294 } else {
295 $vname = $filename;
296 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800297 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298 chomp;
299 push(@rawlines, $_);
300 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800301 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303 $exit = 1;
304 }
305 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800306 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700307}
308
309exit($exit);
310
311sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700312 my ($root) = @_;
313
314 my @tree_check = (
315 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
316 "README", "Documentation", "arch", "include", "drivers",
317 "fs", "init", "ipc", "kernel", "lib", "scripts",
318 );
319
320 foreach my $check (@tree_check) {
321 if (! -e $root . '/' . $check) {
322 return 0;
323 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700324 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700325 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700326}
327
328sub expand_tabs {
329 my ($str) = @_;
330
331 my $res = '';
332 my $n = 0;
333 for my $c (split(//, $str)) {
334 if ($c eq "\t") {
335 $res .= ' ';
336 $n++;
337 for (; ($n % 8) != 0; $n++) {
338 $res .= ' ';
339 }
340 next;
341 }
342 $res .= $c;
343 $n++;
344 }
345
346 return $res;
347}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700348sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700349 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700350 return $res;
351}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700352
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700353sub line_stats {
354 my ($line) = @_;
355
356 # Drop the diff line leader and expand tabs
357 $line =~ s/^.//;
358 $line = expand_tabs($line);
359
360 # Pick the indent from the front of the line.
361 my ($white) = ($line =~ /^(\s*)/);
362
363 return (length($line), length($white));
364}
365
Andy Whitcroft773647a2008-03-28 14:15:58 -0700366my $sanitise_quote = '';
367
368sub sanitise_line_reset {
369 my ($in_comment) = @_;
370
371 if ($in_comment) {
372 $sanitise_quote = '*/';
373 } else {
374 $sanitise_quote = '';
375 }
376}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700377sub sanitise_line {
378 my ($line) = @_;
379
380 my $res = '';
381 my $l = '';
382
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800383 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700384 my $off = 0;
385 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700386
Andy Whitcroft773647a2008-03-28 14:15:58 -0700387 # Always copy over the diff marker.
388 $res = substr($line, 0, 1);
389
390 for ($off = 1; $off < length($line); $off++) {
391 $c = substr($line, $off, 1);
392
393 # Comments we are wacking completly including the begin
394 # and end, all to $;.
395 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
396 $sanitise_quote = '*/';
397
398 substr($res, $off, 2, "$;$;");
399 $off++;
400 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800401 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700402 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700403 $sanitise_quote = '';
404 substr($res, $off, 2, "$;$;");
405 $off++;
406 next;
407 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700408 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
409 $sanitise_quote = '//';
410
411 substr($res, $off, 2, $sanitise_quote);
412 $off++;
413 next;
414 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700415
416 # A \ in a string means ignore the next character.
417 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
418 $c eq "\\") {
419 substr($res, $off, 2, 'XX');
420 $off++;
421 next;
422 }
423 # Regular quotes.
424 if ($c eq "'" || $c eq '"') {
425 if ($sanitise_quote eq '') {
426 $sanitise_quote = $c;
427
428 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700429 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700430 } elsif ($sanitise_quote eq $c) {
431 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700432 }
433 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700434
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800435 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700436 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
437 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700438 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
439 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700440 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
441 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700442 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700443 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700444 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800445 }
446
Daniel Walker113f04a2009-09-21 17:04:35 -0700447 if ($sanitise_quote eq '//') {
448 $sanitise_quote = '';
449 }
450
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800451 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700452 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800453 my $clean = 'X' x length($1);
454 $res =~ s@\<.*\>@<$clean>@;
455
456 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700457 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800458 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700459 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800460 }
461
Andy Whitcroft00df3442007-06-08 13:47:06 -0700462 return $res;
463}
464
Andy Whitcroft8905a672007-11-28 16:21:06 -0800465sub ctx_statement_block {
466 my ($linenr, $remain, $off) = @_;
467 my $line = $linenr - 1;
468 my $blk = '';
469 my $soff = $off;
470 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700471 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800472
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800473 my $loff = 0;
474
Andy Whitcroft8905a672007-11-28 16:21:06 -0800475 my $type = '';
476 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800477 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800478 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800479 my $c;
480 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800481
482 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800483 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800484 @stack = (['', 0]) if ($#stack == -1);
485
Andy Whitcroft773647a2008-03-28 14:15:58 -0700486 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487 # If we are about to drop off the end, pull in more
488 # context.
489 if ($off >= $len) {
490 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700491 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800492 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800493 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800494 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800495 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800496 $len = length($blk);
497 $line++;
498 last;
499 }
500 # Bail if there is no further context.
501 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800502 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503 last;
504 }
505 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800506 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800507 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800508 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509
Andy Whitcroft773647a2008-03-28 14:15:58 -0700510 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800511
512 # Handle nested #if/#else.
513 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
514 push(@stack, [ $type, $level ]);
515 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
516 ($type, $level) = @{$stack[$#stack - 1]};
517 } elsif ($remainder =~ /^#\s*endif\b/) {
518 ($type, $level) = @{pop(@stack)};
519 }
520
Andy Whitcroft8905a672007-11-28 16:21:06 -0800521 # Statement ends at the ';' or a close '}' at the
522 # outermost level.
523 if ($level == 0 && $c eq ';') {
524 last;
525 }
526
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800527 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700528 if ($level == 0 && $coff_set == 0 &&
529 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
530 $remainder =~ /^(else)(?:\s|{)/ &&
531 $remainder !~ /^else\s+if\b/) {
532 $coff = $off + length($1) - 1;
533 $coff_set = 1;
534 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
535 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800536 }
537
Andy Whitcroft8905a672007-11-28 16:21:06 -0800538 if (($type eq '' || $type eq '(') && $c eq '(') {
539 $level++;
540 $type = '(';
541 }
542 if ($type eq '(' && $c eq ')') {
543 $level--;
544 $type = ($level != 0)? '(' : '';
545
546 if ($level == 0 && $coff < $soff) {
547 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700548 $coff_set = 1;
549 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800550 }
551 }
552 if (($type eq '' || $type eq '{') && $c eq '{') {
553 $level++;
554 $type = '{';
555 }
556 if ($type eq '{' && $c eq '}') {
557 $level--;
558 $type = ($level != 0)? '{' : '';
559
560 if ($level == 0) {
561 last;
562 }
563 }
564 $off++;
565 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700566 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800567 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700568 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800569 $line++;
570 $remain--;
571 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800572
573 my $statement = substr($blk, $soff, $off - $soff + 1);
574 my $condition = substr($blk, $soff, $coff - $soff + 1);
575
576 #warn "STATEMENT<$statement>\n";
577 #warn "CONDITION<$condition>\n";
578
Andy Whitcroft773647a2008-03-28 14:15:58 -0700579 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800580
581 return ($statement, $condition,
582 $line, $remain + 1, $off - $loff + 1, $level);
583}
584
Andy Whitcroftcf655042008-03-04 14:28:20 -0800585sub statement_lines {
586 my ($stmt) = @_;
587
588 # Strip the diff line prefixes and rip blank lines at start and end.
589 $stmt =~ s/(^|\n)./$1/g;
590 $stmt =~ s/^\s*//;
591 $stmt =~ s/\s*$//;
592
593 my @stmt_lines = ($stmt =~ /\n/g);
594
595 return $#stmt_lines + 2;
596}
597
598sub statement_rawlines {
599 my ($stmt) = @_;
600
601 my @stmt_lines = ($stmt =~ /\n/g);
602
603 return $#stmt_lines + 2;
604}
605
606sub statement_block_size {
607 my ($stmt) = @_;
608
609 $stmt =~ s/(^|\n)./$1/g;
610 $stmt =~ s/^\s*{//;
611 $stmt =~ s/}\s*$//;
612 $stmt =~ s/^\s*//;
613 $stmt =~ s/\s*$//;
614
615 my @stmt_lines = ($stmt =~ /\n/g);
616 my @stmt_statements = ($stmt =~ /;/g);
617
618 my $stmt_lines = $#stmt_lines + 2;
619 my $stmt_statements = $#stmt_statements + 1;
620
621 if ($stmt_lines > $stmt_statements) {
622 return $stmt_lines;
623 } else {
624 return $stmt_statements;
625 }
626}
627
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800628sub ctx_statement_full {
629 my ($linenr, $remain, $off) = @_;
630 my ($statement, $condition, $level);
631
632 my (@chunks);
633
Andy Whitcroftcf655042008-03-04 14:28:20 -0800634 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800635 ($statement, $condition, $linenr, $remain, $off, $level) =
636 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700637 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800638 push(@chunks, [ $condition, $statement ]);
639 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
640 return ($level, $linenr, @chunks);
641 }
642
643 # Pull in the following conditional/block pairs and see if they
644 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800645 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800646 ($statement, $condition, $linenr, $remain, $off, $level) =
647 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800648 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700649 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800650 #print "C: push\n";
651 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800652 }
653
654 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800655}
656
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700657sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700658 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700659 my $line;
660 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700661 my $blk = '';
662 my @o;
663 my @c;
664 my @res = ();
665
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700666 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800667 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700668 for ($line = $start; $remain > 0; $line++) {
669 next if ($rawlines[$line] =~ /^-/);
670 $remain--;
671
672 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800673
674 # Handle nested #if/#else.
675 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
676 push(@stack, $level);
677 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
678 $level = $stack[$#stack - 1];
679 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
680 $level = pop(@stack);
681 }
682
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700683 foreach my $c (split(//, $rawlines[$line])) {
684 ##print "C<$c>L<$level><$open$close>O<$off>\n";
685 if ($off > 0) {
686 $off--;
687 next;
688 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700689
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700690 if ($c eq $close && $level > 0) {
691 $level--;
692 last if ($level == 0);
693 } elsif ($c eq $open) {
694 $level++;
695 }
696 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700697
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700698 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700699 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700700 }
701
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700702 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700703 }
704
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700705 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700706}
707sub ctx_block_outer {
708 my ($linenr, $remain) = @_;
709
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700710 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
711 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700712}
713sub ctx_block {
714 my ($linenr, $remain) = @_;
715
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700716 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
717 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700718}
719sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700720 my ($linenr, $remain, $off) = @_;
721
722 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
723 return @r;
724}
725sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700726 my ($linenr, $remain) = @_;
727
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700728 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700729}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700730sub ctx_statement_level {
731 my ($linenr, $remain, $off) = @_;
732
733 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
734}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700735
736sub ctx_locate_comment {
737 my ($first_line, $end_line) = @_;
738
739 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700740 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700741 return $current_comment if (defined $current_comment);
742
743 # Look through the context and try and figure out if there is a
744 # comment.
745 my $in_comment = 0;
746 $current_comment = '';
747 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700748 my $line = $rawlines[$linenr - 1];
749 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700750 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
751 $in_comment = 1;
752 }
753 if ($line =~ m@/\*@) {
754 $in_comment = 1;
755 }
756 if (!$in_comment && $current_comment ne '') {
757 $current_comment = '';
758 }
759 $current_comment .= $line . "\n" if ($in_comment);
760 if ($line =~ m@\*/@) {
761 $in_comment = 0;
762 }
763 }
764
765 chomp($current_comment);
766 return($current_comment);
767}
768sub ctx_has_comment {
769 my ($first_line, $end_line) = @_;
770 my $cmt = ctx_locate_comment($first_line, $end_line);
771
Andy Whitcroft00df3442007-06-08 13:47:06 -0700772 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700773 ##print "CMMT: $cmt\n";
774
775 return ($cmt ne '');
776}
777
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700778sub raw_line {
779 my ($linenr, $cnt) = @_;
780
781 my $offset = $linenr - 1;
782 $cnt++;
783
784 my $line;
785 while ($cnt) {
786 $line = $rawlines[$offset++];
787 next if (defined($line) && $line =~ /^-/);
788 $cnt--;
789 }
790
791 return $line;
792}
793
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700794sub cat_vet {
795 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700796 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700797
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700798 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700799 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
800 $res .= $1;
801 if ($2 ne '') {
802 $coded = sprintf("^%c", unpack('C', $2) + 64);
803 $res .= $coded;
804 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700805 }
806 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700807
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700808 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700809}
810
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800811my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800812my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800813my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700814my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800815
816sub annotate_reset {
817 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800818 $av_pending = '_';
819 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700820 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800821}
822
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700823sub annotate_values {
824 my ($stream, $type) = @_;
825
826 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700827 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700828 my $cur = $stream;
829
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800830 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700832 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700833 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800834 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700835 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700836 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800837 print "WS($1)\n" if ($dbg_values > 1);
838 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800839 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800840 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700841 }
842
Andy Whitcroft8ea3eb92008-07-23 21:29:08 -0700843 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800844 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700845 $type = 'T';
846
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700847 } elsif ($cur =~ /^($Modifier)\s*/) {
848 print "MODIFIER($1)\n" if ($dbg_values > 1);
849 $type = 'T';
850
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700851 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700852 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800853 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700854 push(@av_paren_type, $type);
855 if ($2 ne '') {
856 $av_pending = 'N';
857 }
858 $type = 'E';
859
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700860 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700861 print "UNDEF($1)\n" if ($dbg_values > 1);
862 $av_preprocessor = 1;
863 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700864
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700865 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800866 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800867 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800868
869 push(@av_paren_type, $type);
870 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700871 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800872
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700873 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800874 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
875 $av_preprocessor = 1;
876
877 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
878
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700879 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800880
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700881 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800882 print "PRE_END($1)\n" if ($dbg_values > 1);
883
884 $av_preprocessor = 1;
885
886 # Assume all arms of the conditional end as this
887 # one does, and continue as if the #endif was not here.
888 pop(@av_paren_type);
889 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700890 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700891
892 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800893 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700894
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700895 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
896 print "ATTR($1)\n" if ($dbg_values > 1);
897 $av_pending = $type;
898 $type = 'N';
899
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700900 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800901 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700902 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800903 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700904 }
905 $type = 'N';
906
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700907 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800908 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700909 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700910 $type = 'N';
911
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700912 } elsif ($cur =~/^(case)/o) {
913 print "CASE($1)\n" if ($dbg_values > 1);
914 $av_pend_colon = 'C';
915 $type = 'N';
916
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700917 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800918 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700919 $type = 'N';
920
921 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800922 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800923 push(@av_paren_type, $av_pending);
924 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700925 $type = 'N';
926
927 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800928 my $new_type = pop(@av_paren_type);
929 if ($new_type ne '_') {
930 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800931 print "PAREN('$1') -> $type\n"
932 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700933 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800934 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700935 }
936
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700937 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800938 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700939 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800940 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700941
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800942 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
943 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700944 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800945 } elsif ($type eq 'E') {
946 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700947 }
948 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
949 $type = 'V';
950
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700951 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800952 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700953 $type = 'V';
954
955 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800956 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700957 $type = 'N';
958
Andy Whitcroftcf655042008-03-04 14:28:20 -0800959 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800960 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800961 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700962 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800963
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800964 } elsif ($cur =~/^(,)/) {
965 print "COMMA($1)\n" if ($dbg_values > 1);
966 $type = 'C';
967
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700968 } elsif ($cur =~ /^(\?)/o) {
969 print "QUESTION($1)\n" if ($dbg_values > 1);
970 $type = 'N';
971
972 } elsif ($cur =~ /^(:)/o) {
973 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
974
975 substr($var, length($res), 1, $av_pend_colon);
976 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
977 $type = 'E';
978 } else {
979 $type = 'N';
980 }
981 $av_pend_colon = 'O';
982
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800983 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800984 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700985 $type = 'N';
986
Andy Whitcroft0d413862008-10-15 22:02:16 -0700987 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700988 my $variant;
989
990 print "OPV($1)\n" if ($dbg_values > 1);
991 if ($type eq 'V') {
992 $variant = 'B';
993 } else {
994 $variant = 'U';
995 }
996
997 substr($var, length($res), 1, $variant);
998 $type = 'N';
999
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001001 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001002 if ($1 ne '++' && $1 ne '--') {
1003 $type = 'N';
1004 }
1005
1006 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001007 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001008 }
1009 if (defined $1) {
1010 $cur = substr($cur, length($1));
1011 $res .= $type x length($1);
1012 }
1013 }
1014
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001015 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001016}
1017
Andy Whitcroft8905a672007-11-28 16:21:06 -08001018sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001019 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001020 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001021 ^(?:
1022 $Modifier|
1023 $Storage|
1024 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001025 DEFINE_\S+
1026 )$|
1027 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001028 goto|
1029 return|
1030 case|
1031 else|
1032 asm|__asm__|
1033 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001034 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001035 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001036 )}x;
1037 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1038 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001039 # Check for modifiers.
1040 $possible =~ s/\s*$Storage\s*//g;
1041 $possible =~ s/\s*$Sparse\s*//g;
1042 if ($possible =~ /^\s*$/) {
1043
1044 } elsif ($possible =~ /\s/) {
1045 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001046 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001047 if ($modifier !~ $notPermitted) {
1048 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1049 push(@modifierList, $modifier);
1050 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001051 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001052
1053 } else {
1054 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1055 push(@typeList, $possible);
1056 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001057 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001058 } else {
1059 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001060 }
1061}
1062
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001063my $prefix = '';
1064
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001065sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001066 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1067 return 0;
1068 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001069 my $line = $prefix . $_[0];
1070
1071 $line = (split('\n', $line))[0] . "\n" if ($terse);
1072
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001073 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001074
1075 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001076}
1077sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001078 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001079}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001080sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 if (report("ERROR: $_[0]\n")) {
1082 our $clean = 0;
1083 our $cnt_error++;
1084 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001085}
1086sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001087 if (report("WARNING: $_[0]\n")) {
1088 our $clean = 0;
1089 our $cnt_warn++;
1090 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001091}
1092sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001093 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001094 our $clean = 0;
1095 our $cnt_chk++;
1096 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001097}
1098
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001099sub check_absolute_file {
1100 my ($absolute, $herecurr) = @_;
1101 my $file = $absolute;
1102
1103 ##print "absolute<$absolute>\n";
1104
1105 # See if any suffix of this path is a path within the tree.
1106 while ($file =~ s@^[^/]*/@@) {
1107 if (-f "$root/$file") {
1108 ##print "file<$file>\n";
1109 last;
1110 }
1111 }
1112 if (! -f _) {
1113 return 0;
1114 }
1115
1116 # It is, so see if the prefix is acceptable.
1117 my $prefix = $absolute;
1118 substr($prefix, -length($file)) = '';
1119
1120 ##print "prefix<$prefix>\n";
1121 if ($prefix ne ".../") {
1122 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1123 }
1124}
1125
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001126sub process {
1127 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001128
1129 my $linenr=0;
1130 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001131 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001132 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001133 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001134
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001135 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001136 my $indent;
1137 my $previndent=0;
1138 my $stashindent=0;
1139
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001140 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001141 my $signoff = 0;
1142 my $is_patch = 0;
1143
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001144 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001145 our $cnt_lines = 0;
1146 our $cnt_error = 0;
1147 our $cnt_warn = 0;
1148 our $cnt_chk = 0;
1149
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001150 # Trace the real file/line as we go.
1151 my $realfile = '';
1152 my $realline = 0;
1153 my $realcnt = 0;
1154 my $here = '';
1155 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001156 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001157 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001158 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001159
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001160 my $prev_values = 'E';
1161
1162 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001163 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001164 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001165 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001166
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001167 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001168 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001170 my @setup_docs = ();
1171 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001172
1173 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001174 my $line;
1175 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001176 $linenr++;
1177 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001178
Andy Whitcroft773647a2008-03-28 14:15:58 -07001179 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001180 $setup_docs = 0;
1181 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1182 $setup_docs = 1;
1183 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001184 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001185 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001186 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1187 $realline=$1-1;
1188 if (defined $2) {
1189 $realcnt=$3+1;
1190 } else {
1191 $realcnt=1+1;
1192 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001193 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001194
1195 # Guestimate if this is a continuing comment. Run
1196 # the context looking for a comment "edge". If this
1197 # edge is a close comment then we must be in a comment
1198 # at context start.
1199 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001200 my $cnt = $realcnt;
1201 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1202 next if (defined $rawlines[$ln - 1] &&
1203 $rawlines[$ln - 1] =~ /^-/);
1204 $cnt--;
1205 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001206 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001207 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1208 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1209 ($edge) = $1;
1210 last;
1211 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001212 }
1213 if (defined $edge && $edge eq '*/') {
1214 $in_comment = 1;
1215 }
1216
1217 # Guestimate if this is a continuing comment. If this
1218 # is the start of a diff block and this line starts
1219 # ' *' then it is very likely a comment.
1220 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001221 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001222 {
1223 $in_comment = 1;
1224 }
1225
1226 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1227 sanitise_line_reset($in_comment);
1228
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001229 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001230 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001231 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232 $line = sanitise_line($rawline);
1233 }
1234 push(@lines, $line);
1235
1236 if ($realcnt > 1) {
1237 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1238 } else {
1239 $realcnt = 0;
1240 }
1241
1242 #print "==>$rawline\n";
1243 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001244
1245 if ($setup_docs && $line =~ /^\+/) {
1246 push(@setup_docs, $line);
1247 }
1248 }
1249
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001250 $prefix = '';
1251
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 $realcnt = 0;
1253 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001254 foreach my $line (@lines) {
1255 $linenr++;
1256
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001257 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001258
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001259#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001260 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001261 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001262 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001263 $realline=$1-1;
1264 if (defined $2) {
1265 $realcnt=$3+1;
1266 } else {
1267 $realcnt=1+1;
1268 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001269 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001270 $prev_values = 'E';
1271
Andy Whitcroft773647a2008-03-28 14:15:58 -07001272 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001273 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001274 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001275 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001276
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001277# track the line number as we move through the hunk, note that
1278# new versions of GNU diff omit the leading space on completely
1279# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001281 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001282 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001283
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001284 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001285 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001286
1287 # Track the previous line.
1288 ($prevline, $stashline) = ($stashline, $line);
1289 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001290 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1291
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001293
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001294 } elsif ($realcnt == 1) {
1295 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001296 }
1297
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001298 my $hunk_line = ($realcnt != 0);
1299
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001300#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001301 $prefix = "$filename:$realline: " if ($emacs && $file);
1302 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1303
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304 $here = "#$linenr: " if (!$file);
1305 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306
1307 # extract the filename as it passes
1308 if ($line=~/^\+\+\+\s+(\S+)/) {
1309 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001310 $realfile =~ s@^([^/]*)/@@;
1311
1312 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001313 if (!$file && $tree && $p1_prefix ne '' &&
1314 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001315 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1316 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001317
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001318 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1320 }
1321 next;
1322 }
1323
Randy Dunlap389834b2007-06-08 13:47:03 -07001324 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001325
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326 my $hereline = "$here\n$rawline\n";
1327 my $herecurr = "$here\n$rawline\n";
1328 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001329
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001330 $cnt_lines++ if ($realcnt != 0);
1331
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001332#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001333 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001334 # This is a signoff, if ugly, so do not double report.
1335 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001336 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001337 WARN("Signed-off-by: is the preferred form\n" .
1338 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001339 }
1340 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001342 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001343 }
1344 }
1345
Andy Whitcroft00df3442007-06-08 13:47:06 -07001346# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001347 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001348 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001349 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001350 }
1351
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001352# Check for absolute kernel paths.
1353 if ($tree) {
1354 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1355 my $file = $1;
1356
1357 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1358 check_absolute_file($1, $herecurr)) {
1359 #
1360 } else {
1361 check_absolute_file($file, $herecurr);
1362 }
1363 }
1364 }
1365
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001366# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1367 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001368 $rawline !~ m/^$UTF8*$/) {
1369 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1370
1371 my $blank = copy_spacing($rawline);
1372 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1373 my $hereptr = "$hereline$ptr\n";
1374
1375 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001376 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001377
Andy Whitcroft306708542008-10-15 22:02:28 -07001378# ignore non-hunk lines and lines being removed
1379 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001380
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001381#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001382 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001383 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001384 ERROR("DOS line endings\n" . $herevet);
1385
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001386 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1387 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001388 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001389 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001390
Andi Kleen33549572010-05-24 14:33:29 -07001391# check for Kconfig help text having a real description
1392 if ($realfile =~ /Kconfig/ &&
1393 $line =~ /\+?\s*(---)?help(---)?$/) {
1394 my $length = 0;
1395 for (my $l = $linenr; defined($lines[$l]); $l++) {
1396 my $f = $lines[$l];
1397 $f =~ s/#.*//;
1398 $f =~ s/^\s+//;
1399 next if ($f =~ /^$/);
1400 last if ($f =~ /^\s*config\s/);
1401 $length++;
1402 }
1403 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1404 }
1405
Andy Whitcroft5368df22008-10-15 22:02:27 -07001406# check we are in a valid source file if not then ignore this hunk
1407 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1408
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001409#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001410 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001411 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches8bbea962010-08-09 17:21:01 -07001412 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1413 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001414 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001415 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001416 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001417 }
1418
Joe Perches5e79d962010-03-05 13:43:55 -08001419# check for spaces before a quoted newline
1420 if ($rawline =~ /^.*\".*\s\\n/) {
1421 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1422 }
1423
Andy Whitcroft8905a672007-11-28 16:21:06 -08001424# check for adding lines without a newline.
1425 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1426 WARN("adding a line without newline at end of file\n" . $herecurr);
1427 }
1428
Mike Frysinger42e41c52009-09-21 17:04:40 -07001429# Blackfin: use hi/lo macros
1430 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1431 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1432 my $herevet = "$here\n" . cat_vet($line) . "\n";
1433 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1434 }
1435 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1436 my $herevet = "$here\n" . cat_vet($line) . "\n";
1437 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1438 }
1439 }
1440
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001441# check we are in a valid source file C or perl if not then ignore this hunk
1442 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001443
1444# at the beginning of a line any tabs must come first and anything
1445# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001446 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1447 $rawline =~ /^\+\s* \s*/) {
1448 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001449 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001450 }
1451
Alberto Panizzo08e44362010-03-05 13:43:54 -08001452# check for space before tabs.
1453 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1454 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1455 WARN("please, no space before tabs\n" . $herevet);
1456 }
1457
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001458# check for spaces at the beginning of a line.
1459 if ($rawline =~ /^\+ / && $rawline !~ /\+ +\*/) {
1460 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1461 WARN("please, no space for starting a line, \
1462 excluding comments\n" . $herevet);
1463 }
1464
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001465# check we are in a valid C source file if not then ignore this hunk
1466 next if ($realfile !~ /\.(h|c)$/);
1467
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001468# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001469 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1471 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001472
Mike Frysinger42e41c52009-09-21 17:04:40 -07001473# Blackfin: don't use __builtin_bfin_[cs]sync
1474 if ($line =~ /__builtin_bfin_csync/) {
1475 my $herevet = "$here\n" . cat_vet($line) . "\n";
1476 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1477 }
1478 if ($line =~ /__builtin_bfin_ssync/) {
1479 my $herevet = "$here\n" . cat_vet($line) . "\n";
1480 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1481 }
1482
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001483# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001484 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1485 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001486 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001487 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001488 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001489 $stat =~ s/\n./\n /g;
1490 $cond =~ s/\n./\n /g;
1491
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001492 # Find the real next line.
1493 $realline_next = $line_nr_next;
1494 if (defined $realline_next &&
1495 (!defined $lines[$realline_next - 1] ||
1496 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1497 $realline_next++;
1498 }
1499
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001500 my $s = $stat;
1501 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001502
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001503 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001504 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001505
1506 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001507 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001508
Andy Whitcroft463f2862009-09-21 17:04:34 -07001509 } elsif ($s =~ /^.\s*else\b/s) {
1510
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001511 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001512 } 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 -07001513 my $type = $1;
1514 $type =~ s/\s+/ /g;
1515 possible($type, "A:" . $s);
1516
Andy Whitcroft8905a672007-11-28 16:21:06 -08001517 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001518 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001519 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001520 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001521
1522 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001523 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001524 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001525 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001526
1527 # Check for any sort of function declaration.
1528 # int foo(something bar, other baz);
1529 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001530 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 -08001531 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001532
Andy Whitcroftcf655042008-03-04 14:28:20 -08001533 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001534 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001535 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001536
Andy Whitcroft8905a672007-11-28 16:21:06 -08001537 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001538 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001539
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001540 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001541 }
1542 }
1543 }
1544
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001545 }
1546
Andy Whitcroft653d4872007-06-23 17:16:34 -07001547#
1548# Checks which may be anchored in the context.
1549#
1550
1551# Check for switch () and associated case and default
1552# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001553 if ($line=~/\bswitch\s*\(.*\)/) {
1554 my $err = '';
1555 my $sep = '';
1556 my @ctx = ctx_block_outer($linenr, $realcnt);
1557 shift(@ctx);
1558 for my $ctx (@ctx) {
1559 my ($clen, $cindent) = line_stats($ctx);
1560 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1561 $indent != $cindent) {
1562 $err .= "$sep$ctx\n";
1563 $sep = '';
1564 } else {
1565 $sep = "[...]\n";
1566 }
1567 }
1568 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001569 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001570 }
1571 }
1572
1573# if/while/etc brace do not go on next line, unless defining a do while loop,
1574# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001575 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001576 my $pre_ctx = "$1$2";
1577
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001578 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001579 my $ctx_cnt = $realcnt - $#ctx - 1;
1580 my $ctx = join("\n", @ctx);
1581
Andy Whitcroft548596d2008-07-23 21:29:01 -07001582 my $ctx_ln = $linenr;
1583 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001584
Andy Whitcroft548596d2008-07-23 21:29:01 -07001585 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1586 defined $lines[$ctx_ln - 1] &&
1587 $lines[$ctx_ln - 1] =~ /^-/)) {
1588 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1589 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001590 $ctx_ln++;
1591 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001592
Andy Whitcroft53210162008-07-23 21:29:03 -07001593 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1594 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001595
1596 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1597 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001598 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001599 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001600 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1601 $ctx =~ /\)\s*\;\s*$/ &&
1602 defined $lines[$ctx_ln - 1])
1603 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001604 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1605 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001606 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001607 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001608 }
1609 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001610 }
1611
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001612# Check relative indent for conditionals and blocks.
1613 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1614 my ($s, $c) = ($stat, $cond);
1615
1616 substr($s, 0, length($c), '');
1617
1618 # Make sure we remove the line prefixes as we have
1619 # none on the first line, and are going to readd them
1620 # where necessary.
1621 $s =~ s/\n./\n/gs;
1622
1623 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001624 my @newlines = ($c =~ /\n/gs);
1625 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001626
1627 # We want to check the first line inside the block
1628 # starting at the end of the conditional, so remove:
1629 # 1) any blank line termination
1630 # 2) any opening brace { on end of the line
1631 # 3) any do (...) {
1632 my $continuation = 0;
1633 my $check = 0;
1634 $s =~ s/^.*\bdo\b//;
1635 $s =~ s/^\s*{//;
1636 if ($s =~ s/^\s*\\//) {
1637 $continuation = 1;
1638 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001639 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001640 $check = 1;
1641 $cond_lines++;
1642 }
1643
1644 # Also ignore a loop construct at the end of a
1645 # preprocessor statement.
1646 if (($prevline =~ /^.\s*#\s*define\s/ ||
1647 $prevline =~ /\\\s*$/) && $continuation == 0) {
1648 $check = 0;
1649 }
1650
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001651 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001652 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001653 while ($cond_ptr != $cond_lines) {
1654 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001655
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001656 # If we see an #else/#elif then the code
1657 # is not linear.
1658 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1659 $check = 0;
1660 }
1661
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001662 # Ignore:
1663 # 1) blank lines, they should be at 0,
1664 # 2) preprocessor lines, and
1665 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001666 if ($continuation ||
1667 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001668 $s =~ /^\s*#\s*?/ ||
1669 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001670 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001671 if ($s =~ s/^.*?\n//) {
1672 $cond_lines++;
1673 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001674 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001675 }
1676
1677 my (undef, $sindent) = line_stats("+" . $s);
1678 my $stat_real = raw_line($linenr, $cond_lines);
1679
1680 # Check if either of these lines are modified, else
1681 # this is not this patch's fault.
1682 if (!defined($stat_real) ||
1683 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1684 $check = 0;
1685 }
1686 if (defined($stat_real) && $cond_lines > 1) {
1687 $stat_real = "[...]\n$stat_real";
1688 }
1689
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001690 #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 -07001691
1692 if ($check && (($sindent % 8) != 0 ||
1693 ($sindent <= $indent && $s ne ''))) {
1694 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1695 }
1696 }
1697
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001698 # Track the 'values' across context and added lines.
1699 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001700 my ($curr_values, $curr_vars) =
1701 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001702 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001703 if ($dbg_values) {
1704 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001705 print "$linenr > .$outline\n";
1706 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001707 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001708 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 $prev_values = substr($curr_values, -1);
1710
Andy Whitcroft00df3442007-06-08 13:47:06 -07001711#ignore lines not being added
1712 if ($line=~/^[^\+]/) {next;}
1713
Andy Whitcroft653d4872007-06-23 17:16:34 -07001714# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001715 if ($dbg_type) {
1716 if ($line =~ /^.\s*$Declare\s*$/) {
1717 ERROR("TEST: is type\n" . $herecurr);
1718 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1719 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1720 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001721 next;
1722 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001723# TEST: allow direct testing of the attribute matcher.
1724 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001725 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001726 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001727 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001728 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1729 }
1730 next;
1731 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001732
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001733# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001734 if ($line =~ /^.\s*{/ &&
1735 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001736 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001737 }
1738
Andy Whitcroft653d4872007-06-23 17:16:34 -07001739#
1740# Checks which are anchored on the added line.
1741#
1742
1743# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001744 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001745 my $path = $1;
1746 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001747 ERROR("malformed #include filename\n" .
1748 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001749 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001750 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001751
1752# no C99 // comments
1753 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001754 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001755 }
1756 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001757 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001758 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001759
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001760# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1761# the whole statement.
1762#print "APW <$lines[$realline_next - 1]>\n";
1763 if (defined $realline_next &&
1764 exists $lines[$realline_next - 1] &&
1765 !defined $suppress_export{$realline_next} &&
1766 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1767 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001768 my $name = $1;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001769 if ($stat !~ /(?:
1770 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001771 ^.DEFINE_$Ident\(\Q$name\E\)|
1772 ^.DECLARE_$Ident\(\Q$name\E\)|
1773 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001774 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1775 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001776 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001777#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1778 $suppress_export{$realline_next} = 2;
1779 } else {
1780 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001781 }
1782 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001783 if (!defined $suppress_export{$linenr} &&
1784 $prevline =~ /^.\s*$/ &&
1785 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1786 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1787#print "FOO B <$lines[$linenr - 1]>\n";
1788 $suppress_export{$linenr} = 2;
1789 }
1790 if (defined $suppress_export{$linenr} &&
1791 $suppress_export{$linenr} == 2) {
1792 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1793 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001794
Joe Eloff5150bda2010-08-09 17:21:00 -07001795# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001796 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001797 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001798 $herecurr);
1799 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001800# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001801 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001802 ERROR("do not initialise statics to 0 or NULL\n" .
1803 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001804 }
1805
Andy Whitcroft653d4872007-06-23 17:16:34 -07001806# check for new typedefs, only function parameters and sparse annotations
1807# make sense.
1808 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001809 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001810 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001811 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001812 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001813 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001814 }
1815
1816# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001817 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001818 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001819 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001820
Andy Whitcroft65863862009-01-06 14:41:21 -08001821 # Should start with a space.
1822 $to =~ s/^(\S)/ $1/;
1823 # Should not end with a space.
1824 $to =~ s/\s+$//;
1825 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001826 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001827 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001828
Andy Whitcroft65863862009-01-06 14:41:21 -08001829 #print "from<$from> to<$to>\n";
1830 if ($from ne $to) {
1831 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1832 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001833 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001834 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001835
Andy Whitcroft65863862009-01-06 14:41:21 -08001836 # Should start with a space.
1837 $to =~ s/^(\S)/ $1/;
1838 # Should not end with a space.
1839 $to =~ s/\s+$//;
1840 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001841 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001842 }
1843 # Modifiers should have spaces.
1844 $to =~ s/(\b$Modifier$)/$1 /;
1845
Andy Whitcroft667026e2009-02-27 14:03:08 -08001846 #print "from<$from> to<$to> ident<$ident>\n";
1847 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001848 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1849 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001850 }
1851
1852# # no BUG() or BUG_ON()
1853# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1854# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1855# print "$herecurr";
1856# $clean = 0;
1857# }
1858
Andy Whitcroft8905a672007-11-28 16:21:06 -08001859 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 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 -08001861 }
1862
Andy Whitcroft00df3442007-06-08 13:47:06 -07001863# printk should use KERN_* levels. Note that follow on printk's on the
1864# same line do not need a level, so we use the current block context
1865# to try and find and validate the current printk. In summary the current
1866# printk includes all preceeding printk's which have no newline on the end.
1867# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001868 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001869 my $ok = 0;
1870 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1871 #print "CHECK<$lines[$ln - 1]\n";
1872 # we have a preceeding printk if it ends
1873 # with "\n" ignore it, else it is to blame
1874 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1875 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1876 $ok = 1;
1877 }
1878 last;
1879 }
1880 }
1881 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001882 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001883 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001884 }
1885
Andy Whitcroft653d4872007-06-23 17:16:34 -07001886# function brace can't be on same line, except for #defines of do while,
1887# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001888 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1889 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001890 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001891 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001892
Andy Whitcroft8905a672007-11-28 16:21:06 -08001893# open braces for enum, union and struct go on the same line.
1894 if ($line =~ /^.\s*{/ &&
1895 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1896 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1897 }
1898
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001899# check for spacing round square brackets; allowed:
1900# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001901# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1902# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001903 while ($line =~ /(.*?\s)\[/g) {
1904 my ($where, $prefix) = ($-[1], $1);
1905 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001906 ($where != 0 || $prefix !~ /^.\s+$/) &&
1907 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001908 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1909 }
1910 }
1911
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001912# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001913 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001914 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001915 my $ctx_before = substr($line, 0, $-[1]);
1916 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917
1918 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001919 if ($name =~ /^(?:
1920 if|for|while|switch|return|case|
1921 volatile|__volatile__|
1922 __attribute__|format|__extension__|
1923 asm|__asm__)$/x)
1924 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001925
1926 # cpp #define statements have non-optional spaces, ie
1927 # if there is a space between the name and the open
1928 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001929 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001930
1931 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001932 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933
1934 # If this whole things ends with a type its most
1935 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001936 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001937
1938 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001939 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001940 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001941 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001942# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001943 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001944 my $ops = qr{
1945 <<=|>>=|<=|>=|==|!=|
1946 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1947 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001948 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1949 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001950 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001951 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001952 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001953
1954 my $blank = copy_spacing($opline);
1955
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001956 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001957 $off += length($elements[$n]);
1958
Andy Whitcroft773647a2008-03-28 14:15:58 -07001959 # Pick up the preceeding and succeeding characters.
1960 my $ca = substr($opline, 0, $off);
1961 my $cc = '';
1962 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1963 $cc = substr($opline, $off + length($elements[$n + 1]));
1964 }
1965 my $cb = "$ca$;$cc";
1966
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001967 my $a = '';
1968 $a = 'V' if ($elements[$n] ne '');
1969 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001970 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001971 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1972 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001973 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001974
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001975 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001976
1977 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001978 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001979 $c = 'V' if ($elements[$n + 2] ne '');
1980 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001981 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001982 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1983 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08001984 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001985 } else {
1986 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001987 }
1988
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001989 my $ctx = "${a}x${c}";
1990
1991 my $at = "(ctx:$ctx)";
1992
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001993 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001994 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001995
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001996 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001997 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001998
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001999 # Get the full operator variant.
2000 my $opv = $op . substr($curr_vars, $off, 1);
2001
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002002 # Ignore operators passed as parameters.
2003 if ($op_type ne 'V' &&
2004 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2005
Andy Whitcroftcf655042008-03-04 14:28:20 -08002006# # Ignore comments
2007# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002008
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002009 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002010 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002011 if ($ctx !~ /.x[WEBC]/ &&
2012 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002013 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002014 }
2015
2016 # // is a comment
2017 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002018
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002019 # No spaces for:
2020 # ->
2021 # : when part of a bitfield
2022 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002023 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002024 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002025 }
2026
2027 # , must have a space on the right.
2028 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002029 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002031 }
2032
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002033 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002034 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002035 #warn "'*' is part of type\n";
2036
2037 # unary operators should have a space before and
2038 # none after. May be left adjacent to another
2039 # unary operator, or a cast
2040 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002041 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002042 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002043 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002044 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002045 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002046 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002047 # A unary '*' may be const
2048
2049 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002050 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002051 }
2052
2053 # unary ++ and unary -- are allowed no space on one side.
2054 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002055 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2056 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002057 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002058 if ($ctx =~ /Wx[BE]/ ||
2059 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2060 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002061 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002062 if ($ctx =~ /ExW/) {
2063 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2064 }
2065
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002066
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002067 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002068 } elsif ($op eq '<<' or $op eq '>>' or
2069 $op eq '&' or $op eq '^' or $op eq '|' or
2070 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002071 $op eq '*' or $op eq '/' or
2072 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002073 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002074 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002075 ERROR("need consistent spacing around '$op' $at\n" .
2076 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002077 }
2078
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002079 # A colon needs no spaces before when it is
2080 # terminating a case value or a label.
2081 } elsif ($opv eq ':C' || $opv eq ':L') {
2082 if ($ctx =~ /Wx./) {
2083 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2084 }
2085
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002086 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002087 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002088 my $ok = 0;
2089
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002090 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002091 if (($op eq '<' &&
2092 $cc =~ /^\S+\@\S+>/) ||
2093 ($op eq '>' &&
2094 $ca =~ /<\S+\@\S+$/))
2095 {
2096 $ok = 1;
2097 }
2098
2099 # Ignore ?:
2100 if (($opv eq ':O' && $ca =~ /\?$/) ||
2101 ($op eq '?' && $cc =~ /^:/)) {
2102 $ok = 1;
2103 }
2104
2105 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002106 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002107 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002108 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002109 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002110 }
2111 }
2112
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002113# check for multiple assignments
2114 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002115 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002116 }
2117
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002118## # check for multiple declarations, allowing for a function declaration
2119## # continuation.
2120## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2121## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2122##
2123## # Remove any bracketed sections to ensure we do not
2124## # falsly report the parameters of functions.
2125## my $ln = $line;
2126## while ($ln =~ s/\([^\(\)]*\)//g) {
2127## }
2128## if ($ln =~ /,/) {
2129## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2130## }
2131## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002132
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002133#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002134 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2135 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002136 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002137 }
2138
2139# closing brace should have a space following it when it has anything
2140# on the line
2141 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002142 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002143 }
2144
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002145# check spacing on square brackets
2146 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002147 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002148 }
2149 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002150 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002151 }
2152
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002153# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002154 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2155 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002157 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002158 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002159 $line !~ /for\s*\(.*;\s+\)/ &&
2160 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002161 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002162 }
2163
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002164#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002165 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002167 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002168 }
2169
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002170# Return is not a function.
2171 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2172 my $spacing = $1;
2173 my $value = $2;
2174
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002175 # Flatten any parentheses
Andy Whitcroftfee61c42008-07-23 21:28:56 -07002176 $value =~ s/\)\(/\) \(/g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002177 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2178 $value !~ /(?:$Ident|-?$Constant)\s*
2179 $Compare\s*
2180 (?:$Ident|-?$Constant)/x &&
2181 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002182 }
2183
2184 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2185 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2186
2187 } elsif ($spacing !~ /\s+/) {
2188 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2189 }
2190 }
2191
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002192# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002193 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002194 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002195 }
2196
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002197# Check for illegal assignment in if conditional -- and check for trailing
2198# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002199 if ($line =~ /do\s*(?!{)/) {
2200 my ($stat_next) = ctx_statement_block($line_nr_next,
2201 $remain_next, $off_next);
2202 $stat_next =~ s/\n./\n /g;
2203 ##print "stat<$stat> stat_next<$stat_next>\n";
2204
2205 if ($stat_next =~ /^\s*while\b/) {
2206 # If the statement carries leading newlines,
2207 # then count those as offsets.
2208 my ($whitespace) =
2209 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2210 my $offset =
2211 statement_rawlines($whitespace) - 1;
2212
2213 $suppress_whiletrailers{$line_nr_next +
2214 $offset} = 1;
2215 }
2216 }
2217 if (!defined $suppress_whiletrailers{$linenr} &&
2218 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002219 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002220
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002221 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002222 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002223 }
2224
2225 # Find out what is on the end of the line after the
2226 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002227 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002228 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002229 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002230 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2231 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002232 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002233 # Find out how long the conditional actually is.
2234 my @newlines = ($c =~ /\n/gs);
2235 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002236 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002237
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002238 $stat_real = raw_line($linenr, $cond_lines)
2239 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002240 if (defined($stat_real) && $cond_lines > 1) {
2241 $stat_real = "[...]\n$stat_real";
2242 }
2243
2244 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002245 }
2246 }
2247
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002248# Check for bitwise tests written as boolean
2249 if ($line =~ /
2250 (?:
2251 (?:\[|\(|\&\&|\|\|)
2252 \s*0[xX][0-9]+\s*
2253 (?:\&\&|\|\|)
2254 |
2255 (?:\&\&|\|\|)
2256 \s*0[xX][0-9]+\s*
2257 (?:\&\&|\|\||\)|\])
2258 )/x)
2259 {
2260 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2261 }
2262
Andy Whitcroft8905a672007-11-28 16:21:06 -08002263# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002264 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2265 my $s = $1;
2266 $s =~ s/$;//g; # Remove any comments
2267 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2268 ERROR("trailing statements should be on next line\n" . $herecurr);
2269 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002270 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002271# if should not continue a brace
2272 if ($line =~ /}\s*if\b/) {
2273 ERROR("trailing statements should be on next line\n" .
2274 $herecurr);
2275 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002276# case and default should not have general statements after them
2277 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2278 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002279 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002280 \s*return\s+
2281 )/xg)
2282 {
2283 ERROR("trailing statements should be on next line\n" . $herecurr);
2284 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002285
2286 # Check for }<nl>else {, these must be at the same
2287 # indent level to be relevant to each other.
2288 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2289 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002290 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002291 }
2292
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002293 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2294 $previndent == $indent) {
2295 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2296
2297 # Find out what is on the end of the line after the
2298 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002299 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002300 $s =~ s/\n.*//g;
2301
2302 if ($s =~ /^\s*;/) {
2303 ERROR("while should follow close brace '}'\n" . $hereprev);
2304 }
2305 }
2306
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002307#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2308# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2309# print "No studly caps, use _\n";
2310# print "$herecurr";
2311# $clean = 0;
2312# }
2313
2314#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002315 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002316 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002317 }
2318
Andy Whitcroft653d4872007-06-23 17:16:34 -07002319#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002320 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002321 my $file = "$1.h";
2322 my $checkfile = "include/linux/$file";
2323 if (-f "$root/$checkfile" &&
2324 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002325 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002326 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002327 if ($realfile =~ m{^arch/}) {
2328 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2329 } else {
2330 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2331 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002332 }
2333 }
2334
Andy Whitcroft653d4872007-06-23 17:16:34 -07002335# multi-statement macros should be enclosed in a do while loop, grab the
2336# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002337# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002338 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2339 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002340 my $ln = $linenr;
2341 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002342 my ($off, $dstat, $dcond, $rest);
2343 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002344
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002345 my $args = defined($1);
2346
2347 # Find the end of the macro and limit our statement
2348 # search to that.
2349 while ($cnt > 0 && defined $lines[$ln - 1] &&
2350 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2351 {
2352 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002353 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002354 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002355 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002356 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002357
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002358 ($dstat, $dcond, $ln, $cnt, $off) =
2359 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2360 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002361 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002362
2363 # Extract the remainder of the define (if any) and
2364 # rip off surrounding spaces, and trailing \'s.
2365 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002366 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2367 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002368 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2369 $rest .= substr($lines[$ln - 1], $off) . "\n";
2370 $cnt--;
2371 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002372 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002373 $off = 0;
2374 }
2375 $rest =~ s/\\\n.//g;
2376 $rest =~ s/^\s*//s;
2377 $rest =~ s/\s*$//s;
2378
2379 # Clean up the original statement.
2380 if ($args) {
2381 substr($dstat, 0, length($dcond), '');
2382 } else {
2383 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2384 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002385 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002386 $dstat =~ s/\\\n.//g;
2387 $dstat =~ s/^\s*//s;
2388 $dstat =~ s/\s*$//s;
2389
2390 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002391 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2392 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2393 $dstat =~ s/\[[^\{\}]*\]/1/)
2394 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002395 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002396
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002397 my $exceptions = qr{
2398 $Declare|
2399 module_param_named|
2400 MODULE_PARAM_DESC|
2401 DECLARE_PER_CPU|
2402 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002403 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002404 union|
2405 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002406 \.$Ident\s*=\s*|
2407 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002408 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002409 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002410 if ($rest ne '') {
2411 if ($rest !~ /while\s*\(/ &&
2412 $dstat !~ /$exceptions/)
2413 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002414 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 -07002415 }
2416
2417 } elsif ($ctx !~ /;/) {
2418 if ($dstat ne '' &&
2419 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2420 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002421 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002422 $dstat =~ /$Operators/)
2423 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002424 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002425 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002426 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002427 }
2428
Mike Frysinger080ba922009-01-06 14:41:25 -08002429# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2430# all assignments may have only one of the following with an assignment:
2431# .
2432# ALIGN(...)
2433# VMLINUX_SYMBOL(...)
2434 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2435 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2436 }
2437
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002438# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002439 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2440 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002441 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002442 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002443 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2444 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002445 my $allowed = 0;
2446 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002447 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002448 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002449 for my $chunk (@chunks) {
2450 my ($cond, $block) = @{$chunk};
2451
Andy Whitcroft773647a2008-03-28 14:15:58 -07002452 # If the condition carries leading newlines, then count those as offsets.
2453 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2454 my $offset = statement_rawlines($whitespace) - 1;
2455
2456 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2457
2458 # We have looked at and allowed this specific line.
2459 $suppress_ifbraces{$ln + $offset} = 1;
2460
2461 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002462 $ln += statement_rawlines($block) - 1;
2463
Andy Whitcroft773647a2008-03-28 14:15:58 -07002464 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002465
2466 $seen++ if ($block =~ /^\s*{/);
2467
Andy Whitcroftcf655042008-03-04 14:28:20 -08002468 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2469 if (statement_lines($cond) > 1) {
2470 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002471 $allowed = 1;
2472 }
2473 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002474 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002475 $allowed = 1;
2476 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002477 if (statement_block_size($block) > 1) {
2478 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002479 $allowed = 1;
2480 }
2481 }
2482 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002483 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002484 }
2485 }
2486 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002487 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002488 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002489 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002490
Andy Whitcroftcf655042008-03-04 14:28:20 -08002491 # Check the pre-context.
2492 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2493 #print "APW: ALLOWED: pre<$1>\n";
2494 $allowed = 1;
2495 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002496
2497 my ($level, $endln, @chunks) =
2498 ctx_statement_full($linenr, $realcnt, $-[0]);
2499
Andy Whitcroftcf655042008-03-04 14:28:20 -08002500 # Check the condition.
2501 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002502 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002503 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002504 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002505 }
2506 if (statement_lines($cond) > 1) {
2507 #print "APW: ALLOWED: cond<$cond>\n";
2508 $allowed = 1;
2509 }
2510 if ($block =~/\b(?:if|for|while)\b/) {
2511 #print "APW: ALLOWED: block<$block>\n";
2512 $allowed = 1;
2513 }
2514 if (statement_block_size($block) > 1) {
2515 #print "APW: ALLOWED: lines block<$block>\n";
2516 $allowed = 1;
2517 }
2518 # Check the post-context.
2519 if (defined $chunks[1]) {
2520 my ($cond, $block) = @{$chunks[1]};
2521 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002522 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002523 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002524 if ($block =~ /^\s*\{/) {
2525 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2526 $allowed = 1;
2527 }
2528 }
2529 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2530 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002531 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002532
Andy Whitcroftf0556632008-10-15 22:02:23 -07002533 for (my $n = 0; $n < $cnt; $n++) {
2534 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002535 }
2536
2537 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002538 }
2539 }
2540
Andy Whitcroft653d4872007-06-23 17:16:34 -07002541# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002542 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002543 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002544 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002545 }
2546 }
2547
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002548# don't use deprecated functions
2549 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002550 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002551 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002552 }
2553 }
2554
2555# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002556 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2557 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002558 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002559 }
2560
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002561# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2562 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2563 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2564 }
2565
Andy Whitcroft00df3442007-06-08 13:47:06 -07002566# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002567 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002568 CHK("if this code is redundant consider removing it\n" .
2569 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002570 }
2571
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002572# check for needless kfree() checks
2573 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2574 my $expr = $1;
2575 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002576 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002577 }
2578 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002579# check for needless usb_free_urb() checks
2580 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2581 my $expr = $1;
2582 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2583 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2584 }
2585 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002586
Andy Whitcroft00df3442007-06-08 13:47:06 -07002587# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002588# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002589# print "#ifdef in C files should be avoided\n";
2590# print "$herecurr";
2591# $clean = 0;
2592# }
2593
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002594# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002595 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002596 ERROR("exactly one space required after that #$1\n" . $herecurr);
2597 }
2598
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002599# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002600 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2601 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002602 my $which = $1;
2603 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002604 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002605 }
2606 }
2607# check for memory barriers without a comment.
2608 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2609 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002610 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002611 }
2612 }
2613# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002614 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002615 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002616 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002617
Tobias Klauserd4977c72010-05-24 14:33:30 -07002618# Check that the storage class is at the beginning of a declaration
2619 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2620 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2621 }
2622
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002623# check the location of the inline attribute, that it is between
2624# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002625 if ($line =~ /\b$Type\s+$Inline\b/ ||
2626 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002627 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2628 }
2629
Andy Whitcroft8905a672007-11-28 16:21:06 -08002630# Check for __inline__ and __inline, prefer inline
2631 if ($line =~ /\b(__inline__|__inline)\b/) {
2632 WARN("plain inline is preferred over $1\n" . $herecurr);
2633 }
2634
Joe Perches8f53a9b2010-03-05 13:43:48 -08002635# check for sizeof(&)
2636 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2637 WARN("sizeof(& should be avoided\n" . $herecurr);
2638 }
2639
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002640# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002641 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002642 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002643 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002644 my $function_name = $1;
2645 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002646
2647 my $s = $stat;
2648 if (defined $cond) {
2649 substr($s, 0, length($cond), '');
2650 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002651 if ($s =~ /^\s*;/ &&
2652 $function_name ne 'uninitialized_var')
2653 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002654 WARN("externs should be avoided in .c files\n" . $herecurr);
2655 }
2656
2657 if ($paren_space =~ /\n/) {
2658 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2659 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002660
2661 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2662 $stat =~ /^.\s*extern\s+/)
2663 {
2664 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002665 }
2666
2667# checks for new __setup's
2668 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2669 my $name = $1;
2670
2671 if (!grep(/$name/, @setup_docs)) {
2672 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2673 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002674 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002675
2676# check for pointless casting of kmalloc return
2677 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2678 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2679 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002680
2681# check for gcc specific __FUNCTION__
2682 if ($line =~ /__FUNCTION__/) {
2683 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2684 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002685
2686# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002687 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002688 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2689 }
2690# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002691 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002692 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002693
Andy Whitcroft773647a2008-03-28 14:15:58 -07002694 }
2695# recommend strict_strto* over simple_strto*
2696 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2697 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2698 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002699# check for __initcall(), use device_initcall() explicitly please
2700 if ($line =~ /^.\s*__initcall\s*\(/) {
2701 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2702 }
Emese Revfy79404842010-03-05 13:43:53 -08002703# check for various ops structs, ensure they are const.
2704 my $struct_ops = qr{acpi_dock_ops|
2705 address_space_operations|
2706 backlight_ops|
2707 block_device_operations|
2708 dentry_operations|
2709 dev_pm_ops|
2710 dma_map_ops|
2711 extent_io_ops|
2712 file_lock_operations|
2713 file_operations|
2714 hv_ops|
2715 ide_dma_ops|
2716 intel_dvo_dev_ops|
2717 item_operations|
2718 iwl_ops|
2719 kgdb_arch|
2720 kgdb_io|
2721 kset_uevent_ops|
2722 lock_manager_operations|
2723 microcode_ops|
2724 mtrr_ops|
2725 neigh_ops|
2726 nlmsvc_binding|
2727 pci_raw_ops|
2728 pipe_buf_operations|
2729 platform_hibernation_ops|
2730 platform_suspend_ops|
2731 proto_ops|
2732 rpc_pipe_ops|
2733 seq_operations|
2734 snd_ac97_build_ops|
2735 soc_pcmcia_socket_ops|
2736 stacktrace_ops|
2737 sysfs_ops|
2738 tty_operations|
2739 usb_mon_operations|
2740 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002741 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08002742 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002743 WARN("struct $1 should normally be const\n" .
2744 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002745 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002746
2747# use of NR_CPUS is usually wrong
2748# ignore definitions of NR_CPUS and usage to define arrays as likely right
2749 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002750 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2751 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002752 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2753 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2754 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002755 {
2756 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2757 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002758
2759# check for %L{u,d,i} in strings
2760 my $string;
2761 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2762 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002763 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002764 if ($string =~ /(?<!%)%L[udi]/) {
2765 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2766 last;
2767 }
2768 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002769
2770# whine mightly about in_atomic
2771 if ($line =~ /\bin_atomic\s*\(/) {
2772 if ($realfile =~ m@^drivers/@) {
2773 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08002774 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002775 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2776 }
2777 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01002778
2779# check for lockdep_set_novalidate_class
2780 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2781 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2782 if ($realfile !~ m@^kernel/lockdep@ &&
2783 $realfile !~ m@^include/linux/lockdep@ &&
2784 $realfile !~ m@^drivers/base/core@) {
2785 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2786 }
2787 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002788 }
2789
2790 # If we have no input at all, then there is nothing to report on
2791 # so just keep quiet.
2792 if ($#rawlines == -1) {
2793 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002794 }
2795
Andy Whitcroft8905a672007-11-28 16:21:06 -08002796 # In mailback mode only produce a report in the negative, for
2797 # things that appear to be patches.
2798 if ($mailback && ($clean == 1 || !$is_patch)) {
2799 exit(0);
2800 }
2801
2802 # This is not a patch, and we are are in 'no-patch' mode so
2803 # just keep quiet.
2804 if (!$chk_patch && !$is_patch) {
2805 exit(0);
2806 }
2807
2808 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002809 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002810 }
2811 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002812 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002813 }
2814
Andy Whitcroft8905a672007-11-28 16:21:06 -08002815 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002816 if ($summary && !($clean == 1 && $quiet == 1)) {
2817 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002818 print "total: $cnt_error errors, $cnt_warn warnings, " .
2819 (($check)? "$cnt_chk checks, " : "") .
2820 "$cnt_lines lines checked\n";
2821 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002822 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002823
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002824 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002825 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002826 }
2827 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002828 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002829 print "are false positives report them to the maintainer, see\n";
2830 print "CHECKPATCH in MAINTAINERS.\n";
2831 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002832
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002833 return $clean;
2834}