blob: 0d41625a1d07bb5c8e5b9bec4c586e29f2a5f6d5 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070010use constant BEFORE_SHORTTEXT => 0;
11use constant IN_SHORTTEXT => 1;
12use constant AFTER_SHORTTEXT => 2;
13use constant CHECK_NEXT_SHORTTEXT => 3;
14use constant SHORTTEXT_LIMIT => 75;
15
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070017$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070018
Andy Whitcroft267ad8f2010-10-26 14:23:19 -070019my $V = '0.31';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070020
21use Getopt::Long qw(:config no_auto_abbrev);
22
23my $quiet = 0;
24my $tree = 1;
25my $chk_signoff = 1;
26my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070027my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070028my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080029my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $file = 0;
31my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080032my $summary = 1;
33my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080034my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080036my %debug;
Hannes Eder77f5b102009-09-21 17:04:37 -070037my $help = 0;
38
39sub help {
40 my ($exitcode) = @_;
41
42 print << "EOM";
43Usage: $P [OPTION]... [FILE]...
44Version: $V
45
46Options:
47 -q, --quiet quiet
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
50 --patch treat FILE as patchfile (default)
51 --emacs emacs compile window format
52 --terse one line per report
53 -f, --file treat FILE as regular source file
54 --subjective, --strict enable more subjective tests
55 --root=PATH PATH to the kernel tree root
56 --no-summary suppress the per-file summary
57 --mailback only produce a report in case of warnings/errors
58 --summary-file include the filename in summary
59 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
60 'values', 'possible', 'type', and 'attr' (default
61 is all off)
62 --test-only=WORD report only warnings/errors containing WORD
63 literally
64 -h, --help, --version display this help and exit
65
66When FILE is - read standard input.
67EOM
68
69 exit($exitcode);
70}
71
Andy Whitcroft0a920b52007-06-01 00:46:48 -070072GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070073 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070074 'tree!' => \$tree,
75 'signoff!' => \$chk_signoff,
76 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070077 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080078 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070079 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070080 'subjective!' => \$check,
81 'strict!' => \$check,
82 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080083 'summary!' => \$summary,
84 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080085 'summary-file!' => \$summary_file,
86
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080087 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070088 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070089 'h|help' => \$help,
90 'version' => \$help
91) or help(1);
92
93help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070094
95my $exit = 0;
96
97if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070098 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070099 exit(1);
100}
101
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800102my $dbg_values = 0;
103my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700104my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700105my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800106for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800107 ## no critic
108 eval "\${dbg_$key} = '$debug{$key}';";
109 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800110}
111
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700112my $rpt_cleaners = 0;
113
Andy Whitcroft8905a672007-11-28 16:21:06 -0800114if ($terse) {
115 $emacs = 1;
116 $quiet++;
117}
118
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700119if ($tree) {
120 if (defined $root) {
121 if (!top_of_kernel_tree($root)) {
122 die "$P: $root: --root does not point at a valid tree\n";
123 }
124 } else {
125 if (top_of_kernel_tree('.')) {
126 $root = '.';
127 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
128 top_of_kernel_tree($1)) {
129 $root = $1;
130 }
131 }
132
133 if (!defined $root) {
134 print "Must be run from the top-level dir. of a kernel tree\n";
135 exit(2);
136 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700137}
138
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139my $emitted_corrupt = 0;
140
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700141our $Ident = qr{
142 [A-Za-z_][A-Za-z\d_]*
143 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
144 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700145our $Storage = qr{extern|static|asmlinkage};
146our $Sparse = qr{
147 __user|
148 __kernel|
149 __force|
150 __iomem|
151 __must_check|
152 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800153 __kprobes|
154 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700155 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800156
157# Notes to $Attribute:
158# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700159our $Attribute = qr{
160 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700161 __percpu|
162 __nocast|
163 __safe|
164 __bitwise__|
165 __packed__|
166 __packed2__|
167 __naked|
168 __maybe_unused|
169 __always_unused|
170 __noreturn|
171 __used|
172 __cold|
173 __noclone|
174 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700175 __read_mostly|
176 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800177 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700178 ____cacheline_aligned|
179 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800180 ____cacheline_internodealigned_in_smp|
181 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700182 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700183our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700184our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
186our $Lval = qr{$Ident(?:$Member)*};
187
188our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
189our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800190our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700191our $Operators = qr{
192 <=|>=|==|!=|
193 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800194 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 }x;
196
Andy Whitcroft8905a672007-11-28 16:21:06 -0800197our $NonptrType;
198our $Type;
199our $Declare;
200
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700201our $UTF8 = qr {
202 [\x09\x0A\x0D\x20-\x7E] # ASCII
203 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
204 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
205 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
206 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
207 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
208 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
209 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
210}x;
211
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700212our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700213 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700214 atomic_t
215)};
216
Joe Perches691e6692010-03-05 13:43:51 -0800217our $logFunctions = qr{(?x:
218 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700219 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800220 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700221 panic|
222 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800223)};
224
Andy Whitcroft8905a672007-11-28 16:21:06 -0800225our @typeList = (
226 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700227 qr{(?:unsigned\s+)?char},
228 qr{(?:unsigned\s+)?short},
229 qr{(?:unsigned\s+)?int},
230 qr{(?:unsigned\s+)?long},
231 qr{(?:unsigned\s+)?long\s+int},
232 qr{(?:unsigned\s+)?long\s+long},
233 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800234 qr{unsigned},
235 qr{float},
236 qr{double},
237 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800238 qr{struct\s+$Ident},
239 qr{union\s+$Ident},
240 qr{enum\s+$Ident},
241 qr{${Ident}_t},
242 qr{${Ident}_handler},
243 qr{${Ident}_handler_fn},
244);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700245our @modifierList = (
246 qr{fastcall},
247);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800248
Wolfram Sang7840a942010-08-09 17:20:57 -0700249our $allowed_asm_includes = qr{(?x:
250 irq|
251 memory
252)};
253# memory.h: ARM has a custom one
254
Andy Whitcroft8905a672007-11-28 16:21:06 -0800255sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700256 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
257 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700258 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800259 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700260 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800261 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700262 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700263 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700264 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800265 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700266 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800267 }x;
268 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700269 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800270 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700271 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800272 }x;
273 $Declare = qr{(?:$Storage\s+)?$Type};
274}
275build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700276
277$chk_signoff = 0 if ($file);
278
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700279my @dep_includes = ();
280my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700281my $removal = "Documentation/feature-removal-schedule.txt";
282if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800283 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800285 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700286 if (/^Check:\s+(.*\S)/) {
287 for my $entry (split(/[, ]+/, $1)) {
288 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700289 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700290
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700291 } elsif ($entry !~ m@/@) {
292 push(@dep_functions, $entry);
293 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700294 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700295 }
296 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800297 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700298}
299
Andy Whitcroft00df3442007-06-08 13:47:06 -0700300my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800301my @lines = ();
302my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800304 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700305 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800306 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700307 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800308 } elsif ($filename eq '-') {
309 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800311 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700312 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700313 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800314 if ($filename eq '-') {
315 $vname = 'Your patch';
316 } else {
317 $vname = $filename;
318 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800319 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700320 chomp;
321 push(@rawlines, $_);
322 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800324 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700325 $exit = 1;
326 }
327 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800328 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700329}
330
331exit($exit);
332
333sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700334 my ($root) = @_;
335
336 my @tree_check = (
337 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
338 "README", "Documentation", "arch", "include", "drivers",
339 "fs", "init", "ipc", "kernel", "lib", "scripts",
340 );
341
342 foreach my $check (@tree_check) {
343 if (! -e $root . '/' . $check) {
344 return 0;
345 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700346 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700347 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700348}
349
350sub expand_tabs {
351 my ($str) = @_;
352
353 my $res = '';
354 my $n = 0;
355 for my $c (split(//, $str)) {
356 if ($c eq "\t") {
357 $res .= ' ';
358 $n++;
359 for (; ($n % 8) != 0; $n++) {
360 $res .= ' ';
361 }
362 next;
363 }
364 $res .= $c;
365 $n++;
366 }
367
368 return $res;
369}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700371 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372 return $res;
373}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700374
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700375sub line_stats {
376 my ($line) = @_;
377
378 # Drop the diff line leader and expand tabs
379 $line =~ s/^.//;
380 $line = expand_tabs($line);
381
382 # Pick the indent from the front of the line.
383 my ($white) = ($line =~ /^(\s*)/);
384
385 return (length($line), length($white));
386}
387
Andy Whitcroft773647a2008-03-28 14:15:58 -0700388my $sanitise_quote = '';
389
390sub sanitise_line_reset {
391 my ($in_comment) = @_;
392
393 if ($in_comment) {
394 $sanitise_quote = '*/';
395 } else {
396 $sanitise_quote = '';
397 }
398}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700399sub sanitise_line {
400 my ($line) = @_;
401
402 my $res = '';
403 my $l = '';
404
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800405 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700406 my $off = 0;
407 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700408
Andy Whitcroft773647a2008-03-28 14:15:58 -0700409 # Always copy over the diff marker.
410 $res = substr($line, 0, 1);
411
412 for ($off = 1; $off < length($line); $off++) {
413 $c = substr($line, $off, 1);
414
415 # Comments we are wacking completly including the begin
416 # and end, all to $;.
417 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
418 $sanitise_quote = '*/';
419
420 substr($res, $off, 2, "$;$;");
421 $off++;
422 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800423 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700424 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700425 $sanitise_quote = '';
426 substr($res, $off, 2, "$;$;");
427 $off++;
428 next;
429 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700430 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
431 $sanitise_quote = '//';
432
433 substr($res, $off, 2, $sanitise_quote);
434 $off++;
435 next;
436 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700437
438 # A \ in a string means ignore the next character.
439 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
440 $c eq "\\") {
441 substr($res, $off, 2, 'XX');
442 $off++;
443 next;
444 }
445 # Regular quotes.
446 if ($c eq "'" || $c eq '"') {
447 if ($sanitise_quote eq '') {
448 $sanitise_quote = $c;
449
450 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700451 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700452 } elsif ($sanitise_quote eq $c) {
453 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700454 }
455 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700456
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800457 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700458 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
459 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700460 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
461 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700462 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
463 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700464 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700465 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700466 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800467 }
468
Daniel Walker113f04a2009-09-21 17:04:35 -0700469 if ($sanitise_quote eq '//') {
470 $sanitise_quote = '';
471 }
472
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800473 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700474 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800475 my $clean = 'X' x length($1);
476 $res =~ s@\<.*\>@<$clean>@;
477
478 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700479 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800480 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700481 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800482 }
483
Andy Whitcroft00df3442007-06-08 13:47:06 -0700484 return $res;
485}
486
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487sub ctx_statement_block {
488 my ($linenr, $remain, $off) = @_;
489 my $line = $linenr - 1;
490 my $blk = '';
491 my $soff = $off;
492 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700493 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800494
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800495 my $loff = 0;
496
Andy Whitcroft8905a672007-11-28 16:21:06 -0800497 my $type = '';
498 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800499 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800500 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800501 my $c;
502 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800503
504 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800505 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800506 @stack = (['', 0]) if ($#stack == -1);
507
Andy Whitcroft773647a2008-03-28 14:15:58 -0700508 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 # If we are about to drop off the end, pull in more
510 # context.
511 if ($off >= $len) {
512 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700513 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800514 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800515 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800516 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800517 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518 $len = length($blk);
519 $line++;
520 last;
521 }
522 # Bail if there is no further context.
523 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800524 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800525 last;
526 }
527 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800528 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800529 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800530 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800531
Andy Whitcroft773647a2008-03-28 14:15:58 -0700532 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800533
534 # Handle nested #if/#else.
535 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
536 push(@stack, [ $type, $level ]);
537 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
538 ($type, $level) = @{$stack[$#stack - 1]};
539 } elsif ($remainder =~ /^#\s*endif\b/) {
540 ($type, $level) = @{pop(@stack)};
541 }
542
Andy Whitcroft8905a672007-11-28 16:21:06 -0800543 # Statement ends at the ';' or a close '}' at the
544 # outermost level.
545 if ($level == 0 && $c eq ';') {
546 last;
547 }
548
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800549 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700550 if ($level == 0 && $coff_set == 0 &&
551 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
552 $remainder =~ /^(else)(?:\s|{)/ &&
553 $remainder !~ /^else\s+if\b/) {
554 $coff = $off + length($1) - 1;
555 $coff_set = 1;
556 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
557 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800558 }
559
Andy Whitcroft8905a672007-11-28 16:21:06 -0800560 if (($type eq '' || $type eq '(') && $c eq '(') {
561 $level++;
562 $type = '(';
563 }
564 if ($type eq '(' && $c eq ')') {
565 $level--;
566 $type = ($level != 0)? '(' : '';
567
568 if ($level == 0 && $coff < $soff) {
569 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700570 $coff_set = 1;
571 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800572 }
573 }
574 if (($type eq '' || $type eq '{') && $c eq '{') {
575 $level++;
576 $type = '{';
577 }
578 if ($type eq '{' && $c eq '}') {
579 $level--;
580 $type = ($level != 0)? '{' : '';
581
582 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700583 if (substr($blk, $off + 1, 1) eq ';') {
584 $off++;
585 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800586 last;
587 }
588 }
589 $off++;
590 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700591 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800592 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700593 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800594 $line++;
595 $remain--;
596 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800597
598 my $statement = substr($blk, $soff, $off - $soff + 1);
599 my $condition = substr($blk, $soff, $coff - $soff + 1);
600
601 #warn "STATEMENT<$statement>\n";
602 #warn "CONDITION<$condition>\n";
603
Andy Whitcroft773647a2008-03-28 14:15:58 -0700604 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800605
606 return ($statement, $condition,
607 $line, $remain + 1, $off - $loff + 1, $level);
608}
609
Andy Whitcroftcf655042008-03-04 14:28:20 -0800610sub statement_lines {
611 my ($stmt) = @_;
612
613 # Strip the diff line prefixes and rip blank lines at start and end.
614 $stmt =~ s/(^|\n)./$1/g;
615 $stmt =~ s/^\s*//;
616 $stmt =~ s/\s*$//;
617
618 my @stmt_lines = ($stmt =~ /\n/g);
619
620 return $#stmt_lines + 2;
621}
622
623sub statement_rawlines {
624 my ($stmt) = @_;
625
626 my @stmt_lines = ($stmt =~ /\n/g);
627
628 return $#stmt_lines + 2;
629}
630
631sub statement_block_size {
632 my ($stmt) = @_;
633
634 $stmt =~ s/(^|\n)./$1/g;
635 $stmt =~ s/^\s*{//;
636 $stmt =~ s/}\s*$//;
637 $stmt =~ s/^\s*//;
638 $stmt =~ s/\s*$//;
639
640 my @stmt_lines = ($stmt =~ /\n/g);
641 my @stmt_statements = ($stmt =~ /;/g);
642
643 my $stmt_lines = $#stmt_lines + 2;
644 my $stmt_statements = $#stmt_statements + 1;
645
646 if ($stmt_lines > $stmt_statements) {
647 return $stmt_lines;
648 } else {
649 return $stmt_statements;
650 }
651}
652
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800653sub ctx_statement_full {
654 my ($linenr, $remain, $off) = @_;
655 my ($statement, $condition, $level);
656
657 my (@chunks);
658
Andy Whitcroftcf655042008-03-04 14:28:20 -0800659 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 ($statement, $condition, $linenr, $remain, $off, $level) =
661 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700662 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800663 push(@chunks, [ $condition, $statement ]);
664 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
665 return ($level, $linenr, @chunks);
666 }
667
668 # Pull in the following conditional/block pairs and see if they
669 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800670 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800671 ($statement, $condition, $linenr, $remain, $off, $level) =
672 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800673 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700674 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800675 #print "C: push\n";
676 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800677 }
678
679 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680}
681
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700682sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700683 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700684 my $line;
685 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700686 my $blk = '';
687 my @o;
688 my @c;
689 my @res = ();
690
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700691 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800692 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700693 for ($line = $start; $remain > 0; $line++) {
694 next if ($rawlines[$line] =~ /^-/);
695 $remain--;
696
697 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800698
699 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700700 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800701 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700702 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800703 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700704 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800705 $level = pop(@stack);
706 }
707
Andy Whitcroft01464f32010-10-26 14:23:19 -0700708 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700709 ##print "C<$c>L<$level><$open$close>O<$off>\n";
710 if ($off > 0) {
711 $off--;
712 next;
713 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700714
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700715 if ($c eq $close && $level > 0) {
716 $level--;
717 last if ($level == 0);
718 } elsif ($c eq $open) {
719 $level++;
720 }
721 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700722
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700723 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700724 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700725 }
726
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700727 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700728 }
729
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700730 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700731}
732sub ctx_block_outer {
733 my ($linenr, $remain) = @_;
734
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700735 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
736 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700737}
738sub ctx_block {
739 my ($linenr, $remain) = @_;
740
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700741 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
742 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700743}
744sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700745 my ($linenr, $remain, $off) = @_;
746
747 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
748 return @r;
749}
750sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700751 my ($linenr, $remain) = @_;
752
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700753 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700754}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700755sub ctx_statement_level {
756 my ($linenr, $remain, $off) = @_;
757
758 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
759}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700760
761sub ctx_locate_comment {
762 my ($first_line, $end_line) = @_;
763
764 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700765 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700766 return $current_comment if (defined $current_comment);
767
768 # Look through the context and try and figure out if there is a
769 # comment.
770 my $in_comment = 0;
771 $current_comment = '';
772 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700773 my $line = $rawlines[$linenr - 1];
774 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700775 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
776 $in_comment = 1;
777 }
778 if ($line =~ m@/\*@) {
779 $in_comment = 1;
780 }
781 if (!$in_comment && $current_comment ne '') {
782 $current_comment = '';
783 }
784 $current_comment .= $line . "\n" if ($in_comment);
785 if ($line =~ m@\*/@) {
786 $in_comment = 0;
787 }
788 }
789
790 chomp($current_comment);
791 return($current_comment);
792}
793sub ctx_has_comment {
794 my ($first_line, $end_line) = @_;
795 my $cmt = ctx_locate_comment($first_line, $end_line);
796
Andy Whitcroft00df3442007-06-08 13:47:06 -0700797 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700798 ##print "CMMT: $cmt\n";
799
800 return ($cmt ne '');
801}
802
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700803sub raw_line {
804 my ($linenr, $cnt) = @_;
805
806 my $offset = $linenr - 1;
807 $cnt++;
808
809 my $line;
810 while ($cnt) {
811 $line = $rawlines[$offset++];
812 next if (defined($line) && $line =~ /^-/);
813 $cnt--;
814 }
815
816 return $line;
817}
818
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700819sub cat_vet {
820 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700821 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700822
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700823 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700824 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
825 $res .= $1;
826 if ($2 ne '') {
827 $coded = sprintf("^%c", unpack('C', $2) + 64);
828 $res .= $coded;
829 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700830 }
831 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700832
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700833 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700834}
835
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800836my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800837my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800838my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700839my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800840
841sub annotate_reset {
842 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800843 $av_pending = '_';
844 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700845 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800846}
847
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700848sub annotate_values {
849 my ($stream, $type) = @_;
850
851 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700852 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 my $cur = $stream;
854
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800855 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700856
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700857 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700858 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800859 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700860 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700861 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800862 print "WS($1)\n" if ($dbg_values > 1);
863 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800864 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800865 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700866 }
867
Florian Micklerc023e4732011-01-12 16:59:58 -0800868 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700869 print "CAST($1)\n" if ($dbg_values > 1);
870 push(@av_paren_type, $type);
871 $type = 'C';
872
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700873 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700875 $type = 'T';
876
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700877 } elsif ($cur =~ /^($Modifier)\s*/) {
878 print "MODIFIER($1)\n" if ($dbg_values > 1);
879 $type = 'T';
880
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700881 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700882 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800883 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700884 push(@av_paren_type, $type);
885 if ($2 ne '') {
886 $av_pending = 'N';
887 }
888 $type = 'E';
889
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700890 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700891 print "UNDEF($1)\n" if ($dbg_values > 1);
892 $av_preprocessor = 1;
893 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700894
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700895 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800896 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800897 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800898
899 push(@av_paren_type, $type);
900 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700901 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800902
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700903 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800904 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
905 $av_preprocessor = 1;
906
907 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
908
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700909 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800910
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700911 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800912 print "PRE_END($1)\n" if ($dbg_values > 1);
913
914 $av_preprocessor = 1;
915
916 # Assume all arms of the conditional end as this
917 # one does, and continue as if the #endif was not here.
918 pop(@av_paren_type);
919 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700920 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700921
922 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800923 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700924
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700925 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
926 print "ATTR($1)\n" if ($dbg_values > 1);
927 $av_pending = $type;
928 $type = 'N';
929
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700930 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800931 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800933 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700934 }
935 $type = 'N';
936
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700937 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800938 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700939 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940 $type = 'N';
941
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700942 } elsif ($cur =~/^(case)/o) {
943 print "CASE($1)\n" if ($dbg_values > 1);
944 $av_pend_colon = 'C';
945 $type = 'N';
946
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700947 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800948 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700949 $type = 'N';
950
951 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800952 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800953 push(@av_paren_type, $av_pending);
954 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700955 $type = 'N';
956
957 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800958 my $new_type = pop(@av_paren_type);
959 if ($new_type ne '_') {
960 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800961 print "PAREN('$1') -> $type\n"
962 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700963 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800964 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700965 }
966
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700967 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800968 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700969 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800970 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700971
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800972 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
973 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700974 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800975 } elsif ($type eq 'E') {
976 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700977 }
978 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
979 $type = 'V';
980
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700981 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800982 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700983 $type = 'V';
984
985 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800986 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700987 $type = 'N';
988
Andy Whitcroftcf655042008-03-04 14:28:20 -0800989 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800990 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800991 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700992 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800993
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800994 } elsif ($cur =~/^(,)/) {
995 print "COMMA($1)\n" if ($dbg_values > 1);
996 $type = 'C';
997
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700998 } elsif ($cur =~ /^(\?)/o) {
999 print "QUESTION($1)\n" if ($dbg_values > 1);
1000 $type = 'N';
1001
1002 } elsif ($cur =~ /^(:)/o) {
1003 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1004
1005 substr($var, length($res), 1, $av_pend_colon);
1006 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1007 $type = 'E';
1008 } else {
1009 $type = 'N';
1010 }
1011 $av_pend_colon = 'O';
1012
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001013 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001014 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001015 $type = 'N';
1016
Andy Whitcroft0d413862008-10-15 22:02:16 -07001017 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001018 my $variant;
1019
1020 print "OPV($1)\n" if ($dbg_values > 1);
1021 if ($type eq 'V') {
1022 $variant = 'B';
1023 } else {
1024 $variant = 'U';
1025 }
1026
1027 substr($var, length($res), 1, $variant);
1028 $type = 'N';
1029
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001030 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032 if ($1 ne '++' && $1 ne '--') {
1033 $type = 'N';
1034 }
1035
1036 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 }
1039 if (defined $1) {
1040 $cur = substr($cur, length($1));
1041 $res .= $type x length($1);
1042 }
1043 }
1044
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001045 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001046}
1047
Andy Whitcroft8905a672007-11-28 16:21:06 -08001048sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001049 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001050 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001051 ^(?:
1052 $Modifier|
1053 $Storage|
1054 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001055 DEFINE_\S+
1056 )$|
1057 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001058 goto|
1059 return|
1060 case|
1061 else|
1062 asm|__asm__|
1063 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001064 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001065 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001066 )}x;
1067 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1068 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001069 # Check for modifiers.
1070 $possible =~ s/\s*$Storage\s*//g;
1071 $possible =~ s/\s*$Sparse\s*//g;
1072 if ($possible =~ /^\s*$/) {
1073
1074 } elsif ($possible =~ /\s/) {
1075 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001076 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001077 if ($modifier !~ $notPermitted) {
1078 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1079 push(@modifierList, $modifier);
1080 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001081 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001082
1083 } else {
1084 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1085 push(@typeList, $possible);
1086 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001088 } else {
1089 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001090 }
1091}
1092
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001093my $prefix = '';
1094
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001095sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001096 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1097 return 0;
1098 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001099 my $line = $prefix . $_[0];
1100
1101 $line = (split('\n', $line))[0] . "\n" if ($terse);
1102
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001103 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001104
1105 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001106}
1107sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001108 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001109}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001110sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 if (report("ERROR: $_[0]\n")) {
1112 our $clean = 0;
1113 our $cnt_error++;
1114 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001115}
1116sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001117 if (report("WARNING: $_[0]\n")) {
1118 our $clean = 0;
1119 our $cnt_warn++;
1120 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001121}
1122sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001123 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001124 our $clean = 0;
1125 our $cnt_chk++;
1126 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001127}
1128
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001129sub check_absolute_file {
1130 my ($absolute, $herecurr) = @_;
1131 my $file = $absolute;
1132
1133 ##print "absolute<$absolute>\n";
1134
1135 # See if any suffix of this path is a path within the tree.
1136 while ($file =~ s@^[^/]*/@@) {
1137 if (-f "$root/$file") {
1138 ##print "file<$file>\n";
1139 last;
1140 }
1141 }
1142 if (! -f _) {
1143 return 0;
1144 }
1145
1146 # It is, so see if the prefix is acceptable.
1147 my $prefix = $absolute;
1148 substr($prefix, -length($file)) = '';
1149
1150 ##print "prefix<$prefix>\n";
1151 if ($prefix ne ".../") {
1152 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1153 }
1154}
1155
Gregory Bean3f66eea2011-07-19 16:37:49 -07001156sub cleanup_continuation_headers {
1157 # Collapse any header-continuation lines into a single line so they
1158 # can be parsed meaningfully, as the parser only has one line
1159 # of context to work with.
1160 my $again;
1161 do {
1162 $again = 0;
1163 foreach my $n (0 .. scalar(@rawlines) - 2) {
1164 if ($rawlines[$n]=~/^\s*$/) {
1165 # A blank line means there's no more chance
1166 # of finding headers. Shortcut to done.
1167 return;
1168 }
1169 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
1170 $rawlines[$n+1]=~/^\s+/) {
1171 # Continuation header. Collapse it.
1172 my $line = splice @rawlines, $n+1, 1;
1173 $line=~s/^\s+/ /;
1174 $rawlines[$n] .= $line;
1175 # We've 'destabilized' the list, so restart.
1176 $again = 1;
1177 last;
1178 }
1179 }
1180 } while ($again);
1181}
1182
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001183sub process {
1184 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001185
1186 my $linenr=0;
1187 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001189 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001190 my $stashrawline="";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191 my $subjectline="";
1192 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001193
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001194 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001195 my $indent;
1196 my $previndent=0;
1197 my $stashindent=0;
1198
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001199 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001200 my $signoff = 0;
1201 my $is_patch = 0;
1202
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001203 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001204 our $cnt_lines = 0;
1205 our $cnt_error = 0;
1206 our $cnt_warn = 0;
1207 our $cnt_chk = 0;
1208
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001209 # Trace the real file/line as we go.
1210 my $realfile = '';
1211 my $realline = 0;
1212 my $realcnt = 0;
1213 my $here = '';
1214 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001216 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001217 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001218
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001219 my $prev_values = 'E';
1220
1221 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001222 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001223 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001224 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001225
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001226 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001227 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001228 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001229 my @setup_docs = ();
1230 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 my $exec_file = "";
1233
1234 my $shorttext = BEFORE_SHORTTEXT;
1235 my $shorttext_exspc = 0;
1236
Andy Whitcroft773647a2008-03-28 14:15:58 -07001237 sanitise_line_reset();
Gregory Bean3f66eea2011-07-19 16:37:49 -07001238 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001239 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001241 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001242 $linenr++;
1243 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001244
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001246 $setup_docs = 0;
1247 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1248 $setup_docs = 1;
1249 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001250 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001251 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1253 $realline=$1-1;
1254 if (defined $2) {
1255 $realcnt=$3+1;
1256 } else {
1257 $realcnt=1+1;
1258 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001259 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260
1261 # Guestimate if this is a continuing comment. Run
1262 # the context looking for a comment "edge". If this
1263 # edge is a close comment then we must be in a comment
1264 # at context start.
1265 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001266 my $cnt = $realcnt;
1267 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1268 next if (defined $rawlines[$ln - 1] &&
1269 $rawlines[$ln - 1] =~ /^-/);
1270 $cnt--;
1271 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001272 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001273 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1274 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1275 ($edge) = $1;
1276 last;
1277 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001278 }
1279 if (defined $edge && $edge eq '*/') {
1280 $in_comment = 1;
1281 }
1282
1283 # Guestimate if this is a continuing comment. If this
1284 # is the start of a diff block and this line starts
1285 # ' *' then it is very likely a comment.
1286 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001287 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001288 {
1289 $in_comment = 1;
1290 }
1291
1292 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1293 sanitise_line_reset($in_comment);
1294
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001295 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001297 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298 $line = sanitise_line($rawline);
1299 }
1300 push(@lines, $line);
1301
1302 if ($realcnt > 1) {
1303 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1304 } else {
1305 $realcnt = 0;
1306 }
1307
1308 #print "==>$rawline\n";
1309 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001310
1311 if ($setup_docs && $line =~ /^\+/) {
1312 push(@setup_docs, $line);
1313 }
1314 }
1315
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001316 $prefix = '';
1317
Andy Whitcroft773647a2008-03-28 14:15:58 -07001318 $realcnt = 0;
1319 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001320 foreach my $line (@lines) {
1321 $linenr++;
1322
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001324
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001325#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001327 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001328 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001329 $realline=$1-1;
1330 if (defined $2) {
1331 $realcnt=$3+1;
1332 } else {
1333 $realcnt=1+1;
1334 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001335 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001336 $prev_values = 'E';
1337
Andy Whitcroft773647a2008-03-28 14:15:58 -07001338 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001339 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001340 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001341 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001342
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001343# track the line number as we move through the hunk, note that
1344# new versions of GNU diff omit the leading space on completely
1345# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001346 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001347 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001348 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001349
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001350 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001351 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001352
1353 # Track the previous line.
1354 ($prevline, $stashline) = ($stashline, $line);
1355 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1357
Andy Whitcroft773647a2008-03-28 14:15:58 -07001358 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001359
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001360 } elsif ($realcnt == 1) {
1361 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001362 }
1363
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001364 my $hunk_line = ($realcnt != 0);
1365
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001366#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 $prefix = "$filename:$realline: " if ($emacs && $file);
1368 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1369
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001370 $here = "#$linenr: " if (!$file);
1371 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001372
1373 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001374 if ($line =~ /^diff --git.*?(\S+)$/) {
1375 $realfile = $1;
1376 $realfile =~ s@^([^/]*)/@@;
Gregory Bean246e1f12011-10-17 12:27:01 -07001377 $exec_file = $realfile;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001378
1379 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001380 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001381 $realfile =~ s@^([^/]*)/@@;
1382
1383 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001384 if (!$file && $tree && $p1_prefix ne '' &&
1385 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001386 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1387 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001388
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001389 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1391 }
Gregory Bean246e1f12011-10-17 12:27:01 -07001392 $exec_file = "";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001393 next;
1394 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1396 $exec_file = $1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 }
1398 #Check state to make sure we aren't in code block.
Gregory Bean246e1f12011-10-17 12:27:01 -07001399 elsif (($exec_file =~ /^.+\.[chS]$/ or
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001400 $exec_file =~ /^.+\.txt$/ or
1401 $exec_file =~ /^.+\.ihex$/ or
1402 $exec_file =~ /^.+\.hex$/ or
1403 $exec_file =~ /^.+\.HEX$/ or
1404 $exec_file =~ /^.+defconfig$/ or
1405 $exec_file =~ /^Makefile$/ or
1406 $exec_file =~ /^Kconfig$/) &&
1407 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1408 (oct($2) & 0111)) {
1409 ERROR("Source file has +x permissions: " .
1410 "$exec_file\n");
1411 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001412 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001413
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001414 my $hereline = "$here\n$rawline\n";
1415 my $herecurr = "$here\n$rawline\n";
1416 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001417
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 if ($shorttext != AFTER_SHORTTEXT) {
1419 if ($shorttext == IN_SHORTTEXT) {
1420 if ($line=~/^---/ || $line=~/^diff.*/) {
1421 $shorttext = AFTER_SHORTTEXT;
1422 } elsif (length($line) > (SHORTTEXT_LIMIT +
1423 $shorttext_exspc)
1424 && $line !~ /^:([0-7]{6}\s){2}
1425 ([[:xdigit:]]+\.*
1426 \s){2}\w+\s\w+/xms) {
1427 WARN("commit text line over " .
1428 SHORTTEXT_LIMIT .
1429 " characters\n" . $herecurr);
1430 }
1431 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1432# The Subject line doesn't have to be the last header in the patch.
1433# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1434# Per RFC5322, continuation lines must be folded, so any left-justified
1435# text which looks like a header is definitely a header.
1436 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1437 $shorttext = IN_SHORTTEXT;
1438# Check for Subject line followed by a blank line.
1439 if (length($line) != 0) {
1440 WARN("non-blank line after " .
1441 "summary line\n" .
1442 $sublinenr . $here .
1443 "\n" . $subjectline .
1444 "\n" . $line . "\n");
1445 }
1446 }
1447 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
1448 $shorttext = CHECK_NEXT_SHORTTEXT;
1449 $subjectline = $line;
1450 $sublinenr = "#$linenr & ";
1451# Check for Subject line less than line limit
1452 if (length($1) > SHORTTEXT_LIMIT) {
1453 WARN("summary line over " .
1454 SHORTTEXT_LIMIT .
1455 " characters\n" . $herecurr);
1456 }
1457 } elsif ($line=~/^ (.*)/) {
1458 $shorttext = IN_SHORTTEXT;
1459 $shorttext_exspc = 4;
1460 if (length($1) > SHORTTEXT_LIMIT) {
1461 WARN("summary line over " .
1462 SHORTTEXT_LIMIT .
1463 " characters\n" . $herecurr);
1464 }
1465 }
1466 }
1467
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001468 $cnt_lines++ if ($realcnt != 0);
1469
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001470# Check for incorrect file permissions
1471 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1472 my $permhere = $here . "FILE: $realfile\n";
1473 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1474 ERROR("do not set execute permissions for source files\n" . $permhere);
1475 }
1476 }
1477
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001478#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001479 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001480 # This is a signoff, if ugly, so do not double report.
1481 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001482 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001483 WARN("Signed-off-by: is the preferred form\n" .
1484 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001485 }
1486 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001487 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001488 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001489 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
1491 WARN("invalid Signed-off-by identity\n" . $line );
1492 }
1493 }
1494
1495#check the patch for invalid author credentials
1496 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
1497 WARN("invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001498 }
1499
Andy Whitcroft00df3442007-06-08 13:47:06 -07001500# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001501 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001502 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001504 }
1505
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001506# Check for absolute kernel paths.
1507 if ($tree) {
1508 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1509 my $file = $1;
1510
1511 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1512 check_absolute_file($1, $herecurr)) {
1513 #
1514 } else {
1515 check_absolute_file($file, $herecurr);
1516 }
1517 }
1518 }
1519
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001520# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1521 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001522 $rawline !~ m/^$UTF8*$/) {
1523 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1524
1525 my $blank = copy_spacing($rawline);
1526 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1527 my $hereptr = "$hereline$ptr\n";
1528
1529 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001530 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001531
Andy Whitcroft306708542008-10-15 22:02:28 -07001532# ignore non-hunk lines and lines being removed
1533 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001534
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001535#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001536 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001537 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001538 ERROR("DOS line endings\n" . $herevet);
1539
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001540 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1541 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001542 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001543 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001544 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001545
1546# check we are in a valid source file if not then ignore this hunk
1547 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1548
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001549#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001550 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001551 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001552 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001553 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001555 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001556 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001557 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001558 }
1559
Joe Perches5e79d962010-03-05 13:43:55 -08001560# check for spaces before a quoted newline
1561 if ($rawline =~ /^.*\".*\s\\n/) {
1562 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1563 }
1564
Andy Whitcroft8905a672007-11-28 16:21:06 -08001565# check for adding lines without a newline.
1566 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1567 WARN("adding a line without newline at end of file\n" . $herecurr);
1568 }
1569
Mike Frysinger42e41c52009-09-21 17:04:40 -07001570# Blackfin: use hi/lo macros
1571 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1572 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1573 my $herevet = "$here\n" . cat_vet($line) . "\n";
1574 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1575 }
1576 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1577 my $herevet = "$here\n" . cat_vet($line) . "\n";
1578 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1579 }
1580 }
1581
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001582# check we are in a valid source file C or perl if not then ignore this hunk
1583 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001584
1585# at the beginning of a line any tabs must come first and anything
1586# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001587 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1588 $rawline =~ /^\+\s* \s*/) {
1589 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001590 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001591 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001592 }
1593
Alberto Panizzo08e44362010-03-05 13:43:54 -08001594# check for space before tabs.
1595 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1596 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1597 WARN("please, no space before tabs\n" . $herevet);
1598 }
1599
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001600# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001601# Exceptions:
1602# 1) within comments
1603# 2) indented preprocessor commands
1604# 3) hanging labels
1605 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001606 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001607 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001608 }
1609
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001610# check we are in a valid C source file if not then ignore this hunk
1611 next if ($realfile !~ /\.(h|c)$/);
1612
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001613# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001614 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001615 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1616 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001617
Mike Frysinger42e41c52009-09-21 17:04:40 -07001618# Blackfin: don't use __builtin_bfin_[cs]sync
1619 if ($line =~ /__builtin_bfin_csync/) {
1620 my $herevet = "$here\n" . cat_vet($line) . "\n";
1621 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1622 }
1623 if ($line =~ /__builtin_bfin_ssync/) {
1624 my $herevet = "$here\n" . cat_vet($line) . "\n";
1625 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1626 }
1627
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001628# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001629 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1630 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001631 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001632 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001633 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001634 $stat =~ s/\n./\n /g;
1635 $cond =~ s/\n./\n /g;
1636
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001637 # Find the real next line.
1638 $realline_next = $line_nr_next;
1639 if (defined $realline_next &&
1640 (!defined $lines[$realline_next - 1] ||
1641 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1642 $realline_next++;
1643 }
1644
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001645 my $s = $stat;
1646 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001647
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001648 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001649 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001650
1651 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001652 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001653
Andy Whitcroft463f2862009-09-21 17:04:34 -07001654 } elsif ($s =~ /^.\s*else\b/s) {
1655
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001656 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001657 } 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 -07001658 my $type = $1;
1659 $type =~ s/\s+/ /g;
1660 possible($type, "A:" . $s);
1661
Andy Whitcroft8905a672007-11-28 16:21:06 -08001662 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001663 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001664 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001665 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001666
1667 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001668 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001669 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001670 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001671
1672 # Check for any sort of function declaration.
1673 # int foo(something bar, other baz);
1674 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001675 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 -08001676 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001677
Andy Whitcroftcf655042008-03-04 14:28:20 -08001678 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001679 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001680 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001681
Andy Whitcroft8905a672007-11-28 16:21:06 -08001682 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001683 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001684
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001685 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001686 }
1687 }
1688 }
1689
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001690 }
1691
Andy Whitcroft653d4872007-06-23 17:16:34 -07001692#
1693# Checks which may be anchored in the context.
1694#
1695
1696# Check for switch () and associated case and default
1697# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001698 if ($line=~/\bswitch\s*\(.*\)/) {
1699 my $err = '';
1700 my $sep = '';
1701 my @ctx = ctx_block_outer($linenr, $realcnt);
1702 shift(@ctx);
1703 for my $ctx (@ctx) {
1704 my ($clen, $cindent) = line_stats($ctx);
1705 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1706 $indent != $cindent) {
1707 $err .= "$sep$ctx\n";
1708 $sep = '';
1709 } else {
1710 $sep = "[...]\n";
1711 }
1712 }
1713 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001714 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001715 }
1716 }
1717
1718# if/while/etc brace do not go on next line, unless defining a do while loop,
1719# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001720 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001721 my $pre_ctx = "$1$2";
1722
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001723 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001724 my $ctx_cnt = $realcnt - $#ctx - 1;
1725 my $ctx = join("\n", @ctx);
1726
Andy Whitcroft548596d2008-07-23 21:29:01 -07001727 my $ctx_ln = $linenr;
1728 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001729
Andy Whitcroft548596d2008-07-23 21:29:01 -07001730 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1731 defined $lines[$ctx_ln - 1] &&
1732 $lines[$ctx_ln - 1] =~ /^-/)) {
1733 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1734 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001735 $ctx_ln++;
1736 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001737
Andy Whitcroft53210162008-07-23 21:29:03 -07001738 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1739 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001740
1741 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1742 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001743 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001744 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001745 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1746 $ctx =~ /\)\s*\;\s*$/ &&
1747 defined $lines[$ctx_ln - 1])
1748 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001749 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1750 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001751 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001752 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001753 }
1754 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001755 }
1756
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001757# Check relative indent for conditionals and blocks.
1758 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1759 my ($s, $c) = ($stat, $cond);
1760
1761 substr($s, 0, length($c), '');
1762
1763 # Make sure we remove the line prefixes as we have
1764 # none on the first line, and are going to readd them
1765 # where necessary.
1766 $s =~ s/\n./\n/gs;
1767
1768 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001769 my @newlines = ($c =~ /\n/gs);
1770 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001771
1772 # We want to check the first line inside the block
1773 # starting at the end of the conditional, so remove:
1774 # 1) any blank line termination
1775 # 2) any opening brace { on end of the line
1776 # 3) any do (...) {
1777 my $continuation = 0;
1778 my $check = 0;
1779 $s =~ s/^.*\bdo\b//;
1780 $s =~ s/^\s*{//;
1781 if ($s =~ s/^\s*\\//) {
1782 $continuation = 1;
1783 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001784 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001785 $check = 1;
1786 $cond_lines++;
1787 }
1788
1789 # Also ignore a loop construct at the end of a
1790 # preprocessor statement.
1791 if (($prevline =~ /^.\s*#\s*define\s/ ||
1792 $prevline =~ /\\\s*$/) && $continuation == 0) {
1793 $check = 0;
1794 }
1795
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001796 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001797 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001798 while ($cond_ptr != $cond_lines) {
1799 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001800
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001801 # If we see an #else/#elif then the code
1802 # is not linear.
1803 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1804 $check = 0;
1805 }
1806
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001807 # Ignore:
1808 # 1) blank lines, they should be at 0,
1809 # 2) preprocessor lines, and
1810 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001811 if ($continuation ||
1812 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001813 $s =~ /^\s*#\s*?/ ||
1814 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001815 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001816 if ($s =~ s/^.*?\n//) {
1817 $cond_lines++;
1818 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001819 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001820 }
1821
1822 my (undef, $sindent) = line_stats("+" . $s);
1823 my $stat_real = raw_line($linenr, $cond_lines);
1824
1825 # Check if either of these lines are modified, else
1826 # this is not this patch's fault.
1827 if (!defined($stat_real) ||
1828 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1829 $check = 0;
1830 }
1831 if (defined($stat_real) && $cond_lines > 1) {
1832 $stat_real = "[...]\n$stat_real";
1833 }
1834
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001835 #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 -07001836
1837 if ($check && (($sindent % 8) != 0 ||
1838 ($sindent <= $indent && $s ne ''))) {
1839 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1840 }
1841 }
1842
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001843 # Track the 'values' across context and added lines.
1844 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001845 my ($curr_values, $curr_vars) =
1846 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001847 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 if ($dbg_values) {
1849 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001850 print "$linenr > .$outline\n";
1851 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001852 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001853 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001854 $prev_values = substr($curr_values, -1);
1855
Andy Whitcroft00df3442007-06-08 13:47:06 -07001856#ignore lines not being added
1857 if ($line=~/^[^\+]/) {next;}
1858
Andy Whitcroft653d4872007-06-23 17:16:34 -07001859# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001860 if ($dbg_type) {
1861 if ($line =~ /^.\s*$Declare\s*$/) {
1862 ERROR("TEST: is type\n" . $herecurr);
1863 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1864 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1865 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001866 next;
1867 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001868# TEST: allow direct testing of the attribute matcher.
1869 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001870 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001871 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001872 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001873 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1874 }
1875 next;
1876 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001877
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001878# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001879 if ($line =~ /^.\s*{/ &&
1880 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001881 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001882 }
1883
Andy Whitcroft653d4872007-06-23 17:16:34 -07001884#
1885# Checks which are anchored on the added line.
1886#
1887
1888# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001889 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001890 my $path = $1;
1891 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001892 ERROR("malformed #include filename\n" .
1893 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001894 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001895 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001896
1897# no C99 // comments
1898 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001899 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001900 }
1901 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001902 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001903 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001904
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001905# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1906# the whole statement.
1907#print "APW <$lines[$realline_next - 1]>\n";
1908 if (defined $realline_next &&
1909 exists $lines[$realline_next - 1] &&
1910 !defined $suppress_export{$realline_next} &&
1911 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1912 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001913 # Handle definitions which produce identifiers with
1914 # a prefix:
1915 # XXX(foo);
1916 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001917 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001918 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1919 $name =~ /^${Ident}_$2/) {
1920#print "FOO C name<$name>\n";
1921 $suppress_export{$realline_next} = 1;
1922
1923 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001924 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001925 ^.DEFINE_$Ident\(\Q$name\E\)|
1926 ^.DECLARE_$Ident\(\Q$name\E\)|
1927 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001928 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1929 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001930 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001931#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1932 $suppress_export{$realline_next} = 2;
1933 } else {
1934 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001935 }
1936 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001937 if (!defined $suppress_export{$linenr} &&
1938 $prevline =~ /^.\s*$/ &&
1939 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1940 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1941#print "FOO B <$lines[$linenr - 1]>\n";
1942 $suppress_export{$linenr} = 2;
1943 }
1944 if (defined $suppress_export{$linenr} &&
1945 $suppress_export{$linenr} == 2) {
1946 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1947 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001948
Joe Eloff5150bda2010-08-09 17:21:00 -07001949# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001950 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001951 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001952 $herecurr);
1953 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001954# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001955 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001956 ERROR("do not initialise statics to 0 or NULL\n" .
1957 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001958 }
1959
Joe Perchescb710ec2010-10-26 14:23:20 -07001960# check for static char foo[] = "bar" declarations.
1961 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1962 WARN("static char array declaration should probably be static const char\n" .
1963 $herecurr);
1964 }
1965
Joe Perches93ed0e22010-10-26 14:23:21 -07001966# check for declarations of struct pci_device_id
1967 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1968 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1969 }
1970
Andy Whitcroft653d4872007-06-23 17:16:34 -07001971# check for new typedefs, only function parameters and sparse annotations
1972# make sense.
1973 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001974 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001975 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001976 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001977 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001978 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001979 }
1980
1981# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001982 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001983 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001984 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001985
Andy Whitcroft65863862009-01-06 14:41:21 -08001986 # Should start with a space.
1987 $to =~ s/^(\S)/ $1/;
1988 # Should not end with a space.
1989 $to =~ s/\s+$//;
1990 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001991 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001992 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001993
Andy Whitcroft65863862009-01-06 14:41:21 -08001994 #print "from<$from> to<$to>\n";
1995 if ($from ne $to) {
1996 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1997 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001998 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001999 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002000
Andy Whitcroft65863862009-01-06 14:41:21 -08002001 # Should start with a space.
2002 $to =~ s/^(\S)/ $1/;
2003 # Should not end with a space.
2004 $to =~ s/\s+$//;
2005 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002006 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002007 }
2008 # Modifiers should have spaces.
2009 $to =~ s/(\b$Modifier$)/$1 /;
2010
Andy Whitcroft667026e2009-02-27 14:03:08 -08002011 #print "from<$from> to<$to> ident<$ident>\n";
2012 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002013 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2014 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002015 }
2016
2017# # no BUG() or BUG_ON()
2018# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2019# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2020# print "$herecurr";
2021# $clean = 0;
2022# }
2023
Andy Whitcroft8905a672007-11-28 16:21:06 -08002024 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002025 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 -08002026 }
2027
Joe Perches17441222011-06-15 15:08:17 -07002028# check for uses of printk_ratelimit
2029 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2030 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2031 }
2032
Andy Whitcroft00df3442007-06-08 13:47:06 -07002033# printk should use KERN_* levels. Note that follow on printk's on the
2034# same line do not need a level, so we use the current block context
2035# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002036# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002037# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002038 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002039 my $ok = 0;
2040 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2041 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002042 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002043 # with "\n" ignore it, else it is to blame
2044 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2045 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2046 $ok = 1;
2047 }
2048 last;
2049 }
2050 }
2051 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002052 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002053 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002054 }
2055
Andy Whitcroft653d4872007-06-23 17:16:34 -07002056# function brace can't be on same line, except for #defines of do while,
2057# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002058 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2059 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002060 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002061 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002062
Andy Whitcroft8905a672007-11-28 16:21:06 -08002063# open braces for enum, union and struct go on the same line.
2064 if ($line =~ /^.\s*{/ &&
2065 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2066 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2067 }
2068
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002069# missing space after union, struct or enum definition
2070 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2071 WARN("missing space after $1 definition\n" . $herecurr);
2072 }
2073
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002074# check for spacing round square brackets; allowed:
2075# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002076# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2077# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002078 while ($line =~ /(.*?\s)\[/g) {
2079 my ($where, $prefix) = ($-[1], $1);
2080 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002081 ($where != 0 || $prefix !~ /^.\s+$/) &&
2082 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002083 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2084 }
2085 }
2086
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002087# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002088 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002089 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002090 my $ctx_before = substr($line, 0, $-[1]);
2091 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002092
2093 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002094 if ($name =~ /^(?:
2095 if|for|while|switch|return|case|
2096 volatile|__volatile__|
2097 __attribute__|format|__extension__|
2098 asm|__asm__)$/x)
2099 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002100
2101 # cpp #define statements have non-optional spaces, ie
2102 # if there is a space between the name and the open
2103 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002104 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002105
2106 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002107 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002108
2109 # If this whole things ends with a type its most
2110 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002111 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002112
2113 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002114 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002115 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002116 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002117# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002118 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002119 my $ops = qr{
2120 <<=|>>=|<=|>=|==|!=|
2121 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2122 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002123 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2124 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002125 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002126 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002127 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002128
2129 my $blank = copy_spacing($opline);
2130
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002131 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002132 $off += length($elements[$n]);
2133
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002134 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002135 my $ca = substr($opline, 0, $off);
2136 my $cc = '';
2137 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2138 $cc = substr($opline, $off + length($elements[$n + 1]));
2139 }
2140 my $cb = "$ca$;$cc";
2141
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002142 my $a = '';
2143 $a = 'V' if ($elements[$n] ne '');
2144 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002145 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002146 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2147 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002148 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002149
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002150 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002151
2152 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002153 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002154 $c = 'V' if ($elements[$n + 2] ne '');
2155 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002156 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002157 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2158 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002159 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002160 } else {
2161 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002162 }
2163
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002164 my $ctx = "${a}x${c}";
2165
2166 my $at = "(ctx:$ctx)";
2167
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002168 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002169 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002170
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002171 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002172 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002173
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002174 # Get the full operator variant.
2175 my $opv = $op . substr($curr_vars, $off, 1);
2176
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002177 # Ignore operators passed as parameters.
2178 if ($op_type ne 'V' &&
2179 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2180
Andy Whitcroftcf655042008-03-04 14:28:20 -08002181# # Ignore comments
2182# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002183
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002184 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002185 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002186 if ($ctx !~ /.x[WEBC]/ &&
2187 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002189 }
2190
2191 # // is a comment
2192 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002193
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002194 # No spaces for:
2195 # ->
2196 # : when part of a bitfield
2197 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002198 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002199 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002200 }
2201
2202 # , must have a space on the right.
2203 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002204 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002205 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002206 }
2207
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002208 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002209 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002210 #warn "'*' is part of type\n";
2211
2212 # unary operators should have a space before and
2213 # none after. May be left adjacent to another
2214 # unary operator, or a cast
2215 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002216 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002217 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002218 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002220 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002221 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002222 # A unary '*' may be const
2223
2224 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002225 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002226 }
2227
2228 # unary ++ and unary -- are allowed no space on one side.
2229 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002230 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2231 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002232 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002233 if ($ctx =~ /Wx[BE]/ ||
2234 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2235 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002236 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002237 if ($ctx =~ /ExW/) {
2238 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2239 }
2240
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002241
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002242 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002243 } elsif ($op eq '<<' or $op eq '>>' or
2244 $op eq '&' or $op eq '^' or $op eq '|' or
2245 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002246 $op eq '*' or $op eq '/' or
2247 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002248 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002250 ERROR("need consistent spacing around '$op' $at\n" .
2251 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002252 }
2253
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002254 # A colon needs no spaces before when it is
2255 # terminating a case value or a label.
2256 } elsif ($opv eq ':C' || $opv eq ':L') {
2257 if ($ctx =~ /Wx./) {
2258 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2259 }
2260
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002261 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002262 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002263 my $ok = 0;
2264
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002265 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002266 if (($op eq '<' &&
2267 $cc =~ /^\S+\@\S+>/) ||
2268 ($op eq '>' &&
2269 $ca =~ /<\S+\@\S+$/))
2270 {
2271 $ok = 1;
2272 }
2273
2274 # Ignore ?:
2275 if (($opv eq ':O' && $ca =~ /\?$/) ||
2276 ($op eq '?' && $cc =~ /^:/)) {
2277 $ok = 1;
2278 }
2279
2280 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002281 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002282 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002283 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002284 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002285 }
2286 }
2287
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002288# check for multiple assignments
2289 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002290 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002291 }
2292
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002293## # check for multiple declarations, allowing for a function declaration
2294## # continuation.
2295## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2296## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2297##
2298## # Remove any bracketed sections to ensure we do not
2299## # falsly report the parameters of functions.
2300## my $ln = $line;
2301## while ($ln =~ s/\([^\(\)]*\)//g) {
2302## }
2303## if ($ln =~ /,/) {
2304## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2305## }
2306## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002307
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002308#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002309 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2310 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002311 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002312 }
2313
2314# closing brace should have a space following it when it has anything
2315# on the line
2316 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002317 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318 }
2319
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002320# check spacing on square brackets
2321 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002322 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002323 }
2324 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002325 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002326 }
2327
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002328# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002329 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002331 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002332 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002333 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002334 $line !~ /for\s*\(.*;\s+\)/ &&
2335 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002336 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002337 }
2338
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002339#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002340 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002341 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002342 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002343 }
2344
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002345# Return is not a function.
2346 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2347 my $spacing = $1;
2348 my $value = $2;
2349
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002350 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002351 $value =~ s/\(/ \(/g;
2352 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002353 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2354 $value !~ /(?:$Ident|-?$Constant)\s*
2355 $Compare\s*
2356 (?:$Ident|-?$Constant)/x &&
2357 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002358 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002359#print "value<$value>\n";
2360 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002361 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2362
2363 } elsif ($spacing !~ /\s+/) {
2364 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2365 }
2366 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002367# Return of what appears to be an errno should normally be -'ve
2368 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2369 my $name = $1;
2370 if ($name ne 'EOF' && $name ne 'ERROR') {
2371 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2372 }
2373 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002374
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002375# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002376 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002377 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002378 }
2379
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002380# Check for illegal assignment in if conditional -- and check for trailing
2381# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002382 if ($line =~ /do\s*(?!{)/) {
2383 my ($stat_next) = ctx_statement_block($line_nr_next,
2384 $remain_next, $off_next);
2385 $stat_next =~ s/\n./\n /g;
2386 ##print "stat<$stat> stat_next<$stat_next>\n";
2387
2388 if ($stat_next =~ /^\s*while\b/) {
2389 # If the statement carries leading newlines,
2390 # then count those as offsets.
2391 my ($whitespace) =
2392 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2393 my $offset =
2394 statement_rawlines($whitespace) - 1;
2395
2396 $suppress_whiletrailers{$line_nr_next +
2397 $offset} = 1;
2398 }
2399 }
2400 if (!defined $suppress_whiletrailers{$linenr} &&
2401 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002402 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002403
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002404 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002405 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002406 }
2407
2408 # Find out what is on the end of the line after the
2409 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002410 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002411 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002412 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002413 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2414 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002415 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002416 # Find out how long the conditional actually is.
2417 my @newlines = ($c =~ /\n/gs);
2418 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002419 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002420
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002421 $stat_real = raw_line($linenr, $cond_lines)
2422 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002423 if (defined($stat_real) && $cond_lines > 1) {
2424 $stat_real = "[...]\n$stat_real";
2425 }
2426
2427 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002428 }
2429 }
2430
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002431# Check for bitwise tests written as boolean
2432 if ($line =~ /
2433 (?:
2434 (?:\[|\(|\&\&|\|\|)
2435 \s*0[xX][0-9]+\s*
2436 (?:\&\&|\|\|)
2437 |
2438 (?:\&\&|\|\|)
2439 \s*0[xX][0-9]+\s*
2440 (?:\&\&|\|\||\)|\])
2441 )/x)
2442 {
2443 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2444 }
2445
Andy Whitcroft8905a672007-11-28 16:21:06 -08002446# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002447 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2448 my $s = $1;
2449 $s =~ s/$;//g; # Remove any comments
2450 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2451 ERROR("trailing statements should be on next line\n" . $herecurr);
2452 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002453 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002454# if should not continue a brace
2455 if ($line =~ /}\s*if\b/) {
2456 ERROR("trailing statements should be on next line\n" .
2457 $herecurr);
2458 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002459# case and default should not have general statements after them
2460 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2461 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002462 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002463 \s*return\s+
2464 )/xg)
2465 {
2466 ERROR("trailing statements should be on next line\n" . $herecurr);
2467 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002468
2469 # Check for }<nl>else {, these must be at the same
2470 # indent level to be relevant to each other.
2471 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2472 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002473 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002474 }
2475
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002476 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2477 $previndent == $indent) {
2478 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2479
2480 # Find out what is on the end of the line after the
2481 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002482 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002483 $s =~ s/\n.*//g;
2484
2485 if ($s =~ /^\s*;/) {
2486 ERROR("while should follow close brace '}'\n" . $hereprev);
2487 }
2488 }
2489
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002490#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2491# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2492# print "No studly caps, use _\n";
2493# print "$herecurr";
2494# $clean = 0;
2495# }
2496
2497#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002498 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002499 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002500 }
2501
Andy Whitcroft653d4872007-06-23 17:16:34 -07002502#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002503 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002504 my $file = "$1.h";
2505 my $checkfile = "include/linux/$file";
2506 if (-f "$root/$checkfile" &&
2507 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002508 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002509 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002510 if ($realfile =~ m{^arch/}) {
2511 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2512 } else {
2513 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2514 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002515 }
2516 }
2517
Andy Whitcroft653d4872007-06-23 17:16:34 -07002518# multi-statement macros should be enclosed in a do while loop, grab the
2519# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002520# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002521 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2522 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002523 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002525 my ($off, $dstat, $dcond, $rest);
2526 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002527
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002528 my $args = defined($1);
2529
2530 # Find the end of the macro and limit our statement
2531 # search to that.
2532 while ($cnt > 0 && defined $lines[$ln - 1] &&
2533 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2534 {
2535 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002536 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002537 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002538 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002539 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002540
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002541 ($dstat, $dcond, $ln, $cnt, $off) =
2542 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2543 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002544 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002545
2546 # Extract the remainder of the define (if any) and
2547 # rip off surrounding spaces, and trailing \'s.
2548 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002549 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2550 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002551 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2552 $rest .= substr($lines[$ln - 1], $off) . "\n";
2553 $cnt--;
2554 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002555 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002556 $off = 0;
2557 }
2558 $rest =~ s/\\\n.//g;
2559 $rest =~ s/^\s*//s;
2560 $rest =~ s/\s*$//s;
2561
2562 # Clean up the original statement.
2563 if ($args) {
2564 substr($dstat, 0, length($dcond), '');
2565 } else {
2566 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2567 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002568 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002569 $dstat =~ s/\\\n.//g;
2570 $dstat =~ s/^\s*//s;
2571 $dstat =~ s/\s*$//s;
2572
2573 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002574 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2575 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2576 $dstat =~ s/\[[^\{\}]*\]/1/)
2577 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002578 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580 # Extremely long macros may fall off the end of the
2581 # available context without closing. Give a dangling
2582 # backslash the benefit of the doubt and allow it
2583 # to gobble any hanging open-parens.
2584 $dstat =~ s/\(.+\\$/1/;
2585
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002586 my $exceptions = qr{
2587 $Declare|
2588 module_param_named|
2589 MODULE_PARAM_DESC|
2590 DECLARE_PER_CPU|
2591 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002593 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002594 union|
2595 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002596 \.$Ident\s*=\s*|
2597 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002598 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002599 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2600 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002601 if ($rest !~ /while\s*\(/ &&
2602 $dstat !~ /$exceptions/)
2603 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002604 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 -07002605 }
2606
2607 } elsif ($ctx !~ /;/) {
2608 if ($dstat ne '' &&
2609 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2610 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002611 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002612 $dstat =~ /$Operators/)
2613 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002614 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002615 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002616 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002617 }
2618
Mike Frysinger080ba922009-01-06 14:41:25 -08002619# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2620# all assignments may have only one of the following with an assignment:
2621# .
2622# ALIGN(...)
2623# VMLINUX_SYMBOL(...)
2624 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2625 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2626 }
2627
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002628# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002629 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2630 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002631 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002632 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002633 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2634 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002635 my $allowed = 0;
2636 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002637 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002638 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002639 for my $chunk (@chunks) {
2640 my ($cond, $block) = @{$chunk};
2641
Andy Whitcroft773647a2008-03-28 14:15:58 -07002642 # If the condition carries leading newlines, then count those as offsets.
2643 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2644 my $offset = statement_rawlines($whitespace) - 1;
2645
2646 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2647
2648 # We have looked at and allowed this specific line.
2649 $suppress_ifbraces{$ln + $offset} = 1;
2650
2651 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002652 $ln += statement_rawlines($block) - 1;
2653
Andy Whitcroft773647a2008-03-28 14:15:58 -07002654 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002655
2656 $seen++ if ($block =~ /^\s*{/);
2657
Andy Whitcroftcf655042008-03-04 14:28:20 -08002658 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2659 if (statement_lines($cond) > 1) {
2660 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002661 $allowed = 1;
2662 }
2663 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002664 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002665 $allowed = 1;
2666 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002667 if (statement_block_size($block) > 1) {
2668 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002669 $allowed = 1;
2670 }
2671 }
2672 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002673 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002674 }
2675 }
2676 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002677 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002678 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002679 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002680
Andy Whitcroftcf655042008-03-04 14:28:20 -08002681 # Check the pre-context.
2682 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2683 #print "APW: ALLOWED: pre<$1>\n";
2684 $allowed = 1;
2685 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002686
2687 my ($level, $endln, @chunks) =
2688 ctx_statement_full($linenr, $realcnt, $-[0]);
2689
Andy Whitcroftcf655042008-03-04 14:28:20 -08002690 # Check the condition.
2691 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002692 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002693 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002694 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002695 }
2696 if (statement_lines($cond) > 1) {
2697 #print "APW: ALLOWED: cond<$cond>\n";
2698 $allowed = 1;
2699 }
2700 if ($block =~/\b(?:if|for|while)\b/) {
2701 #print "APW: ALLOWED: block<$block>\n";
2702 $allowed = 1;
2703 }
2704 if (statement_block_size($block) > 1) {
2705 #print "APW: ALLOWED: lines block<$block>\n";
2706 $allowed = 1;
2707 }
2708 # Check the post-context.
2709 if (defined $chunks[1]) {
2710 my ($cond, $block) = @{$chunks[1]};
2711 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002712 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002713 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002714 if ($block =~ /^\s*\{/) {
2715 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2716 $allowed = 1;
2717 }
2718 }
2719 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2720 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002721 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002722
Andy Whitcroftf0556632008-10-15 22:02:23 -07002723 for (my $n = 0; $n < $cnt; $n++) {
2724 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002725 }
2726
2727 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002728 }
2729 }
2730
Andy Whitcroft653d4872007-06-23 17:16:34 -07002731# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002732 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002733 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002734 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002735 }
2736 }
2737
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002738# don't use deprecated functions
2739 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002740 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002741 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002742 }
2743 }
2744
2745# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002746 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2747 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002748 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002749 }
2750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002751# sys_open/read/write/close are not allowed in the kernel
2752 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
2753 ERROR("$1 is inappropriate in kernel code.\n" .
2754 $herecurr);
2755 }
2756
2757# filp_open is a backdoor for sys_open
2758 if ($line =~ /\b(filp_open)\b/) {
2759 ERROR("$1 is inappropriate in kernel code.\n" .
2760 $herecurr);
2761 }
2762
2763# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
2764 if ($line =~ /\b((?:read|write)[bwl])\b/) {
2765 ERROR("Use of $1 is deprecated: use $1_relaxed\n\t" .
2766 "with appropriate memory barriers instead.\n" .
2767 $herecurr);
2768 }
2769
2770# likewise, in/out[bwl] should be __raw_read/write[bwl]...
2771 if ($line =~ /\b((in|out)([bwl]))\b/) {
2772 my ($all, $pref, $suf) = ($1, $2, $3);
2773 $pref =~ s/in/read/;
2774 $pref =~ s/out/write/;
2775 ERROR("Use of $all is deprecated: use " .
2776 "__raw_$pref$suf\n\t" .
2777 "with appropriate memory barriers instead.\n" .
2778 $herecurr);
2779 }
2780
2781# dsb is too ARMish, and should usually be mb.
2782 if ($line =~ /\bdsb\b/) {
2783 WARN("Use of dsb is discouranged: prefer mb.\n" .
2784 $herecurr);
2785 }
2786
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08002787# MSM - check if a non board-gpiomux file has any gpiomux declarations
2788 if ($realfile =~ /\/mach-msm\/board-[0-9]+/ &&
2789 $realfile !~ /camera/ && $realfile !~ /gpiomux/ &&
2790 $line =~ /\s*struct msm_gpiomux_config\s*/ ) {
2791 WARN("Non gpiomux board file cannot have a gpiomux config declarations. Please declare gpiomux configs in board-*-gpiomux.c file.\n" . $herecurr);
2792 }
2793
Pankaj Kumaradfb9332012-01-06 15:02:19 +05302794# MSM - check if vreg_xxx function are used
2795 if ($line =~ /\b(vreg_(get|put|set_level|enable|disable))\b/) {
2796 WARN("Use of $1 API is deprecated: " .
2797 "use regulator APIs\n" . $herecurr);
2798 }
2799
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002800# unbounded string functions are overflow risks
2801 my %str_fns = (
2802 "sprintf" => "snprintf",
2803 "strcpy" => "strncpy",
2804 "strcat" => "strncat",
2805 "strcmp" => "strncmp",
2806 "strcasecmp" => "strncasecmp",
2807 "strchr" => "strnchr",
2808 "strstr" => "strnstr",
2809 "strlen" => "strnlen",
2810 );
2811 foreach my $k (keys %str_fns) {
2812 if ($line =~ /\b$k\b/) {
2813 ERROR("Use of $k is deprecated: " .
2814 "use $str_fns{$k} instead.\n" .
2815 $herecurr);
2816 }
2817 }
2818
Andy Whitcroft00df3442007-06-08 13:47:06 -07002819# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002820 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002821 WARN("if this code is redundant consider removing it\n"
2822 . $herecurr);
2823 }
2824# warn about #if 1
2825 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
2826 WARN("if this code is required consider removing"
2827 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002828 }
2829
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002830# check for needless kfree() checks
2831 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2832 my $expr = $1;
2833 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002834 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002835 }
2836 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002837# check for needless usb_free_urb() checks
2838 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2839 my $expr = $1;
2840 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2841 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2842 }
2843 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002844
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002845# prefer usleep_range over udelay
2846 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2847 # ignore udelay's < 10, however
2848 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2849 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2850 }
2851 }
2852
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002853# warn about unexpectedly long msleep's
2854 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2855 if ($1 < 20) {
2856 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2857 }
2858 }
2859
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002860# check the patch for use of mdelay
2861 if ($line =~ /\bmdelay\s*\(/) {
2862 WARN("use of mdelay() found: msleep() is the preferred API.\n" . $line );
2863 }
2864
Andy Whitcroft00df3442007-06-08 13:47:06 -07002865# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002866# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002867# print "#ifdef in C files should be avoided\n";
2868# print "$herecurr";
2869# $clean = 0;
2870# }
2871
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002872# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002873 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002874 ERROR("exactly one space required after that #$1\n" . $herecurr);
2875 }
2876
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002877# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002878 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2879 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002880 my $which = $1;
2881 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002882 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002883 }
2884 }
2885# check for memory barriers without a comment.
2886 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2887 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002888 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002889 }
2890 }
2891# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002892 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002893 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002894 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002895
Tobias Klauserd4977c72010-05-24 14:33:30 -07002896# Check that the storage class is at the beginning of a declaration
2897 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2898 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2899 }
2900
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002901# check the location of the inline attribute, that it is between
2902# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002903 if ($line =~ /\b$Type\s+$Inline\b/ ||
2904 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002905 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2906 }
2907
Andy Whitcroft8905a672007-11-28 16:21:06 -08002908# Check for __inline__ and __inline, prefer inline
2909 if ($line =~ /\b(__inline__|__inline)\b/) {
2910 WARN("plain inline is preferred over $1\n" . $herecurr);
2911 }
2912
Joe Perches3d130fd2011-01-12 17:00:00 -08002913# Check for __attribute__ packed, prefer __packed
2914 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2915 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2916 }
2917
Joe Perches8f53a9b2010-03-05 13:43:48 -08002918# check for sizeof(&)
2919 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2920 WARN("sizeof(& should be avoided\n" . $herecurr);
2921 }
2922
Joe Perches428e2fd2011-05-24 17:13:39 -07002923# check for line continuations in quoted strings with odd counts of "
2924 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2925 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2926 }
2927
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002928# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002929 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002930 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002931 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002932 my $function_name = $1;
2933 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002934
2935 my $s = $stat;
2936 if (defined $cond) {
2937 substr($s, 0, length($cond), '');
2938 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002939 if ($s =~ /^\s*;/ &&
2940 $function_name ne 'uninitialized_var')
2941 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002942 WARN("externs should be avoided in .c files\n" . $herecurr);
2943 }
2944
2945 if ($paren_space =~ /\n/) {
2946 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2947 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002948
2949 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2950 $stat =~ /^.\s*extern\s+/)
2951 {
2952 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002953 }
2954
2955# checks for new __setup's
2956 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2957 my $name = $1;
2958
2959 if (!grep(/$name/, @setup_docs)) {
2960 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2961 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002962 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002963
2964# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002965 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002966 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2967 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002968
Joe Perchescaf2a542011-01-12 16:59:56 -08002969# check for multiple semicolons
2970 if ($line =~ /;\s*;\s*$/) {
2971 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2972 }
2973
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002974# check for return codes on error paths
2975 if ($line =~ /\breturn\s+-\d+/) {
2976 ERROR("illegal return value, please use an error code\n" . $herecurr);
2977 }
2978
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002979# check for gcc specific __FUNCTION__
2980 if ($line =~ /__FUNCTION__/) {
2981 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2982 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002983
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002984# check for semaphores initialized locked
2985 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002986 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002987
Andy Whitcroft773647a2008-03-28 14:15:58 -07002988 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002989# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002990 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002991 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002992 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002993# check for __initcall(), use device_initcall() explicitly please
2994 if ($line =~ /^.\s*__initcall\s*\(/) {
2995 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2996 }
Emese Revfy79404842010-03-05 13:43:53 -08002997# check for various ops structs, ensure they are const.
2998 my $struct_ops = qr{acpi_dock_ops|
2999 address_space_operations|
3000 backlight_ops|
3001 block_device_operations|
3002 dentry_operations|
3003 dev_pm_ops|
3004 dma_map_ops|
3005 extent_io_ops|
3006 file_lock_operations|
3007 file_operations|
3008 hv_ops|
3009 ide_dma_ops|
3010 intel_dvo_dev_ops|
3011 item_operations|
3012 iwl_ops|
3013 kgdb_arch|
3014 kgdb_io|
3015 kset_uevent_ops|
3016 lock_manager_operations|
3017 microcode_ops|
3018 mtrr_ops|
3019 neigh_ops|
3020 nlmsvc_binding|
3021 pci_raw_ops|
3022 pipe_buf_operations|
3023 platform_hibernation_ops|
3024 platform_suspend_ops|
3025 proto_ops|
3026 rpc_pipe_ops|
3027 seq_operations|
3028 snd_ac97_build_ops|
3029 soc_pcmcia_socket_ops|
3030 stacktrace_ops|
3031 sysfs_ops|
3032 tty_operations|
3033 usb_mon_operations|
3034 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003035 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003036 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003037 WARN("struct $1 should normally be const\n" .
3038 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003039 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003040
3041# use of NR_CPUS is usually wrong
3042# ignore definitions of NR_CPUS and usage to define arrays as likely right
3043 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003044 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3045 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003046 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3047 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3048 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003049 {
3050 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3051 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003052
3053# check for %L{u,d,i} in strings
3054 my $string;
3055 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3056 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003057 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003058 if ($string =~ /(?<!%)%L[udi]/) {
3059 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3060 last;
3061 }
3062 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003063
3064# whine mightly about in_atomic
3065 if ($line =~ /\bin_atomic\s*\(/) {
3066 if ($realfile =~ m@^drivers/@) {
3067 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003068 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003069 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3070 }
3071 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003072
3073# check for lockdep_set_novalidate_class
3074 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3075 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3076 if ($realfile !~ m@^kernel/lockdep@ &&
3077 $realfile !~ m@^include/linux/lockdep@ &&
3078 $realfile !~ m@^drivers/base/core@) {
3079 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3080 }
3081 }
Dave Jones88f88312011-01-12 16:59:59 -08003082
3083 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3084 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3085 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3086 }
Dave Jones309c00c2011-03-22 16:34:44 -07003087
3088 # Check for memset with swapped arguments
3089 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3090 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3091 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003092 }
3093
3094 # If we have no input at all, then there is nothing to report on
3095 # so just keep quiet.
3096 if ($#rawlines == -1) {
3097 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003098 }
3099
Andy Whitcroft8905a672007-11-28 16:21:06 -08003100 # In mailback mode only produce a report in the negative, for
3101 # things that appear to be patches.
3102 if ($mailback && ($clean == 1 || !$is_patch)) {
3103 exit(0);
3104 }
3105
3106 # This is not a patch, and we are are in 'no-patch' mode so
3107 # just keep quiet.
3108 if (!$chk_patch && !$is_patch) {
3109 exit(0);
3110 }
3111
3112 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003113 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003114 }
3115 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003116 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003117 }
3118
Andy Whitcroft8905a672007-11-28 16:21:06 -08003119 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003120 if ($summary && !($clean == 1 && $quiet == 1)) {
3121 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003122 print "total: $cnt_error errors, $cnt_warn warnings, " .
3123 (($check)? "$cnt_chk checks, " : "") .
3124 "$cnt_lines lines checked\n";
3125 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003126 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003127
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003128 if ($quiet == 0) {
3129 # If there were whitespace errors which cleanpatch can fix
3130 # then suggest that.
3131 if ($rpt_cleaners) {
3132 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3133 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003134 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003135 }
3136 }
3137
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003138 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003139 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003140 }
3141 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003142 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003143 print "are false positives report them to the maintainer, see\n";
3144 print "CHECKPATCH in MAINTAINERS.\n";
3145 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003146
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003147 return $clean;
3148}