blob: b0aa2c680593d0c09857b9447d54ef933861ed3b [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070012
Andy Whitcroft267ad8f2010-10-26 14:23:19 -070013my $V = '0.31';
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 Whitcroftd2c0a232010-10-26 14:23:12 -0700106my $rpt_cleaners = 0;
107
Andy Whitcroft8905a672007-11-28 16:21:06 -0800108if ($terse) {
109 $emacs = 1;
110 $quiet++;
111}
112
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113if ($tree) {
114 if (defined $root) {
115 if (!top_of_kernel_tree($root)) {
116 die "$P: $root: --root does not point at a valid tree\n";
117 }
118 } else {
119 if (top_of_kernel_tree('.')) {
120 $root = '.';
121 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
122 top_of_kernel_tree($1)) {
123 $root = $1;
124 }
125 }
126
127 if (!defined $root) {
128 print "Must be run from the top-level dir. of a kernel tree\n";
129 exit(2);
130 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700131}
132
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133my $emitted_corrupt = 0;
134
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700135our $Ident = qr{
136 [A-Za-z_][A-Za-z\d_]*
137 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
138 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139our $Storage = qr{extern|static|asmlinkage};
140our $Sparse = qr{
141 __user|
142 __kernel|
143 __force|
144 __iomem|
145 __must_check|
146 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800147 __kprobes|
148 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700149 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800150
151# Notes to $Attribute:
152# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700153our $Attribute = qr{
154 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700155 __percpu|
156 __nocast|
157 __safe|
158 __bitwise__|
159 __packed__|
160 __packed2__|
161 __naked|
162 __maybe_unused|
163 __always_unused|
164 __noreturn|
165 __used|
166 __cold|
167 __noclone|
168 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700169 __read_mostly|
170 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800171 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700172 ____cacheline_aligned|
173 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800174 ____cacheline_internodealigned_in_smp|
175 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700176 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700177our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700178our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700179our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
180our $Lval = qr{$Ident(?:$Member)*};
181
182our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
183our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800184our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185our $Operators = qr{
186 <=|>=|==|!=|
187 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800188 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189 }x;
190
Andy Whitcroft8905a672007-11-28 16:21:06 -0800191our $NonptrType;
192our $Type;
193our $Declare;
194
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700195our $UTF8 = qr {
196 [\x09\x0A\x0D\x20-\x7E] # ASCII
197 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
198 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
199 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
200 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
201 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
202 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
203 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
204}x;
205
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700206our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700207 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700208 atomic_t
209)};
210
Joe Perches691e6692010-03-05 13:43:51 -0800211our $logFunctions = qr{(?x:
212 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700213 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800214 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700215 panic|
216 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800217)};
218
Andy Whitcroft8905a672007-11-28 16:21:06 -0800219our @typeList = (
220 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700221 qr{(?:unsigned\s+)?char},
222 qr{(?:unsigned\s+)?short},
223 qr{(?:unsigned\s+)?int},
224 qr{(?:unsigned\s+)?long},
225 qr{(?:unsigned\s+)?long\s+int},
226 qr{(?:unsigned\s+)?long\s+long},
227 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800228 qr{unsigned},
229 qr{float},
230 qr{double},
231 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800232 qr{struct\s+$Ident},
233 qr{union\s+$Ident},
234 qr{enum\s+$Ident},
235 qr{${Ident}_t},
236 qr{${Ident}_handler},
237 qr{${Ident}_handler_fn},
238);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700239our @modifierList = (
240 qr{fastcall},
241);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800242
Wolfram Sang7840a942010-08-09 17:20:57 -0700243our $allowed_asm_includes = qr{(?x:
244 irq|
245 memory
246)};
247# memory.h: ARM has a custom one
248
Andy Whitcroft8905a672007-11-28 16:21:06 -0800249sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700250 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
251 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700252 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800253 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700254 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800255 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700256 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700257 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700258 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800259 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700260 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800261 }x;
262 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700263 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800264 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700265 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800266 }x;
267 $Declare = qr{(?:$Storage\s+)?$Type};
268}
269build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700270
271$chk_signoff = 0 if ($file);
272
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700273my @dep_includes = ();
274my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700275my $removal = "Documentation/feature-removal-schedule.txt";
276if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800277 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700278 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800279 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700280 if (/^Check:\s+(.*\S)/) {
281 for my $entry (split(/[, ]+/, $1)) {
282 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700283 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700284
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700285 } elsif ($entry !~ m@/@) {
286 push(@dep_functions, $entry);
287 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700288 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700289 }
290 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800291 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700292}
293
Andy Whitcroft00df3442007-06-08 13:47:06 -0700294my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800295my @lines = ();
296my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700297for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800298 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700299 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800300 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700301 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800302 } elsif ($filename eq '-') {
303 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700304 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700306 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700307 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308 if ($filename eq '-') {
309 $vname = 'Your patch';
310 } else {
311 $vname = $filename;
312 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800313 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314 chomp;
315 push(@rawlines, $_);
316 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800317 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319 $exit = 1;
320 }
321 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800322 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700323}
324
325exit($exit);
326
327sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700328 my ($root) = @_;
329
330 my @tree_check = (
331 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
332 "README", "Documentation", "arch", "include", "drivers",
333 "fs", "init", "ipc", "kernel", "lib", "scripts",
334 );
335
336 foreach my $check (@tree_check) {
337 if (! -e $root . '/' . $check) {
338 return 0;
339 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700340 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700341 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700342}
343
344sub expand_tabs {
345 my ($str) = @_;
346
347 my $res = '';
348 my $n = 0;
349 for my $c (split(//, $str)) {
350 if ($c eq "\t") {
351 $res .= ' ';
352 $n++;
353 for (; ($n % 8) != 0; $n++) {
354 $res .= ' ';
355 }
356 next;
357 }
358 $res .= $c;
359 $n++;
360 }
361
362 return $res;
363}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700365 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700366 return $res;
367}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700368
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700369sub line_stats {
370 my ($line) = @_;
371
372 # Drop the diff line leader and expand tabs
373 $line =~ s/^.//;
374 $line = expand_tabs($line);
375
376 # Pick the indent from the front of the line.
377 my ($white) = ($line =~ /^(\s*)/);
378
379 return (length($line), length($white));
380}
381
Andy Whitcroft773647a2008-03-28 14:15:58 -0700382my $sanitise_quote = '';
383
384sub sanitise_line_reset {
385 my ($in_comment) = @_;
386
387 if ($in_comment) {
388 $sanitise_quote = '*/';
389 } else {
390 $sanitise_quote = '';
391 }
392}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700393sub sanitise_line {
394 my ($line) = @_;
395
396 my $res = '';
397 my $l = '';
398
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800399 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700400 my $off = 0;
401 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700402
Andy Whitcroft773647a2008-03-28 14:15:58 -0700403 # Always copy over the diff marker.
404 $res = substr($line, 0, 1);
405
406 for ($off = 1; $off < length($line); $off++) {
407 $c = substr($line, $off, 1);
408
409 # Comments we are wacking completly including the begin
410 # and end, all to $;.
411 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
412 $sanitise_quote = '*/';
413
414 substr($res, $off, 2, "$;$;");
415 $off++;
416 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800417 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700418 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700419 $sanitise_quote = '';
420 substr($res, $off, 2, "$;$;");
421 $off++;
422 next;
423 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700424 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
425 $sanitise_quote = '//';
426
427 substr($res, $off, 2, $sanitise_quote);
428 $off++;
429 next;
430 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700431
432 # A \ in a string means ignore the next character.
433 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
434 $c eq "\\") {
435 substr($res, $off, 2, 'XX');
436 $off++;
437 next;
438 }
439 # Regular quotes.
440 if ($c eq "'" || $c eq '"') {
441 if ($sanitise_quote eq '') {
442 $sanitise_quote = $c;
443
444 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700445 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700446 } elsif ($sanitise_quote eq $c) {
447 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700448 }
449 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700450
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800451 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700452 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
453 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700454 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
455 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700456 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
457 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700458 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700459 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700460 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800461 }
462
Daniel Walker113f04a2009-09-21 17:04:35 -0700463 if ($sanitise_quote eq '//') {
464 $sanitise_quote = '';
465 }
466
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800467 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700468 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800469 my $clean = 'X' x length($1);
470 $res =~ s@\<.*\>@<$clean>@;
471
472 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700473 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800474 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700475 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800476 }
477
Andy Whitcroft00df3442007-06-08 13:47:06 -0700478 return $res;
479}
480
Andy Whitcroft8905a672007-11-28 16:21:06 -0800481sub ctx_statement_block {
482 my ($linenr, $remain, $off) = @_;
483 my $line = $linenr - 1;
484 my $blk = '';
485 my $soff = $off;
486 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700487 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800489 my $loff = 0;
490
Andy Whitcroft8905a672007-11-28 16:21:06 -0800491 my $type = '';
492 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800493 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800494 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800495 my $c;
496 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800497
498 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800499 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800500 @stack = (['', 0]) if ($#stack == -1);
501
Andy Whitcroft773647a2008-03-28 14:15:58 -0700502 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503 # If we are about to drop off the end, pull in more
504 # context.
505 if ($off >= $len) {
506 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700507 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800508 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800510 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800511 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512 $len = length($blk);
513 $line++;
514 last;
515 }
516 # Bail if there is no further context.
517 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800518 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519 last;
520 }
521 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800522 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800524 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800525
Andy Whitcroft773647a2008-03-28 14:15:58 -0700526 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800527
528 # Handle nested #if/#else.
529 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
530 push(@stack, [ $type, $level ]);
531 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
532 ($type, $level) = @{$stack[$#stack - 1]};
533 } elsif ($remainder =~ /^#\s*endif\b/) {
534 ($type, $level) = @{pop(@stack)};
535 }
536
Andy Whitcroft8905a672007-11-28 16:21:06 -0800537 # Statement ends at the ';' or a close '}' at the
538 # outermost level.
539 if ($level == 0 && $c eq ';') {
540 last;
541 }
542
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800543 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700544 if ($level == 0 && $coff_set == 0 &&
545 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
546 $remainder =~ /^(else)(?:\s|{)/ &&
547 $remainder !~ /^else\s+if\b/) {
548 $coff = $off + length($1) - 1;
549 $coff_set = 1;
550 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
551 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800552 }
553
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554 if (($type eq '' || $type eq '(') && $c eq '(') {
555 $level++;
556 $type = '(';
557 }
558 if ($type eq '(' && $c eq ')') {
559 $level--;
560 $type = ($level != 0)? '(' : '';
561
562 if ($level == 0 && $coff < $soff) {
563 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700564 $coff_set = 1;
565 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800566 }
567 }
568 if (($type eq '' || $type eq '{') && $c eq '{') {
569 $level++;
570 $type = '{';
571 }
572 if ($type eq '{' && $c eq '}') {
573 $level--;
574 $type = ($level != 0)? '{' : '';
575
576 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700577 if (substr($blk, $off + 1, 1) eq ';') {
578 $off++;
579 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800580 last;
581 }
582 }
583 $off++;
584 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700585 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800586 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700587 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800588 $line++;
589 $remain--;
590 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800591
592 my $statement = substr($blk, $soff, $off - $soff + 1);
593 my $condition = substr($blk, $soff, $coff - $soff + 1);
594
595 #warn "STATEMENT<$statement>\n";
596 #warn "CONDITION<$condition>\n";
597
Andy Whitcroft773647a2008-03-28 14:15:58 -0700598 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800599
600 return ($statement, $condition,
601 $line, $remain + 1, $off - $loff + 1, $level);
602}
603
Andy Whitcroftcf655042008-03-04 14:28:20 -0800604sub statement_lines {
605 my ($stmt) = @_;
606
607 # Strip the diff line prefixes and rip blank lines at start and end.
608 $stmt =~ s/(^|\n)./$1/g;
609 $stmt =~ s/^\s*//;
610 $stmt =~ s/\s*$//;
611
612 my @stmt_lines = ($stmt =~ /\n/g);
613
614 return $#stmt_lines + 2;
615}
616
617sub statement_rawlines {
618 my ($stmt) = @_;
619
620 my @stmt_lines = ($stmt =~ /\n/g);
621
622 return $#stmt_lines + 2;
623}
624
625sub statement_block_size {
626 my ($stmt) = @_;
627
628 $stmt =~ s/(^|\n)./$1/g;
629 $stmt =~ s/^\s*{//;
630 $stmt =~ s/}\s*$//;
631 $stmt =~ s/^\s*//;
632 $stmt =~ s/\s*$//;
633
634 my @stmt_lines = ($stmt =~ /\n/g);
635 my @stmt_statements = ($stmt =~ /;/g);
636
637 my $stmt_lines = $#stmt_lines + 2;
638 my $stmt_statements = $#stmt_statements + 1;
639
640 if ($stmt_lines > $stmt_statements) {
641 return $stmt_lines;
642 } else {
643 return $stmt_statements;
644 }
645}
646
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800647sub ctx_statement_full {
648 my ($linenr, $remain, $off) = @_;
649 my ($statement, $condition, $level);
650
651 my (@chunks);
652
Andy Whitcroftcf655042008-03-04 14:28:20 -0800653 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800654 ($statement, $condition, $linenr, $remain, $off, $level) =
655 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700656 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800657 push(@chunks, [ $condition, $statement ]);
658 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
659 return ($level, $linenr, @chunks);
660 }
661
662 # Pull in the following conditional/block pairs and see if they
663 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800664 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800665 ($statement, $condition, $linenr, $remain, $off, $level) =
666 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800667 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700668 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800669 #print "C: push\n";
670 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800671 }
672
673 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674}
675
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700676sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700677 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700678 my $line;
679 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700680 my $blk = '';
681 my @o;
682 my @c;
683 my @res = ();
684
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700685 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800686 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700687 for ($line = $start; $remain > 0; $line++) {
688 next if ($rawlines[$line] =~ /^-/);
689 $remain--;
690
691 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800692
693 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700694 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800695 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700696 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800697 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700698 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800699 $level = pop(@stack);
700 }
701
Andy Whitcroft01464f32010-10-26 14:23:19 -0700702 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700703 ##print "C<$c>L<$level><$open$close>O<$off>\n";
704 if ($off > 0) {
705 $off--;
706 next;
707 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700708
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700709 if ($c eq $close && $level > 0) {
710 $level--;
711 last if ($level == 0);
712 } elsif ($c eq $open) {
713 $level++;
714 }
715 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700716
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700717 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700718 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700719 }
720
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700721 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700722 }
723
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700724 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700725}
726sub ctx_block_outer {
727 my ($linenr, $remain) = @_;
728
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700729 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
730 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700731}
732sub ctx_block {
733 my ($linenr, $remain) = @_;
734
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700735 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
736 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700737}
738sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700739 my ($linenr, $remain, $off) = @_;
740
741 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
742 return @r;
743}
744sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700745 my ($linenr, $remain) = @_;
746
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700747 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700748}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700749sub ctx_statement_level {
750 my ($linenr, $remain, $off) = @_;
751
752 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
753}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700754
755sub ctx_locate_comment {
756 my ($first_line, $end_line) = @_;
757
758 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700759 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700760 return $current_comment if (defined $current_comment);
761
762 # Look through the context and try and figure out if there is a
763 # comment.
764 my $in_comment = 0;
765 $current_comment = '';
766 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700767 my $line = $rawlines[$linenr - 1];
768 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700769 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
770 $in_comment = 1;
771 }
772 if ($line =~ m@/\*@) {
773 $in_comment = 1;
774 }
775 if (!$in_comment && $current_comment ne '') {
776 $current_comment = '';
777 }
778 $current_comment .= $line . "\n" if ($in_comment);
779 if ($line =~ m@\*/@) {
780 $in_comment = 0;
781 }
782 }
783
784 chomp($current_comment);
785 return($current_comment);
786}
787sub ctx_has_comment {
788 my ($first_line, $end_line) = @_;
789 my $cmt = ctx_locate_comment($first_line, $end_line);
790
Andy Whitcroft00df3442007-06-08 13:47:06 -0700791 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700792 ##print "CMMT: $cmt\n";
793
794 return ($cmt ne '');
795}
796
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700797sub raw_line {
798 my ($linenr, $cnt) = @_;
799
800 my $offset = $linenr - 1;
801 $cnt++;
802
803 my $line;
804 while ($cnt) {
805 $line = $rawlines[$offset++];
806 next if (defined($line) && $line =~ /^-/);
807 $cnt--;
808 }
809
810 return $line;
811}
812
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700813sub cat_vet {
814 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700815 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700816
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700817 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700818 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
819 $res .= $1;
820 if ($2 ne '') {
821 $coded = sprintf("^%c", unpack('C', $2) + 64);
822 $res .= $coded;
823 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700824 }
825 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700826
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700827 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700828}
829
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800830my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800831my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800832my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700833my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800834
835sub annotate_reset {
836 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800837 $av_pending = '_';
838 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700839 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800840}
841
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700842sub annotate_values {
843 my ($stream, $type) = @_;
844
845 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700846 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700847 my $cur = $stream;
848
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800849 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700850
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700851 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700852 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800853 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700854 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700855 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800856 print "WS($1)\n" if ($dbg_values > 1);
857 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800858 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800859 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700860 }
861
Florian Micklerc023e4732011-01-12 16:59:58 -0800862 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700863 print "CAST($1)\n" if ($dbg_values > 1);
864 push(@av_paren_type, $type);
865 $type = 'C';
866
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700867 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800868 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700869 $type = 'T';
870
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700871 } elsif ($cur =~ /^($Modifier)\s*/) {
872 print "MODIFIER($1)\n" if ($dbg_values > 1);
873 $type = 'T';
874
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700875 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700876 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800877 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700878 push(@av_paren_type, $type);
879 if ($2 ne '') {
880 $av_pending = 'N';
881 }
882 $type = 'E';
883
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700884 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700885 print "UNDEF($1)\n" if ($dbg_values > 1);
886 $av_preprocessor = 1;
887 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700888
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700889 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800890 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800891 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800892
893 push(@av_paren_type, $type);
894 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700895 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800896
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700897 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800898 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
899 $av_preprocessor = 1;
900
901 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
902
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700903 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800904
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700905 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800906 print "PRE_END($1)\n" if ($dbg_values > 1);
907
908 $av_preprocessor = 1;
909
910 # Assume all arms of the conditional end as this
911 # one does, and continue as if the #endif was not here.
912 pop(@av_paren_type);
913 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700914 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915
916 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800917 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700918
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700919 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
920 print "ATTR($1)\n" if ($dbg_values > 1);
921 $av_pending = $type;
922 $type = 'N';
923
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700924 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800925 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700926 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800927 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700928 }
929 $type = 'N';
930
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700931 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800932 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700933 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700934 $type = 'N';
935
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700936 } elsif ($cur =~/^(case)/o) {
937 print "CASE($1)\n" if ($dbg_values > 1);
938 $av_pend_colon = 'C';
939 $type = 'N';
940
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700941 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800942 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700943 $type = 'N';
944
945 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800946 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800947 push(@av_paren_type, $av_pending);
948 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700949 $type = 'N';
950
951 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800952 my $new_type = pop(@av_paren_type);
953 if ($new_type ne '_') {
954 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800955 print "PAREN('$1') -> $type\n"
956 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700957 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800958 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700959 }
960
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700961 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800962 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700963 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800964 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700965
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800966 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
967 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700968 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800969 } elsif ($type eq 'E') {
970 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700971 }
972 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
973 $type = 'V';
974
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700975 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800976 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700977 $type = 'V';
978
979 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800980 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700981 $type = 'N';
982
Andy Whitcroftcf655042008-03-04 14:28:20 -0800983 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800984 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800985 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700986 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800987
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800988 } elsif ($cur =~/^(,)/) {
989 print "COMMA($1)\n" if ($dbg_values > 1);
990 $type = 'C';
991
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700992 } elsif ($cur =~ /^(\?)/o) {
993 print "QUESTION($1)\n" if ($dbg_values > 1);
994 $type = 'N';
995
996 } elsif ($cur =~ /^(:)/o) {
997 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
998
999 substr($var, length($res), 1, $av_pend_colon);
1000 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1001 $type = 'E';
1002 } else {
1003 $type = 'N';
1004 }
1005 $av_pend_colon = 'O';
1006
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001007 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001008 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001009 $type = 'N';
1010
Andy Whitcroft0d413862008-10-15 22:02:16 -07001011 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001012 my $variant;
1013
1014 print "OPV($1)\n" if ($dbg_values > 1);
1015 if ($type eq 'V') {
1016 $variant = 'B';
1017 } else {
1018 $variant = 'U';
1019 }
1020
1021 substr($var, length($res), 1, $variant);
1022 $type = 'N';
1023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001025 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001026 if ($1 ne '++' && $1 ne '--') {
1027 $type = 'N';
1028 }
1029
1030 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032 }
1033 if (defined $1) {
1034 $cur = substr($cur, length($1));
1035 $res .= $type x length($1);
1036 }
1037 }
1038
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001039 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001040}
1041
Andy Whitcroft8905a672007-11-28 16:21:06 -08001042sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001043 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001044 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001045 ^(?:
1046 $Modifier|
1047 $Storage|
1048 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001049 DEFINE_\S+
1050 )$|
1051 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001052 goto|
1053 return|
1054 case|
1055 else|
1056 asm|__asm__|
1057 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001058 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001059 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001060 )}x;
1061 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1062 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001063 # Check for modifiers.
1064 $possible =~ s/\s*$Storage\s*//g;
1065 $possible =~ s/\s*$Sparse\s*//g;
1066 if ($possible =~ /^\s*$/) {
1067
1068 } elsif ($possible =~ /\s/) {
1069 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001070 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001071 if ($modifier !~ $notPermitted) {
1072 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1073 push(@modifierList, $modifier);
1074 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001075 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001076
1077 } else {
1078 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1079 push(@typeList, $possible);
1080 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001081 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001082 } else {
1083 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001084 }
1085}
1086
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001087my $prefix = '';
1088
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001089sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001090 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1091 return 0;
1092 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001093 my $line = $prefix . $_[0];
1094
1095 $line = (split('\n', $line))[0] . "\n" if ($terse);
1096
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001097 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001098
1099 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001100}
1101sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001102 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001103}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001104sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001105 if (report("ERROR: $_[0]\n")) {
1106 our $clean = 0;
1107 our $cnt_error++;
1108 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001109}
1110sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 if (report("WARNING: $_[0]\n")) {
1112 our $clean = 0;
1113 our $cnt_warn++;
1114 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001115}
1116sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001117 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001118 our $clean = 0;
1119 our $cnt_chk++;
1120 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001121}
1122
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001123sub check_absolute_file {
1124 my ($absolute, $herecurr) = @_;
1125 my $file = $absolute;
1126
1127 ##print "absolute<$absolute>\n";
1128
1129 # See if any suffix of this path is a path within the tree.
1130 while ($file =~ s@^[^/]*/@@) {
1131 if (-f "$root/$file") {
1132 ##print "file<$file>\n";
1133 last;
1134 }
1135 }
1136 if (! -f _) {
1137 return 0;
1138 }
1139
1140 # It is, so see if the prefix is acceptable.
1141 my $prefix = $absolute;
1142 substr($prefix, -length($file)) = '';
1143
1144 ##print "prefix<$prefix>\n";
1145 if ($prefix ne ".../") {
1146 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1147 }
1148}
1149
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001150sub process {
1151 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001152
1153 my $linenr=0;
1154 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001156 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001158
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001159 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001160 my $indent;
1161 my $previndent=0;
1162 my $stashindent=0;
1163
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001164 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001165 my $signoff = 0;
1166 my $is_patch = 0;
1167
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001168 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001169 our $cnt_lines = 0;
1170 our $cnt_error = 0;
1171 our $cnt_warn = 0;
1172 our $cnt_chk = 0;
1173
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001174 # Trace the real file/line as we go.
1175 my $realfile = '';
1176 my $realline = 0;
1177 my $realcnt = 0;
1178 my $here = '';
1179 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001180 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001181 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001182 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001183
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001184 my $prev_values = 'E';
1185
1186 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001187 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001188 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001189 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001190
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001191 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001192 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001193 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001194 my @setup_docs = ();
1195 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196
1197 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001198 my $line;
1199 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 $linenr++;
1201 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001202
Andy Whitcroft773647a2008-03-28 14:15:58 -07001203 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001204 $setup_docs = 0;
1205 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1206 $setup_docs = 1;
1207 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001208 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001209 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001210 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1211 $realline=$1-1;
1212 if (defined $2) {
1213 $realcnt=$3+1;
1214 } else {
1215 $realcnt=1+1;
1216 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001217 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001218
1219 # Guestimate if this is a continuing comment. Run
1220 # the context looking for a comment "edge". If this
1221 # edge is a close comment then we must be in a comment
1222 # at context start.
1223 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001224 my $cnt = $realcnt;
1225 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1226 next if (defined $rawlines[$ln - 1] &&
1227 $rawlines[$ln - 1] =~ /^-/);
1228 $cnt--;
1229 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001230 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001231 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1232 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1233 ($edge) = $1;
1234 last;
1235 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001236 }
1237 if (defined $edge && $edge eq '*/') {
1238 $in_comment = 1;
1239 }
1240
1241 # Guestimate if this is a continuing comment. If this
1242 # is the start of a diff block and this line starts
1243 # ' *' then it is very likely a comment.
1244 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001245 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 {
1247 $in_comment = 1;
1248 }
1249
1250 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1251 sanitise_line_reset($in_comment);
1252
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001253 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001254 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001255 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001256 $line = sanitise_line($rawline);
1257 }
1258 push(@lines, $line);
1259
1260 if ($realcnt > 1) {
1261 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1262 } else {
1263 $realcnt = 0;
1264 }
1265
1266 #print "==>$rawline\n";
1267 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001268
1269 if ($setup_docs && $line =~ /^\+/) {
1270 push(@setup_docs, $line);
1271 }
1272 }
1273
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001274 $prefix = '';
1275
Andy Whitcroft773647a2008-03-28 14:15:58 -07001276 $realcnt = 0;
1277 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001278 foreach my $line (@lines) {
1279 $linenr++;
1280
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001282
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001283#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001284 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001285 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001286 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001287 $realline=$1-1;
1288 if (defined $2) {
1289 $realcnt=$3+1;
1290 } else {
1291 $realcnt=1+1;
1292 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001293 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001294 $prev_values = 'E';
1295
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001297 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001298 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001299 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001300
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001301# track the line number as we move through the hunk, note that
1302# new versions of GNU diff omit the leading space on completely
1303# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001304 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001305 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001306 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001307
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001308 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001309 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001310
1311 # Track the previous line.
1312 ($prevline, $stashline) = ($stashline, $line);
1313 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001314 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1315
Andy Whitcroft773647a2008-03-28 14:15:58 -07001316 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001317
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001318 } elsif ($realcnt == 1) {
1319 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001320 }
1321
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001322 my $hunk_line = ($realcnt != 0);
1323
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001324#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001325 $prefix = "$filename:$realline: " if ($emacs && $file);
1326 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1327
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001328 $here = "#$linenr: " if (!$file);
1329 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001330
1331 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001332 if ($line =~ /^diff --git.*?(\S+)$/) {
1333 $realfile = $1;
1334 $realfile =~ s@^([^/]*)/@@;
1335
1336 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001337 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001338 $realfile =~ s@^([^/]*)/@@;
1339
1340 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001341 if (!$file && $tree && $p1_prefix ne '' &&
1342 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001343 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1344 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001346 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1348 }
1349 next;
1350 }
1351
Randy Dunlap389834b2007-06-08 13:47:03 -07001352 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001353
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001354 my $hereline = "$here\n$rawline\n";
1355 my $herecurr = "$here\n$rawline\n";
1356 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001357
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001358 $cnt_lines++ if ($realcnt != 0);
1359
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001360# Check for incorrect file permissions
1361 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1362 my $permhere = $here . "FILE: $realfile\n";
1363 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1364 ERROR("do not set execute permissions for source files\n" . $permhere);
1365 }
1366 }
1367
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001368#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001369 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001370 # This is a signoff, if ugly, so do not double report.
1371 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001372 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001373 WARN("Signed-off-by: is the preferred form\n" .
1374 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001375 }
1376 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001377 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001378 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001379 }
1380 }
1381
Andy Whitcroft00df3442007-06-08 13:47:06 -07001382# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001383 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001384 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001385 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001386 }
1387
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001388# Check for absolute kernel paths.
1389 if ($tree) {
1390 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1391 my $file = $1;
1392
1393 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1394 check_absolute_file($1, $herecurr)) {
1395 #
1396 } else {
1397 check_absolute_file($file, $herecurr);
1398 }
1399 }
1400 }
1401
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001402# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1403 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001404 $rawline !~ m/^$UTF8*$/) {
1405 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1406
1407 my $blank = copy_spacing($rawline);
1408 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1409 my $hereptr = "$hereline$ptr\n";
1410
1411 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001412 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001413
Andy Whitcroft306708542008-10-15 22:02:28 -07001414# ignore non-hunk lines and lines being removed
1415 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001416
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001417#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001418 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001420 ERROR("DOS line endings\n" . $herevet);
1421
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001422 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1423 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001424 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001425 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001426 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001427
Andi Kleen33549572010-05-24 14:33:29 -07001428# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001429# Only applies when adding the entry originally, after that we do not have
1430# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001431 if ($realfile =~ /Kconfig/ &&
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001432 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
Andi Kleen33549572010-05-24 14:33:29 -07001433 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001434 my $cnt = $realcnt;
1435 my $ln = $linenr + 1;
1436 my $f;
1437 my $is_end = 0;
1438 while ($cnt > 0 && defined $lines[$ln - 1]) {
1439 $f = $lines[$ln - 1];
1440 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1441 $is_end = $lines[$ln - 1] =~ /^\+/;
1442 $ln++;
1443
1444 next if ($f =~ /^-/);
1445 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001446 $f =~ s/#.*//;
1447 $f =~ s/^\s+//;
1448 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001449 if ($f =~ /^\s*config\s/) {
1450 $is_end = 1;
1451 last;
1452 }
Andi Kleen33549572010-05-24 14:33:29 -07001453 $length++;
1454 }
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001455 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1456 #print "is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001457 }
1458
Andy Whitcroft5368df22008-10-15 22:02:27 -07001459# check we are in a valid source file if not then ignore this hunk
1460 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1461
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001462#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001463 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001464 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001465 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001466 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001467 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001468 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001469 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001470 }
1471
Joe Perches5e79d962010-03-05 13:43:55 -08001472# check for spaces before a quoted newline
1473 if ($rawline =~ /^.*\".*\s\\n/) {
1474 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1475 }
1476
Andy Whitcroft8905a672007-11-28 16:21:06 -08001477# check for adding lines without a newline.
1478 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1479 WARN("adding a line without newline at end of file\n" . $herecurr);
1480 }
1481
Mike Frysinger42e41c52009-09-21 17:04:40 -07001482# Blackfin: use hi/lo macros
1483 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1484 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1485 my $herevet = "$here\n" . cat_vet($line) . "\n";
1486 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1487 }
1488 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1489 my $herevet = "$here\n" . cat_vet($line) . "\n";
1490 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1491 }
1492 }
1493
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001494# check we are in a valid source file C or perl if not then ignore this hunk
1495 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001496
1497# at the beginning of a line any tabs must come first and anything
1498# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001499 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1500 $rawline =~ /^\+\s* \s*/) {
1501 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001502 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001503 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001504 }
1505
Alberto Panizzo08e44362010-03-05 13:43:54 -08001506# check for space before tabs.
1507 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1508 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1509 WARN("please, no space before tabs\n" . $herevet);
1510 }
1511
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001512# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001513# Exceptions:
1514# 1) within comments
1515# 2) indented preprocessor commands
1516# 3) hanging labels
1517 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001518 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001519 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001520 }
1521
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001522# check we are in a valid C source file if not then ignore this hunk
1523 next if ($realfile !~ /\.(h|c)$/);
1524
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001525# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001526 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001527 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1528 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001529
Mike Frysinger42e41c52009-09-21 17:04:40 -07001530# Blackfin: don't use __builtin_bfin_[cs]sync
1531 if ($line =~ /__builtin_bfin_csync/) {
1532 my $herevet = "$here\n" . cat_vet($line) . "\n";
1533 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1534 }
1535 if ($line =~ /__builtin_bfin_ssync/) {
1536 my $herevet = "$here\n" . cat_vet($line) . "\n";
1537 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1538 }
1539
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001540# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001541 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1542 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001543 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001544 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001545 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001546 $stat =~ s/\n./\n /g;
1547 $cond =~ s/\n./\n /g;
1548
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001549 # Find the real next line.
1550 $realline_next = $line_nr_next;
1551 if (defined $realline_next &&
1552 (!defined $lines[$realline_next - 1] ||
1553 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1554 $realline_next++;
1555 }
1556
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001557 my $s = $stat;
1558 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001559
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001560 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001561 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001562
1563 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001564 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001565
Andy Whitcroft463f2862009-09-21 17:04:34 -07001566 } elsif ($s =~ /^.\s*else\b/s) {
1567
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001568 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001569 } 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 -07001570 my $type = $1;
1571 $type =~ s/\s+/ /g;
1572 possible($type, "A:" . $s);
1573
Andy Whitcroft8905a672007-11-28 16:21:06 -08001574 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001575 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001576 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001577 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001578
1579 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001580 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001581 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001582 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001583
1584 # Check for any sort of function declaration.
1585 # int foo(something bar, other baz);
1586 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001587 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 -08001588 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001589
Andy Whitcroftcf655042008-03-04 14:28:20 -08001590 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001591 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001592 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001593
Andy Whitcroft8905a672007-11-28 16:21:06 -08001594 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001595 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001596
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001597 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001598 }
1599 }
1600 }
1601
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001602 }
1603
Andy Whitcroft653d4872007-06-23 17:16:34 -07001604#
1605# Checks which may be anchored in the context.
1606#
1607
1608# Check for switch () and associated case and default
1609# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001610 if ($line=~/\bswitch\s*\(.*\)/) {
1611 my $err = '';
1612 my $sep = '';
1613 my @ctx = ctx_block_outer($linenr, $realcnt);
1614 shift(@ctx);
1615 for my $ctx (@ctx) {
1616 my ($clen, $cindent) = line_stats($ctx);
1617 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1618 $indent != $cindent) {
1619 $err .= "$sep$ctx\n";
1620 $sep = '';
1621 } else {
1622 $sep = "[...]\n";
1623 }
1624 }
1625 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001626 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001627 }
1628 }
1629
1630# if/while/etc brace do not go on next line, unless defining a do while loop,
1631# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001632 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001633 my $pre_ctx = "$1$2";
1634
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001635 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001636 my $ctx_cnt = $realcnt - $#ctx - 1;
1637 my $ctx = join("\n", @ctx);
1638
Andy Whitcroft548596d2008-07-23 21:29:01 -07001639 my $ctx_ln = $linenr;
1640 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001641
Andy Whitcroft548596d2008-07-23 21:29:01 -07001642 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1643 defined $lines[$ctx_ln - 1] &&
1644 $lines[$ctx_ln - 1] =~ /^-/)) {
1645 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1646 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001647 $ctx_ln++;
1648 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001649
Andy Whitcroft53210162008-07-23 21:29:03 -07001650 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1651 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001652
1653 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1654 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001655 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001656 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001657 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1658 $ctx =~ /\)\s*\;\s*$/ &&
1659 defined $lines[$ctx_ln - 1])
1660 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001661 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1662 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001663 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001664 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001665 }
1666 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001667 }
1668
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001669# Check relative indent for conditionals and blocks.
1670 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1671 my ($s, $c) = ($stat, $cond);
1672
1673 substr($s, 0, length($c), '');
1674
1675 # Make sure we remove the line prefixes as we have
1676 # none on the first line, and are going to readd them
1677 # where necessary.
1678 $s =~ s/\n./\n/gs;
1679
1680 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001681 my @newlines = ($c =~ /\n/gs);
1682 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001683
1684 # We want to check the first line inside the block
1685 # starting at the end of the conditional, so remove:
1686 # 1) any blank line termination
1687 # 2) any opening brace { on end of the line
1688 # 3) any do (...) {
1689 my $continuation = 0;
1690 my $check = 0;
1691 $s =~ s/^.*\bdo\b//;
1692 $s =~ s/^\s*{//;
1693 if ($s =~ s/^\s*\\//) {
1694 $continuation = 1;
1695 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001696 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001697 $check = 1;
1698 $cond_lines++;
1699 }
1700
1701 # Also ignore a loop construct at the end of a
1702 # preprocessor statement.
1703 if (($prevline =~ /^.\s*#\s*define\s/ ||
1704 $prevline =~ /\\\s*$/) && $continuation == 0) {
1705 $check = 0;
1706 }
1707
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001708 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001709 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001710 while ($cond_ptr != $cond_lines) {
1711 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001712
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001713 # If we see an #else/#elif then the code
1714 # is not linear.
1715 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1716 $check = 0;
1717 }
1718
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001719 # Ignore:
1720 # 1) blank lines, they should be at 0,
1721 # 2) preprocessor lines, and
1722 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001723 if ($continuation ||
1724 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001725 $s =~ /^\s*#\s*?/ ||
1726 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001727 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001728 if ($s =~ s/^.*?\n//) {
1729 $cond_lines++;
1730 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001731 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001732 }
1733
1734 my (undef, $sindent) = line_stats("+" . $s);
1735 my $stat_real = raw_line($linenr, $cond_lines);
1736
1737 # Check if either of these lines are modified, else
1738 # this is not this patch's fault.
1739 if (!defined($stat_real) ||
1740 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1741 $check = 0;
1742 }
1743 if (defined($stat_real) && $cond_lines > 1) {
1744 $stat_real = "[...]\n$stat_real";
1745 }
1746
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001747 #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 -07001748
1749 if ($check && (($sindent % 8) != 0 ||
1750 ($sindent <= $indent && $s ne ''))) {
1751 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1752 }
1753 }
1754
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001755 # Track the 'values' across context and added lines.
1756 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001757 my ($curr_values, $curr_vars) =
1758 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001759 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001760 if ($dbg_values) {
1761 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001762 print "$linenr > .$outline\n";
1763 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001764 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001765 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001766 $prev_values = substr($curr_values, -1);
1767
Andy Whitcroft00df3442007-06-08 13:47:06 -07001768#ignore lines not being added
1769 if ($line=~/^[^\+]/) {next;}
1770
Andy Whitcroft653d4872007-06-23 17:16:34 -07001771# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001772 if ($dbg_type) {
1773 if ($line =~ /^.\s*$Declare\s*$/) {
1774 ERROR("TEST: is type\n" . $herecurr);
1775 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1776 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1777 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001778 next;
1779 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001780# TEST: allow direct testing of the attribute matcher.
1781 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001782 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001783 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001784 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001785 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1786 }
1787 next;
1788 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001789
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001790# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001791 if ($line =~ /^.\s*{/ &&
1792 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001794 }
1795
Andy Whitcroft653d4872007-06-23 17:16:34 -07001796#
1797# Checks which are anchored on the added line.
1798#
1799
1800# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001801 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001802 my $path = $1;
1803 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001804 ERROR("malformed #include filename\n" .
1805 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001806 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001807 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001808
1809# no C99 // comments
1810 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001811 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001812 }
1813 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001814 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001816
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001817# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1818# the whole statement.
1819#print "APW <$lines[$realline_next - 1]>\n";
1820 if (defined $realline_next &&
1821 exists $lines[$realline_next - 1] &&
1822 !defined $suppress_export{$realline_next} &&
1823 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1824 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001825 # Handle definitions which produce identifiers with
1826 # a prefix:
1827 # XXX(foo);
1828 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001829 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001830 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1831 $name =~ /^${Ident}_$2/) {
1832#print "FOO C name<$name>\n";
1833 $suppress_export{$realline_next} = 1;
1834
1835 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001836 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001837 ^.DEFINE_$Ident\(\Q$name\E\)|
1838 ^.DECLARE_$Ident\(\Q$name\E\)|
1839 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001840 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1841 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001842 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001843#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1844 $suppress_export{$realline_next} = 2;
1845 } else {
1846 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001847 }
1848 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001849 if (!defined $suppress_export{$linenr} &&
1850 $prevline =~ /^.\s*$/ &&
1851 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1852 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1853#print "FOO B <$lines[$linenr - 1]>\n";
1854 $suppress_export{$linenr} = 2;
1855 }
1856 if (defined $suppress_export{$linenr} &&
1857 $suppress_export{$linenr} == 2) {
1858 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1859 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001860
Joe Eloff5150bda2010-08-09 17:21:00 -07001861# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001862 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001863 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001864 $herecurr);
1865 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001866# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001867 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001868 ERROR("do not initialise statics to 0 or NULL\n" .
1869 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001870 }
1871
Joe Perchescb710ec2010-10-26 14:23:20 -07001872# check for static const char * arrays.
1873 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1874 WARN("static const char * array should probably be static const char * const\n" .
1875 $herecurr);
1876 }
1877
1878# check for static char foo[] = "bar" declarations.
1879 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1880 WARN("static char array declaration should probably be static const char\n" .
1881 $herecurr);
1882 }
1883
Joe Perches93ed0e22010-10-26 14:23:21 -07001884# check for declarations of struct pci_device_id
1885 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1886 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1887 }
1888
Andy Whitcroft653d4872007-06-23 17:16:34 -07001889# check for new typedefs, only function parameters and sparse annotations
1890# make sense.
1891 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001892 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001893 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001894 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001895 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001896 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001897 }
1898
1899# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001900 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001901 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001902 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001903
Andy Whitcroft65863862009-01-06 14:41:21 -08001904 # Should start with a space.
1905 $to =~ s/^(\S)/ $1/;
1906 # Should not end with a space.
1907 $to =~ s/\s+$//;
1908 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001909 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001910 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001911
Andy Whitcroft65863862009-01-06 14:41:21 -08001912 #print "from<$from> to<$to>\n";
1913 if ($from ne $to) {
1914 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1915 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001916 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001917 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001918
Andy Whitcroft65863862009-01-06 14:41:21 -08001919 # Should start with a space.
1920 $to =~ s/^(\S)/ $1/;
1921 # Should not end with a space.
1922 $to =~ s/\s+$//;
1923 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001924 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001925 }
1926 # Modifiers should have spaces.
1927 $to =~ s/(\b$Modifier$)/$1 /;
1928
Andy Whitcroft667026e2009-02-27 14:03:08 -08001929 #print "from<$from> to<$to> ident<$ident>\n";
1930 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001931 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1932 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001933 }
1934
1935# # no BUG() or BUG_ON()
1936# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1937# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1938# print "$herecurr";
1939# $clean = 0;
1940# }
1941
Andy Whitcroft8905a672007-11-28 16:21:06 -08001942 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001943 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 -08001944 }
1945
Joe Perches17441222011-06-15 15:08:17 -07001946# check for uses of printk_ratelimit
1947 if ($line =~ /\bprintk_ratelimit\s*\(/) {
1948 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
1949 }
1950
Andy Whitcroft00df3442007-06-08 13:47:06 -07001951# printk should use KERN_* levels. Note that follow on printk's on the
1952# same line do not need a level, so we use the current block context
1953# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001954# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001955# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001956 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001957 my $ok = 0;
1958 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1959 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001960 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07001961 # with "\n" ignore it, else it is to blame
1962 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1963 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1964 $ok = 1;
1965 }
1966 last;
1967 }
1968 }
1969 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001970 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001971 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001972 }
1973
Andy Whitcroft653d4872007-06-23 17:16:34 -07001974# function brace can't be on same line, except for #defines of do while,
1975# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001976 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1977 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001978 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001979 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001980
Andy Whitcroft8905a672007-11-28 16:21:06 -08001981# open braces for enum, union and struct go on the same line.
1982 if ($line =~ /^.\s*{/ &&
1983 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1984 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1985 }
1986
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07001987# missing space after union, struct or enum definition
1988 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1989 WARN("missing space after $1 definition\n" . $herecurr);
1990 }
1991
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001992# check for spacing round square brackets; allowed:
1993# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001994# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1995# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001996 while ($line =~ /(.*?\s)\[/g) {
1997 my ($where, $prefix) = ($-[1], $1);
1998 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001999 ($where != 0 || $prefix !~ /^.\s+$/) &&
2000 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002001 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2002 }
2003 }
2004
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002005# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002006 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002007 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 my $ctx_before = substr($line, 0, $-[1]);
2009 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002010
2011 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002012 if ($name =~ /^(?:
2013 if|for|while|switch|return|case|
2014 volatile|__volatile__|
2015 __attribute__|format|__extension__|
2016 asm|__asm__)$/x)
2017 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002018
2019 # cpp #define statements have non-optional spaces, ie
2020 # if there is a space between the name and the open
2021 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002022 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023
2024 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002025 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002026
2027 # If this whole things ends with a type its most
2028 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002029 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002030
2031 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002032 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002033 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002034 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002035# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002036 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002037 my $ops = qr{
2038 <<=|>>=|<=|>=|==|!=|
2039 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2040 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002041 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2042 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002043 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002044 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002045 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002046
2047 my $blank = copy_spacing($opline);
2048
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002049 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002050 $off += length($elements[$n]);
2051
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002052 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002053 my $ca = substr($opline, 0, $off);
2054 my $cc = '';
2055 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2056 $cc = substr($opline, $off + length($elements[$n + 1]));
2057 }
2058 my $cb = "$ca$;$cc";
2059
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002060 my $a = '';
2061 $a = 'V' if ($elements[$n] ne '');
2062 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002063 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002064 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2065 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002066 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002067
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002068 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002069
2070 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002071 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002072 $c = 'V' if ($elements[$n + 2] ne '');
2073 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002074 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002075 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2076 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002077 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002078 } else {
2079 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002080 }
2081
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002082 my $ctx = "${a}x${c}";
2083
2084 my $at = "(ctx:$ctx)";
2085
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002086 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002087 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002088
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002089 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002090 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002092 # Get the full operator variant.
2093 my $opv = $op . substr($curr_vars, $off, 1);
2094
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002095 # Ignore operators passed as parameters.
2096 if ($op_type ne 'V' &&
2097 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2098
Andy Whitcroftcf655042008-03-04 14:28:20 -08002099# # Ignore comments
2100# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002101
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002102 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002103 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002104 if ($ctx !~ /.x[WEBC]/ &&
2105 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002106 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002107 }
2108
2109 # // is a comment
2110 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002111
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002112 # No spaces for:
2113 # ->
2114 # : when part of a bitfield
2115 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002116 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002117 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002118 }
2119
2120 # , must have a space on the right.
2121 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002122 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002123 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002124 }
2125
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002126 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002127 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002128 #warn "'*' is part of type\n";
2129
2130 # unary operators should have a space before and
2131 # none after. May be left adjacent to another
2132 # unary operator, or a cast
2133 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002134 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002135 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002136 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002137 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002138 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002139 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002140 # A unary '*' may be const
2141
2142 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002143 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002144 }
2145
2146 # unary ++ and unary -- are allowed no space on one side.
2147 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002148 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2149 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002150 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151 if ($ctx =~ /Wx[BE]/ ||
2152 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2153 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002154 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002155 if ($ctx =~ /ExW/) {
2156 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2157 }
2158
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002159
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002160 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002161 } elsif ($op eq '<<' or $op eq '>>' or
2162 $op eq '&' or $op eq '^' or $op eq '|' or
2163 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002164 $op eq '*' or $op eq '/' or
2165 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002167 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002168 ERROR("need consistent spacing around '$op' $at\n" .
2169 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002170 }
2171
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002172 # A colon needs no spaces before when it is
2173 # terminating a case value or a label.
2174 } elsif ($opv eq ':C' || $opv eq ':L') {
2175 if ($ctx =~ /Wx./) {
2176 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2177 }
2178
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002179 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002180 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002181 my $ok = 0;
2182
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002183 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002184 if (($op eq '<' &&
2185 $cc =~ /^\S+\@\S+>/) ||
2186 ($op eq '>' &&
2187 $ca =~ /<\S+\@\S+$/))
2188 {
2189 $ok = 1;
2190 }
2191
2192 # Ignore ?:
2193 if (($opv eq ':O' && $ca =~ /\?$/) ||
2194 ($op eq '?' && $cc =~ /^:/)) {
2195 $ok = 1;
2196 }
2197
2198 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002199 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002200 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002202 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002203 }
2204 }
2205
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002206# check for multiple assignments
2207 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002208 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002209 }
2210
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002211## # check for multiple declarations, allowing for a function declaration
2212## # continuation.
2213## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2214## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2215##
2216## # Remove any bracketed sections to ensure we do not
2217## # falsly report the parameters of functions.
2218## my $ln = $line;
2219## while ($ln =~ s/\([^\(\)]*\)//g) {
2220## }
2221## if ($ln =~ /,/) {
2222## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2223## }
2224## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002225
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002226#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002227 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2228 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002230 }
2231
2232# closing brace should have a space following it when it has anything
2233# on the line
2234 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002235 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002236 }
2237
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002238# check spacing on square brackets
2239 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002240 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002241 }
2242 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002243 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002244 }
2245
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002246# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002247 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2248 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002250 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002251 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002252 $line !~ /for\s*\(.*;\s+\)/ &&
2253 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002254 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002255 }
2256
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002257#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002258 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002259 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002260 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002261 }
2262
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002263# Return is not a function.
2264 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2265 my $spacing = $1;
2266 my $value = $2;
2267
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002268 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002269 $value =~ s/\(/ \(/g;
2270 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002271 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2272 $value !~ /(?:$Ident|-?$Constant)\s*
2273 $Compare\s*
2274 (?:$Ident|-?$Constant)/x &&
2275 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002276 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002277#print "value<$value>\n";
2278 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002279 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2280
2281 } elsif ($spacing !~ /\s+/) {
2282 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2283 }
2284 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002285# Return of what appears to be an errno should normally be -'ve
2286 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2287 my $name = $1;
2288 if ($name ne 'EOF' && $name ne 'ERROR') {
2289 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2290 }
2291 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002292
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002293# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002294 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002295 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002296 }
2297
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002298# Check for illegal assignment in if conditional -- and check for trailing
2299# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002300 if ($line =~ /do\s*(?!{)/) {
2301 my ($stat_next) = ctx_statement_block($line_nr_next,
2302 $remain_next, $off_next);
2303 $stat_next =~ s/\n./\n /g;
2304 ##print "stat<$stat> stat_next<$stat_next>\n";
2305
2306 if ($stat_next =~ /^\s*while\b/) {
2307 # If the statement carries leading newlines,
2308 # then count those as offsets.
2309 my ($whitespace) =
2310 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2311 my $offset =
2312 statement_rawlines($whitespace) - 1;
2313
2314 $suppress_whiletrailers{$line_nr_next +
2315 $offset} = 1;
2316 }
2317 }
2318 if (!defined $suppress_whiletrailers{$linenr} &&
2319 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002320 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002321
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002322 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002323 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002324 }
2325
2326 # Find out what is on the end of the line after the
2327 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002328 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002329 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002330 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002331 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2332 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002334 # Find out how long the conditional actually is.
2335 my @newlines = ($c =~ /\n/gs);
2336 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002337 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002338
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002339 $stat_real = raw_line($linenr, $cond_lines)
2340 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002341 if (defined($stat_real) && $cond_lines > 1) {
2342 $stat_real = "[...]\n$stat_real";
2343 }
2344
2345 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002346 }
2347 }
2348
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002349# Check for bitwise tests written as boolean
2350 if ($line =~ /
2351 (?:
2352 (?:\[|\(|\&\&|\|\|)
2353 \s*0[xX][0-9]+\s*
2354 (?:\&\&|\|\|)
2355 |
2356 (?:\&\&|\|\|)
2357 \s*0[xX][0-9]+\s*
2358 (?:\&\&|\|\||\)|\])
2359 )/x)
2360 {
2361 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2362 }
2363
Andy Whitcroft8905a672007-11-28 16:21:06 -08002364# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002365 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2366 my $s = $1;
2367 $s =~ s/$;//g; # Remove any comments
2368 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2369 ERROR("trailing statements should be on next line\n" . $herecurr);
2370 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002371 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002372# if should not continue a brace
2373 if ($line =~ /}\s*if\b/) {
2374 ERROR("trailing statements should be on next line\n" .
2375 $herecurr);
2376 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002377# case and default should not have general statements after them
2378 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2379 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002380 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002381 \s*return\s+
2382 )/xg)
2383 {
2384 ERROR("trailing statements should be on next line\n" . $herecurr);
2385 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002386
2387 # Check for }<nl>else {, these must be at the same
2388 # indent level to be relevant to each other.
2389 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2390 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002391 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002392 }
2393
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002394 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2395 $previndent == $indent) {
2396 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2397
2398 # Find out what is on the end of the line after the
2399 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002400 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002401 $s =~ s/\n.*//g;
2402
2403 if ($s =~ /^\s*;/) {
2404 ERROR("while should follow close brace '}'\n" . $hereprev);
2405 }
2406 }
2407
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002408#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2409# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2410# print "No studly caps, use _\n";
2411# print "$herecurr";
2412# $clean = 0;
2413# }
2414
2415#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002416 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002417 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002418 }
2419
Andy Whitcroft653d4872007-06-23 17:16:34 -07002420#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002421 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002422 my $file = "$1.h";
2423 my $checkfile = "include/linux/$file";
2424 if (-f "$root/$checkfile" &&
2425 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002426 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002427 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002428 if ($realfile =~ m{^arch/}) {
2429 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2430 } else {
2431 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2432 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002433 }
2434 }
2435
Andy Whitcroft653d4872007-06-23 17:16:34 -07002436# multi-statement macros should be enclosed in a do while loop, grab the
2437# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002438# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002439 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2440 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002441 my $ln = $linenr;
2442 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002443 my ($off, $dstat, $dcond, $rest);
2444 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002445
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002446 my $args = defined($1);
2447
2448 # Find the end of the macro and limit our statement
2449 # search to that.
2450 while ($cnt > 0 && defined $lines[$ln - 1] &&
2451 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2452 {
2453 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002454 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002455 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002456 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002457 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002458
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002459 ($dstat, $dcond, $ln, $cnt, $off) =
2460 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2461 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002462 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002463
2464 # Extract the remainder of the define (if any) and
2465 # rip off surrounding spaces, and trailing \'s.
2466 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002467 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2468 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002469 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2470 $rest .= substr($lines[$ln - 1], $off) . "\n";
2471 $cnt--;
2472 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002473 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002474 $off = 0;
2475 }
2476 $rest =~ s/\\\n.//g;
2477 $rest =~ s/^\s*//s;
2478 $rest =~ s/\s*$//s;
2479
2480 # Clean up the original statement.
2481 if ($args) {
2482 substr($dstat, 0, length($dcond), '');
2483 } else {
2484 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2485 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002486 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002487 $dstat =~ s/\\\n.//g;
2488 $dstat =~ s/^\s*//s;
2489 $dstat =~ s/\s*$//s;
2490
2491 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002492 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2493 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2494 $dstat =~ s/\[[^\{\}]*\]/1/)
2495 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002496 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002497
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002498 my $exceptions = qr{
2499 $Declare|
2500 module_param_named|
2501 MODULE_PARAM_DESC|
2502 DECLARE_PER_CPU|
2503 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002504 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002505 union|
2506 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002507 \.$Ident\s*=\s*|
2508 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002509 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002510 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2511 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002512 if ($rest !~ /while\s*\(/ &&
2513 $dstat !~ /$exceptions/)
2514 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002515 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 -07002516 }
2517
2518 } elsif ($ctx !~ /;/) {
2519 if ($dstat ne '' &&
2520 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2521 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002522 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002523 $dstat =~ /$Operators/)
2524 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002525 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002526 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002527 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002528 }
2529
Mike Frysinger080ba922009-01-06 14:41:25 -08002530# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2531# all assignments may have only one of the following with an assignment:
2532# .
2533# ALIGN(...)
2534# VMLINUX_SYMBOL(...)
2535 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2536 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2537 }
2538
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002539# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002540 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2541 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002542 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002543 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002544 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2545 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002546 my $allowed = 0;
2547 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002548 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002549 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002550 for my $chunk (@chunks) {
2551 my ($cond, $block) = @{$chunk};
2552
Andy Whitcroft773647a2008-03-28 14:15:58 -07002553 # If the condition carries leading newlines, then count those as offsets.
2554 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2555 my $offset = statement_rawlines($whitespace) - 1;
2556
2557 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2558
2559 # We have looked at and allowed this specific line.
2560 $suppress_ifbraces{$ln + $offset} = 1;
2561
2562 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002563 $ln += statement_rawlines($block) - 1;
2564
Andy Whitcroft773647a2008-03-28 14:15:58 -07002565 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002566
2567 $seen++ if ($block =~ /^\s*{/);
2568
Andy Whitcroftcf655042008-03-04 14:28:20 -08002569 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2570 if (statement_lines($cond) > 1) {
2571 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002572 $allowed = 1;
2573 }
2574 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002575 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002576 $allowed = 1;
2577 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002578 if (statement_block_size($block) > 1) {
2579 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002580 $allowed = 1;
2581 }
2582 }
2583 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002584 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002585 }
2586 }
2587 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002588 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002589 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002590 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002591
Andy Whitcroftcf655042008-03-04 14:28:20 -08002592 # Check the pre-context.
2593 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2594 #print "APW: ALLOWED: pre<$1>\n";
2595 $allowed = 1;
2596 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002597
2598 my ($level, $endln, @chunks) =
2599 ctx_statement_full($linenr, $realcnt, $-[0]);
2600
Andy Whitcroftcf655042008-03-04 14:28:20 -08002601 # Check the condition.
2602 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002603 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002604 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002605 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002606 }
2607 if (statement_lines($cond) > 1) {
2608 #print "APW: ALLOWED: cond<$cond>\n";
2609 $allowed = 1;
2610 }
2611 if ($block =~/\b(?:if|for|while)\b/) {
2612 #print "APW: ALLOWED: block<$block>\n";
2613 $allowed = 1;
2614 }
2615 if (statement_block_size($block) > 1) {
2616 #print "APW: ALLOWED: lines block<$block>\n";
2617 $allowed = 1;
2618 }
2619 # Check the post-context.
2620 if (defined $chunks[1]) {
2621 my ($cond, $block) = @{$chunks[1]};
2622 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002623 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002624 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002625 if ($block =~ /^\s*\{/) {
2626 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2627 $allowed = 1;
2628 }
2629 }
2630 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2631 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002632 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002633
Andy Whitcroftf0556632008-10-15 22:02:23 -07002634 for (my $n = 0; $n < $cnt; $n++) {
2635 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002636 }
2637
2638 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002639 }
2640 }
2641
Andy Whitcroft653d4872007-06-23 17:16:34 -07002642# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002643 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002644 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002645 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002646 }
2647 }
2648
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002649# don't use deprecated functions
2650 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002651 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002652 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002653 }
2654 }
2655
2656# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002657 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2658 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002659 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002660 }
2661
Andy Whitcroft00df3442007-06-08 13:47:06 -07002662# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002663 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002664 CHK("if this code is redundant consider removing it\n" .
2665 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002666 }
2667
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002668# check for needless kfree() checks
2669 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2670 my $expr = $1;
2671 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002672 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002673 }
2674 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002675# check for needless usb_free_urb() checks
2676 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2677 my $expr = $1;
2678 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2679 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2680 }
2681 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002682
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002683# prefer usleep_range over udelay
2684 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2685 # ignore udelay's < 10, however
2686 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2687 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2688 }
2689 }
2690
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002691# warn about unexpectedly long msleep's
2692 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2693 if ($1 < 20) {
2694 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2695 }
2696 }
2697
Andy Whitcroft00df3442007-06-08 13:47:06 -07002698# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002699# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002700# print "#ifdef in C files should be avoided\n";
2701# print "$herecurr";
2702# $clean = 0;
2703# }
2704
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002705# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002706 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002707 ERROR("exactly one space required after that #$1\n" . $herecurr);
2708 }
2709
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002710# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002711 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2712 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002713 my $which = $1;
2714 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002715 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002716 }
2717 }
2718# check for memory barriers without a comment.
2719 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2720 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002721 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002722 }
2723 }
2724# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002725 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002726 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002727 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002728
Tobias Klauserd4977c72010-05-24 14:33:30 -07002729# Check that the storage class is at the beginning of a declaration
2730 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2731 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2732 }
2733
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002734# check the location of the inline attribute, that it is between
2735# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002736 if ($line =~ /\b$Type\s+$Inline\b/ ||
2737 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002738 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2739 }
2740
Andy Whitcroft8905a672007-11-28 16:21:06 -08002741# Check for __inline__ and __inline, prefer inline
2742 if ($line =~ /\b(__inline__|__inline)\b/) {
2743 WARN("plain inline is preferred over $1\n" . $herecurr);
2744 }
2745
Joe Perches3d130fd2011-01-12 17:00:00 -08002746# Check for __attribute__ packed, prefer __packed
2747 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2748 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2749 }
2750
Joe Perches8f53a9b2010-03-05 13:43:48 -08002751# check for sizeof(&)
2752 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2753 WARN("sizeof(& should be avoided\n" . $herecurr);
2754 }
2755
Joe Perches428e2fd2011-05-24 17:13:39 -07002756# check for line continuations in quoted strings with odd counts of "
2757 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2758 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2759 }
2760
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002761# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002762 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002763 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002764 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002765 my $function_name = $1;
2766 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002767
2768 my $s = $stat;
2769 if (defined $cond) {
2770 substr($s, 0, length($cond), '');
2771 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002772 if ($s =~ /^\s*;/ &&
2773 $function_name ne 'uninitialized_var')
2774 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002775 WARN("externs should be avoided in .c files\n" . $herecurr);
2776 }
2777
2778 if ($paren_space =~ /\n/) {
2779 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2780 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002781
2782 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2783 $stat =~ /^.\s*extern\s+/)
2784 {
2785 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002786 }
2787
2788# checks for new __setup's
2789 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2790 my $name = $1;
2791
2792 if (!grep(/$name/, @setup_docs)) {
2793 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2794 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002795 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002796
2797# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002798 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002799 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2800 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002801
Joe Perchescaf2a542011-01-12 16:59:56 -08002802# check for multiple semicolons
2803 if ($line =~ /;\s*;\s*$/) {
2804 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2805 }
2806
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002807# check for gcc specific __FUNCTION__
2808 if ($line =~ /__FUNCTION__/) {
2809 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2810 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002811
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002812# check for semaphores initialized locked
2813 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002814 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002815
Andy Whitcroft773647a2008-03-28 14:15:58 -07002816 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002817# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002818 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002819 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002820 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002821# check for __initcall(), use device_initcall() explicitly please
2822 if ($line =~ /^.\s*__initcall\s*\(/) {
2823 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2824 }
Emese Revfy79404842010-03-05 13:43:53 -08002825# check for various ops structs, ensure they are const.
2826 my $struct_ops = qr{acpi_dock_ops|
2827 address_space_operations|
2828 backlight_ops|
2829 block_device_operations|
2830 dentry_operations|
2831 dev_pm_ops|
2832 dma_map_ops|
2833 extent_io_ops|
2834 file_lock_operations|
2835 file_operations|
2836 hv_ops|
2837 ide_dma_ops|
2838 intel_dvo_dev_ops|
2839 item_operations|
2840 iwl_ops|
2841 kgdb_arch|
2842 kgdb_io|
2843 kset_uevent_ops|
2844 lock_manager_operations|
2845 microcode_ops|
2846 mtrr_ops|
2847 neigh_ops|
2848 nlmsvc_binding|
2849 pci_raw_ops|
2850 pipe_buf_operations|
2851 platform_hibernation_ops|
2852 platform_suspend_ops|
2853 proto_ops|
2854 rpc_pipe_ops|
2855 seq_operations|
2856 snd_ac97_build_ops|
2857 soc_pcmcia_socket_ops|
2858 stacktrace_ops|
2859 sysfs_ops|
2860 tty_operations|
2861 usb_mon_operations|
2862 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002863 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08002864 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002865 WARN("struct $1 should normally be const\n" .
2866 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002867 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002868
2869# use of NR_CPUS is usually wrong
2870# ignore definitions of NR_CPUS and usage to define arrays as likely right
2871 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002872 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2873 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002874 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2875 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2876 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002877 {
2878 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2879 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002880
2881# check for %L{u,d,i} in strings
2882 my $string;
2883 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2884 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002885 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002886 if ($string =~ /(?<!%)%L[udi]/) {
2887 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2888 last;
2889 }
2890 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002891
2892# whine mightly about in_atomic
2893 if ($line =~ /\bin_atomic\s*\(/) {
2894 if ($realfile =~ m@^drivers/@) {
2895 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08002896 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002897 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2898 }
2899 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01002900
2901# check for lockdep_set_novalidate_class
2902 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2903 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2904 if ($realfile !~ m@^kernel/lockdep@ &&
2905 $realfile !~ m@^include/linux/lockdep@ &&
2906 $realfile !~ m@^drivers/base/core@) {
2907 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2908 }
2909 }
Dave Jones88f88312011-01-12 16:59:59 -08002910
2911 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
2912 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
2913 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
2914 }
Dave Jones309c00c2011-03-22 16:34:44 -07002915
2916 # Check for memset with swapped arguments
2917 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2918 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2919 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002920 }
2921
2922 # If we have no input at all, then there is nothing to report on
2923 # so just keep quiet.
2924 if ($#rawlines == -1) {
2925 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002926 }
2927
Andy Whitcroft8905a672007-11-28 16:21:06 -08002928 # In mailback mode only produce a report in the negative, for
2929 # things that appear to be patches.
2930 if ($mailback && ($clean == 1 || !$is_patch)) {
2931 exit(0);
2932 }
2933
2934 # This is not a patch, and we are are in 'no-patch' mode so
2935 # just keep quiet.
2936 if (!$chk_patch && !$is_patch) {
2937 exit(0);
2938 }
2939
2940 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002941 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002942 }
2943 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002944 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002945 }
2946
Andy Whitcroft8905a672007-11-28 16:21:06 -08002947 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002948 if ($summary && !($clean == 1 && $quiet == 1)) {
2949 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002950 print "total: $cnt_error errors, $cnt_warn warnings, " .
2951 (($check)? "$cnt_chk checks, " : "") .
2952 "$cnt_lines lines checked\n";
2953 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002954 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002955
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002956 if ($quiet == 0) {
2957 # If there were whitespace errors which cleanpatch can fix
2958 # then suggest that.
2959 if ($rpt_cleaners) {
2960 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2961 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07002962 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002963 }
2964 }
2965
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002966 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002967 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002968 }
2969 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002970 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002971 print "are false positives report them to the maintainer, see\n";
2972 print "CHECKPATCH in MAINTAINERS.\n";
2973 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002974
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002975 return $clean;
2976}