blob: 46a49e72f39a9820bb2b2a7b3fc6d34ff1b1438d [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;
Steve Muckled3b3b642012-03-05 10:12:04 -080011use constant IN_SHORTTEXT_BLANKLINE => 1;
12use constant IN_SHORTTEXT => 2;
13use constant AFTER_SHORTTEXT => 3;
14use constant CHECK_NEXT_SHORTTEXT => 4;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015use constant SHORTTEXT_LIMIT => 75;
16
Andy Whitcroft0a920b52007-06-01 00:46:48 -070017my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070018$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070019
Andy Whitcroft267ad8f2010-10-26 14:23:19 -070020my $V = '0.31';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021
22use Getopt::Long qw(:config no_auto_abbrev);
23
24my $quiet = 0;
25my $tree = 1;
26my $chk_signoff = 1;
27my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070028my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070031my $file = 0;
32my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080033my $summary = 1;
34my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080035my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Hannes Eder77f5b102009-09-21 17:04:37 -070038my $help = 0;
39
40sub help {
41 my ($exitcode) = @_;
42
43 print << "EOM";
44Usage: $P [OPTION]... [FILE]...
45Version: $V
46
47Options:
48 -q, --quiet quiet
49 --no-tree run without a kernel tree
50 --no-signoff do not check for 'Signed-off-by' line
51 --patch treat FILE as patchfile (default)
52 --emacs emacs compile window format
53 --terse one line per report
54 -f, --file treat FILE as regular source file
55 --subjective, --strict enable more subjective tests
56 --root=PATH PATH to the kernel tree root
57 --no-summary suppress the per-file summary
58 --mailback only produce a report in case of warnings/errors
59 --summary-file include the filename in summary
60 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
61 'values', 'possible', 'type', and 'attr' (default
62 is all off)
63 --test-only=WORD report only warnings/errors containing WORD
64 literally
65 -h, --help, --version display this help and exit
66
67When FILE is - read standard input.
68EOM
69
70 exit($exitcode);
71}
72
Andy Whitcroft0a920b52007-06-01 00:46:48 -070073GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070074 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070075 'tree!' => \$tree,
76 'signoff!' => \$chk_signoff,
77 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070078 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080079 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070080 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070081 'subjective!' => \$check,
82 'strict!' => \$check,
83 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080084 'summary!' => \$summary,
85 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080086 'summary-file!' => \$summary_file,
87
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080088 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070089 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070090 'h|help' => \$help,
91 'version' => \$help
92) or help(1);
93
94help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070095
96my $exit = 0;
97
98if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070099 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700100 exit(1);
101}
102
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800103my $dbg_values = 0;
104my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700105my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700106my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800107for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800108 ## no critic
109 eval "\${dbg_$key} = '$debug{$key}';";
110 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800111}
112
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700113my $rpt_cleaners = 0;
114
Andy Whitcroft8905a672007-11-28 16:21:06 -0800115if ($terse) {
116 $emacs = 1;
117 $quiet++;
118}
119
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700120if ($tree) {
121 if (defined $root) {
122 if (!top_of_kernel_tree($root)) {
123 die "$P: $root: --root does not point at a valid tree\n";
124 }
125 } else {
126 if (top_of_kernel_tree('.')) {
127 $root = '.';
128 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
129 top_of_kernel_tree($1)) {
130 $root = $1;
131 }
132 }
133
134 if (!defined $root) {
135 print "Must be run from the top-level dir. of a kernel tree\n";
136 exit(2);
137 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700138}
139
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700140my $emitted_corrupt = 0;
141
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700142our $Ident = qr{
143 [A-Za-z_][A-Za-z\d_]*
144 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
145 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700146our $Storage = qr{extern|static|asmlinkage};
147our $Sparse = qr{
148 __user|
149 __kernel|
150 __force|
151 __iomem|
152 __must_check|
153 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800154 __kprobes|
155 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700156 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800157
158# Notes to $Attribute:
159# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700160our $Attribute = qr{
161 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700162 __percpu|
163 __nocast|
164 __safe|
165 __bitwise__|
166 __packed__|
167 __packed2__|
168 __naked|
169 __maybe_unused|
170 __always_unused|
171 __noreturn|
172 __used|
173 __cold|
174 __noclone|
175 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700176 __read_mostly|
177 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800178 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700179 ____cacheline_aligned|
180 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800181 ____cacheline_internodealigned_in_smp|
182 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700183 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700184our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700186our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
187our $Lval = qr{$Ident(?:$Member)*};
188
189our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
190our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800191our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700192our $Operators = qr{
193 <=|>=|==|!=|
194 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800195 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700196 }x;
197
Andy Whitcroft8905a672007-11-28 16:21:06 -0800198our $NonptrType;
199our $Type;
200our $Declare;
201
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700202our $UTF8 = qr {
203 [\x09\x0A\x0D\x20-\x7E] # ASCII
204 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
205 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
206 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
207 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
208 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
209 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
210 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
211}x;
212
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700213our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700214 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700215 atomic_t
216)};
217
Joe Perches691e6692010-03-05 13:43:51 -0800218our $logFunctions = qr{(?x:
219 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700220 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800221 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700222 panic|
223 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800224)};
225
Andy Whitcroft8905a672007-11-28 16:21:06 -0800226our @typeList = (
227 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700228 qr{(?:unsigned\s+)?char},
229 qr{(?:unsigned\s+)?short},
230 qr{(?:unsigned\s+)?int},
231 qr{(?:unsigned\s+)?long},
232 qr{(?:unsigned\s+)?long\s+int},
233 qr{(?:unsigned\s+)?long\s+long},
234 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800235 qr{unsigned},
236 qr{float},
237 qr{double},
238 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800239 qr{struct\s+$Ident},
240 qr{union\s+$Ident},
241 qr{enum\s+$Ident},
242 qr{${Ident}_t},
243 qr{${Ident}_handler},
244 qr{${Ident}_handler_fn},
245);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700246our @modifierList = (
247 qr{fastcall},
248);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800249
Wolfram Sang7840a942010-08-09 17:20:57 -0700250our $allowed_asm_includes = qr{(?x:
251 irq|
252 memory
253)};
254# memory.h: ARM has a custom one
255
Andy Whitcroft8905a672007-11-28 16:21:06 -0800256sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700257 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
258 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700259 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800260 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700261 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800262 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700263 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700264 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700265 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800266 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700267 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800268 }x;
269 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700270 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800271 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700272 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800273 }x;
274 $Declare = qr{(?:$Storage\s+)?$Type};
275}
276build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700277
278$chk_signoff = 0 if ($file);
279
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700280my @dep_includes = ();
281my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700282my $removal = "Documentation/feature-removal-schedule.txt";
283if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800284 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700285 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700287 if (/^Check:\s+(.*\S)/) {
288 for my $entry (split(/[, ]+/, $1)) {
289 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700290 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700291
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700292 } elsif ($entry !~ m@/@) {
293 push(@dep_functions, $entry);
294 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700295 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700296 }
297 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800298 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700299}
300
Andy Whitcroft00df3442007-06-08 13:47:06 -0700301my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302my @lines = ();
303my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700304for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700306 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800307 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700308 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800309 } elsif ($filename eq '-') {
310 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700311 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800312 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700314 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800315 if ($filename eq '-') {
316 $vname = 'Your patch';
317 } else {
318 $vname = $filename;
319 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800320 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700321 chomp;
322 push(@rawlines, $_);
323 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800324 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800325 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700326 $exit = 1;
327 }
328 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800329 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700330}
331
332exit($exit);
333
334sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335 my ($root) = @_;
336
337 my @tree_check = (
338 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
339 "README", "Documentation", "arch", "include", "drivers",
340 "fs", "init", "ipc", "kernel", "lib", "scripts",
341 );
342
343 foreach my $check (@tree_check) {
344 if (! -e $root . '/' . $check) {
345 return 0;
346 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700347 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700348 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700349}
350
351sub expand_tabs {
352 my ($str) = @_;
353
354 my $res = '';
355 my $n = 0;
356 for my $c (split(//, $str)) {
357 if ($c eq "\t") {
358 $res .= ' ';
359 $n++;
360 for (; ($n % 8) != 0; $n++) {
361 $res .= ' ';
362 }
363 next;
364 }
365 $res .= $c;
366 $n++;
367 }
368
369 return $res;
370}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700371sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700372 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700373 return $res;
374}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700375
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700376sub line_stats {
377 my ($line) = @_;
378
379 # Drop the diff line leader and expand tabs
380 $line =~ s/^.//;
381 $line = expand_tabs($line);
382
383 # Pick the indent from the front of the line.
384 my ($white) = ($line =~ /^(\s*)/);
385
386 return (length($line), length($white));
387}
388
Andy Whitcroft773647a2008-03-28 14:15:58 -0700389my $sanitise_quote = '';
390
391sub sanitise_line_reset {
392 my ($in_comment) = @_;
393
394 if ($in_comment) {
395 $sanitise_quote = '*/';
396 } else {
397 $sanitise_quote = '';
398 }
399}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700400sub sanitise_line {
401 my ($line) = @_;
402
403 my $res = '';
404 my $l = '';
405
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800406 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700407 my $off = 0;
408 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700409
Andy Whitcroft773647a2008-03-28 14:15:58 -0700410 # Always copy over the diff marker.
411 $res = substr($line, 0, 1);
412
413 for ($off = 1; $off < length($line); $off++) {
414 $c = substr($line, $off, 1);
415
416 # Comments we are wacking completly including the begin
417 # and end, all to $;.
418 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
419 $sanitise_quote = '*/';
420
421 substr($res, $off, 2, "$;$;");
422 $off++;
423 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800424 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700425 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700426 $sanitise_quote = '';
427 substr($res, $off, 2, "$;$;");
428 $off++;
429 next;
430 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700431 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
432 $sanitise_quote = '//';
433
434 substr($res, $off, 2, $sanitise_quote);
435 $off++;
436 next;
437 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700438
439 # A \ in a string means ignore the next character.
440 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
441 $c eq "\\") {
442 substr($res, $off, 2, 'XX');
443 $off++;
444 next;
445 }
446 # Regular quotes.
447 if ($c eq "'" || $c eq '"') {
448 if ($sanitise_quote eq '') {
449 $sanitise_quote = $c;
450
451 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700452 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700453 } elsif ($sanitise_quote eq $c) {
454 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700455 }
456 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700457
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800458 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700459 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
460 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700461 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
462 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700463 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
464 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700465 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700466 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700467 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800468 }
469
Daniel Walker113f04a2009-09-21 17:04:35 -0700470 if ($sanitise_quote eq '//') {
471 $sanitise_quote = '';
472 }
473
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800474 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700475 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800476 my $clean = 'X' x length($1);
477 $res =~ s@\<.*\>@<$clean>@;
478
479 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700480 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800481 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700482 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800483 }
484
Andy Whitcroft00df3442007-06-08 13:47:06 -0700485 return $res;
486}
487
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488sub ctx_statement_block {
489 my ($linenr, $remain, $off) = @_;
490 my $line = $linenr - 1;
491 my $blk = '';
492 my $soff = $off;
493 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700494 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800495
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800496 my $loff = 0;
497
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498 my $type = '';
499 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800500 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800501 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800502 my $c;
503 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800504
505 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800506 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800507 @stack = (['', 0]) if ($#stack == -1);
508
Andy Whitcroft773647a2008-03-28 14:15:58 -0700509 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510 # If we are about to drop off the end, pull in more
511 # context.
512 if ($off >= $len) {
513 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700514 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800515 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800516 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800517 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800518 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519 $len = length($blk);
520 $line++;
521 last;
522 }
523 # Bail if there is no further context.
524 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800525 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 last;
527 }
528 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800529 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800530 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800531 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532
Andy Whitcroft773647a2008-03-28 14:15:58 -0700533 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800534
535 # Handle nested #if/#else.
536 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
537 push(@stack, [ $type, $level ]);
538 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
539 ($type, $level) = @{$stack[$#stack - 1]};
540 } elsif ($remainder =~ /^#\s*endif\b/) {
541 ($type, $level) = @{pop(@stack)};
542 }
543
Andy Whitcroft8905a672007-11-28 16:21:06 -0800544 # Statement ends at the ';' or a close '}' at the
545 # outermost level.
546 if ($level == 0 && $c eq ';') {
547 last;
548 }
549
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800550 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700551 if ($level == 0 && $coff_set == 0 &&
552 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
553 $remainder =~ /^(else)(?:\s|{)/ &&
554 $remainder !~ /^else\s+if\b/) {
555 $coff = $off + length($1) - 1;
556 $coff_set = 1;
557 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
558 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800559 }
560
Andy Whitcroft8905a672007-11-28 16:21:06 -0800561 if (($type eq '' || $type eq '(') && $c eq '(') {
562 $level++;
563 $type = '(';
564 }
565 if ($type eq '(' && $c eq ')') {
566 $level--;
567 $type = ($level != 0)? '(' : '';
568
569 if ($level == 0 && $coff < $soff) {
570 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700571 $coff_set = 1;
572 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800573 }
574 }
575 if (($type eq '' || $type eq '{') && $c eq '{') {
576 $level++;
577 $type = '{';
578 }
579 if ($type eq '{' && $c eq '}') {
580 $level--;
581 $type = ($level != 0)? '{' : '';
582
583 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700584 if (substr($blk, $off + 1, 1) eq ';') {
585 $off++;
586 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800587 last;
588 }
589 }
590 $off++;
591 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700592 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800593 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700594 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800595 $line++;
596 $remain--;
597 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800598
599 my $statement = substr($blk, $soff, $off - $soff + 1);
600 my $condition = substr($blk, $soff, $coff - $soff + 1);
601
602 #warn "STATEMENT<$statement>\n";
603 #warn "CONDITION<$condition>\n";
604
Andy Whitcroft773647a2008-03-28 14:15:58 -0700605 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800606
607 return ($statement, $condition,
608 $line, $remain + 1, $off - $loff + 1, $level);
609}
610
Andy Whitcroftcf655042008-03-04 14:28:20 -0800611sub statement_lines {
612 my ($stmt) = @_;
613
614 # Strip the diff line prefixes and rip blank lines at start and end.
615 $stmt =~ s/(^|\n)./$1/g;
616 $stmt =~ s/^\s*//;
617 $stmt =~ s/\s*$//;
618
619 my @stmt_lines = ($stmt =~ /\n/g);
620
621 return $#stmt_lines + 2;
622}
623
624sub statement_rawlines {
625 my ($stmt) = @_;
626
627 my @stmt_lines = ($stmt =~ /\n/g);
628
629 return $#stmt_lines + 2;
630}
631
632sub statement_block_size {
633 my ($stmt) = @_;
634
635 $stmt =~ s/(^|\n)./$1/g;
636 $stmt =~ s/^\s*{//;
637 $stmt =~ s/}\s*$//;
638 $stmt =~ s/^\s*//;
639 $stmt =~ s/\s*$//;
640
641 my @stmt_lines = ($stmt =~ /\n/g);
642 my @stmt_statements = ($stmt =~ /;/g);
643
644 my $stmt_lines = $#stmt_lines + 2;
645 my $stmt_statements = $#stmt_statements + 1;
646
647 if ($stmt_lines > $stmt_statements) {
648 return $stmt_lines;
649 } else {
650 return $stmt_statements;
651 }
652}
653
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800654sub ctx_statement_full {
655 my ($linenr, $remain, $off) = @_;
656 my ($statement, $condition, $level);
657
658 my (@chunks);
659
Andy Whitcroftcf655042008-03-04 14:28:20 -0800660 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800661 ($statement, $condition, $linenr, $remain, $off, $level) =
662 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700663 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800664 push(@chunks, [ $condition, $statement ]);
665 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
666 return ($level, $linenr, @chunks);
667 }
668
669 # Pull in the following conditional/block pairs and see if they
670 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800671 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800672 ($statement, $condition, $linenr, $remain, $off, $level) =
673 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800674 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700675 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800676 #print "C: push\n";
677 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800678 }
679
680 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800681}
682
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700683sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700684 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700685 my $line;
686 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700687 my $blk = '';
688 my @o;
689 my @c;
690 my @res = ();
691
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700692 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800693 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700694 for ($line = $start; $remain > 0; $line++) {
695 next if ($rawlines[$line] =~ /^-/);
696 $remain--;
697
698 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800699
700 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700701 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800702 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700703 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800704 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700705 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800706 $level = pop(@stack);
707 }
708
Andy Whitcroft01464f32010-10-26 14:23:19 -0700709 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700710 ##print "C<$c>L<$level><$open$close>O<$off>\n";
711 if ($off > 0) {
712 $off--;
713 next;
714 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700715
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700716 if ($c eq $close && $level > 0) {
717 $level--;
718 last if ($level == 0);
719 } elsif ($c eq $open) {
720 $level++;
721 }
722 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700723
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700724 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700725 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700726 }
727
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700728 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700729 }
730
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700731 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700732}
733sub ctx_block_outer {
734 my ($linenr, $remain) = @_;
735
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700736 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
737 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700738}
739sub ctx_block {
740 my ($linenr, $remain) = @_;
741
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700742 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
743 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700744}
745sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700746 my ($linenr, $remain, $off) = @_;
747
748 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
749 return @r;
750}
751sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700752 my ($linenr, $remain) = @_;
753
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700754 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700755}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700756sub ctx_statement_level {
757 my ($linenr, $remain, $off) = @_;
758
759 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
760}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700761
762sub ctx_locate_comment {
763 my ($first_line, $end_line) = @_;
764
765 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700766 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700767 return $current_comment if (defined $current_comment);
768
769 # Look through the context and try and figure out if there is a
770 # comment.
771 my $in_comment = 0;
772 $current_comment = '';
773 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700774 my $line = $rawlines[$linenr - 1];
775 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700776 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
777 $in_comment = 1;
778 }
779 if ($line =~ m@/\*@) {
780 $in_comment = 1;
781 }
782 if (!$in_comment && $current_comment ne '') {
783 $current_comment = '';
784 }
785 $current_comment .= $line . "\n" if ($in_comment);
786 if ($line =~ m@\*/@) {
787 $in_comment = 0;
788 }
789 }
790
791 chomp($current_comment);
792 return($current_comment);
793}
794sub ctx_has_comment {
795 my ($first_line, $end_line) = @_;
796 my $cmt = ctx_locate_comment($first_line, $end_line);
797
Andy Whitcroft00df3442007-06-08 13:47:06 -0700798 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700799 ##print "CMMT: $cmt\n";
800
801 return ($cmt ne '');
802}
803
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700804sub raw_line {
805 my ($linenr, $cnt) = @_;
806
807 my $offset = $linenr - 1;
808 $cnt++;
809
810 my $line;
811 while ($cnt) {
812 $line = $rawlines[$offset++];
813 next if (defined($line) && $line =~ /^-/);
814 $cnt--;
815 }
816
817 return $line;
818}
819
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700820sub cat_vet {
821 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700822 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700823
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700824 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700825 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
826 $res .= $1;
827 if ($2 ne '') {
828 $coded = sprintf("^%c", unpack('C', $2) + 64);
829 $res .= $coded;
830 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700831 }
832 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700833
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700834 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700835}
836
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800837my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800838my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800839my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700840my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800841
842sub annotate_reset {
843 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800844 $av_pending = '_';
845 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700846 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800847}
848
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700849sub annotate_values {
850 my ($stream, $type) = @_;
851
852 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700853 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700854 my $cur = $stream;
855
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800856 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700857
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700858 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700859 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800860 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700861 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700862 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800863 print "WS($1)\n" if ($dbg_values > 1);
864 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800865 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800866 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700867 }
868
Florian Micklerc023e4732011-01-12 16:59:58 -0800869 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700870 print "CAST($1)\n" if ($dbg_values > 1);
871 push(@av_paren_type, $type);
872 $type = 'C';
873
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700874 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800875 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700876 $type = 'T';
877
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700878 } elsif ($cur =~ /^($Modifier)\s*/) {
879 print "MODIFIER($1)\n" if ($dbg_values > 1);
880 $type = 'T';
881
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700882 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700883 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800884 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700885 push(@av_paren_type, $type);
886 if ($2 ne '') {
887 $av_pending = 'N';
888 }
889 $type = 'E';
890
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700891 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700892 print "UNDEF($1)\n" if ($dbg_values > 1);
893 $av_preprocessor = 1;
894 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700895
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700896 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800897 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800898 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800899
900 push(@av_paren_type, $type);
901 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700902 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800903
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700904 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800905 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
906 $av_preprocessor = 1;
907
908 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
909
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700910 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800911
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700912 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800913 print "PRE_END($1)\n" if ($dbg_values > 1);
914
915 $av_preprocessor = 1;
916
917 # Assume all arms of the conditional end as this
918 # one does, and continue as if the #endif was not here.
919 pop(@av_paren_type);
920 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700921 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922
923 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800924 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700925
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700926 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
927 print "ATTR($1)\n" if ($dbg_values > 1);
928 $av_pending = $type;
929 $type = 'N';
930
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700931 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800932 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700933 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800934 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700935 }
936 $type = 'N';
937
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700938 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800939 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700940 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700941 $type = 'N';
942
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700943 } elsif ($cur =~/^(case)/o) {
944 print "CASE($1)\n" if ($dbg_values > 1);
945 $av_pend_colon = 'C';
946 $type = 'N';
947
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700948 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800949 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700950 $type = 'N';
951
952 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800953 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800954 push(@av_paren_type, $av_pending);
955 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700956 $type = 'N';
957
958 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800959 my $new_type = pop(@av_paren_type);
960 if ($new_type ne '_') {
961 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800962 print "PAREN('$1') -> $type\n"
963 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700964 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800965 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700966 }
967
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700968 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800969 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700970 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800971 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700972
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800973 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
974 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700975 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800976 } elsif ($type eq 'E') {
977 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700978 }
979 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
980 $type = 'V';
981
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700982 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800983 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700984 $type = 'V';
985
986 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800987 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700988 $type = 'N';
989
Andy Whitcroftcf655042008-03-04 14:28:20 -0800990 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800991 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800992 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700993 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800994
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800995 } elsif ($cur =~/^(,)/) {
996 print "COMMA($1)\n" if ($dbg_values > 1);
997 $type = 'C';
998
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700999 } elsif ($cur =~ /^(\?)/o) {
1000 print "QUESTION($1)\n" if ($dbg_values > 1);
1001 $type = 'N';
1002
1003 } elsif ($cur =~ /^(:)/o) {
1004 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1005
1006 substr($var, length($res), 1, $av_pend_colon);
1007 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1008 $type = 'E';
1009 } else {
1010 $type = 'N';
1011 }
1012 $av_pend_colon = 'O';
1013
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001014 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001015 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001016 $type = 'N';
1017
Andy Whitcroft0d413862008-10-15 22:02:16 -07001018 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001019 my $variant;
1020
1021 print "OPV($1)\n" if ($dbg_values > 1);
1022 if ($type eq 'V') {
1023 $variant = 'B';
1024 } else {
1025 $variant = 'U';
1026 }
1027
1028 substr($var, length($res), 1, $variant);
1029 $type = 'N';
1030
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001032 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001033 if ($1 ne '++' && $1 ne '--') {
1034 $type = 'N';
1035 }
1036
1037 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001039 }
1040 if (defined $1) {
1041 $cur = substr($cur, length($1));
1042 $res .= $type x length($1);
1043 }
1044 }
1045
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001046 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001047}
1048
Andy Whitcroft8905a672007-11-28 16:21:06 -08001049sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001050 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001051 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001052 ^(?:
1053 $Modifier|
1054 $Storage|
1055 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001056 DEFINE_\S+
1057 )$|
1058 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001059 goto|
1060 return|
1061 case|
1062 else|
1063 asm|__asm__|
1064 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001065 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001066 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001067 )}x;
1068 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1069 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001070 # Check for modifiers.
1071 $possible =~ s/\s*$Storage\s*//g;
1072 $possible =~ s/\s*$Sparse\s*//g;
1073 if ($possible =~ /^\s*$/) {
1074
1075 } elsif ($possible =~ /\s/) {
1076 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001077 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001078 if ($modifier !~ $notPermitted) {
1079 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1080 push(@modifierList, $modifier);
1081 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001082 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001083
1084 } else {
1085 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1086 push(@typeList, $possible);
1087 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001088 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001089 } else {
1090 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001091 }
1092}
1093
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001094my $prefix = '';
1095
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001096sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001097 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1098 return 0;
1099 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001100 my $line = $prefix . $_[0];
1101
1102 $line = (split('\n', $line))[0] . "\n" if ($terse);
1103
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001104 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001105
1106 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001107}
1108sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001109 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001110}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001111sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001112 if (report("ERROR: $_[0]\n")) {
1113 our $clean = 0;
1114 our $cnt_error++;
1115 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001116}
1117sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001118 if (report("WARNING: $_[0]\n")) {
1119 our $clean = 0;
1120 our $cnt_warn++;
1121 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001122}
1123sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001124 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 our $clean = 0;
1126 our $cnt_chk++;
1127 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001128}
1129
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001130sub check_absolute_file {
1131 my ($absolute, $herecurr) = @_;
1132 my $file = $absolute;
1133
1134 ##print "absolute<$absolute>\n";
1135
1136 # See if any suffix of this path is a path within the tree.
1137 while ($file =~ s@^[^/]*/@@) {
1138 if (-f "$root/$file") {
1139 ##print "file<$file>\n";
1140 last;
1141 }
1142 }
1143 if (! -f _) {
1144 return 0;
1145 }
1146
1147 # It is, so see if the prefix is acceptable.
1148 my $prefix = $absolute;
1149 substr($prefix, -length($file)) = '';
1150
1151 ##print "prefix<$prefix>\n";
1152 if ($prefix ne ".../") {
1153 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1154 }
1155}
1156
Gregory Bean3f66eea2011-07-19 16:37:49 -07001157sub cleanup_continuation_headers {
1158 # Collapse any header-continuation lines into a single line so they
1159 # can be parsed meaningfully, as the parser only has one line
1160 # of context to work with.
1161 my $again;
1162 do {
1163 $again = 0;
1164 foreach my $n (0 .. scalar(@rawlines) - 2) {
1165 if ($rawlines[$n]=~/^\s*$/) {
1166 # A blank line means there's no more chance
1167 # of finding headers. Shortcut to done.
1168 return;
1169 }
1170 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
1171 $rawlines[$n+1]=~/^\s+/) {
1172 # Continuation header. Collapse it.
1173 my $line = splice @rawlines, $n+1, 1;
1174 $line=~s/^\s+/ /;
1175 $rawlines[$n] .= $line;
1176 # We've 'destabilized' the list, so restart.
1177 $again = 1;
1178 last;
1179 }
1180 }
1181 } while ($again);
1182}
1183
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001184sub process {
1185 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001186
1187 my $linenr=0;
1188 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001189 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001190 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001191 my $stashrawline="";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 my $subjectline="";
1193 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001194
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001195 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001196 my $indent;
1197 my $previndent=0;
1198 my $stashindent=0;
1199
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001200 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001201 my $signoff = 0;
1202 my $is_patch = 0;
1203
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001204 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001205 our $cnt_lines = 0;
1206 our $cnt_error = 0;
1207 our $cnt_warn = 0;
1208 our $cnt_chk = 0;
1209
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001210 # Trace the real file/line as we go.
1211 my $realfile = '';
1212 my $realline = 0;
1213 my $realcnt = 0;
1214 my $here = '';
1215 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001216 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001217 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001218 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001219
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001220 my $prev_values = 'E';
1221
1222 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001223 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001224 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001225 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001226
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001227 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001228 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001229 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001230 my @setup_docs = ();
1231 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 my $exec_file = "";
1234
1235 my $shorttext = BEFORE_SHORTTEXT;
1236 my $shorttext_exspc = 0;
Steve Muckled3b3b642012-03-05 10:12:04 -08001237 my $commit_text_present = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 sanitise_line_reset();
Gregory Bean3f66eea2011-07-19 16:37:49 -07001240 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001241 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001243 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001244 $linenr++;
1245 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001246
Andy Whitcroft773647a2008-03-28 14:15:58 -07001247 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001248 $setup_docs = 0;
1249 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1250 $setup_docs = 1;
1251 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001253 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001254 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1255 $realline=$1-1;
1256 if (defined $2) {
1257 $realcnt=$3+1;
1258 } else {
1259 $realcnt=1+1;
1260 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001261 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001262
1263 # Guestimate if this is a continuing comment. Run
1264 # the context looking for a comment "edge". If this
1265 # edge is a close comment then we must be in a comment
1266 # at context start.
1267 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001268 my $cnt = $realcnt;
1269 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1270 next if (defined $rawlines[$ln - 1] &&
1271 $rawlines[$ln - 1] =~ /^-/);
1272 $cnt--;
1273 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001274 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001275 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1276 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1277 ($edge) = $1;
1278 last;
1279 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280 }
1281 if (defined $edge && $edge eq '*/') {
1282 $in_comment = 1;
1283 }
1284
1285 # Guestimate if this is a continuing comment. If this
1286 # is the start of a diff block and this line starts
1287 # ' *' then it is very likely a comment.
1288 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001289 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001290 {
1291 $in_comment = 1;
1292 }
1293
1294 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1295 sanitise_line_reset($in_comment);
1296
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001297 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001299 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 $line = sanitise_line($rawline);
1301 }
1302 push(@lines, $line);
1303
1304 if ($realcnt > 1) {
1305 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1306 } else {
1307 $realcnt = 0;
1308 }
1309
1310 #print "==>$rawline\n";
1311 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001312
1313 if ($setup_docs && $line =~ /^\+/) {
1314 push(@setup_docs, $line);
1315 }
1316 }
1317
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001318 $prefix = '';
1319
Andy Whitcroft773647a2008-03-28 14:15:58 -07001320 $realcnt = 0;
1321 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001322 foreach my $line (@lines) {
1323 $linenr++;
1324
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001325 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001327#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001328 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001329 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001330 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001331 $realline=$1-1;
1332 if (defined $2) {
1333 $realcnt=$3+1;
1334 } else {
1335 $realcnt=1+1;
1336 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001337 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001338 $prev_values = 'E';
1339
Andy Whitcroft773647a2008-03-28 14:15:58 -07001340 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001341 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001342 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001343 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001344
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001345# track the line number as we move through the hunk, note that
1346# new versions of GNU diff omit the leading space on completely
1347# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001348 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001349 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001350 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001351
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001352 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001353 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001354
1355 # Track the previous line.
1356 ($prevline, $stashline) = ($stashline, $line);
1357 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001358 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1359
Andy Whitcroft773647a2008-03-28 14:15:58 -07001360 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001361
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001362 } elsif ($realcnt == 1) {
1363 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001364 }
1365
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001366 my $hunk_line = ($realcnt != 0);
1367
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001368#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001369 $prefix = "$filename:$realline: " if ($emacs && $file);
1370 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1371
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372 $here = "#$linenr: " if (!$file);
1373 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001374
1375 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001376 if ($line =~ /^diff --git.*?(\S+)$/) {
1377 $realfile = $1;
1378 $realfile =~ s@^([^/]*)/@@;
Gregory Bean246e1f12011-10-17 12:27:01 -07001379 $exec_file = $realfile;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001380
1381 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001382 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001383 $realfile =~ s@^([^/]*)/@@;
1384
1385 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001386 if (!$file && $tree && $p1_prefix ne '' &&
1387 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001388 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1389 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001390
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001391 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1393 }
Gregory Bean246e1f12011-10-17 12:27:01 -07001394 $exec_file = "";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001395 next;
1396 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1398 $exec_file = $1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 }
1400 #Check state to make sure we aren't in code block.
Gregory Bean246e1f12011-10-17 12:27:01 -07001401 elsif (($exec_file =~ /^.+\.[chS]$/ or
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 $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) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001421 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
1422 # the subject line was just processed,
1423 # a blank line must be next
1424 WARN("non-blank line after summary line\n" . $herecurr);
1425 $shorttext = IN_SHORTTEXT;
1426 # this non-blank line may or may not be commit text -
1427 # a warning has been generated so assume it is commit
1428 # text and move on
1429 $commit_text_present = 1;
1430 # fall through and treat this line as IN_SHORTTEXT
1431 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 if ($shorttext == IN_SHORTTEXT) {
1433 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001434 if ($commit_text_present == 0) {
1435 WARN("please add commit text explaining " .
1436 "*why* the change is needed\n" .
1437 $herecurr);
1438 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001439 $shorttext = AFTER_SHORTTEXT;
1440 } elsif (length($line) > (SHORTTEXT_LIMIT +
1441 $shorttext_exspc)
1442 && $line !~ /^:([0-7]{6}\s){2}
1443 ([[:xdigit:]]+\.*
1444 \s){2}\w+\s\w+/xms) {
1445 WARN("commit text line over " .
1446 SHORTTEXT_LIMIT .
1447 " characters\n" . $herecurr);
Steve Muckle336a3ae2012-03-09 11:15:24 -08001448 } elsif ($line=~/^\s*change-id:/i ||
1449 $line=~/^\s*signed-off-by:/i ||
1450 $line=~/^\s*crs-fixed:/i ||
1451 $line=~/^\s*acked-by:/i) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001452 # this is a tag, there must be commit
1453 # text by now
1454 if ($commit_text_present == 0) {
1455 WARN("please add commit text explaining " .
1456 "*why* the change is needed\n" .
1457 $herecurr);
1458 # prevent duplicate warnings
1459 $commit_text_present = 1;
1460 }
1461 } elsif ($line=~/\S/) {
1462 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001463 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001464 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
1465 # case of non-blank line in this state handled above
1466 $shorttext = IN_SHORTTEXT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1468# The Subject line doesn't have to be the last header in the patch.
1469# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1470# Per RFC5322, continuation lines must be folded, so any left-justified
1471# text which looks like a header is definitely a header.
1472 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1473 $shorttext = IN_SHORTTEXT;
Steve Muckled3b3b642012-03-05 10:12:04 -08001474 # Check for Subject line followed by a blank line.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001475 if (length($line) != 0) {
1476 WARN("non-blank line after " .
1477 "summary line\n" .
1478 $sublinenr . $here .
1479 "\n" . $subjectline .
1480 "\n" . $line . "\n");
Steve Muckled3b3b642012-03-05 10:12:04 -08001481 # this non-blank line may or may not
1482 # be commit text - a warning has been
1483 # generated so assume it is commit
1484 # text and move on
1485 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 }
1487 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001488 # The next two cases are BEFORE_SHORTTEXT.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001490 # This is the subject line. Go to
1491 # CHECK_NEXT_SHORTTEXT to wait for the commit
1492 # text to show up.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493 $shorttext = CHECK_NEXT_SHORTTEXT;
1494 $subjectline = $line;
1495 $sublinenr = "#$linenr & ";
1496# Check for Subject line less than line limit
1497 if (length($1) > SHORTTEXT_LIMIT) {
1498 WARN("summary line over " .
1499 SHORTTEXT_LIMIT .
1500 " characters\n" . $herecurr);
1501 }
1502 } elsif ($line=~/^ (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001503 # Indented format, this must be the summary
1504 # line (i.e. git show). There will be no more
1505 # headers so we are now in the shorttext.
1506 $shorttext = IN_SHORTTEXT_BLANKLINE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 $shorttext_exspc = 4;
1508 if (length($1) > SHORTTEXT_LIMIT) {
1509 WARN("summary line over " .
1510 SHORTTEXT_LIMIT .
1511 " characters\n" . $herecurr);
1512 }
1513 }
1514 }
1515
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001516 $cnt_lines++ if ($realcnt != 0);
1517
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001518# Check for incorrect file permissions
1519 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1520 my $permhere = $here . "FILE: $realfile\n";
1521 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1522 ERROR("do not set execute permissions for source files\n" . $permhere);
1523 }
1524 }
1525
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001526#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001527 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001528 # This is a signoff, if ugly, so do not double report.
1529 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001530 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001531 WARN("Signed-off-by: is the preferred form\n" .
1532 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001533 }
1534 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001535 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001536 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001537 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001538 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
1539 WARN("invalid Signed-off-by identity\n" . $line );
1540 }
1541 }
1542
1543#check the patch for invalid author credentials
1544 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
1545 WARN("invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001546 }
1547
Andy Whitcroft00df3442007-06-08 13:47:06 -07001548# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001549 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001550 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001551 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001552 }
1553
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001554# Check for absolute kernel paths.
1555 if ($tree) {
1556 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1557 my $file = $1;
1558
1559 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1560 check_absolute_file($1, $herecurr)) {
1561 #
1562 } else {
1563 check_absolute_file($file, $herecurr);
1564 }
1565 }
1566 }
1567
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001568# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1569 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001570 $rawline !~ m/^$UTF8*$/) {
1571 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1572
1573 my $blank = copy_spacing($rawline);
1574 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1575 my $hereptr = "$hereline$ptr\n";
1576
1577 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001578 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001579
Andy Whitcroft306708542008-10-15 22:02:28 -07001580# ignore non-hunk lines and lines being removed
1581 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001582
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001583#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001584 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001585 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001586 ERROR("DOS line endings\n" . $herevet);
1587
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001588 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1589 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001590 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001591 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001592 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001593
1594# check we are in a valid source file if not then ignore this hunk
1595 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1596
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001597#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001598 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001599 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001600 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001601 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001602 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001603 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001604 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001605 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001606 }
1607
Joe Perches5e79d962010-03-05 13:43:55 -08001608# check for spaces before a quoted newline
1609 if ($rawline =~ /^.*\".*\s\\n/) {
1610 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1611 }
1612
Andy Whitcroft8905a672007-11-28 16:21:06 -08001613# check for adding lines without a newline.
1614 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1615 WARN("adding a line without newline at end of file\n" . $herecurr);
1616 }
1617
Mike Frysinger42e41c52009-09-21 17:04:40 -07001618# Blackfin: use hi/lo macros
1619 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1620 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1621 my $herevet = "$here\n" . cat_vet($line) . "\n";
1622 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1623 }
1624 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1625 my $herevet = "$here\n" . cat_vet($line) . "\n";
1626 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1627 }
1628 }
1629
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001630# check we are in a valid source file C or perl if not then ignore this hunk
1631 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001632
1633# at the beginning of a line any tabs must come first and anything
1634# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001635 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1636 $rawline =~ /^\+\s* \s*/) {
1637 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001638 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001639 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001640 }
1641
Alberto Panizzo08e44362010-03-05 13:43:54 -08001642# check for space before tabs.
1643 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1644 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1645 WARN("please, no space before tabs\n" . $herevet);
1646 }
1647
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001648# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001649# Exceptions:
1650# 1) within comments
1651# 2) indented preprocessor commands
1652# 3) hanging labels
1653 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001654 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001655 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001656 }
1657
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001658# check we are in a valid C source file if not then ignore this hunk
1659 next if ($realfile !~ /\.(h|c)$/);
1660
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001661# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001662 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001663 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1664 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001665
Mike Frysinger42e41c52009-09-21 17:04:40 -07001666# Blackfin: don't use __builtin_bfin_[cs]sync
1667 if ($line =~ /__builtin_bfin_csync/) {
1668 my $herevet = "$here\n" . cat_vet($line) . "\n";
1669 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1670 }
1671 if ($line =~ /__builtin_bfin_ssync/) {
1672 my $herevet = "$here\n" . cat_vet($line) . "\n";
1673 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1674 }
1675
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001676# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001677 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1678 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001679 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001680 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001681 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001682 $stat =~ s/\n./\n /g;
1683 $cond =~ s/\n./\n /g;
1684
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001685 # Find the real next line.
1686 $realline_next = $line_nr_next;
1687 if (defined $realline_next &&
1688 (!defined $lines[$realline_next - 1] ||
1689 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1690 $realline_next++;
1691 }
1692
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001693 my $s = $stat;
1694 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001695
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001696 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001697 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001698
1699 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001700 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001701
Andy Whitcroft463f2862009-09-21 17:04:34 -07001702 } elsif ($s =~ /^.\s*else\b/s) {
1703
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001704 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001705 } 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 -07001706 my $type = $1;
1707 $type =~ s/\s+/ /g;
1708 possible($type, "A:" . $s);
1709
Andy Whitcroft8905a672007-11-28 16:21:06 -08001710 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001711 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001712 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001713 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001714
1715 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001716 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001717 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001718 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001719
1720 # Check for any sort of function declaration.
1721 # int foo(something bar, other baz);
1722 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001723 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 -08001724 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001725
Andy Whitcroftcf655042008-03-04 14:28:20 -08001726 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001727 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001728 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001729
Andy Whitcroft8905a672007-11-28 16:21:06 -08001730 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001731 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001732
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001733 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001734 }
1735 }
1736 }
1737
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001738 }
1739
Andy Whitcroft653d4872007-06-23 17:16:34 -07001740#
1741# Checks which may be anchored in the context.
1742#
1743
1744# Check for switch () and associated case and default
1745# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001746 if ($line=~/\bswitch\s*\(.*\)/) {
1747 my $err = '';
1748 my $sep = '';
1749 my @ctx = ctx_block_outer($linenr, $realcnt);
1750 shift(@ctx);
1751 for my $ctx (@ctx) {
1752 my ($clen, $cindent) = line_stats($ctx);
1753 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1754 $indent != $cindent) {
1755 $err .= "$sep$ctx\n";
1756 $sep = '';
1757 } else {
1758 $sep = "[...]\n";
1759 }
1760 }
1761 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001762 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001763 }
1764 }
1765
1766# if/while/etc brace do not go on next line, unless defining a do while loop,
1767# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001768 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001769 my $pre_ctx = "$1$2";
1770
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001771 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001772 my $ctx_cnt = $realcnt - $#ctx - 1;
1773 my $ctx = join("\n", @ctx);
1774
Andy Whitcroft548596d2008-07-23 21:29:01 -07001775 my $ctx_ln = $linenr;
1776 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001777
Andy Whitcroft548596d2008-07-23 21:29:01 -07001778 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1779 defined $lines[$ctx_ln - 1] &&
1780 $lines[$ctx_ln - 1] =~ /^-/)) {
1781 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1782 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001783 $ctx_ln++;
1784 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001785
Andy Whitcroft53210162008-07-23 21:29:03 -07001786 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1787 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001788
1789 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1790 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001791 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001792 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1794 $ctx =~ /\)\s*\;\s*$/ &&
1795 defined $lines[$ctx_ln - 1])
1796 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001797 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1798 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001799 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001800 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001801 }
1802 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001803 }
1804
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001805# Check relative indent for conditionals and blocks.
1806 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1807 my ($s, $c) = ($stat, $cond);
1808
1809 substr($s, 0, length($c), '');
1810
1811 # Make sure we remove the line prefixes as we have
1812 # none on the first line, and are going to readd them
1813 # where necessary.
1814 $s =~ s/\n./\n/gs;
1815
1816 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001817 my @newlines = ($c =~ /\n/gs);
1818 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001819
1820 # We want to check the first line inside the block
1821 # starting at the end of the conditional, so remove:
1822 # 1) any blank line termination
1823 # 2) any opening brace { on end of the line
1824 # 3) any do (...) {
1825 my $continuation = 0;
1826 my $check = 0;
1827 $s =~ s/^.*\bdo\b//;
1828 $s =~ s/^\s*{//;
1829 if ($s =~ s/^\s*\\//) {
1830 $continuation = 1;
1831 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001832 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001833 $check = 1;
1834 $cond_lines++;
1835 }
1836
1837 # Also ignore a loop construct at the end of a
1838 # preprocessor statement.
1839 if (($prevline =~ /^.\s*#\s*define\s/ ||
1840 $prevline =~ /\\\s*$/) && $continuation == 0) {
1841 $check = 0;
1842 }
1843
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001844 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001845 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001846 while ($cond_ptr != $cond_lines) {
1847 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001848
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001849 # If we see an #else/#elif then the code
1850 # is not linear.
1851 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1852 $check = 0;
1853 }
1854
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001855 # Ignore:
1856 # 1) blank lines, they should be at 0,
1857 # 2) preprocessor lines, and
1858 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001859 if ($continuation ||
1860 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001861 $s =~ /^\s*#\s*?/ ||
1862 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001863 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001864 if ($s =~ s/^.*?\n//) {
1865 $cond_lines++;
1866 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001867 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001868 }
1869
1870 my (undef, $sindent) = line_stats("+" . $s);
1871 my $stat_real = raw_line($linenr, $cond_lines);
1872
1873 # Check if either of these lines are modified, else
1874 # this is not this patch's fault.
1875 if (!defined($stat_real) ||
1876 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1877 $check = 0;
1878 }
1879 if (defined($stat_real) && $cond_lines > 1) {
1880 $stat_real = "[...]\n$stat_real";
1881 }
1882
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001883 #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 -07001884
1885 if ($check && (($sindent % 8) != 0 ||
1886 ($sindent <= $indent && $s ne ''))) {
1887 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1888 }
1889 }
1890
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001891 # Track the 'values' across context and added lines.
1892 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001893 my ($curr_values, $curr_vars) =
1894 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001895 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001896 if ($dbg_values) {
1897 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001898 print "$linenr > .$outline\n";
1899 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001900 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001901 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001902 $prev_values = substr($curr_values, -1);
1903
Andy Whitcroft00df3442007-06-08 13:47:06 -07001904#ignore lines not being added
1905 if ($line=~/^[^\+]/) {next;}
1906
Andy Whitcroft653d4872007-06-23 17:16:34 -07001907# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001908 if ($dbg_type) {
1909 if ($line =~ /^.\s*$Declare\s*$/) {
1910 ERROR("TEST: is type\n" . $herecurr);
1911 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1912 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1913 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001914 next;
1915 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001916# TEST: allow direct testing of the attribute matcher.
1917 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001918 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001919 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001920 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001921 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1922 }
1923 next;
1924 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001925
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001926# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001927 if ($line =~ /^.\s*{/ &&
1928 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001929 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001930 }
1931
Andy Whitcroft653d4872007-06-23 17:16:34 -07001932#
1933# Checks which are anchored on the added line.
1934#
1935
1936# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001937 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001938 my $path = $1;
1939 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001940 ERROR("malformed #include filename\n" .
1941 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001942 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001943 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001944
1945# no C99 // comments
1946 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001947 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001948 }
1949 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001950 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001951 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001952
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001953# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1954# the whole statement.
1955#print "APW <$lines[$realline_next - 1]>\n";
1956 if (defined $realline_next &&
1957 exists $lines[$realline_next - 1] &&
1958 !defined $suppress_export{$realline_next} &&
1959 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1960 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001961 # Handle definitions which produce identifiers with
1962 # a prefix:
1963 # XXX(foo);
1964 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001965 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001966 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1967 $name =~ /^${Ident}_$2/) {
1968#print "FOO C name<$name>\n";
1969 $suppress_export{$realline_next} = 1;
1970
1971 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001972 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001973 ^.DEFINE_$Ident\(\Q$name\E\)|
1974 ^.DECLARE_$Ident\(\Q$name\E\)|
1975 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001976 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1977 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001978 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001979#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1980 $suppress_export{$realline_next} = 2;
1981 } else {
1982 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001983 }
1984 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001985 if (!defined $suppress_export{$linenr} &&
1986 $prevline =~ /^.\s*$/ &&
1987 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1988 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1989#print "FOO B <$lines[$linenr - 1]>\n";
1990 $suppress_export{$linenr} = 2;
1991 }
1992 if (defined $suppress_export{$linenr} &&
1993 $suppress_export{$linenr} == 2) {
1994 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1995 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001996
Joe Eloff5150bda2010-08-09 17:21:00 -07001997# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001998 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001999 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002000 $herecurr);
2001 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002002# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002003 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002004 ERROR("do not initialise statics to 0 or NULL\n" .
2005 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002006 }
2007
Joe Perchescb710ec2010-10-26 14:23:20 -07002008# check for static char foo[] = "bar" declarations.
2009 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2010 WARN("static char array declaration should probably be static const char\n" .
2011 $herecurr);
2012 }
2013
Joe Perches93ed0e22010-10-26 14:23:21 -07002014# check for declarations of struct pci_device_id
2015 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2016 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2017 }
2018
Andy Whitcroft653d4872007-06-23 17:16:34 -07002019# check for new typedefs, only function parameters and sparse annotations
2020# make sense.
2021 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002022 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002023 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002024 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002025 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002026 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002027 }
2028
2029# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002030 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08002031 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002032 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002033
Andy Whitcroft65863862009-01-06 14:41:21 -08002034 # Should start with a space.
2035 $to =~ s/^(\S)/ $1/;
2036 # Should not end with a space.
2037 $to =~ s/\s+$//;
2038 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002039 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002040 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002041
Andy Whitcroft65863862009-01-06 14:41:21 -08002042 #print "from<$from> to<$to>\n";
2043 if ($from ne $to) {
2044 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2045 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08002046 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002047 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002048
Andy Whitcroft65863862009-01-06 14:41:21 -08002049 # Should start with a space.
2050 $to =~ s/^(\S)/ $1/;
2051 # Should not end with a space.
2052 $to =~ s/\s+$//;
2053 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002054 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002055 }
2056 # Modifiers should have spaces.
2057 $to =~ s/(\b$Modifier$)/$1 /;
2058
Andy Whitcroft667026e2009-02-27 14:03:08 -08002059 #print "from<$from> to<$to> ident<$ident>\n";
2060 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002061 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2062 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002063 }
2064
2065# # no BUG() or BUG_ON()
2066# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2067# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2068# print "$herecurr";
2069# $clean = 0;
2070# }
2071
Andy Whitcroft8905a672007-11-28 16:21:06 -08002072 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002073 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 -08002074 }
2075
Joe Perches17441222011-06-15 15:08:17 -07002076# check for uses of printk_ratelimit
2077 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2078 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2079 }
2080
Andy Whitcroft00df3442007-06-08 13:47:06 -07002081# printk should use KERN_* levels. Note that follow on printk's on the
2082# same line do not need a level, so we use the current block context
2083# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002084# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002085# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002086 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002087 my $ok = 0;
2088 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2089 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002090 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002091 # with "\n" ignore it, else it is to blame
2092 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2093 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2094 $ok = 1;
2095 }
2096 last;
2097 }
2098 }
2099 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002100 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002101 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002102 }
2103
Andy Whitcroft653d4872007-06-23 17:16:34 -07002104# function brace can't be on same line, except for #defines of do while,
2105# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002106 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2107 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002108 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002109 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002110
Andy Whitcroft8905a672007-11-28 16:21:06 -08002111# open braces for enum, union and struct go on the same line.
2112 if ($line =~ /^.\s*{/ &&
2113 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2114 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2115 }
2116
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002117# missing space after union, struct or enum definition
2118 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2119 WARN("missing space after $1 definition\n" . $herecurr);
2120 }
2121
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002122# check for spacing round square brackets; allowed:
2123# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002124# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2125# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002126 while ($line =~ /(.*?\s)\[/g) {
2127 my ($where, $prefix) = ($-[1], $1);
2128 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002129 ($where != 0 || $prefix !~ /^.\s+$/) &&
2130 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002131 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2132 }
2133 }
2134
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002135# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002136 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002137 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002138 my $ctx_before = substr($line, 0, $-[1]);
2139 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002140
2141 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002142 if ($name =~ /^(?:
2143 if|for|while|switch|return|case|
2144 volatile|__volatile__|
2145 __attribute__|format|__extension__|
2146 asm|__asm__)$/x)
2147 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002148
2149 # cpp #define statements have non-optional spaces, ie
2150 # if there is a space between the name and the open
2151 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002152 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002153
2154 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002155 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002156
2157 # If this whole things ends with a type its most
2158 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002159 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002160
2161 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002162 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002163 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002164 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002165# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002167 my $ops = qr{
2168 <<=|>>=|<=|>=|==|!=|
2169 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2170 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002171 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2172 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002173 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002174 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002175 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002176
2177 my $blank = copy_spacing($opline);
2178
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002179 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002180 $off += length($elements[$n]);
2181
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002182 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002183 my $ca = substr($opline, 0, $off);
2184 my $cc = '';
2185 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2186 $cc = substr($opline, $off + length($elements[$n + 1]));
2187 }
2188 my $cb = "$ca$;$cc";
2189
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002190 my $a = '';
2191 $a = 'V' if ($elements[$n] ne '');
2192 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002193 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002194 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2195 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002196 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002197
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002198 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002199
2200 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002202 $c = 'V' if ($elements[$n + 2] ne '');
2203 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002204 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002205 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2206 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002207 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002208 } else {
2209 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002210 }
2211
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002212 my $ctx = "${a}x${c}";
2213
2214 my $at = "(ctx:$ctx)";
2215
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002216 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002217 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002218
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002219 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002220 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002221
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002222 # Get the full operator variant.
2223 my $opv = $op . substr($curr_vars, $off, 1);
2224
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002225 # Ignore operators passed as parameters.
2226 if ($op_type ne 'V' &&
2227 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2228
Andy Whitcroftcf655042008-03-04 14:28:20 -08002229# # Ignore comments
2230# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002231
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002232 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002233 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002234 if ($ctx !~ /.x[WEBC]/ &&
2235 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002236 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002237 }
2238
2239 # // is a comment
2240 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002241
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002242 # No spaces for:
2243 # ->
2244 # : when part of a bitfield
2245 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002246 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002247 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002248 }
2249
2250 # , must have a space on the right.
2251 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002252 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002253 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002254 }
2255
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002256 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002257 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002258 #warn "'*' is part of type\n";
2259
2260 # unary operators should have a space before and
2261 # none after. May be left adjacent to another
2262 # unary operator, or a cast
2263 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002264 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002265 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002266 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002268 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002269 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002270 # A unary '*' may be const
2271
2272 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002273 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002274 }
2275
2276 # unary ++ and unary -- are allowed no space on one side.
2277 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002278 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2279 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002280 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002281 if ($ctx =~ /Wx[BE]/ ||
2282 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2283 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002284 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 if ($ctx =~ /ExW/) {
2286 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2287 }
2288
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002289
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002290 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002291 } elsif ($op eq '<<' or $op eq '>>' or
2292 $op eq '&' or $op eq '^' or $op eq '|' or
2293 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002294 $op eq '*' or $op eq '/' or
2295 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002296 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002298 ERROR("need consistent spacing around '$op' $at\n" .
2299 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002300 }
2301
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002302 # A colon needs no spaces before when it is
2303 # terminating a case value or a label.
2304 } elsif ($opv eq ':C' || $opv eq ':L') {
2305 if ($ctx =~ /Wx./) {
2306 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2307 }
2308
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002309 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002310 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002311 my $ok = 0;
2312
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002313 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002314 if (($op eq '<' &&
2315 $cc =~ /^\S+\@\S+>/) ||
2316 ($op eq '>' &&
2317 $ca =~ /<\S+\@\S+$/))
2318 {
2319 $ok = 1;
2320 }
2321
2322 # Ignore ?:
2323 if (($opv eq ':O' && $ca =~ /\?$/) ||
2324 ($op eq '?' && $cc =~ /^:/)) {
2325 $ok = 1;
2326 }
2327
2328 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002329 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002330 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002331 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002332 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002333 }
2334 }
2335
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002336# check for multiple assignments
2337 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002338 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002339 }
2340
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002341## # check for multiple declarations, allowing for a function declaration
2342## # continuation.
2343## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2344## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2345##
2346## # Remove any bracketed sections to ensure we do not
2347## # falsly report the parameters of functions.
2348## my $ln = $line;
2349## while ($ln =~ s/\([^\(\)]*\)//g) {
2350## }
2351## if ($ln =~ /,/) {
2352## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2353## }
2354## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002355
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002356#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002357 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2358 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002359 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002360 }
2361
2362# closing brace should have a space following it when it has anything
2363# on the line
2364 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002366 }
2367
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002368# check spacing on square brackets
2369 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002370 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002371 }
2372 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002373 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002374 }
2375
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002376# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002377 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002378 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002379 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002380 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002381 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002382 $line !~ /for\s*\(.*;\s+\)/ &&
2383 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002384 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002385 }
2386
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002387#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002388 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002389 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002390 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002391 }
2392
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002393# Return is not a function.
2394 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2395 my $spacing = $1;
2396 my $value = $2;
2397
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002398 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002399 $value =~ s/\(/ \(/g;
2400 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002401 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2402 $value !~ /(?:$Ident|-?$Constant)\s*
2403 $Compare\s*
2404 (?:$Ident|-?$Constant)/x &&
2405 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002406 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002407#print "value<$value>\n";
2408 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002409 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2410
2411 } elsif ($spacing !~ /\s+/) {
2412 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2413 }
2414 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002415# Return of what appears to be an errno should normally be -'ve
2416 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2417 my $name = $1;
2418 if ($name ne 'EOF' && $name ne 'ERROR') {
2419 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2420 }
2421 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002422
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002423# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002424 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002425 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002426 }
2427
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002428# Check for illegal assignment in if conditional -- and check for trailing
2429# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002430 if ($line =~ /do\s*(?!{)/) {
2431 my ($stat_next) = ctx_statement_block($line_nr_next,
2432 $remain_next, $off_next);
2433 $stat_next =~ s/\n./\n /g;
2434 ##print "stat<$stat> stat_next<$stat_next>\n";
2435
2436 if ($stat_next =~ /^\s*while\b/) {
2437 # If the statement carries leading newlines,
2438 # then count those as offsets.
2439 my ($whitespace) =
2440 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2441 my $offset =
2442 statement_rawlines($whitespace) - 1;
2443
2444 $suppress_whiletrailers{$line_nr_next +
2445 $offset} = 1;
2446 }
2447 }
2448 if (!defined $suppress_whiletrailers{$linenr} &&
2449 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002450 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002451
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002452 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002453 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002454 }
2455
2456 # Find out what is on the end of the line after the
2457 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002458 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002459 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002460 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002461 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2462 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002463 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002464 # Find out how long the conditional actually is.
2465 my @newlines = ($c =~ /\n/gs);
2466 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002467 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002468
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002469 $stat_real = raw_line($linenr, $cond_lines)
2470 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002471 if (defined($stat_real) && $cond_lines > 1) {
2472 $stat_real = "[...]\n$stat_real";
2473 }
2474
2475 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002476 }
2477 }
2478
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002479# Check for bitwise tests written as boolean
2480 if ($line =~ /
2481 (?:
2482 (?:\[|\(|\&\&|\|\|)
2483 \s*0[xX][0-9]+\s*
2484 (?:\&\&|\|\|)
2485 |
2486 (?:\&\&|\|\|)
2487 \s*0[xX][0-9]+\s*
2488 (?:\&\&|\|\||\)|\])
2489 )/x)
2490 {
2491 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2492 }
2493
Andy Whitcroft8905a672007-11-28 16:21:06 -08002494# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002495 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2496 my $s = $1;
2497 $s =~ s/$;//g; # Remove any comments
2498 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2499 ERROR("trailing statements should be on next line\n" . $herecurr);
2500 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002501 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002502# if should not continue a brace
2503 if ($line =~ /}\s*if\b/) {
2504 ERROR("trailing statements should be on next line\n" .
2505 $herecurr);
2506 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002507# case and default should not have general statements after them
2508 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2509 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002510 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002511 \s*return\s+
2512 )/xg)
2513 {
2514 ERROR("trailing statements should be on next line\n" . $herecurr);
2515 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002516
2517 # Check for }<nl>else {, these must be at the same
2518 # indent level to be relevant to each other.
2519 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2520 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002521 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002522 }
2523
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002524 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2525 $previndent == $indent) {
2526 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2527
2528 # Find out what is on the end of the line after the
2529 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002530 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002531 $s =~ s/\n.*//g;
2532
2533 if ($s =~ /^\s*;/) {
2534 ERROR("while should follow close brace '}'\n" . $hereprev);
2535 }
2536 }
2537
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002538#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2539# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2540# print "No studly caps, use _\n";
2541# print "$herecurr";
2542# $clean = 0;
2543# }
2544
2545#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002546 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002547 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002548 }
2549
Andy Whitcroft653d4872007-06-23 17:16:34 -07002550#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002551 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002552 my $file = "$1.h";
2553 my $checkfile = "include/linux/$file";
2554 if (-f "$root/$checkfile" &&
2555 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002556 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002557 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002558 if ($realfile =~ m{^arch/}) {
2559 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2560 } else {
2561 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2562 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002563 }
2564 }
2565
Andy Whitcroft653d4872007-06-23 17:16:34 -07002566# multi-statement macros should be enclosed in a do while loop, grab the
2567# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002568# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002569 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2570 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002571 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002572 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002573 my ($off, $dstat, $dcond, $rest);
2574 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002575
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002576 my $args = defined($1);
2577
2578 # Find the end of the macro and limit our statement
2579 # search to that.
2580 while ($cnt > 0 && defined $lines[$ln - 1] &&
2581 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2582 {
2583 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002584 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002585 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002586 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002587 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002588
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002589 ($dstat, $dcond, $ln, $cnt, $off) =
2590 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2591 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002592 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002593
2594 # Extract the remainder of the define (if any) and
2595 # rip off surrounding spaces, and trailing \'s.
2596 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002597 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2598 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002599 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2600 $rest .= substr($lines[$ln - 1], $off) . "\n";
2601 $cnt--;
2602 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002603 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002604 $off = 0;
2605 }
2606 $rest =~ s/\\\n.//g;
2607 $rest =~ s/^\s*//s;
2608 $rest =~ s/\s*$//s;
2609
2610 # Clean up the original statement.
2611 if ($args) {
2612 substr($dstat, 0, length($dcond), '');
2613 } else {
2614 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2615 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002616 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002617 $dstat =~ s/\\\n.//g;
2618 $dstat =~ s/^\s*//s;
2619 $dstat =~ s/\s*$//s;
2620
2621 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002622 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2623 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2624 $dstat =~ s/\[[^\{\}]*\]/1/)
2625 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002626 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002627
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002628 # Extremely long macros may fall off the end of the
2629 # available context without closing. Give a dangling
2630 # backslash the benefit of the doubt and allow it
2631 # to gobble any hanging open-parens.
2632 $dstat =~ s/\(.+\\$/1/;
2633
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002634 my $exceptions = qr{
2635 $Declare|
2636 module_param_named|
2637 MODULE_PARAM_DESC|
2638 DECLARE_PER_CPU|
2639 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002640 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002641 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002642 union|
2643 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002644 \.$Ident\s*=\s*|
2645 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002646 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002647 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2648 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002649 if ($rest !~ /while\s*\(/ &&
2650 $dstat !~ /$exceptions/)
2651 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002652 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 -07002653 }
2654
2655 } elsif ($ctx !~ /;/) {
2656 if ($dstat ne '' &&
2657 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2658 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002659 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002660 $dstat =~ /$Operators/)
2661 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002662 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002663 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002664 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002665 }
2666
Mike Frysinger080ba922009-01-06 14:41:25 -08002667# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2668# all assignments may have only one of the following with an assignment:
2669# .
2670# ALIGN(...)
2671# VMLINUX_SYMBOL(...)
2672 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2673 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2674 }
2675
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002676# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002677 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2678 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002679 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002680 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002681 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2682 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002683 my $allowed = 0;
2684 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002685 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002686 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002687 for my $chunk (@chunks) {
2688 my ($cond, $block) = @{$chunk};
2689
Andy Whitcroft773647a2008-03-28 14:15:58 -07002690 # If the condition carries leading newlines, then count those as offsets.
2691 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2692 my $offset = statement_rawlines($whitespace) - 1;
2693
2694 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2695
2696 # We have looked at and allowed this specific line.
2697 $suppress_ifbraces{$ln + $offset} = 1;
2698
2699 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002700 $ln += statement_rawlines($block) - 1;
2701
Andy Whitcroft773647a2008-03-28 14:15:58 -07002702 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002703
2704 $seen++ if ($block =~ /^\s*{/);
2705
Andy Whitcroftcf655042008-03-04 14:28:20 -08002706 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2707 if (statement_lines($cond) > 1) {
2708 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002709 $allowed = 1;
2710 }
2711 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002712 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002713 $allowed = 1;
2714 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002715 if (statement_block_size($block) > 1) {
2716 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002717 $allowed = 1;
2718 }
2719 }
2720 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002721 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002722 }
2723 }
2724 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002725 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002726 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002727 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002728
Andy Whitcroftcf655042008-03-04 14:28:20 -08002729 # Check the pre-context.
2730 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2731 #print "APW: ALLOWED: pre<$1>\n";
2732 $allowed = 1;
2733 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002734
2735 my ($level, $endln, @chunks) =
2736 ctx_statement_full($linenr, $realcnt, $-[0]);
2737
Andy Whitcroftcf655042008-03-04 14:28:20 -08002738 # Check the condition.
2739 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002740 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002741 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002742 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002743 }
2744 if (statement_lines($cond) > 1) {
2745 #print "APW: ALLOWED: cond<$cond>\n";
2746 $allowed = 1;
2747 }
2748 if ($block =~/\b(?:if|for|while)\b/) {
2749 #print "APW: ALLOWED: block<$block>\n";
2750 $allowed = 1;
2751 }
2752 if (statement_block_size($block) > 1) {
2753 #print "APW: ALLOWED: lines block<$block>\n";
2754 $allowed = 1;
2755 }
2756 # Check the post-context.
2757 if (defined $chunks[1]) {
2758 my ($cond, $block) = @{$chunks[1]};
2759 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002760 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002761 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002762 if ($block =~ /^\s*\{/) {
2763 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2764 $allowed = 1;
2765 }
2766 }
2767 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2768 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002769 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002770
Andy Whitcroftf0556632008-10-15 22:02:23 -07002771 for (my $n = 0; $n < $cnt; $n++) {
2772 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002773 }
2774
2775 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002776 }
2777 }
2778
Andy Whitcroft653d4872007-06-23 17:16:34 -07002779# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002780 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002781 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002782 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002783 }
2784 }
2785
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002786# don't use deprecated functions
2787 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002788 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002789 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002790 }
2791 }
2792
2793# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002794 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2795 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002796 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002797 }
2798
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002799# sys_open/read/write/close are not allowed in the kernel
2800 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
2801 ERROR("$1 is inappropriate in kernel code.\n" .
2802 $herecurr);
2803 }
2804
2805# filp_open is a backdoor for sys_open
2806 if ($line =~ /\b(filp_open)\b/) {
2807 ERROR("$1 is inappropriate in kernel code.\n" .
2808 $herecurr);
2809 }
2810
2811# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
2812 if ($line =~ /\b((?:read|write)[bwl])\b/) {
2813 ERROR("Use of $1 is deprecated: use $1_relaxed\n\t" .
2814 "with appropriate memory barriers instead.\n" .
2815 $herecurr);
2816 }
2817
2818# likewise, in/out[bwl] should be __raw_read/write[bwl]...
2819 if ($line =~ /\b((in|out)([bwl]))\b/) {
2820 my ($all, $pref, $suf) = ($1, $2, $3);
2821 $pref =~ s/in/read/;
2822 $pref =~ s/out/write/;
2823 ERROR("Use of $all is deprecated: use " .
2824 "__raw_$pref$suf\n\t" .
2825 "with appropriate memory barriers instead.\n" .
2826 $herecurr);
2827 }
2828
2829# dsb is too ARMish, and should usually be mb.
2830 if ($line =~ /\bdsb\b/) {
2831 WARN("Use of dsb is discouranged: prefer mb.\n" .
2832 $herecurr);
2833 }
2834
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08002835# MSM - check if a non board-gpiomux file has any gpiomux declarations
2836 if ($realfile =~ /\/mach-msm\/board-[0-9]+/ &&
2837 $realfile !~ /camera/ && $realfile !~ /gpiomux/ &&
2838 $line =~ /\s*struct msm_gpiomux_config\s*/ ) {
2839 WARN("Non gpiomux board file cannot have a gpiomux config declarations. Please declare gpiomux configs in board-*-gpiomux.c file.\n" . $herecurr);
2840 }
2841
Pankaj Kumaradfb9332012-01-06 15:02:19 +05302842# MSM - check if vreg_xxx function are used
2843 if ($line =~ /\b(vreg_(get|put|set_level|enable|disable))\b/) {
2844 WARN("Use of $1 API is deprecated: " .
2845 "use regulator APIs\n" . $herecurr);
2846 }
2847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002848# unbounded string functions are overflow risks
2849 my %str_fns = (
2850 "sprintf" => "snprintf",
Olav Haugan665be0d2012-02-06 09:42:18 -08002851 "strcpy" => "strlcpy",
2852 "strncpy" => "strlcpy",
2853 "strcat" => "strlcat",
2854 "strncat" => "strlcat",
2855 "vsprintf" => "vsnprintf",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856 "strcmp" => "strncmp",
2857 "strcasecmp" => "strncasecmp",
2858 "strchr" => "strnchr",
2859 "strstr" => "strnstr",
2860 "strlen" => "strnlen",
2861 );
2862 foreach my $k (keys %str_fns) {
2863 if ($line =~ /\b$k\b/) {
2864 ERROR("Use of $k is deprecated: " .
2865 "use $str_fns{$k} instead.\n" .
2866 $herecurr);
2867 }
2868 }
2869
Andy Whitcroft00df3442007-06-08 13:47:06 -07002870# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002871 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872 WARN("if this code is redundant consider removing it\n"
2873 . $herecurr);
2874 }
2875# warn about #if 1
2876 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
2877 WARN("if this code is required consider removing"
2878 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002879 }
2880
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002881# check for needless kfree() checks
2882 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2883 my $expr = $1;
2884 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002885 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002886 }
2887 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002888# check for needless usb_free_urb() checks
2889 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2890 my $expr = $1;
2891 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2892 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2893 }
2894 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002895
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002896# prefer usleep_range over udelay
2897 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2898 # ignore udelay's < 10, however
2899 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2900 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2901 }
2902 }
2903
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002904# warn about unexpectedly long msleep's
2905 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2906 if ($1 < 20) {
2907 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2908 }
2909 }
2910
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002911# check the patch for use of mdelay
2912 if ($line =~ /\bmdelay\s*\(/) {
2913 WARN("use of mdelay() found: msleep() is the preferred API.\n" . $line );
2914 }
2915
Andy Whitcroft00df3442007-06-08 13:47:06 -07002916# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002917# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002918# print "#ifdef in C files should be avoided\n";
2919# print "$herecurr";
2920# $clean = 0;
2921# }
2922
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002923# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002924 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002925 ERROR("exactly one space required after that #$1\n" . $herecurr);
2926 }
2927
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002928# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002929 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2930 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002931 my $which = $1;
2932 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002933 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002934 }
2935 }
2936# check for memory barriers without a comment.
2937 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2938 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002939 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002940 }
2941 }
2942# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002943 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002944 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002945 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002946
Tobias Klauserd4977c72010-05-24 14:33:30 -07002947# Check that the storage class is at the beginning of a declaration
2948 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2949 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2950 }
2951
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002952# check the location of the inline attribute, that it is between
2953# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002954 if ($line =~ /\b$Type\s+$Inline\b/ ||
2955 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002956 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2957 }
2958
Andy Whitcroft8905a672007-11-28 16:21:06 -08002959# Check for __inline__ and __inline, prefer inline
2960 if ($line =~ /\b(__inline__|__inline)\b/) {
2961 WARN("plain inline is preferred over $1\n" . $herecurr);
2962 }
2963
Joe Perches3d130fd2011-01-12 17:00:00 -08002964# Check for __attribute__ packed, prefer __packed
2965 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2966 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2967 }
2968
Joe Perches8f53a9b2010-03-05 13:43:48 -08002969# check for sizeof(&)
2970 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2971 WARN("sizeof(& should be avoided\n" . $herecurr);
2972 }
2973
Joe Perches428e2fd2011-05-24 17:13:39 -07002974# check for line continuations in quoted strings with odd counts of "
2975 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2976 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2977 }
2978
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002979# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002980 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002981 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002982 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002983 my $function_name = $1;
2984 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002985
2986 my $s = $stat;
2987 if (defined $cond) {
2988 substr($s, 0, length($cond), '');
2989 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002990 if ($s =~ /^\s*;/ &&
2991 $function_name ne 'uninitialized_var')
2992 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002993 WARN("externs should be avoided in .c files\n" . $herecurr);
2994 }
2995
2996 if ($paren_space =~ /\n/) {
2997 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2998 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002999
3000 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3001 $stat =~ /^.\s*extern\s+/)
3002 {
3003 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003004 }
3005
3006# checks for new __setup's
3007 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3008 my $name = $1;
3009
3010 if (!grep(/$name/, @setup_docs)) {
3011 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3012 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003013 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003014
3015# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003016 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003017 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3018 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003019
Joe Perchescaf2a542011-01-12 16:59:56 -08003020# check for multiple semicolons
3021 if ($line =~ /;\s*;\s*$/) {
3022 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
3023 }
3024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003025# check for return codes on error paths
3026 if ($line =~ /\breturn\s+-\d+/) {
3027 ERROR("illegal return value, please use an error code\n" . $herecurr);
3028 }
3029
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003030# check for gcc specific __FUNCTION__
3031 if ($line =~ /__FUNCTION__/) {
3032 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3033 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003034
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003035# check for semaphores initialized locked
3036 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003037 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003038
Andy Whitcroft773647a2008-03-28 14:15:58 -07003039 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07003040# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07003041 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07003042 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003043 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07003044# check for __initcall(), use device_initcall() explicitly please
3045 if ($line =~ /^.\s*__initcall\s*\(/) {
3046 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
3047 }
Emese Revfy79404842010-03-05 13:43:53 -08003048# check for various ops structs, ensure they are const.
3049 my $struct_ops = qr{acpi_dock_ops|
3050 address_space_operations|
3051 backlight_ops|
3052 block_device_operations|
3053 dentry_operations|
3054 dev_pm_ops|
3055 dma_map_ops|
3056 extent_io_ops|
3057 file_lock_operations|
3058 file_operations|
3059 hv_ops|
3060 ide_dma_ops|
3061 intel_dvo_dev_ops|
3062 item_operations|
3063 iwl_ops|
3064 kgdb_arch|
3065 kgdb_io|
3066 kset_uevent_ops|
3067 lock_manager_operations|
3068 microcode_ops|
3069 mtrr_ops|
3070 neigh_ops|
3071 nlmsvc_binding|
3072 pci_raw_ops|
3073 pipe_buf_operations|
3074 platform_hibernation_ops|
3075 platform_suspend_ops|
3076 proto_ops|
3077 rpc_pipe_ops|
3078 seq_operations|
3079 snd_ac97_build_ops|
3080 soc_pcmcia_socket_ops|
3081 stacktrace_ops|
3082 sysfs_ops|
3083 tty_operations|
3084 usb_mon_operations|
3085 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003086 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003087 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003088 WARN("struct $1 should normally be const\n" .
3089 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003090 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003091
3092# use of NR_CPUS is usually wrong
3093# ignore definitions of NR_CPUS and usage to define arrays as likely right
3094 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003095 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3096 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003097 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3098 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3099 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003100 {
3101 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3102 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003103
3104# check for %L{u,d,i} in strings
3105 my $string;
3106 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3107 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003108 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003109 if ($string =~ /(?<!%)%L[udi]/) {
3110 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3111 last;
3112 }
3113 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003114
3115# whine mightly about in_atomic
3116 if ($line =~ /\bin_atomic\s*\(/) {
3117 if ($realfile =~ m@^drivers/@) {
3118 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003119 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003120 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3121 }
3122 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003123
3124# check for lockdep_set_novalidate_class
3125 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3126 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3127 if ($realfile !~ m@^kernel/lockdep@ &&
3128 $realfile !~ m@^include/linux/lockdep@ &&
3129 $realfile !~ m@^drivers/base/core@) {
3130 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3131 }
3132 }
Dave Jones88f88312011-01-12 16:59:59 -08003133
3134 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3135 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3136 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3137 }
Dave Jones309c00c2011-03-22 16:34:44 -07003138
3139 # Check for memset with swapped arguments
3140 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3141 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3142 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003143 }
3144
3145 # If we have no input at all, then there is nothing to report on
3146 # so just keep quiet.
3147 if ($#rawlines == -1) {
3148 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003149 }
3150
Andy Whitcroft8905a672007-11-28 16:21:06 -08003151 # In mailback mode only produce a report in the negative, for
3152 # things that appear to be patches.
3153 if ($mailback && ($clean == 1 || !$is_patch)) {
3154 exit(0);
3155 }
3156
3157 # This is not a patch, and we are are in 'no-patch' mode so
3158 # just keep quiet.
3159 if (!$chk_patch && !$is_patch) {
3160 exit(0);
3161 }
3162
3163 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003164 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003165 }
3166 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003167 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003168 }
3169
Andy Whitcroft8905a672007-11-28 16:21:06 -08003170 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003171 if ($summary && !($clean == 1 && $quiet == 1)) {
3172 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003173 print "total: $cnt_error errors, $cnt_warn warnings, " .
3174 (($check)? "$cnt_chk checks, " : "") .
3175 "$cnt_lines lines checked\n";
3176 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003177 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003178
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003179 if ($quiet == 0) {
3180 # If there were whitespace errors which cleanpatch can fix
3181 # then suggest that.
3182 if ($rpt_cleaners) {
3183 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3184 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003185 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003186 }
3187 }
3188
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003189 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003190 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003191 }
3192 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003193 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003194 print "are false positives report them to the maintainer, see\n";
3195 print "CHECKPATCH in MAINTAINERS.\n";
3196 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003197
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003198 return $clean;
3199}