blob: 1163275a8d4138d2e509d3247174ba0fdef96313 [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 $in_code_block = 0;
1233 my $exec_file = "";
1234
1235 my $shorttext = BEFORE_SHORTTEXT;
1236 my $shorttext_exspc = 0;
1237
Andy Whitcroft773647a2008-03-28 14:15:58 -07001238 sanitise_line_reset();
Gregory Bean3f66eea2011-07-19 16:37:49 -07001239 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001240 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001242 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001243 $linenr++;
1244 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001245
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001247 $setup_docs = 0;
1248 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1249 $setup_docs = 1;
1250 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001251 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001252 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001253 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1254 $realline=$1-1;
1255 if (defined $2) {
1256 $realcnt=$3+1;
1257 } else {
1258 $realcnt=1+1;
1259 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001260 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001261
1262 # Guestimate if this is a continuing comment. Run
1263 # the context looking for a comment "edge". If this
1264 # edge is a close comment then we must be in a comment
1265 # at context start.
1266 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001267 my $cnt = $realcnt;
1268 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1269 next if (defined $rawlines[$ln - 1] &&
1270 $rawlines[$ln - 1] =~ /^-/);
1271 $cnt--;
1272 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001273 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001274 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1275 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1276 ($edge) = $1;
1277 last;
1278 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279 }
1280 if (defined $edge && $edge eq '*/') {
1281 $in_comment = 1;
1282 }
1283
1284 # Guestimate if this is a continuing comment. If this
1285 # is the start of a diff block and this line starts
1286 # ' *' then it is very likely a comment.
1287 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001288 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001289 {
1290 $in_comment = 1;
1291 }
1292
1293 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1294 sanitise_line_reset($in_comment);
1295
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001296 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001297 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001298 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 $line = sanitise_line($rawline);
1300 }
1301 push(@lines, $line);
1302
1303 if ($realcnt > 1) {
1304 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1305 } else {
1306 $realcnt = 0;
1307 }
1308
1309 #print "==>$rawline\n";
1310 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001311
1312 if ($setup_docs && $line =~ /^\+/) {
1313 push(@setup_docs, $line);
1314 }
1315 }
1316
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001317 $prefix = '';
1318
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 $realcnt = 0;
1320 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001321 foreach my $line (@lines) {
1322 $linenr++;
1323
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001324 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001325
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001326#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001327 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001328 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001329 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001330 $realline=$1-1;
1331 if (defined $2) {
1332 $realcnt=$3+1;
1333 } else {
1334 $realcnt=1+1;
1335 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001337 $prev_values = 'E';
1338
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001340 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001341 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001342 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001343
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001344# track the line number as we move through the hunk, note that
1345# new versions of GNU diff omit the leading space on completely
1346# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001347 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001348 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001349 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001350
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001351 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001353
1354 # Track the previous line.
1355 ($prevline, $stashline) = ($stashline, $line);
1356 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001357 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1358
Andy Whitcroft773647a2008-03-28 14:15:58 -07001359 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001360
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001361 } elsif ($realcnt == 1) {
1362 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001363 }
1364
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001365 my $hunk_line = ($realcnt != 0);
1366
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001367#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001368 $prefix = "$filename:$realline: " if ($emacs && $file);
1369 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1370
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001371 $here = "#$linenr: " if (!$file);
1372 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001373
1374 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001375 if ($line =~ /^diff --git.*?(\S+)$/) {
1376 $realfile = $1;
1377 $realfile =~ s@^([^/]*)/@@;
1378
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 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 $in_code_block = 1;
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;
1397 $in_code_block = 0;
1398 }
1399 #Check state to make sure we aren't in code block.
1400 elsif (!$in_code_block &&
1401 ($exec_file =~ /^.+\.[chS]$/ or
1402 $exec_file =~ /^.+\.txt$/ or
1403 $exec_file =~ /^.+\.ihex$/ or
1404 $exec_file =~ /^.+\.hex$/ or
1405 $exec_file =~ /^.+\.HEX$/ or
1406 $exec_file =~ /^.+defconfig$/ or
1407 $exec_file =~ /^Makefile$/ or
1408 $exec_file =~ /^Kconfig$/) &&
1409 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1410 (oct($2) & 0111)) {
1411 ERROR("Source file has +x permissions: " .
1412 "$exec_file\n");
1413 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001414 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001415
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001416 my $hereline = "$here\n$rawline\n";
1417 my $herecurr = "$here\n$rawline\n";
1418 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001419
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 if ($shorttext != AFTER_SHORTTEXT) {
1421 if ($shorttext == IN_SHORTTEXT) {
1422 if ($line=~/^---/ || $line=~/^diff.*/) {
1423 $shorttext = AFTER_SHORTTEXT;
1424 } elsif (length($line) > (SHORTTEXT_LIMIT +
1425 $shorttext_exspc)
1426 && $line !~ /^:([0-7]{6}\s){2}
1427 ([[:xdigit:]]+\.*
1428 \s){2}\w+\s\w+/xms) {
1429 WARN("commit text line over " .
1430 SHORTTEXT_LIMIT .
1431 " characters\n" . $herecurr);
1432 }
1433 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1434# The Subject line doesn't have to be the last header in the patch.
1435# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1436# Per RFC5322, continuation lines must be folded, so any left-justified
1437# text which looks like a header is definitely a header.
1438 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1439 $shorttext = IN_SHORTTEXT;
1440# Check for Subject line followed by a blank line.
1441 if (length($line) != 0) {
1442 WARN("non-blank line after " .
1443 "summary line\n" .
1444 $sublinenr . $here .
1445 "\n" . $subjectline .
1446 "\n" . $line . "\n");
1447 }
1448 }
1449 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
1450 $shorttext = CHECK_NEXT_SHORTTEXT;
1451 $subjectline = $line;
1452 $sublinenr = "#$linenr & ";
1453# Check for Subject line less than line limit
1454 if (length($1) > SHORTTEXT_LIMIT) {
1455 WARN("summary line over " .
1456 SHORTTEXT_LIMIT .
1457 " characters\n" . $herecurr);
1458 }
1459 } elsif ($line=~/^ (.*)/) {
1460 $shorttext = IN_SHORTTEXT;
1461 $shorttext_exspc = 4;
1462 if (length($1) > SHORTTEXT_LIMIT) {
1463 WARN("summary line over " .
1464 SHORTTEXT_LIMIT .
1465 " characters\n" . $herecurr);
1466 }
1467 }
1468 }
1469
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001470 $cnt_lines++ if ($realcnt != 0);
1471
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001472# Check for incorrect file permissions
1473 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1474 my $permhere = $here . "FILE: $realfile\n";
1475 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1476 ERROR("do not set execute permissions for source files\n" . $permhere);
1477 }
1478 }
1479
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001480#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001481 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001482 # This is a signoff, if ugly, so do not double report.
1483 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001484 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001485 WARN("Signed-off-by: is the preferred form\n" .
1486 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001487 }
1488 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001489 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001490 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001491 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001492 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
1493 WARN("invalid Signed-off-by identity\n" . $line );
1494 }
1495 }
1496
1497#check the patch for invalid author credentials
1498 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
1499 WARN("invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001500 }
1501
Andy Whitcroft00df3442007-06-08 13:47:06 -07001502# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001503 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001504 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001505 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001506 }
1507
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001508# Check for absolute kernel paths.
1509 if ($tree) {
1510 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1511 my $file = $1;
1512
1513 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1514 check_absolute_file($1, $herecurr)) {
1515 #
1516 } else {
1517 check_absolute_file($file, $herecurr);
1518 }
1519 }
1520 }
1521
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001522# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1523 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001524 $rawline !~ m/^$UTF8*$/) {
1525 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1526
1527 my $blank = copy_spacing($rawline);
1528 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1529 my $hereptr = "$hereline$ptr\n";
1530
1531 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001532 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001533
Andy Whitcroft306708542008-10-15 22:02:28 -07001534# ignore non-hunk lines and lines being removed
1535 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001536
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001537#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001538 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001539 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001540 ERROR("DOS line endings\n" . $herevet);
1541
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001542 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1543 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001544 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001545 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001546 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001547
1548# check we are in a valid source file if not then ignore this hunk
1549 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1550
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001551#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001552 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001553 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001554 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001555 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001557 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001558 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001559 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001560 }
1561
Joe Perches5e79d962010-03-05 13:43:55 -08001562# check for spaces before a quoted newline
1563 if ($rawline =~ /^.*\".*\s\\n/) {
1564 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1565 }
1566
Andy Whitcroft8905a672007-11-28 16:21:06 -08001567# check for adding lines without a newline.
1568 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1569 WARN("adding a line without newline at end of file\n" . $herecurr);
1570 }
1571
Mike Frysinger42e41c52009-09-21 17:04:40 -07001572# Blackfin: use hi/lo macros
1573 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1574 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1575 my $herevet = "$here\n" . cat_vet($line) . "\n";
1576 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1577 }
1578 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1579 my $herevet = "$here\n" . cat_vet($line) . "\n";
1580 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1581 }
1582 }
1583
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001584# check we are in a valid source file C or perl if not then ignore this hunk
1585 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001586
1587# at the beginning of a line any tabs must come first and anything
1588# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001589 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1590 $rawline =~ /^\+\s* \s*/) {
1591 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001592 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001593 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001594 }
1595
Alberto Panizzo08e44362010-03-05 13:43:54 -08001596# check for space before tabs.
1597 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1598 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1599 WARN("please, no space before tabs\n" . $herevet);
1600 }
1601
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001602# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001603# Exceptions:
1604# 1) within comments
1605# 2) indented preprocessor commands
1606# 3) hanging labels
1607 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001608 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001609 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001610 }
1611
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001612# check we are in a valid C source file if not then ignore this hunk
1613 next if ($realfile !~ /\.(h|c)$/);
1614
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001615# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001616 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001617 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1618 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001619
Mike Frysinger42e41c52009-09-21 17:04:40 -07001620# Blackfin: don't use __builtin_bfin_[cs]sync
1621 if ($line =~ /__builtin_bfin_csync/) {
1622 my $herevet = "$here\n" . cat_vet($line) . "\n";
1623 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1624 }
1625 if ($line =~ /__builtin_bfin_ssync/) {
1626 my $herevet = "$here\n" . cat_vet($line) . "\n";
1627 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1628 }
1629
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001630# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001631 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1632 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001633 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001634 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001635 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001636 $stat =~ s/\n./\n /g;
1637 $cond =~ s/\n./\n /g;
1638
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001639 # Find the real next line.
1640 $realline_next = $line_nr_next;
1641 if (defined $realline_next &&
1642 (!defined $lines[$realline_next - 1] ||
1643 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1644 $realline_next++;
1645 }
1646
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001647 my $s = $stat;
1648 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001649
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001650 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001651 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001652
1653 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001655
Andy Whitcroft463f2862009-09-21 17:04:34 -07001656 } elsif ($s =~ /^.\s*else\b/s) {
1657
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001658 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001659 } 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 -07001660 my $type = $1;
1661 $type =~ s/\s+/ /g;
1662 possible($type, "A:" . $s);
1663
Andy Whitcroft8905a672007-11-28 16:21:06 -08001664 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001665 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001666 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001667 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001668
1669 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001670 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001671 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001672 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001673
1674 # Check for any sort of function declaration.
1675 # int foo(something bar, other baz);
1676 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001677 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 -08001678 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001679
Andy Whitcroftcf655042008-03-04 14:28:20 -08001680 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001681 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001682 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001683
Andy Whitcroft8905a672007-11-28 16:21:06 -08001684 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001685 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001686
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001687 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001688 }
1689 }
1690 }
1691
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001692 }
1693
Andy Whitcroft653d4872007-06-23 17:16:34 -07001694#
1695# Checks which may be anchored in the context.
1696#
1697
1698# Check for switch () and associated case and default
1699# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001700 if ($line=~/\bswitch\s*\(.*\)/) {
1701 my $err = '';
1702 my $sep = '';
1703 my @ctx = ctx_block_outer($linenr, $realcnt);
1704 shift(@ctx);
1705 for my $ctx (@ctx) {
1706 my ($clen, $cindent) = line_stats($ctx);
1707 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1708 $indent != $cindent) {
1709 $err .= "$sep$ctx\n";
1710 $sep = '';
1711 } else {
1712 $sep = "[...]\n";
1713 }
1714 }
1715 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001716 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001717 }
1718 }
1719
1720# if/while/etc brace do not go on next line, unless defining a do while loop,
1721# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001722 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001723 my $pre_ctx = "$1$2";
1724
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001725 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001726 my $ctx_cnt = $realcnt - $#ctx - 1;
1727 my $ctx = join("\n", @ctx);
1728
Andy Whitcroft548596d2008-07-23 21:29:01 -07001729 my $ctx_ln = $linenr;
1730 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001731
Andy Whitcroft548596d2008-07-23 21:29:01 -07001732 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1733 defined $lines[$ctx_ln - 1] &&
1734 $lines[$ctx_ln - 1] =~ /^-/)) {
1735 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1736 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001737 $ctx_ln++;
1738 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001739
Andy Whitcroft53210162008-07-23 21:29:03 -07001740 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1741 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001742
1743 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1744 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001745 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001746 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001747 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1748 $ctx =~ /\)\s*\;\s*$/ &&
1749 defined $lines[$ctx_ln - 1])
1750 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001751 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1752 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001753 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001754 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001755 }
1756 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001757 }
1758
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001759# Check relative indent for conditionals and blocks.
1760 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1761 my ($s, $c) = ($stat, $cond);
1762
1763 substr($s, 0, length($c), '');
1764
1765 # Make sure we remove the line prefixes as we have
1766 # none on the first line, and are going to readd them
1767 # where necessary.
1768 $s =~ s/\n./\n/gs;
1769
1770 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001771 my @newlines = ($c =~ /\n/gs);
1772 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001773
1774 # We want to check the first line inside the block
1775 # starting at the end of the conditional, so remove:
1776 # 1) any blank line termination
1777 # 2) any opening brace { on end of the line
1778 # 3) any do (...) {
1779 my $continuation = 0;
1780 my $check = 0;
1781 $s =~ s/^.*\bdo\b//;
1782 $s =~ s/^\s*{//;
1783 if ($s =~ s/^\s*\\//) {
1784 $continuation = 1;
1785 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001786 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001787 $check = 1;
1788 $cond_lines++;
1789 }
1790
1791 # Also ignore a loop construct at the end of a
1792 # preprocessor statement.
1793 if (($prevline =~ /^.\s*#\s*define\s/ ||
1794 $prevline =~ /\\\s*$/) && $continuation == 0) {
1795 $check = 0;
1796 }
1797
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001798 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001799 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001800 while ($cond_ptr != $cond_lines) {
1801 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001802
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001803 # If we see an #else/#elif then the code
1804 # is not linear.
1805 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1806 $check = 0;
1807 }
1808
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001809 # Ignore:
1810 # 1) blank lines, they should be at 0,
1811 # 2) preprocessor lines, and
1812 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001813 if ($continuation ||
1814 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001815 $s =~ /^\s*#\s*?/ ||
1816 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001817 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001818 if ($s =~ s/^.*?\n//) {
1819 $cond_lines++;
1820 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001821 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001822 }
1823
1824 my (undef, $sindent) = line_stats("+" . $s);
1825 my $stat_real = raw_line($linenr, $cond_lines);
1826
1827 # Check if either of these lines are modified, else
1828 # this is not this patch's fault.
1829 if (!defined($stat_real) ||
1830 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1831 $check = 0;
1832 }
1833 if (defined($stat_real) && $cond_lines > 1) {
1834 $stat_real = "[...]\n$stat_real";
1835 }
1836
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001837 #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 -07001838
1839 if ($check && (($sindent % 8) != 0 ||
1840 ($sindent <= $indent && $s ne ''))) {
1841 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1842 }
1843 }
1844
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001845 # Track the 'values' across context and added lines.
1846 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001847 my ($curr_values, $curr_vars) =
1848 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001849 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001850 if ($dbg_values) {
1851 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001852 print "$linenr > .$outline\n";
1853 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001854 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001855 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001856 $prev_values = substr($curr_values, -1);
1857
Andy Whitcroft00df3442007-06-08 13:47:06 -07001858#ignore lines not being added
1859 if ($line=~/^[^\+]/) {next;}
1860
Andy Whitcroft653d4872007-06-23 17:16:34 -07001861# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001862 if ($dbg_type) {
1863 if ($line =~ /^.\s*$Declare\s*$/) {
1864 ERROR("TEST: is type\n" . $herecurr);
1865 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1866 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1867 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001868 next;
1869 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001870# TEST: allow direct testing of the attribute matcher.
1871 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001872 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001873 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001874 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001875 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1876 }
1877 next;
1878 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001879
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001880# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001881 if ($line =~ /^.\s*{/ &&
1882 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001883 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001884 }
1885
Andy Whitcroft653d4872007-06-23 17:16:34 -07001886#
1887# Checks which are anchored on the added line.
1888#
1889
1890# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001891 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001892 my $path = $1;
1893 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001894 ERROR("malformed #include filename\n" .
1895 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001896 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001897 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001898
1899# no C99 // comments
1900 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001901 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001902 }
1903 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001904 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001905 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001906
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001907# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1908# the whole statement.
1909#print "APW <$lines[$realline_next - 1]>\n";
1910 if (defined $realline_next &&
1911 exists $lines[$realline_next - 1] &&
1912 !defined $suppress_export{$realline_next} &&
1913 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1914 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001915 # Handle definitions which produce identifiers with
1916 # a prefix:
1917 # XXX(foo);
1918 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001919 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001920 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1921 $name =~ /^${Ident}_$2/) {
1922#print "FOO C name<$name>\n";
1923 $suppress_export{$realline_next} = 1;
1924
1925 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001926 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001927 ^.DEFINE_$Ident\(\Q$name\E\)|
1928 ^.DECLARE_$Ident\(\Q$name\E\)|
1929 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001930 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1931 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001932 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001933#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1934 $suppress_export{$realline_next} = 2;
1935 } else {
1936 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001937 }
1938 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001939 if (!defined $suppress_export{$linenr} &&
1940 $prevline =~ /^.\s*$/ &&
1941 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1942 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1943#print "FOO B <$lines[$linenr - 1]>\n";
1944 $suppress_export{$linenr} = 2;
1945 }
1946 if (defined $suppress_export{$linenr} &&
1947 $suppress_export{$linenr} == 2) {
1948 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1949 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001950
Joe Eloff5150bda2010-08-09 17:21:00 -07001951# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001952 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001953 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001954 $herecurr);
1955 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001956# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001957 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001958 ERROR("do not initialise statics to 0 or NULL\n" .
1959 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001960 }
1961
Joe Perchescb710ec2010-10-26 14:23:20 -07001962# check for static const char * arrays.
1963 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1964 WARN("static const char * array should probably be static const char * const\n" .
1965 $herecurr);
1966 }
1967
1968# check for static char foo[] = "bar" declarations.
1969 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1970 WARN("static char array declaration should probably be static const char\n" .
1971 $herecurr);
1972 }
1973
Joe Perches93ed0e22010-10-26 14:23:21 -07001974# check for declarations of struct pci_device_id
1975 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1976 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1977 }
1978
Andy Whitcroft653d4872007-06-23 17:16:34 -07001979# check for new typedefs, only function parameters and sparse annotations
1980# make sense.
1981 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001982 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001983 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001984 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001985 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001986 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001987 }
1988
1989# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001990 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001991 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001992 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001993
Andy Whitcroft65863862009-01-06 14:41:21 -08001994 # Should start with a space.
1995 $to =~ s/^(\S)/ $1/;
1996 # Should not end with a space.
1997 $to =~ s/\s+$//;
1998 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001999 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002000 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002001
Andy Whitcroft65863862009-01-06 14:41:21 -08002002 #print "from<$from> to<$to>\n";
2003 if ($from ne $to) {
2004 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2005 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08002006 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002007 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002008
Andy Whitcroft65863862009-01-06 14:41:21 -08002009 # Should start with a space.
2010 $to =~ s/^(\S)/ $1/;
2011 # Should not end with a space.
2012 $to =~ s/\s+$//;
2013 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002014 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002015 }
2016 # Modifiers should have spaces.
2017 $to =~ s/(\b$Modifier$)/$1 /;
2018
Andy Whitcroft667026e2009-02-27 14:03:08 -08002019 #print "from<$from> to<$to> ident<$ident>\n";
2020 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002021 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2022 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002023 }
2024
2025# # no BUG() or BUG_ON()
2026# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2027# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2028# print "$herecurr";
2029# $clean = 0;
2030# }
2031
Andy Whitcroft8905a672007-11-28 16:21:06 -08002032 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002033 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 -08002034 }
2035
Joe Perches17441222011-06-15 15:08:17 -07002036# check for uses of printk_ratelimit
2037 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2038 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2039 }
2040
Andy Whitcroft00df3442007-06-08 13:47:06 -07002041# printk should use KERN_* levels. Note that follow on printk's on the
2042# same line do not need a level, so we use the current block context
2043# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002044# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002045# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002046 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002047 my $ok = 0;
2048 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2049 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002050 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002051 # with "\n" ignore it, else it is to blame
2052 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2053 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2054 $ok = 1;
2055 }
2056 last;
2057 }
2058 }
2059 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002060 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002061 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002062 }
2063
Andy Whitcroft653d4872007-06-23 17:16:34 -07002064# function brace can't be on same line, except for #defines of do while,
2065# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002066 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2067 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002068 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002069 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002070
Andy Whitcroft8905a672007-11-28 16:21:06 -08002071# open braces for enum, union and struct go on the same line.
2072 if ($line =~ /^.\s*{/ &&
2073 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2074 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2075 }
2076
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002077# missing space after union, struct or enum definition
2078 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2079 WARN("missing space after $1 definition\n" . $herecurr);
2080 }
2081
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002082# check for spacing round square brackets; allowed:
2083# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002084# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2085# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002086 while ($line =~ /(.*?\s)\[/g) {
2087 my ($where, $prefix) = ($-[1], $1);
2088 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002089 ($where != 0 || $prefix !~ /^.\s+$/) &&
2090 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002091 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2092 }
2093 }
2094
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002095# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002096 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002097 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002098 my $ctx_before = substr($line, 0, $-[1]);
2099 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002100
2101 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002102 if ($name =~ /^(?:
2103 if|for|while|switch|return|case|
2104 volatile|__volatile__|
2105 __attribute__|format|__extension__|
2106 asm|__asm__)$/x)
2107 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002108
2109 # cpp #define statements have non-optional spaces, ie
2110 # if there is a space between the name and the open
2111 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002112 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002113
2114 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002115 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002116
2117 # If this whole things ends with a type its most
2118 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002119 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002120
2121 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002122 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002123 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002124 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002125# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002126 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002127 my $ops = qr{
2128 <<=|>>=|<=|>=|==|!=|
2129 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2130 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002131 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2132 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002133 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002134 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002135 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002136
2137 my $blank = copy_spacing($opline);
2138
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002139 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002140 $off += length($elements[$n]);
2141
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002142 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002143 my $ca = substr($opline, 0, $off);
2144 my $cc = '';
2145 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2146 $cc = substr($opline, $off + length($elements[$n + 1]));
2147 }
2148 my $cb = "$ca$;$cc";
2149
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002150 my $a = '';
2151 $a = 'V' if ($elements[$n] ne '');
2152 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002153 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002154 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2155 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002157
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002158 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002159
2160 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002161 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002162 $c = 'V' if ($elements[$n + 2] ne '');
2163 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002164 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002165 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2166 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002167 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002168 } else {
2169 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002170 }
2171
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002172 my $ctx = "${a}x${c}";
2173
2174 my $at = "(ctx:$ctx)";
2175
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002176 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002177 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002178
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002179 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002180 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002181
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002182 # Get the full operator variant.
2183 my $opv = $op . substr($curr_vars, $off, 1);
2184
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002185 # Ignore operators passed as parameters.
2186 if ($op_type ne 'V' &&
2187 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2188
Andy Whitcroftcf655042008-03-04 14:28:20 -08002189# # Ignore comments
2190# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002191
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002192 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002193 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002194 if ($ctx !~ /.x[WEBC]/ &&
2195 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002196 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002197 }
2198
2199 # // is a comment
2200 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002202 # No spaces for:
2203 # ->
2204 # : when part of a bitfield
2205 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002206 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002207 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002208 }
2209
2210 # , must have a space on the right.
2211 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002212 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002213 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002214 }
2215
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002216 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002217 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002218 #warn "'*' is part of type\n";
2219
2220 # unary operators should have a space before and
2221 # none after. May be left adjacent to another
2222 # unary operator, or a cast
2223 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002224 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002225 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002226 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002227 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002228 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002229 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002230 # A unary '*' may be const
2231
2232 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002233 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002234 }
2235
2236 # unary ++ and unary -- are allowed no space on one side.
2237 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002238 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2239 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002241 if ($ctx =~ /Wx[BE]/ ||
2242 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2243 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002244 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002245 if ($ctx =~ /ExW/) {
2246 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2247 }
2248
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002249
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002250 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002251 } elsif ($op eq '<<' or $op eq '>>' or
2252 $op eq '&' or $op eq '^' or $op eq '|' or
2253 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002254 $op eq '*' or $op eq '/' or
2255 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002256 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002257 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002258 ERROR("need consistent spacing around '$op' $at\n" .
2259 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002260 }
2261
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002262 # A colon needs no spaces before when it is
2263 # terminating a case value or a label.
2264 } elsif ($opv eq ':C' || $opv eq ':L') {
2265 if ($ctx =~ /Wx./) {
2266 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2267 }
2268
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002269 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002270 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002271 my $ok = 0;
2272
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002273 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002274 if (($op eq '<' &&
2275 $cc =~ /^\S+\@\S+>/) ||
2276 ($op eq '>' &&
2277 $ca =~ /<\S+\@\S+$/))
2278 {
2279 $ok = 1;
2280 }
2281
2282 # Ignore ?:
2283 if (($opv eq ':O' && $ca =~ /\?$/) ||
2284 ($op eq '?' && $cc =~ /^:/)) {
2285 $ok = 1;
2286 }
2287
2288 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002289 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002290 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002291 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002292 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002293 }
2294 }
2295
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002296# check for multiple assignments
2297 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002298 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002299 }
2300
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002301## # check for multiple declarations, allowing for a function declaration
2302## # continuation.
2303## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2304## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2305##
2306## # Remove any bracketed sections to ensure we do not
2307## # falsly report the parameters of functions.
2308## my $ln = $line;
2309## while ($ln =~ s/\([^\(\)]*\)//g) {
2310## }
2311## if ($ln =~ /,/) {
2312## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2313## }
2314## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002315
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002316#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002317 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2318 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002319 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002320 }
2321
2322# closing brace should have a space following it when it has anything
2323# on the line
2324 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002325 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002326 }
2327
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002328# check spacing on square brackets
2329 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002330 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002331 }
2332 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002334 }
2335
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002336# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002337 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002338 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002339 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002340 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002341 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002342 $line !~ /for\s*\(.*;\s+\)/ &&
2343 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002344 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002345 }
2346
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002347#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002348 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002349 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002350 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002351 }
2352
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002353# Return is not a function.
2354 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2355 my $spacing = $1;
2356 my $value = $2;
2357
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002358 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002359 $value =~ s/\(/ \(/g;
2360 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002361 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2362 $value !~ /(?:$Ident|-?$Constant)\s*
2363 $Compare\s*
2364 (?:$Ident|-?$Constant)/x &&
2365 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002366 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002367#print "value<$value>\n";
2368 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002369 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2370
2371 } elsif ($spacing !~ /\s+/) {
2372 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2373 }
2374 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002375# Return of what appears to be an errno should normally be -'ve
2376 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2377 my $name = $1;
2378 if ($name ne 'EOF' && $name ne 'ERROR') {
2379 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2380 }
2381 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002382
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002383# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002384 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002385 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002386 }
2387
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002388# Check for illegal assignment in if conditional -- and check for trailing
2389# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002390 if ($line =~ /do\s*(?!{)/) {
2391 my ($stat_next) = ctx_statement_block($line_nr_next,
2392 $remain_next, $off_next);
2393 $stat_next =~ s/\n./\n /g;
2394 ##print "stat<$stat> stat_next<$stat_next>\n";
2395
2396 if ($stat_next =~ /^\s*while\b/) {
2397 # If the statement carries leading newlines,
2398 # then count those as offsets.
2399 my ($whitespace) =
2400 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2401 my $offset =
2402 statement_rawlines($whitespace) - 1;
2403
2404 $suppress_whiletrailers{$line_nr_next +
2405 $offset} = 1;
2406 }
2407 }
2408 if (!defined $suppress_whiletrailers{$linenr} &&
2409 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002410 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002411
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002412 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002413 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002414 }
2415
2416 # Find out what is on the end of the line after the
2417 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002418 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002419 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002420 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002421 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2422 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002423 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002424 # Find out how long the conditional actually is.
2425 my @newlines = ($c =~ /\n/gs);
2426 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002427 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002428
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002429 $stat_real = raw_line($linenr, $cond_lines)
2430 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002431 if (defined($stat_real) && $cond_lines > 1) {
2432 $stat_real = "[...]\n$stat_real";
2433 }
2434
2435 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002436 }
2437 }
2438
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002439# Check for bitwise tests written as boolean
2440 if ($line =~ /
2441 (?:
2442 (?:\[|\(|\&\&|\|\|)
2443 \s*0[xX][0-9]+\s*
2444 (?:\&\&|\|\|)
2445 |
2446 (?:\&\&|\|\|)
2447 \s*0[xX][0-9]+\s*
2448 (?:\&\&|\|\||\)|\])
2449 )/x)
2450 {
2451 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2452 }
2453
Andy Whitcroft8905a672007-11-28 16:21:06 -08002454# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002455 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2456 my $s = $1;
2457 $s =~ s/$;//g; # Remove any comments
2458 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2459 ERROR("trailing statements should be on next line\n" . $herecurr);
2460 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002461 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002462# if should not continue a brace
2463 if ($line =~ /}\s*if\b/) {
2464 ERROR("trailing statements should be on next line\n" .
2465 $herecurr);
2466 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002467# case and default should not have general statements after them
2468 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2469 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002470 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002471 \s*return\s+
2472 )/xg)
2473 {
2474 ERROR("trailing statements should be on next line\n" . $herecurr);
2475 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002476
2477 # Check for }<nl>else {, these must be at the same
2478 # indent level to be relevant to each other.
2479 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2480 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002481 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002482 }
2483
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002484 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2485 $previndent == $indent) {
2486 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2487
2488 # Find out what is on the end of the line after the
2489 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002490 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002491 $s =~ s/\n.*//g;
2492
2493 if ($s =~ /^\s*;/) {
2494 ERROR("while should follow close brace '}'\n" . $hereprev);
2495 }
2496 }
2497
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002498#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2499# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2500# print "No studly caps, use _\n";
2501# print "$herecurr";
2502# $clean = 0;
2503# }
2504
2505#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002506 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002507 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002508 }
2509
Andy Whitcroft653d4872007-06-23 17:16:34 -07002510#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002511 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002512 my $file = "$1.h";
2513 my $checkfile = "include/linux/$file";
2514 if (-f "$root/$checkfile" &&
2515 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002516 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002517 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002518 if ($realfile =~ m{^arch/}) {
2519 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2520 } else {
2521 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2522 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002523 }
2524 }
2525
Andy Whitcroft653d4872007-06-23 17:16:34 -07002526# multi-statement macros should be enclosed in a do while loop, grab the
2527# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002528# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002529 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2530 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002531 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002533 my ($off, $dstat, $dcond, $rest);
2534 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002535
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002536 my $args = defined($1);
2537
2538 # Find the end of the macro and limit our statement
2539 # search to that.
2540 while ($cnt > 0 && defined $lines[$ln - 1] &&
2541 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2542 {
2543 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002544 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002545 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002546 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002547 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002548
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002549 ($dstat, $dcond, $ln, $cnt, $off) =
2550 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2551 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002552 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002553
2554 # Extract the remainder of the define (if any) and
2555 # rip off surrounding spaces, and trailing \'s.
2556 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002557 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2558 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002559 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2560 $rest .= substr($lines[$ln - 1], $off) . "\n";
2561 $cnt--;
2562 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002563 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002564 $off = 0;
2565 }
2566 $rest =~ s/\\\n.//g;
2567 $rest =~ s/^\s*//s;
2568 $rest =~ s/\s*$//s;
2569
2570 # Clean up the original statement.
2571 if ($args) {
2572 substr($dstat, 0, length($dcond), '');
2573 } else {
2574 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2575 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002576 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002577 $dstat =~ s/\\\n.//g;
2578 $dstat =~ s/^\s*//s;
2579 $dstat =~ s/\s*$//s;
2580
2581 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002582 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2583 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2584 $dstat =~ s/\[[^\{\}]*\]/1/)
2585 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002586 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588 # Extremely long macros may fall off the end of the
2589 # available context without closing. Give a dangling
2590 # backslash the benefit of the doubt and allow it
2591 # to gobble any hanging open-parens.
2592 $dstat =~ s/\(.+\\$/1/;
2593
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002594 my $exceptions = qr{
2595 $Declare|
2596 module_param_named|
2597 MODULE_PARAM_DESC|
2598 DECLARE_PER_CPU|
2599 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002601 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002602 union|
2603 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002604 \.$Ident\s*=\s*|
2605 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002606 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002607 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2608 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002609 if ($rest !~ /while\s*\(/ &&
2610 $dstat !~ /$exceptions/)
2611 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002612 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 -07002613 }
2614
2615 } elsif ($ctx !~ /;/) {
2616 if ($dstat ne '' &&
2617 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2618 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002619 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002620 $dstat =~ /$Operators/)
2621 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002622 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002623 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002624 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002625 }
2626
Mike Frysinger080ba922009-01-06 14:41:25 -08002627# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2628# all assignments may have only one of the following with an assignment:
2629# .
2630# ALIGN(...)
2631# VMLINUX_SYMBOL(...)
2632 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2633 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2634 }
2635
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002636# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002637 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2638 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002639 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002640 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002641 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2642 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002643 my $allowed = 0;
2644 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002645 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002646 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002647 for my $chunk (@chunks) {
2648 my ($cond, $block) = @{$chunk};
2649
Andy Whitcroft773647a2008-03-28 14:15:58 -07002650 # If the condition carries leading newlines, then count those as offsets.
2651 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2652 my $offset = statement_rawlines($whitespace) - 1;
2653
2654 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2655
2656 # We have looked at and allowed this specific line.
2657 $suppress_ifbraces{$ln + $offset} = 1;
2658
2659 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002660 $ln += statement_rawlines($block) - 1;
2661
Andy Whitcroft773647a2008-03-28 14:15:58 -07002662 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002663
2664 $seen++ if ($block =~ /^\s*{/);
2665
Andy Whitcroftcf655042008-03-04 14:28:20 -08002666 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2667 if (statement_lines($cond) > 1) {
2668 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002669 $allowed = 1;
2670 }
2671 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002672 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002673 $allowed = 1;
2674 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002675 if (statement_block_size($block) > 1) {
2676 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002677 $allowed = 1;
2678 }
2679 }
2680 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002681 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002682 }
2683 }
2684 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002685 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002686 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002687 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002688
Andy Whitcroftcf655042008-03-04 14:28:20 -08002689 # Check the pre-context.
2690 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2691 #print "APW: ALLOWED: pre<$1>\n";
2692 $allowed = 1;
2693 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002694
2695 my ($level, $endln, @chunks) =
2696 ctx_statement_full($linenr, $realcnt, $-[0]);
2697
Andy Whitcroftcf655042008-03-04 14:28:20 -08002698 # Check the condition.
2699 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002700 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002701 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002702 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002703 }
2704 if (statement_lines($cond) > 1) {
2705 #print "APW: ALLOWED: cond<$cond>\n";
2706 $allowed = 1;
2707 }
2708 if ($block =~/\b(?:if|for|while)\b/) {
2709 #print "APW: ALLOWED: block<$block>\n";
2710 $allowed = 1;
2711 }
2712 if (statement_block_size($block) > 1) {
2713 #print "APW: ALLOWED: lines block<$block>\n";
2714 $allowed = 1;
2715 }
2716 # Check the post-context.
2717 if (defined $chunks[1]) {
2718 my ($cond, $block) = @{$chunks[1]};
2719 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002720 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002721 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002722 if ($block =~ /^\s*\{/) {
2723 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2724 $allowed = 1;
2725 }
2726 }
2727 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2728 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002729 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002730
Andy Whitcroftf0556632008-10-15 22:02:23 -07002731 for (my $n = 0; $n < $cnt; $n++) {
2732 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002733 }
2734
2735 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002736 }
2737 }
2738
Andy Whitcroft653d4872007-06-23 17:16:34 -07002739# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002740 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002741 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002742 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002743 }
2744 }
2745
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002746# don't use deprecated functions
2747 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002748 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002749 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002750 }
2751 }
2752
2753# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002754 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2755 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002756 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002757 }
2758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002759# sys_open/read/write/close are not allowed in the kernel
2760 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
2761 ERROR("$1 is inappropriate in kernel code.\n" .
2762 $herecurr);
2763 }
2764
2765# filp_open is a backdoor for sys_open
2766 if ($line =~ /\b(filp_open)\b/) {
2767 ERROR("$1 is inappropriate in kernel code.\n" .
2768 $herecurr);
2769 }
2770
2771# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
2772 if ($line =~ /\b((?:read|write)[bwl])\b/) {
2773 ERROR("Use of $1 is deprecated: use $1_relaxed\n\t" .
2774 "with appropriate memory barriers instead.\n" .
2775 $herecurr);
2776 }
2777
2778# likewise, in/out[bwl] should be __raw_read/write[bwl]...
2779 if ($line =~ /\b((in|out)([bwl]))\b/) {
2780 my ($all, $pref, $suf) = ($1, $2, $3);
2781 $pref =~ s/in/read/;
2782 $pref =~ s/out/write/;
2783 ERROR("Use of $all is deprecated: use " .
2784 "__raw_$pref$suf\n\t" .
2785 "with appropriate memory barriers instead.\n" .
2786 $herecurr);
2787 }
2788
2789# dsb is too ARMish, and should usually be mb.
2790 if ($line =~ /\bdsb\b/) {
2791 WARN("Use of dsb is discouranged: prefer mb.\n" .
2792 $herecurr);
2793 }
2794
2795# unbounded string functions are overflow risks
2796 my %str_fns = (
2797 "sprintf" => "snprintf",
2798 "strcpy" => "strncpy",
2799 "strcat" => "strncat",
2800 "strcmp" => "strncmp",
2801 "strcasecmp" => "strncasecmp",
2802 "strchr" => "strnchr",
2803 "strstr" => "strnstr",
2804 "strlen" => "strnlen",
2805 );
2806 foreach my $k (keys %str_fns) {
2807 if ($line =~ /\b$k\b/) {
2808 ERROR("Use of $k is deprecated: " .
2809 "use $str_fns{$k} instead.\n" .
2810 $herecurr);
2811 }
2812 }
2813
Andy Whitcroft00df3442007-06-08 13:47:06 -07002814# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002815 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002816 WARN("if this code is redundant consider removing it\n"
2817 . $herecurr);
2818 }
2819# warn about #if 1
2820 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
2821 WARN("if this code is required consider removing"
2822 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002823 }
2824
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002825# check for needless kfree() checks
2826 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2827 my $expr = $1;
2828 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002829 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002830 }
2831 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002832# check for needless usb_free_urb() checks
2833 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2834 my $expr = $1;
2835 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2836 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2837 }
2838 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002839
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002840# prefer usleep_range over udelay
2841 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2842 # ignore udelay's < 10, however
2843 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2844 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2845 }
2846 }
2847
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002848# warn about unexpectedly long msleep's
2849 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2850 if ($1 < 20) {
2851 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2852 }
2853 }
2854
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002855# check the patch for use of mdelay
2856 if ($line =~ /\bmdelay\s*\(/) {
2857 WARN("use of mdelay() found: msleep() is the preferred API.\n" . $line );
2858 }
2859
Andy Whitcroft00df3442007-06-08 13:47:06 -07002860# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002861# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002862# print "#ifdef in C files should be avoided\n";
2863# print "$herecurr";
2864# $clean = 0;
2865# }
2866
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002867# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002868 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002869 ERROR("exactly one space required after that #$1\n" . $herecurr);
2870 }
2871
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002872# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002873 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2874 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002875 my $which = $1;
2876 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002877 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002878 }
2879 }
2880# check for memory barriers without a comment.
2881 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2882 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002883 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002884 }
2885 }
2886# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002887 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002888 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002889 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002890
Tobias Klauserd4977c72010-05-24 14:33:30 -07002891# Check that the storage class is at the beginning of a declaration
2892 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2893 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2894 }
2895
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002896# check the location of the inline attribute, that it is between
2897# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002898 if ($line =~ /\b$Type\s+$Inline\b/ ||
2899 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002900 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2901 }
2902
Andy Whitcroft8905a672007-11-28 16:21:06 -08002903# Check for __inline__ and __inline, prefer inline
2904 if ($line =~ /\b(__inline__|__inline)\b/) {
2905 WARN("plain inline is preferred over $1\n" . $herecurr);
2906 }
2907
Joe Perches3d130fd2011-01-12 17:00:00 -08002908# Check for __attribute__ packed, prefer __packed
2909 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2910 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2911 }
2912
Joe Perches8f53a9b2010-03-05 13:43:48 -08002913# check for sizeof(&)
2914 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2915 WARN("sizeof(& should be avoided\n" . $herecurr);
2916 }
2917
Joe Perches428e2fd2011-05-24 17:13:39 -07002918# check for line continuations in quoted strings with odd counts of "
2919 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2920 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2921 }
2922
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002923# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002924 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002925 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002926 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002927 my $function_name = $1;
2928 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002929
2930 my $s = $stat;
2931 if (defined $cond) {
2932 substr($s, 0, length($cond), '');
2933 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002934 if ($s =~ /^\s*;/ &&
2935 $function_name ne 'uninitialized_var')
2936 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002937 WARN("externs should be avoided in .c files\n" . $herecurr);
2938 }
2939
2940 if ($paren_space =~ /\n/) {
2941 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2942 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002943
2944 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2945 $stat =~ /^.\s*extern\s+/)
2946 {
2947 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002948 }
2949
2950# checks for new __setup's
2951 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2952 my $name = $1;
2953
2954 if (!grep(/$name/, @setup_docs)) {
2955 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2956 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002957 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002958
2959# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002960 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002961 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2962 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002963
Joe Perchescaf2a542011-01-12 16:59:56 -08002964# check for multiple semicolons
2965 if ($line =~ /;\s*;\s*$/) {
2966 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2967 }
2968
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002969# check for return codes on error paths
2970 if ($line =~ /\breturn\s+-\d+/) {
2971 ERROR("illegal return value, please use an error code\n" . $herecurr);
2972 }
2973
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002974# check for gcc specific __FUNCTION__
2975 if ($line =~ /__FUNCTION__/) {
2976 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2977 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002978
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002979# check for semaphores initialized locked
2980 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002981 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002982
Andy Whitcroft773647a2008-03-28 14:15:58 -07002983 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002984# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002985 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002986 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002987 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002988# check for __initcall(), use device_initcall() explicitly please
2989 if ($line =~ /^.\s*__initcall\s*\(/) {
2990 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2991 }
Emese Revfy79404842010-03-05 13:43:53 -08002992# check for various ops structs, ensure they are const.
2993 my $struct_ops = qr{acpi_dock_ops|
2994 address_space_operations|
2995 backlight_ops|
2996 block_device_operations|
2997 dentry_operations|
2998 dev_pm_ops|
2999 dma_map_ops|
3000 extent_io_ops|
3001 file_lock_operations|
3002 file_operations|
3003 hv_ops|
3004 ide_dma_ops|
3005 intel_dvo_dev_ops|
3006 item_operations|
3007 iwl_ops|
3008 kgdb_arch|
3009 kgdb_io|
3010 kset_uevent_ops|
3011 lock_manager_operations|
3012 microcode_ops|
3013 mtrr_ops|
3014 neigh_ops|
3015 nlmsvc_binding|
3016 pci_raw_ops|
3017 pipe_buf_operations|
3018 platform_hibernation_ops|
3019 platform_suspend_ops|
3020 proto_ops|
3021 rpc_pipe_ops|
3022 seq_operations|
3023 snd_ac97_build_ops|
3024 soc_pcmcia_socket_ops|
3025 stacktrace_ops|
3026 sysfs_ops|
3027 tty_operations|
3028 usb_mon_operations|
3029 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003030 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003031 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003032 WARN("struct $1 should normally be const\n" .
3033 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003034 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003035
3036# use of NR_CPUS is usually wrong
3037# ignore definitions of NR_CPUS and usage to define arrays as likely right
3038 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003039 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3040 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003041 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3042 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3043 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003044 {
3045 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3046 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003047
3048# check for %L{u,d,i} in strings
3049 my $string;
3050 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3051 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003052 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003053 if ($string =~ /(?<!%)%L[udi]/) {
3054 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3055 last;
3056 }
3057 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003058
3059# whine mightly about in_atomic
3060 if ($line =~ /\bin_atomic\s*\(/) {
3061 if ($realfile =~ m@^drivers/@) {
3062 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003063 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003064 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3065 }
3066 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003067
3068# check for lockdep_set_novalidate_class
3069 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3070 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3071 if ($realfile !~ m@^kernel/lockdep@ &&
3072 $realfile !~ m@^include/linux/lockdep@ &&
3073 $realfile !~ m@^drivers/base/core@) {
3074 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3075 }
3076 }
Dave Jones88f88312011-01-12 16:59:59 -08003077
3078 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3079 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3080 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3081 }
Dave Jones309c00c2011-03-22 16:34:44 -07003082
3083 # Check for memset with swapped arguments
3084 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3085 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3086 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003087 }
3088
3089 # If we have no input at all, then there is nothing to report on
3090 # so just keep quiet.
3091 if ($#rawlines == -1) {
3092 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003093 }
3094
Andy Whitcroft8905a672007-11-28 16:21:06 -08003095 # In mailback mode only produce a report in the negative, for
3096 # things that appear to be patches.
3097 if ($mailback && ($clean == 1 || !$is_patch)) {
3098 exit(0);
3099 }
3100
3101 # This is not a patch, and we are are in 'no-patch' mode so
3102 # just keep quiet.
3103 if (!$chk_patch && !$is_patch) {
3104 exit(0);
3105 }
3106
3107 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003108 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003109 }
3110 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003111 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003112 }
3113
Andy Whitcroft8905a672007-11-28 16:21:06 -08003114 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003115 if ($summary && !($clean == 1 && $quiet == 1)) {
3116 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003117 print "total: $cnt_error errors, $cnt_warn warnings, " .
3118 (($check)? "$cnt_chk checks, " : "") .
3119 "$cnt_lines lines checked\n";
3120 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003121 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003122
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003123 if ($quiet == 0) {
3124 # If there were whitespace errors which cleanpatch can fix
3125 # then suggest that.
3126 if ($rpt_cleaners) {
3127 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3128 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003129 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003130 }
3131 }
3132
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003133 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003134 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003135 }
3136 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003137 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003138 print "are false positives report them to the maintainer, see\n";
3139 print "CHECKPATCH in MAINTAINERS.\n";
3140 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003141
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003142 return $clean;
3143}