blob: 4c655c2c9ed1f583193d63d5dce2c973c8e16333 [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
Joe Perches000d1cc2011-07-25 17:13:25 -070020my $V = '0.32';
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;
Joe Perches000d1cc2011-07-25 17:13:25 -070036my $show_types = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080038my %debug;
Joe Perches000d1cc2011-07-25 17:13:25 -070039my %ignore_type = ();
40my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070041my $help = 0;
Joe Perches000d1cc2011-07-25 17:13:25 -070042my $configuration_file = ".checkpatch.conf";
Hannes Eder77f5b102009-09-21 17:04:37 -070043
44sub help {
45 my ($exitcode) = @_;
46
47 print << "EOM";
48Usage: $P [OPTION]... [FILE]...
49Version: $V
50
51Options:
52 -q, --quiet quiet
53 --no-tree run without a kernel tree
54 --no-signoff do not check for 'Signed-off-by' line
55 --patch treat FILE as patchfile (default)
56 --emacs emacs compile window format
57 --terse one line per report
58 -f, --file treat FILE as regular source file
59 --subjective, --strict enable more subjective tests
Joe Perches000d1cc2011-07-25 17:13:25 -070060 --ignore TYPE(,TYPE2...) ignore various comma separated message types
61 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070062 --root=PATH PATH to the kernel tree root
63 --no-summary suppress the per-file summary
64 --mailback only produce a report in case of warnings/errors
65 --summary-file include the filename in summary
66 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
67 'values', 'possible', 'type', and 'attr' (default
68 is all off)
69 --test-only=WORD report only warnings/errors containing WORD
70 literally
71 -h, --help, --version display this help and exit
72
73When FILE is - read standard input.
74EOM
75
76 exit($exitcode);
77}
78
Joe Perches000d1cc2011-07-25 17:13:25 -070079my $conf = which_conf($configuration_file);
80if (-f $conf) {
81 my @conf_args;
82 open(my $conffile, '<', "$conf")
83 or warn "$P: Can't find a readable $configuration_file file $!\n";
84
85 while (<$conffile>) {
86 my $line = $_;
87
88 $line =~ s/\s*\n?$//g;
89 $line =~ s/^\s*//g;
90 $line =~ s/\s+/ /g;
91
92 next if ($line =~ m/^\s*#/);
93 next if ($line =~ m/^\s*$/);
94
95 my @words = split(" ", $line);
96 foreach my $word (@words) {
97 last if ($word =~ m/^#/);
98 push (@conf_args, $word);
99 }
100 }
101 close($conffile);
102 unshift(@ARGV, @conf_args) if @conf_args;
103}
104
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700105GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700106 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700107 'tree!' => \$tree,
108 'signoff!' => \$chk_signoff,
109 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700110 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800111 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700112 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'subjective!' => \$check,
114 'strict!' => \$check,
Joe Perches000d1cc2011-07-25 17:13:25 -0700115 'ignore=s' => \@ignore,
116 'show-types!' => \$show_types,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700117 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800118 'summary!' => \$summary,
119 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800120 'summary-file!' => \$summary_file,
121
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800122 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700123 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 'h|help' => \$help,
125 'version' => \$help
126) or help(1);
127
128help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700129
130my $exit = 0;
131
132if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700133 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700134 exit(1);
135}
136
Joe Perches000d1cc2011-07-25 17:13:25 -0700137@ignore = split(/,/, join(',',@ignore));
138foreach my $word (@ignore) {
139 $word =~ s/\s*\n?$//g;
140 $word =~ s/^\s*//g;
141 $word =~ s/\s+/ /g;
142 $word =~ tr/[a-z]/[A-Z]/;
143
144 next if ($word =~ m/^\s*#/);
145 next if ($word =~ m/^\s*$/);
146
147 $ignore_type{$word}++;
148}
149
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800150my $dbg_values = 0;
151my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700152my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700153my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800154for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800155 ## no critic
156 eval "\${dbg_$key} = '$debug{$key}';";
157 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800158}
159
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700160my $rpt_cleaners = 0;
161
Andy Whitcroft8905a672007-11-28 16:21:06 -0800162if ($terse) {
163 $emacs = 1;
164 $quiet++;
165}
166
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700167if ($tree) {
168 if (defined $root) {
169 if (!top_of_kernel_tree($root)) {
170 die "$P: $root: --root does not point at a valid tree\n";
171 }
172 } else {
173 if (top_of_kernel_tree('.')) {
174 $root = '.';
175 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
176 top_of_kernel_tree($1)) {
177 $root = $1;
178 }
179 }
180
181 if (!defined $root) {
182 print "Must be run from the top-level dir. of a kernel tree\n";
183 exit(2);
184 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700185}
186
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700187my $emitted_corrupt = 0;
188
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700189our $Ident = qr{
190 [A-Za-z_][A-Za-z\d_]*
191 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
192 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700193our $Storage = qr{extern|static|asmlinkage};
194our $Sparse = qr{
195 __user|
196 __kernel|
197 __force|
198 __iomem|
199 __must_check|
200 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800201 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700202 __ref|
203 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800205
206# Notes to $Attribute:
207# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208our $Attribute = qr{
209 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700210 __percpu|
211 __nocast|
212 __safe|
213 __bitwise__|
214 __packed__|
215 __packed2__|
216 __naked|
217 __maybe_unused|
218 __always_unused|
219 __noreturn|
220 __used|
221 __cold|
222 __noclone|
223 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700224 __read_mostly|
225 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800226 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700227 ____cacheline_aligned|
228 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800229 ____cacheline_internodealigned_in_smp|
230 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700231 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700232our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
235our $Lval = qr{$Ident(?:$Member)*};
236
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800237our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700238our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800239our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240our $Operators = qr{
241 <=|>=|==|!=|
242 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800243 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700244 }x;
245
Andy Whitcroft8905a672007-11-28 16:21:06 -0800246our $NonptrType;
247our $Type;
248our $Declare;
249
Joe Perches15662b32011-10-31 17:13:12 -0700250our $NON_ASCII_UTF8 = qr{
251 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700252 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
253 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
254 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
255 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
256 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
257 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
258}x;
259
Joe Perches15662b32011-10-31 17:13:12 -0700260our $UTF8 = qr{
261 [\x09\x0A\x0D\x20-\x7E] # ASCII
262 | $NON_ASCII_UTF8
263}x;
264
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700265our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700266 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700267 atomic_t
268)};
269
Joe Perches691e6692010-03-05 13:43:51 -0800270our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700271 printk(?:_ratelimited|_once|)|
272 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
273 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700274 panic|
275 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800276)};
277
Joe Perches20112472011-07-25 17:13:23 -0700278our $signature_tags = qr{(?xi:
279 Signed-off-by:|
280 Acked-by:|
281 Tested-by:|
282 Reviewed-by:|
283 Reported-by:|
284 To:|
285 Cc:
286)};
287
Andy Whitcroft8905a672007-11-28 16:21:06 -0800288our @typeList = (
289 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700290 qr{(?:unsigned\s+)?char},
291 qr{(?:unsigned\s+)?short},
292 qr{(?:unsigned\s+)?int},
293 qr{(?:unsigned\s+)?long},
294 qr{(?:unsigned\s+)?long\s+int},
295 qr{(?:unsigned\s+)?long\s+long},
296 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800297 qr{unsigned},
298 qr{float},
299 qr{double},
300 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800301 qr{struct\s+$Ident},
302 qr{union\s+$Ident},
303 qr{enum\s+$Ident},
304 qr{${Ident}_t},
305 qr{${Ident}_handler},
306 qr{${Ident}_handler_fn},
307);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700308our @modifierList = (
309 qr{fastcall},
310);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311
Wolfram Sang7840a942010-08-09 17:20:57 -0700312our $allowed_asm_includes = qr{(?x:
313 irq|
314 memory
315)};
316# memory.h: ARM has a custom one
317
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700319 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
320 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700321 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700323 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800324 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800325 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700326 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700327 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800328 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700329 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330 }x;
331 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700332 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700333 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700334 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800335 }x;
336 $Declare = qr{(?:$Storage\s+)?$Type};
337}
338build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339
Joe Perches7d2367a2011-07-25 17:13:22 -0700340
341our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700342
343# Using $balanced_parens, $LvalOrFunc, or $FuncArg
344# requires at least perl version v5.10.0
345# Any use must be runtime checked with $^V
346
347our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
348our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800349our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700350
351sub deparenthesize {
352 my ($string) = @_;
353 return "" if (!defined($string));
354 $string =~ s@^\s*\(\s*@@g;
355 $string =~ s@\s*\)\s*$@@g;
356 $string =~ s@\s+@ @g;
357 return $string;
358}
359
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700360$chk_signoff = 0 if ($file);
361
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700362my @dep_includes = ();
363my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364my $removal = "Documentation/feature-removal-schedule.txt";
365if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800366 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800368 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700369 if (/^Check:\s+(.*\S)/) {
370 for my $entry (split(/[, ]+/, $1)) {
371 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700372 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700373
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700374 } elsif ($entry !~ m@/@) {
375 push(@dep_functions, $entry);
376 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700377 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700378 }
379 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800380 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700381}
382
Andy Whitcroft00df3442007-06-08 13:47:06 -0700383my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800384my @lines = ();
385my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700386for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800387 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800389 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800391 } elsif ($filename eq '-') {
392 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800394 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700396 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800397 if ($filename eq '-') {
398 $vname = 'Your patch';
399 } else {
400 $vname = $filename;
401 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800402 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700403 chomp;
404 push(@rawlines, $_);
405 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800406 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800407 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 $exit = 1;
409 }
410 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800411 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700412}
413
414exit($exit);
415
416sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700417 my ($root) = @_;
418
419 my @tree_check = (
420 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
421 "README", "Documentation", "arch", "include", "drivers",
422 "fs", "init", "ipc", "kernel", "lib", "scripts",
423 );
424
425 foreach my $check (@tree_check) {
426 if (! -e $root . '/' . $check) {
427 return 0;
428 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700429 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700430 return 1;
Joe Perches000d1cc2011-07-25 17:13:25 -0700431 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700432
Joe Perches20112472011-07-25 17:13:23 -0700433sub parse_email {
434 my ($formatted_email) = @_;
435
436 my $name = "";
437 my $address = "";
438 my $comment = "";
439
440 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
441 $name = $1;
442 $address = $2;
443 $comment = $3 if defined $3;
444 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
445 $address = $1;
446 $comment = $2 if defined $2;
447 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
448 $address = $1;
449 $comment = $2 if defined $2;
450 $formatted_email =~ s/$address.*$//;
451 $name = $formatted_email;
452 $name =~ s/^\s+|\s+$//g;
453 $name =~ s/^\"|\"$//g;
454 # If there's a name left after stripping spaces and
455 # leading quotes, and the address doesn't have both
456 # leading and trailing angle brackets, the address
457 # is invalid. ie:
458 # "joe smith joe@smith.com" bad
459 # "joe smith <joe@smith.com" bad
460 if ($name ne "" && $address !~ /^<[^>]+>$/) {
461 $name = "";
462 $address = "";
463 $comment = "";
464 }
465 }
466
467 $name =~ s/^\s+|\s+$//g;
468 $name =~ s/^\"|\"$//g;
469 $address =~ s/^\s+|\s+$//g;
470 $address =~ s/^\<|\>$//g;
471
472 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
473 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
474 $name = "\"$name\"";
475 }
476
477 return ($name, $address, $comment);
478}
479
480sub format_email {
481 my ($name, $address) = @_;
482
483 my $formatted_email;
484
485 $name =~ s/^\s+|\s+$//g;
486 $name =~ s/^\"|\"$//g;
487 $address =~ s/^\s+|\s+$//g;
488
489 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
490 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
491 $name = "\"$name\"";
492 }
493
494 if ("$name" eq "") {
495 $formatted_email = "$address";
496 } else {
497 $formatted_email = "$name <$address>";
498 }
499
500 return $formatted_email;
501}
502
Joe Perches000d1cc2011-07-25 17:13:25 -0700503sub which_conf {
504 my ($conf) = @_;
505
506 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
507 if (-e "$path/$conf") {
508 return "$path/$conf";
509 }
510 }
511
512 return "";
513}
514
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700515sub expand_tabs {
516 my ($str) = @_;
517
518 my $res = '';
519 my $n = 0;
520 for my $c (split(//, $str)) {
521 if ($c eq "\t") {
522 $res .= ' ';
523 $n++;
524 for (; ($n % 8) != 0; $n++) {
525 $res .= ' ';
526 }
527 next;
528 }
529 $res .= $c;
530 $n++;
531 }
532
533 return $res;
534}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700535sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700536 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700537 return $res;
538}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700539
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700540sub line_stats {
541 my ($line) = @_;
542
543 # Drop the diff line leader and expand tabs
544 $line =~ s/^.//;
545 $line = expand_tabs($line);
546
547 # Pick the indent from the front of the line.
548 my ($white) = ($line =~ /^(\s*)/);
549
550 return (length($line), length($white));
551}
552
Andy Whitcroft773647a2008-03-28 14:15:58 -0700553my $sanitise_quote = '';
554
555sub sanitise_line_reset {
556 my ($in_comment) = @_;
557
558 if ($in_comment) {
559 $sanitise_quote = '*/';
560 } else {
561 $sanitise_quote = '';
562 }
563}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700564sub sanitise_line {
565 my ($line) = @_;
566
567 my $res = '';
568 my $l = '';
569
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800570 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700571 my $off = 0;
572 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700573
Andy Whitcroft773647a2008-03-28 14:15:58 -0700574 # Always copy over the diff marker.
575 $res = substr($line, 0, 1);
576
577 for ($off = 1; $off < length($line); $off++) {
578 $c = substr($line, $off, 1);
579
580 # Comments we are wacking completly including the begin
581 # and end, all to $;.
582 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
583 $sanitise_quote = '*/';
584
585 substr($res, $off, 2, "$;$;");
586 $off++;
587 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800588 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700589 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700590 $sanitise_quote = '';
591 substr($res, $off, 2, "$;$;");
592 $off++;
593 next;
594 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700595 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
596 $sanitise_quote = '//';
597
598 substr($res, $off, 2, $sanitise_quote);
599 $off++;
600 next;
601 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700602
603 # A \ in a string means ignore the next character.
604 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
605 $c eq "\\") {
606 substr($res, $off, 2, 'XX');
607 $off++;
608 next;
609 }
610 # Regular quotes.
611 if ($c eq "'" || $c eq '"') {
612 if ($sanitise_quote eq '') {
613 $sanitise_quote = $c;
614
615 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700616 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700617 } elsif ($sanitise_quote eq $c) {
618 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700619 }
620 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700621
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800622 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700623 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
624 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700625 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
626 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700627 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
628 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700629 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700630 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700631 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800632 }
633
Daniel Walker113f04a2009-09-21 17:04:35 -0700634 if ($sanitise_quote eq '//') {
635 $sanitise_quote = '';
636 }
637
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800638 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700639 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800640 my $clean = 'X' x length($1);
641 $res =~ s@\<.*\>@<$clean>@;
642
643 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700644 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800645 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700646 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800647 }
648
Andy Whitcroft00df3442007-06-08 13:47:06 -0700649 return $res;
650}
651
Andy Whitcroft8905a672007-11-28 16:21:06 -0800652sub ctx_statement_block {
653 my ($linenr, $remain, $off) = @_;
654 my $line = $linenr - 1;
655 my $blk = '';
656 my $soff = $off;
657 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700658 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800659
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 my $loff = 0;
661
Andy Whitcroft8905a672007-11-28 16:21:06 -0800662 my $type = '';
663 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800664 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800665 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800666 my $c;
667 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800668
669 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800670 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800671 @stack = (['', 0]) if ($#stack == -1);
672
Andy Whitcroft773647a2008-03-28 14:15:58 -0700673 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674 # If we are about to drop off the end, pull in more
675 # context.
676 if ($off >= $len) {
677 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700678 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800679 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800681 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800683 $len = length($blk);
684 $line++;
685 last;
686 }
687 # Bail if there is no further context.
688 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800689 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800690 last;
691 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800692 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
693 $level++;
694 $type = '#';
695 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800696 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800697 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800698 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800699 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800700
Andy Whitcroft773647a2008-03-28 14:15:58 -0700701 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800702
703 # Handle nested #if/#else.
704 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
705 push(@stack, [ $type, $level ]);
706 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
707 ($type, $level) = @{$stack[$#stack - 1]};
708 } elsif ($remainder =~ /^#\s*endif\b/) {
709 ($type, $level) = @{pop(@stack)};
710 }
711
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712 # Statement ends at the ';' or a close '}' at the
713 # outermost level.
714 if ($level == 0 && $c eq ';') {
715 last;
716 }
717
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800718 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700719 if ($level == 0 && $coff_set == 0 &&
720 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
721 $remainder =~ /^(else)(?:\s|{)/ &&
722 $remainder !~ /^else\s+if\b/) {
723 $coff = $off + length($1) - 1;
724 $coff_set = 1;
725 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
726 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800727 }
728
Andy Whitcroft8905a672007-11-28 16:21:06 -0800729 if (($type eq '' || $type eq '(') && $c eq '(') {
730 $level++;
731 $type = '(';
732 }
733 if ($type eq '(' && $c eq ')') {
734 $level--;
735 $type = ($level != 0)? '(' : '';
736
737 if ($level == 0 && $coff < $soff) {
738 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700739 $coff_set = 1;
740 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800741 }
742 }
743 if (($type eq '' || $type eq '{') && $c eq '{') {
744 $level++;
745 $type = '{';
746 }
747 if ($type eq '{' && $c eq '}') {
748 $level--;
749 $type = ($level != 0)? '{' : '';
750
751 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700752 if (substr($blk, $off + 1, 1) eq ';') {
753 $off++;
754 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800755 last;
756 }
757 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800758 # Preprocessor commands end at the newline unless escaped.
759 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
760 $level--;
761 $type = '';
762 $off++;
763 last;
764 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800765 $off++;
766 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700767 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800768 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700769 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800770 $line++;
771 $remain--;
772 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800773
774 my $statement = substr($blk, $soff, $off - $soff + 1);
775 my $condition = substr($blk, $soff, $coff - $soff + 1);
776
777 #warn "STATEMENT<$statement>\n";
778 #warn "CONDITION<$condition>\n";
779
Andy Whitcroft773647a2008-03-28 14:15:58 -0700780 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800781
782 return ($statement, $condition,
783 $line, $remain + 1, $off - $loff + 1, $level);
784}
785
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786sub statement_lines {
787 my ($stmt) = @_;
788
789 # Strip the diff line prefixes and rip blank lines at start and end.
790 $stmt =~ s/(^|\n)./$1/g;
791 $stmt =~ s/^\s*//;
792 $stmt =~ s/\s*$//;
793
794 my @stmt_lines = ($stmt =~ /\n/g);
795
796 return $#stmt_lines + 2;
797}
798
799sub statement_rawlines {
800 my ($stmt) = @_;
801
802 my @stmt_lines = ($stmt =~ /\n/g);
803
804 return $#stmt_lines + 2;
805}
806
807sub statement_block_size {
808 my ($stmt) = @_;
809
810 $stmt =~ s/(^|\n)./$1/g;
811 $stmt =~ s/^\s*{//;
812 $stmt =~ s/}\s*$//;
813 $stmt =~ s/^\s*//;
814 $stmt =~ s/\s*$//;
815
816 my @stmt_lines = ($stmt =~ /\n/g);
817 my @stmt_statements = ($stmt =~ /;/g);
818
819 my $stmt_lines = $#stmt_lines + 2;
820 my $stmt_statements = $#stmt_statements + 1;
821
822 if ($stmt_lines > $stmt_statements) {
823 return $stmt_lines;
824 } else {
825 return $stmt_statements;
826 }
827}
828
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800829sub ctx_statement_full {
830 my ($linenr, $remain, $off) = @_;
831 my ($statement, $condition, $level);
832
833 my (@chunks);
834
Andy Whitcroftcf655042008-03-04 14:28:20 -0800835 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800836 ($statement, $condition, $linenr, $remain, $off, $level) =
837 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800839 push(@chunks, [ $condition, $statement ]);
840 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
841 return ($level, $linenr, @chunks);
842 }
843
844 # Pull in the following conditional/block pairs and see if they
845 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800847 ($statement, $condition, $linenr, $remain, $off, $level) =
848 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800849 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700850 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800851 #print "C: push\n";
852 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800853 }
854
855 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800856}
857
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700858sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700859 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700860 my $line;
861 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700862 my $blk = '';
863 my @o;
864 my @c;
865 my @res = ();
866
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700867 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800868 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700869 for ($line = $start; $remain > 0; $line++) {
870 next if ($rawlines[$line] =~ /^-/);
871 $remain--;
872
873 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800874
875 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700876 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800877 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700878 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800879 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700880 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800881 $level = pop(@stack);
882 }
883
Andy Whitcroft01464f32010-10-26 14:23:19 -0700884 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700885 ##print "C<$c>L<$level><$open$close>O<$off>\n";
886 if ($off > 0) {
887 $off--;
888 next;
889 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700890
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700891 if ($c eq $close && $level > 0) {
892 $level--;
893 last if ($level == 0);
894 } elsif ($c eq $open) {
895 $level++;
896 }
897 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700898
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700899 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700900 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700901 }
902
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700903 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700904 }
905
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700906 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700907}
908sub ctx_block_outer {
909 my ($linenr, $remain) = @_;
910
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700911 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
912 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700913}
914sub ctx_block {
915 my ($linenr, $remain) = @_;
916
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700917 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
918 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700919}
920sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700921 my ($linenr, $remain, $off) = @_;
922
923 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
924 return @r;
925}
926sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700927 my ($linenr, $remain) = @_;
928
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700929 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700930}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700931sub ctx_statement_level {
932 my ($linenr, $remain, $off) = @_;
933
934 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
935}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700936
937sub ctx_locate_comment {
938 my ($first_line, $end_line) = @_;
939
940 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700941 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700942 return $current_comment if (defined $current_comment);
943
944 # Look through the context and try and figure out if there is a
945 # comment.
946 my $in_comment = 0;
947 $current_comment = '';
948 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700949 my $line = $rawlines[$linenr - 1];
950 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700951 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
952 $in_comment = 1;
953 }
954 if ($line =~ m@/\*@) {
955 $in_comment = 1;
956 }
957 if (!$in_comment && $current_comment ne '') {
958 $current_comment = '';
959 }
960 $current_comment .= $line . "\n" if ($in_comment);
961 if ($line =~ m@\*/@) {
962 $in_comment = 0;
963 }
964 }
965
966 chomp($current_comment);
967 return($current_comment);
968}
969sub ctx_has_comment {
970 my ($first_line, $end_line) = @_;
971 my $cmt = ctx_locate_comment($first_line, $end_line);
972
Andy Whitcroft00df3442007-06-08 13:47:06 -0700973 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700974 ##print "CMMT: $cmt\n";
975
976 return ($cmt ne '');
977}
978
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700979sub raw_line {
980 my ($linenr, $cnt) = @_;
981
982 my $offset = $linenr - 1;
983 $cnt++;
984
985 my $line;
986 while ($cnt) {
987 $line = $rawlines[$offset++];
988 next if (defined($line) && $line =~ /^-/);
989 $cnt--;
990 }
991
992 return $line;
993}
994
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700995sub cat_vet {
996 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700997 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700998
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700999 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1001 $res .= $1;
1002 if ($2 ne '') {
1003 $coded = sprintf("^%c", unpack('C', $2) + 64);
1004 $res .= $coded;
1005 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001006 }
1007 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001008
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001009 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001010}
1011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001013my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001014my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001015my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001016
1017sub annotate_reset {
1018 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001019 $av_pending = '_';
1020 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001021 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001022}
1023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024sub annotate_values {
1025 my ($stream, $type) = @_;
1026
1027 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001028 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001029 my $cur = $stream;
1030
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001033 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001034 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001035 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001036 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001037 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 print "WS($1)\n" if ($dbg_values > 1);
1039 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001040 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042 }
1043
Florian Micklerc023e4732011-01-12 16:59:58 -08001044 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001045 print "CAST($1)\n" if ($dbg_values > 1);
1046 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001047 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001048
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001049 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001051 $type = 'T';
1052
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001053 } elsif ($cur =~ /^($Modifier)\s*/) {
1054 print "MODIFIER($1)\n" if ($dbg_values > 1);
1055 $type = 'T';
1056
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001057 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001058 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001059 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001060 push(@av_paren_type, $type);
1061 if ($2 ne '') {
1062 $av_pending = 'N';
1063 }
1064 $type = 'E';
1065
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001066 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001067 print "UNDEF($1)\n" if ($dbg_values > 1);
1068 $av_preprocessor = 1;
1069 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001071 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001072 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001073 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001074
1075 push(@av_paren_type, $type);
1076 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001077 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001078
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001079 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001080 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1081 $av_preprocessor = 1;
1082
1083 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1084
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001085 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001086
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001087 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001088 print "PRE_END($1)\n" if ($dbg_values > 1);
1089
1090 $av_preprocessor = 1;
1091
1092 # Assume all arms of the conditional end as this
1093 # one does, and continue as if the #endif was not here.
1094 pop(@av_paren_type);
1095 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001096 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001097
1098 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001100
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001101 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1102 print "ATTR($1)\n" if ($dbg_values > 1);
1103 $av_pending = $type;
1104 $type = 'N';
1105
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001106 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001107 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110 }
1111 $type = 'N';
1112
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001113 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001114 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001115 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116 $type = 'N';
1117
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001118 } elsif ($cur =~/^(case)/o) {
1119 print "CASE($1)\n" if ($dbg_values > 1);
1120 $av_pend_colon = 'C';
1121 $type = 'N';
1122
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001123 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001124 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 $type = 'N';
1126
1127 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001128 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001129 push(@av_paren_type, $av_pending);
1130 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001131 $type = 'N';
1132
1133 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001134 my $new_type = pop(@av_paren_type);
1135 if ($new_type ne '_') {
1136 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001137 print "PAREN('$1') -> $type\n"
1138 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001141 }
1142
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001143 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001144 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001145 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001146 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001147
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001148 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1149 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001150 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001151 } elsif ($type eq 'E') {
1152 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001153 }
1154 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1155 $type = 'V';
1156
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001157 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001159 $type = 'V';
1160
1161 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 $type = 'N';
1164
Andy Whitcroftcf655042008-03-04 14:28:20 -08001165 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001166 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001167 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001168 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001169
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001170 } elsif ($cur =~/^(,)/) {
1171 print "COMMA($1)\n" if ($dbg_values > 1);
1172 $type = 'C';
1173
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001174 } elsif ($cur =~ /^(\?)/o) {
1175 print "QUESTION($1)\n" if ($dbg_values > 1);
1176 $type = 'N';
1177
1178 } elsif ($cur =~ /^(:)/o) {
1179 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1180
1181 substr($var, length($res), 1, $av_pend_colon);
1182 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1183 $type = 'E';
1184 } else {
1185 $type = 'N';
1186 }
1187 $av_pend_colon = 'O';
1188
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001189 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001190 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001191 $type = 'N';
1192
Andy Whitcroft0d413862008-10-15 22:02:16 -07001193 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001194 my $variant;
1195
1196 print "OPV($1)\n" if ($dbg_values > 1);
1197 if ($type eq 'V') {
1198 $variant = 'B';
1199 } else {
1200 $variant = 'U';
1201 }
1202
1203 substr($var, length($res), 1, $variant);
1204 $type = 'N';
1205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001206 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001208 if ($1 ne '++' && $1 ne '--') {
1209 $type = 'N';
1210 }
1211
1212 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214 }
1215 if (defined $1) {
1216 $cur = substr($cur, length($1));
1217 $res .= $type x length($1);
1218 }
1219 }
1220
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001221 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001222}
1223
Andy Whitcroft8905a672007-11-28 16:21:06 -08001224sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001225 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001226 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001227 ^(?:
1228 $Modifier|
1229 $Storage|
1230 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001231 DEFINE_\S+
1232 )$|
1233 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001234 goto|
1235 return|
1236 case|
1237 else|
1238 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001239 do|
1240 \#|
1241 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001242 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001243 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001244 )}x;
1245 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1246 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001247 # Check for modifiers.
1248 $possible =~ s/\s*$Storage\s*//g;
1249 $possible =~ s/\s*$Sparse\s*//g;
1250 if ($possible =~ /^\s*$/) {
1251
1252 } elsif ($possible =~ /\s/) {
1253 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001254 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001255 if ($modifier !~ $notPermitted) {
1256 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1257 push(@modifierList, $modifier);
1258 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001259 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001260
1261 } else {
1262 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1263 push(@typeList, $possible);
1264 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001265 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001266 } else {
1267 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 }
1269}
1270
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001271my $prefix = '';
1272
Joe Perches000d1cc2011-07-25 17:13:25 -07001273sub show_type {
1274 return !defined $ignore_type{$_[0]};
1275}
1276
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001277sub report {
Joe Perches000d1cc2011-07-25 17:13:25 -07001278 if (!show_type($_[1]) ||
1279 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280 return 0;
1281 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001282 my $line;
1283 if ($show_types) {
1284 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1285 } else {
1286 $line = "$prefix$_[0]: $_[2]\n";
1287 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001288 $line = (split('\n', $line))[0] . "\n" if ($terse);
1289
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001290 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291
1292 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001293}
1294sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001295 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001296}
Joe Perches000d1cc2011-07-25 17:13:25 -07001297
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001298sub ERROR {
Joe Perches000d1cc2011-07-25 17:13:25 -07001299 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 our $clean = 0;
1301 our $cnt_error++;
1302 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001303}
1304sub WARN {
Joe Perches000d1cc2011-07-25 17:13:25 -07001305 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 our $clean = 0;
1307 our $cnt_warn++;
1308 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001309}
1310sub CHK {
Joe Perches000d1cc2011-07-25 17:13:25 -07001311 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001312 our $clean = 0;
1313 our $cnt_chk++;
1314 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001315}
1316
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001317sub check_absolute_file {
1318 my ($absolute, $herecurr) = @_;
1319 my $file = $absolute;
1320
1321 ##print "absolute<$absolute>\n";
1322
1323 # See if any suffix of this path is a path within the tree.
1324 while ($file =~ s@^[^/]*/@@) {
1325 if (-f "$root/$file") {
1326 ##print "file<$file>\n";
1327 last;
1328 }
1329 }
1330 if (! -f _) {
1331 return 0;
1332 }
1333
1334 # It is, so see if the prefix is acceptable.
1335 my $prefix = $absolute;
1336 substr($prefix, -length($file)) = '';
1337
1338 ##print "prefix<$prefix>\n";
1339 if ($prefix ne ".../") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001340 WARN("USE_RELATIVE_PATH",
1341 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001342 }
1343}
1344
Gregory Bean3f66eea2011-07-19 16:37:49 -07001345sub cleanup_continuation_headers {
1346 # Collapse any header-continuation lines into a single line so they
1347 # can be parsed meaningfully, as the parser only has one line
1348 # of context to work with.
1349 my $again;
1350 do {
1351 $again = 0;
1352 foreach my $n (0 .. scalar(@rawlines) - 2) {
1353 if ($rawlines[$n]=~/^\s*$/) {
1354 # A blank line means there's no more chance
1355 # of finding headers. Shortcut to done.
1356 return;
1357 }
1358 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
1359 $rawlines[$n+1]=~/^\s+/) {
1360 # Continuation header. Collapse it.
1361 my $line = splice @rawlines, $n+1, 1;
1362 $line=~s/^\s+/ /;
1363 $rawlines[$n] .= $line;
1364 # We've 'destabilized' the list, so restart.
1365 $again = 1;
1366 last;
1367 }
1368 }
1369 } while ($again);
1370}
1371
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001372sub pos_last_openparen {
1373 my ($line) = @_;
1374
1375 my $pos = 0;
1376
1377 my $opens = $line =~ tr/\(/\(/;
1378 my $closes = $line =~ tr/\)/\)/;
1379
1380 my $last_openparen = 0;
1381
1382 if (($opens == 0) || ($closes >= $opens)) {
1383 return -1;
1384 }
1385
1386 my $len = length($line);
1387
1388 for ($pos = 0; $pos < $len; $pos++) {
1389 my $string = substr($line, $pos);
1390 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1391 $pos += length($1) - 1;
1392 } elsif (substr($line, $pos, 1) eq '(') {
1393 $last_openparen = $pos;
1394 } elsif (index($string, '(') == -1) {
1395 last;
1396 }
1397 }
1398
1399 return $last_openparen + 1;
1400}
1401
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001402sub process {
1403 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001404
1405 my $linenr=0;
1406 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001408 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 my $stashrawline="";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 my $subjectline="";
1411 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001412
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001413 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001414 my $indent;
1415 my $previndent=0;
1416 my $stashindent=0;
1417
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001418 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001419 my $signoff = 0;
1420 my $is_patch = 0;
1421
Joe Perches15662b32011-10-31 17:13:12 -07001422 my $in_header_lines = 1;
1423 my $in_commit_log = 0; #Scanning lines before patch
1424
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001425 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001426 our $cnt_lines = 0;
1427 our $cnt_error = 0;
1428 our $cnt_warn = 0;
1429 our $cnt_chk = 0;
1430
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001431 # Trace the real file/line as we go.
1432 my $realfile = '';
1433 my $realline = 0;
1434 my $realcnt = 0;
1435 my $here = '';
1436 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001438 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001439 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001440
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441 my $prev_values = 'E';
1442
1443 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001445 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001446 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001447 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001448
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001450 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001451 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001452 my @setup_docs = ();
1453 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 my $exec_file = "";
1456
1457 my $shorttext = BEFORE_SHORTTEXT;
1458 my $shorttext_exspc = 0;
Steve Muckled3b3b642012-03-05 10:12:04 -08001459 my $commit_text_present = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460
Andy Whitcroft773647a2008-03-28 14:15:58 -07001461 sanitise_line_reset();
Gregory Bean3f66eea2011-07-19 16:37:49 -07001462 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001465 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001466 $linenr++;
1467 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001468
Andy Whitcroft773647a2008-03-28 14:15:58 -07001469 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001470 $setup_docs = 0;
1471 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1472 $setup_docs = 1;
1473 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001474 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001475 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001476 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1477 $realline=$1-1;
1478 if (defined $2) {
1479 $realcnt=$3+1;
1480 } else {
1481 $realcnt=1+1;
1482 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001483 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001484
1485 # Guestimate if this is a continuing comment. Run
1486 # the context looking for a comment "edge". If this
1487 # edge is a close comment then we must be in a comment
1488 # at context start.
1489 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001490 my $cnt = $realcnt;
1491 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1492 next if (defined $rawlines[$ln - 1] &&
1493 $rawlines[$ln - 1] =~ /^-/);
1494 $cnt--;
1495 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001496 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001497 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1498 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1499 ($edge) = $1;
1500 last;
1501 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 }
1503 if (defined $edge && $edge eq '*/') {
1504 $in_comment = 1;
1505 }
1506
1507 # Guestimate if this is a continuing comment. If this
1508 # is the start of a diff block and this line starts
1509 # ' *' then it is very likely a comment.
1510 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001511 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001512 {
1513 $in_comment = 1;
1514 }
1515
1516 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1517 sanitise_line_reset($in_comment);
1518
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001519 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001520 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001521 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001522 $line = sanitise_line($rawline);
1523 }
1524 push(@lines, $line);
1525
1526 if ($realcnt > 1) {
1527 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1528 } else {
1529 $realcnt = 0;
1530 }
1531
1532 #print "==>$rawline\n";
1533 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001534
1535 if ($setup_docs && $line =~ /^\+/) {
1536 push(@setup_docs, $line);
1537 }
1538 }
1539
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001540 $prefix = '';
1541
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 $realcnt = 0;
1543 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001544 foreach my $line (@lines) {
1545 $linenr++;
1546
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001547 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001549#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001550 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001551 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001552 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001553 $realline=$1-1;
1554 if (defined $2) {
1555 $realcnt=$3+1;
1556 } else {
1557 $realcnt=1+1;
1558 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001559 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001560 $prev_values = 'E';
1561
Andy Whitcroft773647a2008-03-28 14:15:58 -07001562 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001563 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001564 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001565 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001566 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001567
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001568# track the line number as we move through the hunk, note that
1569# new versions of GNU diff omit the leading space on completely
1570# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001571 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001572 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001573 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001574
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001575 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001576 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001577
1578 # Track the previous line.
1579 ($prevline, $stashline) = ($stashline, $line);
1580 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001581 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1582
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001584
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001585 } elsif ($realcnt == 1) {
1586 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001587 }
1588
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001589 my $hunk_line = ($realcnt != 0);
1590
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001591#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001592 $prefix = "$filename:$realline: " if ($emacs && $file);
1593 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1594
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001595 $here = "#$linenr: " if (!$file);
1596 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001597
1598 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001599 if ($line =~ /^diff --git.*?(\S+)$/) {
1600 $realfile = $1;
1601 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001602 $in_commit_log = 0;
Gregory Bean246e1f12011-10-17 12:27:01 -07001603 $exec_file = $realfile;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001604 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001605 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001606 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001607 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001608
1609 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001610 if (!$file && $tree && $p1_prefix ne '' &&
1611 -e "$root/$p1_prefix") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001612 WARN("PATCH_PREFIX",
1613 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001614 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001615
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001616 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001617 ERROR("MODIFIED_INCLUDE_ASM",
1618 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07001619 }
Gregory Bean246e1f12011-10-17 12:27:01 -07001620 $exec_file = "";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 next;
1622 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1624 $exec_file = $1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 }
1626 #Check state to make sure we aren't in code block.
Gregory Bean246e1f12011-10-17 12:27:01 -07001627 elsif (($exec_file =~ /^.+\.[chS]$/ or
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 $exec_file =~ /^.+\.txt$/ or
1629 $exec_file =~ /^.+\.ihex$/ or
1630 $exec_file =~ /^.+\.hex$/ or
1631 $exec_file =~ /^.+\.HEX$/ or
1632 $exec_file =~ /^.+defconfig$/ or
1633 $exec_file =~ /^Makefile$/ or
1634 $exec_file =~ /^Kconfig$/) &&
1635 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1636 (oct($2) & 0111)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001637 ERROR("EXECUTE_PERMISSIONS",
1638 "Source file has +x permissions: " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001639 "$exec_file\n");
1640 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001641 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001642
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001643 my $hereline = "$here\n$rawline\n";
1644 my $herecurr = "$here\n$rawline\n";
1645 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001646
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001647 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001648 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
1649 # the subject line was just processed,
1650 # a blank line must be next
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001651 WARN("NONBLANK_AFTER_SUMMARY",
1652 "non-blank line after summary line\n" . $herecurr);
Steve Muckled3b3b642012-03-05 10:12:04 -08001653 $shorttext = IN_SHORTTEXT;
1654 # this non-blank line may or may not be commit text -
1655 # a warning has been generated so assume it is commit
1656 # text and move on
1657 $commit_text_present = 1;
1658 # fall through and treat this line as IN_SHORTTEXT
1659 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 if ($shorttext == IN_SHORTTEXT) {
1661 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001662 if ($commit_text_present == 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001663 WARN("NO_COMMIT_TEXT",
1664 "please add commit text explaining " .
Steve Muckled3b3b642012-03-05 10:12:04 -08001665 "*why* the change is needed\n" .
1666 $herecurr);
1667 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 $shorttext = AFTER_SHORTTEXT;
1669 } elsif (length($line) > (SHORTTEXT_LIMIT +
1670 $shorttext_exspc)
1671 && $line !~ /^:([0-7]{6}\s){2}
1672 ([[:xdigit:]]+\.*
1673 \s){2}\w+\s\w+/xms) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001674 WARN("LONG_COMMIT_TEXT",
1675 "commit text line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 SHORTTEXT_LIMIT .
1677 " characters\n" . $herecurr);
Steve Muckle336a3ae2012-03-09 11:15:24 -08001678 } elsif ($line=~/^\s*change-id:/i ||
1679 $line=~/^\s*signed-off-by:/i ||
1680 $line=~/^\s*crs-fixed:/i ||
1681 $line=~/^\s*acked-by:/i) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001682 # this is a tag, there must be commit
1683 # text by now
1684 if ($commit_text_present == 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001685 WARN("NO_COMMIT_TEXT",
1686 "please add commit text explaining " .
Steve Muckled3b3b642012-03-05 10:12:04 -08001687 "*why* the change is needed\n" .
1688 $herecurr);
1689 # prevent duplicate warnings
1690 $commit_text_present = 1;
1691 }
1692 } elsif ($line=~/\S/) {
1693 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001695 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
1696 # case of non-blank line in this state handled above
1697 $shorttext = IN_SHORTTEXT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1699# The Subject line doesn't have to be the last header in the patch.
1700# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1701# Per RFC5322, continuation lines must be folded, so any left-justified
1702# text which looks like a header is definitely a header.
1703 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1704 $shorttext = IN_SHORTTEXT;
Steve Muckled3b3b642012-03-05 10:12:04 -08001705 # Check for Subject line followed by a blank line.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001706 if (length($line) != 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001707 WARN("NONBLANK_AFTER_SUMMARY",
1708 "non-blank line after " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 "summary line\n" .
1710 $sublinenr . $here .
1711 "\n" . $subjectline .
1712 "\n" . $line . "\n");
Steve Muckled3b3b642012-03-05 10:12:04 -08001713 # this non-blank line may or may not
1714 # be commit text - a warning has been
1715 # generated so assume it is commit
1716 # text and move on
1717 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718 }
1719 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001720 # The next two cases are BEFORE_SHORTTEXT.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001722 # This is the subject line. Go to
1723 # CHECK_NEXT_SHORTTEXT to wait for the commit
1724 # text to show up.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 $shorttext = CHECK_NEXT_SHORTTEXT;
1726 $subjectline = $line;
1727 $sublinenr = "#$linenr & ";
1728# Check for Subject line less than line limit
Matt Wagantall3c09aeb2012-08-18 11:21:00 -07001729 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001730 WARN("LONG_SUMMARY_LINE",
1731 "summary line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001732 SHORTTEXT_LIMIT .
1733 " characters\n" . $herecurr);
1734 }
1735 } elsif ($line=~/^ (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001736 # Indented format, this must be the summary
1737 # line (i.e. git show). There will be no more
1738 # headers so we are now in the shorttext.
1739 $shorttext = IN_SHORTTEXT_BLANKLINE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 $shorttext_exspc = 4;
Matt Wagantall3c09aeb2012-08-18 11:21:00 -07001741 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001742 WARN("LONG_SUMMARY_LINE",
1743 "summary line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001744 SHORTTEXT_LIMIT .
1745 " characters\n" . $herecurr);
1746 }
1747 }
1748 }
1749
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 $cnt_lines++ if ($realcnt != 0);
1751
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001752# Check for incorrect file permissions
1753 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1754 my $permhere = $here . "FILE: $realfile\n";
1755 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001756 ERROR("EXECUTE_PERMISSIONS",
1757 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001758 }
1759 }
1760
Joe Perches20112472011-07-25 17:13:23 -07001761# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001762 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001763 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001764 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001765 }
1766
1767# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001768 if (!$in_header_lines &&
1769 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
Joe Perches20112472011-07-25 17:13:23 -07001770 my $space_before = $1;
1771 my $sign_off = $2;
1772 my $space_after = $3;
1773 my $email = $4;
1774 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1775
1776 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001777 WARN("BAD_SIGN_OFF",
1778 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001779 }
Joe Perches20112472011-07-25 17:13:23 -07001780 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001781 WARN("BAD_SIGN_OFF",
1782 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001783 }
1784 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001785 WARN("BAD_SIGN_OFF",
1786 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001787 }
1788
1789 my ($email_name, $email_address, $comment) = parse_email($email);
1790 my $suggested_email = format_email(($email_name, $email_address));
1791 if ($suggested_email eq "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001792 ERROR("BAD_SIGN_OFF",
1793 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001794 } else {
1795 my $dequoted = $suggested_email;
1796 $dequoted =~ s/^"//;
1797 $dequoted =~ s/" </ </;
1798 # Don't force email to have quotes
1799 # Allow just an angle bracketed address
1800 if ("$dequoted$comment" ne $email &&
1801 "<$email_address>$comment" ne $email &&
1802 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001803 WARN("BAD_SIGN_OFF",
1804 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001805 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001806 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001808 WARN("BAD_SIGN_OFF",
1809 "invalid Signed-off-by identity\n" . $line );
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 }
1811 }
1812
1813#check the patch for invalid author credentials
1814 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001815 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001816 }
1817
Andy Whitcroft00df3442007-06-08 13:47:06 -07001818# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001819 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001820 ERROR("CORRUPTED_PATCH",
1821 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001822 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001823 }
1824
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001825# Check for absolute kernel paths.
1826 if ($tree) {
1827 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1828 my $file = $1;
1829
1830 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1831 check_absolute_file($1, $herecurr)) {
1832 #
1833 } else {
1834 check_absolute_file($file, $herecurr);
1835 }
1836 }
1837 }
1838
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001839# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1840 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001841 $rawline !~ m/^$UTF8*$/) {
1842 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1843
1844 my $blank = copy_spacing($rawline);
1845 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1846 my $hereptr = "$hereline$ptr\n";
1847
Joe Perches34d99212011-07-25 17:13:26 -07001848 CHK("INVALID_UTF8",
1849 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001850 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001851
Joe Perches15662b32011-10-31 17:13:12 -07001852# Check if it's the start of a commit log
1853# (not a header line and we haven't seen the patch filename)
1854 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001855 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001856 $in_header_lines = 0;
1857 $in_commit_log = 1;
1858 }
1859
1860# Still not yet in a patch, check for any UTF-8
1861 if ($in_commit_log && $realfile =~ /^$/ &&
1862 $rawline =~ /$NON_ASCII_UTF8/) {
1863 CHK("UTF8_BEFORE_PATCH",
1864 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1865 }
1866
Andy Whitcroft306708542008-10-15 22:02:28 -07001867# ignore non-hunk lines and lines being removed
1868 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001869
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001870#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001871 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001872 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001873 ERROR("DOS_LINE_ENDINGS",
1874 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001875
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001876 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1877 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001878 ERROR("TRAILING_WHITESPACE",
1879 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001880 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001881 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001882
Andi Kleen33549572010-05-24 14:33:29 -07001883# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001884# Only applies when adding the entry originally, after that we do not have
1885# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001886 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001887 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001888 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001889 my $cnt = $realcnt;
1890 my $ln = $linenr + 1;
1891 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001892 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001893 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001894 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001895 $f = $lines[$ln - 1];
1896 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1897 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001898
1899 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001900
1901 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1902 $is_start = 1;
1903 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1904 $length = -1;
1905 }
1906
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001907 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001908 $f =~ s/#.*//;
1909 $f =~ s/^\s+//;
1910 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001911 if ($f =~ /^\s*config\s/) {
1912 $is_end = 1;
1913 last;
1914 }
Andi Kleen33549572010-05-24 14:33:29 -07001915 $length++;
1916 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001917 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001918 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1919 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001920 }
1921
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001922 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1923 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1924 my $flag = $1;
1925 my $replacement = {
1926 'EXTRA_AFLAGS' => 'asflags-y',
1927 'EXTRA_CFLAGS' => 'ccflags-y',
1928 'EXTRA_CPPFLAGS' => 'cppflags-y',
1929 'EXTRA_LDFLAGS' => 'ldflags-y',
1930 };
1931
1932 WARN("DEPRECATED_VARIABLE",
1933 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1934 }
1935
Andy Whitcroft5368df22008-10-15 22:02:27 -07001936# check we are in a valid source file if not then ignore this hunk
1937 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1938
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001939#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001940 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001941 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001942 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001943 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001945 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001946 {
Joe Perches000d1cc2011-07-25 17:13:25 -07001947 WARN("LONG_LINE",
1948 "line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001949 }
1950
Josh Triplettca56dc02012-03-23 15:02:21 -07001951# Check for user-visible strings broken across lines, which breaks the ability
1952# to grep for the string. Limited to strings used as parameters (those
1953# following an open parenthesis), which almost completely eliminates false
1954# positives, as well as warning only once per parameter rather than once per
1955# line of the string. Make an exception when the previous string ends in a
1956# newline (multiple lines in one string constant) or \n\t (common in inline
1957# assembly to indent the instruction on the following line).
1958 if ($line =~ /^\+\s*"/ &&
1959 $prevline =~ /"\s*$/ &&
1960 $prevline =~ /\(/ &&
1961 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1962 WARN("SPLIT_STRING",
1963 "quoted string split across lines\n" . $hereprev);
1964 }
1965
Joe Perches5e79d962010-03-05 13:43:55 -08001966# check for spaces before a quoted newline
1967 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001968 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1969 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001970 }
1971
Andy Whitcroft8905a672007-11-28 16:21:06 -08001972# check for adding lines without a newline.
1973 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001974 WARN("MISSING_EOF_NEWLINE",
1975 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001976 }
1977
Mike Frysinger42e41c52009-09-21 17:04:40 -07001978# Blackfin: use hi/lo macros
1979 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1980 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1981 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001982 ERROR("LO_MACRO",
1983 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001984 }
1985 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1986 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001987 ERROR("HI_MACRO",
1988 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001989 }
1990 }
1991
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001992# check we are in a valid source file C or perl if not then ignore this hunk
1993 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001994
1995# at the beginning of a line any tabs must come first and anything
1996# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001997 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1998 $rawline =~ /^\+\s* \s*/) {
1999 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002000 ERROR("CODE_INDENT",
2001 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002002 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002003 }
2004
Alberto Panizzo08e44362010-03-05 13:43:54 -08002005# check for space before tabs.
2006 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2007 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002008 WARN("SPACE_BEFORE_TAB",
2009 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08002010 }
2011
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002012# check for && or || at the start of a line
2013 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2014 CHK("LOGICAL_CONTINUATIONS",
2015 "Logical continuations should be on the previous line\n" . $hereprev);
2016 }
2017
2018# check multi-line statement indentation matches previous line
2019 if ($^V && $^V ge 5.10.0 &&
2020 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2021 $prevline =~ /^\+(\t*)(.*)$/;
2022 my $oldindent = $1;
2023 my $rest = $2;
2024
2025 my $pos = pos_last_openparen($rest);
2026 if ($pos >= 0) {
2027 $line =~ /^\+([ \t]*)/;
2028 my $newindent = $1;
2029
2030 my $goodtabindent = $oldindent .
2031 "\t" x ($pos / 8) .
2032 " " x ($pos % 8);
2033 my $goodspaceindent = $oldindent . " " x $pos;
2034
2035 if ($newindent ne $goodtabindent &&
2036 $newindent ne $goodspaceindent) {
2037 CHK("PARENTHESIS_ALIGNMENT",
2038 "Alignment should match open parenthesis\n" . $hereprev);
2039 }
2040 }
2041 }
2042
Joe Perchesaad4f612012-03-23 15:02:19 -07002043 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
2044 CHK("SPACING",
2045 "No space is necessary after a cast\n" . $hereprev);
2046 }
2047
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002048# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002049# Exceptions:
2050# 1) within comments
2051# 2) indented preprocessor commands
2052# 3) hanging labels
2053 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002054 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002055 WARN("LEADING_SPACE",
2056 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002057 }
2058
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002059# check we are in a valid C source file if not then ignore this hunk
2060 next if ($realfile !~ /\.(h|c)$/);
2061
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002062# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002063 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002064 WARN("CVS_KEYWORD",
2065 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002066 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002067
Mike Frysinger42e41c52009-09-21 17:04:40 -07002068# Blackfin: don't use __builtin_bfin_[cs]sync
2069 if ($line =~ /__builtin_bfin_csync/) {
2070 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002071 ERROR("CSYNC",
2072 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002073 }
2074 if ($line =~ /__builtin_bfin_ssync/) {
2075 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002076 ERROR("SSYNC",
2077 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002078 }
2079
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002080# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002081 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2082 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002083#print "LINE<$line>\n";
2084 if ($linenr >= $suppress_statement &&
2085 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002086 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002087 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002088 $stat =~ s/\n./\n /g;
2089 $cond =~ s/\n./\n /g;
2090
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002091#print "linenr<$linenr> <$stat>\n";
2092 # If this statement has no statement boundaries within
2093 # it there is no point in retrying a statement scan
2094 # until we hit end of it.
2095 my $frag = $stat; $frag =~ s/;+\s*$//;
2096 if ($frag !~ /(?:{|;)/) {
2097#print "skip<$line_nr_next>\n";
2098 $suppress_statement = $line_nr_next;
2099 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002100
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002101 # Find the real next line.
2102 $realline_next = $line_nr_next;
2103 if (defined $realline_next &&
2104 (!defined $lines[$realline_next - 1] ||
2105 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2106 $realline_next++;
2107 }
2108
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002109 my $s = $stat;
2110 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002111
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002112 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002113 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002114
2115 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002116 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002117
Andy Whitcroft463f2862009-09-21 17:04:34 -07002118 } elsif ($s =~ /^.\s*else\b/s) {
2119
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002120 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002121 } 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 -07002122 my $type = $1;
2123 $type =~ s/\s+/ /g;
2124 possible($type, "A:" . $s);
2125
Andy Whitcroft8905a672007-11-28 16:21:06 -08002126 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07002127 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002128 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002129 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002130
2131 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002132 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002133 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002134 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002135
2136 # Check for any sort of function declaration.
2137 # int foo(something bar, other baz);
2138 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002139 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 -08002140 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002141
Andy Whitcroftcf655042008-03-04 14:28:20 -08002142 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002143 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002144 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002145
Andy Whitcroft8905a672007-11-28 16:21:06 -08002146 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002147 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002148
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002149 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002150 }
2151 }
2152 }
2153
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002154 }
2155
Andy Whitcroft653d4872007-06-23 17:16:34 -07002156#
2157# Checks which may be anchored in the context.
2158#
2159
2160# Check for switch () and associated case and default
2161# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002162 if ($line=~/\bswitch\s*\(.*\)/) {
2163 my $err = '';
2164 my $sep = '';
2165 my @ctx = ctx_block_outer($linenr, $realcnt);
2166 shift(@ctx);
2167 for my $ctx (@ctx) {
2168 my ($clen, $cindent) = line_stats($ctx);
2169 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2170 $indent != $cindent) {
2171 $err .= "$sep$ctx\n";
2172 $sep = '';
2173 } else {
2174 $sep = "[...]\n";
2175 }
2176 }
2177 if ($err ne '') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002178 ERROR("SWITCH_CASE_INDENT_LEVEL",
2179 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002180 }
2181 }
2182
2183# if/while/etc brace do not go on next line, unless defining a do while loop,
2184# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002185 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002186 my $pre_ctx = "$1$2";
2187
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002188 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002189
2190 if ($line =~ /^\+\t{6,}/) {
2191 WARN("DEEP_INDENTATION",
2192 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2193 }
2194
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002195 my $ctx_cnt = $realcnt - $#ctx - 1;
2196 my $ctx = join("\n", @ctx);
2197
Andy Whitcroft548596d2008-07-23 21:29:01 -07002198 my $ctx_ln = $linenr;
2199 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002200
Andy Whitcroft548596d2008-07-23 21:29:01 -07002201 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2202 defined $lines[$ctx_ln - 1] &&
2203 $lines[$ctx_ln - 1] =~ /^-/)) {
2204 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2205 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 $ctx_ln++;
2207 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002208
Andy Whitcroft53210162008-07-23 21:29:03 -07002209 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2210 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002211
2212 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002213 ERROR("OPEN_BRACE",
2214 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002215 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002216 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002217 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2218 $ctx =~ /\)\s*\;\s*$/ &&
2219 defined $lines[$ctx_ln - 1])
2220 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002221 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2222 if ($nindent > $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002223 WARN("TRAILING_SEMICOLON",
2224 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002225 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002226 }
2227 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002228 }
2229
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002230# Check relative indent for conditionals and blocks.
2231 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002232 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2233 ctx_statement_block($linenr, $realcnt, 0)
2234 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002235 my ($s, $c) = ($stat, $cond);
2236
2237 substr($s, 0, length($c), '');
2238
2239 # Make sure we remove the line prefixes as we have
2240 # none on the first line, and are going to readd them
2241 # where necessary.
2242 $s =~ s/\n./\n/gs;
2243
2244 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002245 my @newlines = ($c =~ /\n/gs);
2246 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002247
2248 # We want to check the first line inside the block
2249 # starting at the end of the conditional, so remove:
2250 # 1) any blank line termination
2251 # 2) any opening brace { on end of the line
2252 # 3) any do (...) {
2253 my $continuation = 0;
2254 my $check = 0;
2255 $s =~ s/^.*\bdo\b//;
2256 $s =~ s/^\s*{//;
2257 if ($s =~ s/^\s*\\//) {
2258 $continuation = 1;
2259 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002260 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002261 $check = 1;
2262 $cond_lines++;
2263 }
2264
2265 # Also ignore a loop construct at the end of a
2266 # preprocessor statement.
2267 if (($prevline =~ /^.\s*#\s*define\s/ ||
2268 $prevline =~ /\\\s*$/) && $continuation == 0) {
2269 $check = 0;
2270 }
2271
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002272 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002273 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002274 while ($cond_ptr != $cond_lines) {
2275 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002276
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002277 # If we see an #else/#elif then the code
2278 # is not linear.
2279 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2280 $check = 0;
2281 }
2282
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002283 # Ignore:
2284 # 1) blank lines, they should be at 0,
2285 # 2) preprocessor lines, and
2286 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002287 if ($continuation ||
2288 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002289 $s =~ /^\s*#\s*?/ ||
2290 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002291 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002292 if ($s =~ s/^.*?\n//) {
2293 $cond_lines++;
2294 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002295 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002296 }
2297
2298 my (undef, $sindent) = line_stats("+" . $s);
2299 my $stat_real = raw_line($linenr, $cond_lines);
2300
2301 # Check if either of these lines are modified, else
2302 # this is not this patch's fault.
2303 if (!defined($stat_real) ||
2304 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2305 $check = 0;
2306 }
2307 if (defined($stat_real) && $cond_lines > 1) {
2308 $stat_real = "[...]\n$stat_real";
2309 }
2310
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002311 #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 -07002312
2313 if ($check && (($sindent % 8) != 0 ||
2314 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002315 WARN("SUSPECT_CODE_INDENT",
2316 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002317 }
2318 }
2319
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002320 # Track the 'values' across context and added lines.
2321 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002322 my ($curr_values, $curr_vars) =
2323 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002324 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002325 if ($dbg_values) {
2326 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002327 print "$linenr > .$outline\n";
2328 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002329 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002330 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002331 $prev_values = substr($curr_values, -1);
2332
Andy Whitcroft00df3442007-06-08 13:47:06 -07002333#ignore lines not being added
2334 if ($line=~/^[^\+]/) {next;}
2335
Andy Whitcroft653d4872007-06-23 17:16:34 -07002336# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002337 if ($dbg_type) {
2338 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002339 ERROR("TEST_TYPE",
2340 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002341 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002342 ERROR("TEST_NOT_TYPE",
2343 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002344 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002345 next;
2346 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002347# TEST: allow direct testing of the attribute matcher.
2348 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002349 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002350 ERROR("TEST_ATTR",
2351 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002352 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002353 ERROR("TEST_NOT_ATTR",
2354 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002355 }
2356 next;
2357 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002358
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002359# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002360 if ($line =~ /^.\s*{/ &&
2361 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002362 ERROR("OPEN_BRACE",
2363 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002364 }
2365
Andy Whitcroft653d4872007-06-23 17:16:34 -07002366#
2367# Checks which are anchored on the added line.
2368#
2369
2370# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002371 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002372 my $path = $1;
2373 if ($path =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002374 ERROR("MALFORMED_INCLUDE",
2375 "malformed #include filename\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002376 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002377 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002378 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002379
2380# no C99 // comments
2381 if ($line =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002382 ERROR("C99_COMMENTS",
2383 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002384 }
2385 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002386 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002387 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002388
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002389# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2390# the whole statement.
2391#print "APW <$lines[$realline_next - 1]>\n";
2392 if (defined $realline_next &&
2393 exists $lines[$realline_next - 1] &&
2394 !defined $suppress_export{$realline_next} &&
2395 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2396 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002397 # Handle definitions which produce identifiers with
2398 # a prefix:
2399 # XXX(foo);
2400 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002401 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002402 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002403 $name =~ /^${Ident}_$2/) {
2404#print "FOO C name<$name>\n";
2405 $suppress_export{$realline_next} = 1;
2406
2407 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002408 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002409 ^.DEFINE_$Ident\(\Q$name\E\)|
2410 ^.DECLARE_$Ident\(\Q$name\E\)|
2411 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002412 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2413 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002414 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002415#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2416 $suppress_export{$realline_next} = 2;
2417 } else {
2418 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002419 }
2420 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002421 if (!defined $suppress_export{$linenr} &&
2422 $prevline =~ /^.\s*$/ &&
2423 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2424 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2425#print "FOO B <$lines[$linenr - 1]>\n";
2426 $suppress_export{$linenr} = 2;
2427 }
2428 if (defined $suppress_export{$linenr} &&
2429 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002430 WARN("EXPORT_SYMBOL",
2431 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002432 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002433
Joe Eloff5150bda2010-08-09 17:21:00 -07002434# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002435 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002436 ERROR("GLOBAL_INITIALISERS",
2437 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002438 $herecurr);
2439 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002440# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002441 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002442 ERROR("INITIALISED_STATIC",
2443 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002444 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002445 }
2446
Joe Perchescb710ec2010-10-26 14:23:20 -07002447# check for static const char * arrays.
2448 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002449 WARN("STATIC_CONST_CHAR_ARRAY",
2450 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002451 $herecurr);
2452 }
2453
2454# check for static char foo[] = "bar" declarations.
2455 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002456 WARN("STATIC_CONST_CHAR_ARRAY",
2457 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002458 $herecurr);
2459 }
2460
Joe Perches93ed0e22010-10-26 14:23:21 -07002461# check for declarations of struct pci_device_id
2462 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002463 WARN("DEFINE_PCI_DEVICE_TABLE",
2464 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002465 }
2466
Andy Whitcroft653d4872007-06-23 17:16:34 -07002467# check for new typedefs, only function parameters and sparse annotations
2468# make sense.
2469 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002470 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002471 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002472 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002473 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002474 WARN("NEW_TYPEDEFS",
2475 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002476 }
2477
2478# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002479 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002480 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2481 #print "AA<$1>\n";
2482 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002483
Andy Whitcroft65863862009-01-06 14:41:21 -08002484 # Should start with a space.
2485 $to =~ s/^(\S)/ $1/;
2486 # Should not end with a space.
2487 $to =~ s/\s+$//;
2488 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002489 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002490 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002491
Andy Whitcroft65863862009-01-06 14:41:21 -08002492 #print "from<$from> to<$to>\n";
2493 if ($from ne $to) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002494 ERROR("POINTER_LOCATION",
2495 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002496 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002497 }
2498 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2499 #print "BB<$1>\n";
2500 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002501
Andy Whitcroft65863862009-01-06 14:41:21 -08002502 # Should start with a space.
2503 $to =~ s/^(\S)/ $1/;
2504 # Should not end with a space.
2505 $to =~ s/\s+$//;
2506 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002507 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002508 }
2509 # Modifiers should have spaces.
2510 $to =~ s/(\b$Modifier$)/$1 /;
2511
Andy Whitcroft667026e2009-02-27 14:03:08 -08002512 #print "from<$from> to<$to> ident<$ident>\n";
2513 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002514 ERROR("POINTER_LOCATION",
2515 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002516 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002517 }
2518
2519# # no BUG() or BUG_ON()
2520# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2521# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2522# print "$herecurr";
2523# $clean = 0;
2524# }
2525
Andy Whitcroft8905a672007-11-28 16:21:06 -08002526 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002527 WARN("LINUX_VERSION_CODE",
2528 "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 -08002529 }
2530
Joe Perches17441222011-06-15 15:08:17 -07002531# check for uses of printk_ratelimit
2532 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002533 WARN("PRINTK_RATELIMITED",
2534"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002535 }
2536
Andy Whitcroft00df3442007-06-08 13:47:06 -07002537# printk should use KERN_* levels. Note that follow on printk's on the
2538# same line do not need a level, so we use the current block context
2539# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002540# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002541# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002542 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002543 my $ok = 0;
2544 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2545 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002546 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002547 # with "\n" ignore it, else it is to blame
2548 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2549 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2550 $ok = 1;
2551 }
2552 last;
2553 }
2554 }
2555 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002556 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2557 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002558 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002559 }
2560
Andy Whitcroft653d4872007-06-23 17:16:34 -07002561# function brace can't be on same line, except for #defines of do while,
2562# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002563 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2564 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002565 ERROR("OPEN_BRACE",
2566 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002567 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002568
Andy Whitcroft8905a672007-11-28 16:21:06 -08002569# open braces for enum, union and struct go on the same line.
2570 if ($line =~ /^.\s*{/ &&
2571 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002572 ERROR("OPEN_BRACE",
2573 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002574 }
2575
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002576# missing space after union, struct or enum definition
2577 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002578 WARN("SPACING",
2579 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002580 }
2581
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002582# check for spacing round square brackets; allowed:
2583# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002584# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2585# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002586 while ($line =~ /(.*?\s)\[/g) {
2587 my ($where, $prefix) = ($-[1], $1);
2588 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002589 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002590 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002591 ERROR("BRACKET_SPACE",
2592 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002593 }
2594 }
2595
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002596# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002597 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002598 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002599 my $ctx_before = substr($line, 0, $-[1]);
2600 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002601
2602 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002603 if ($name =~ /^(?:
2604 if|for|while|switch|return|case|
2605 volatile|__volatile__|
2606 __attribute__|format|__extension__|
2607 asm|__asm__)$/x)
2608 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002609
2610 # cpp #define statements have non-optional spaces, ie
2611 # if there is a space between the name and the open
2612 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002613 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002614
2615 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002616 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002617
2618 # If this whole things ends with a type its most
2619 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002620 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002621
2622 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07002623 WARN("SPACING",
2624 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002625 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002626 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002627# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002628 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002629 my $ops = qr{
2630 <<=|>>=|<=|>=|==|!=|
2631 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2632 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002633 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2634 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002635 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002636 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002637 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002638
2639 my $blank = copy_spacing($opline);
2640
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002641 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002642 $off += length($elements[$n]);
2643
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002644 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002645 my $ca = substr($opline, 0, $off);
2646 my $cc = '';
2647 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2648 $cc = substr($opline, $off + length($elements[$n + 1]));
2649 }
2650 my $cb = "$ca$;$cc";
2651
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002652 my $a = '';
2653 $a = 'V' if ($elements[$n] ne '');
2654 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002655 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002656 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2657 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002659
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002660 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002661
2662 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002663 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002664 $c = 'V' if ($elements[$n + 2] ne '');
2665 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002666 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002667 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2668 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002669 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002670 } else {
2671 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002672 }
2673
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002674 my $ctx = "${a}x${c}";
2675
2676 my $at = "(ctx:$ctx)";
2677
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002678 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002679 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002680
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002681 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002682 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002683
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002684 # Get the full operator variant.
2685 my $opv = $op . substr($curr_vars, $off, 1);
2686
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002687 # Ignore operators passed as parameters.
2688 if ($op_type ne 'V' &&
2689 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2690
Andy Whitcroftcf655042008-03-04 14:28:20 -08002691# # Ignore comments
2692# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002693
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002694 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002695 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002696 if ($ctx !~ /.x[WEBC]/ &&
2697 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002698 ERROR("SPACING",
2699 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002700 }
2701
2702 # // is a comment
2703 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002704
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002705 # No spaces for:
2706 # ->
2707 # : when part of a bitfield
2708 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002709 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002710 ERROR("SPACING",
2711 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002712 }
2713
2714 # , must have a space on the right.
2715 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002716 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002717 ERROR("SPACING",
2718 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002719 }
2720
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002721 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002722 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002723 #warn "'*' is part of type\n";
2724
2725 # unary operators should have a space before and
2726 # none after. May be left adjacent to another
2727 # unary operator, or a cast
2728 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002729 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002730 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002731 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002732 ERROR("SPACING",
2733 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002734 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002735 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002736 # A unary '*' may be const
2737
2738 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002739 ERROR("SPACING",
2740 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002741 }
2742
2743 # unary ++ and unary -- are allowed no space on one side.
2744 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002745 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002746 ERROR("SPACING",
2747 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002748 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002749 if ($ctx =~ /Wx[BE]/ ||
2750 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002751 ERROR("SPACING",
2752 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002753 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002754 if ($ctx =~ /ExW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002755 ERROR("SPACING",
2756 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002757 }
2758
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002759
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002760 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002761 } elsif ($op eq '<<' or $op eq '>>' or
2762 $op eq '&' or $op eq '^' or $op eq '|' or
2763 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002764 $op eq '*' or $op eq '/' or
2765 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002766 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002767 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002768 ERROR("SPACING",
2769 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002770 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002771 }
2772
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002773 # A colon needs no spaces before when it is
2774 # terminating a case value or a label.
2775 } elsif ($opv eq ':C' || $opv eq ':L') {
2776 if ($ctx =~ /Wx./) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002777 ERROR("SPACING",
2778 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002779 }
2780
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002781 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002782 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002783 my $ok = 0;
2784
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002785 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002786 if (($op eq '<' &&
2787 $cc =~ /^\S+\@\S+>/) ||
2788 ($op eq '>' &&
2789 $ca =~ /<\S+\@\S+$/))
2790 {
2791 $ok = 1;
2792 }
2793
2794 # Ignore ?:
2795 if (($opv eq ':O' && $ca =~ /\?$/) ||
2796 ($op eq '?' && $cc =~ /^:/)) {
2797 $ok = 1;
2798 }
2799
2800 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002801 ERROR("SPACING",
2802 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002803 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002804 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002805 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002806 }
2807 }
2808
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002809# check for multiple assignments
2810 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002811 CHK("MULTIPLE_ASSIGNMENTS",
2812 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002813 }
2814
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002815## # check for multiple declarations, allowing for a function declaration
2816## # continuation.
2817## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2818## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2819##
2820## # Remove any bracketed sections to ensure we do not
2821## # falsly report the parameters of functions.
2822## my $ln = $line;
2823## while ($ln =~ s/\([^\(\)]*\)//g) {
2824## }
2825## if ($ln =~ /,/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002826## WARN("MULTIPLE_DECLARATION",
2827## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002828## }
2829## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002830
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002831#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002832 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2833 $line =~ /do{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002834 ERROR("SPACING",
2835 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002836 }
2837
2838# closing brace should have a space following it when it has anything
2839# on the line
2840 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002841 ERROR("SPACING",
2842 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002843 }
2844
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002845# check spacing on square brackets
2846 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002847 ERROR("SPACING",
2848 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002849 }
2850 if ($line =~ /\s\]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002851 ERROR("SPACING",
2852 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002853 }
2854
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002855# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002856 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002857 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002858 ERROR("SPACING",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002859 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002860 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002861 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002862 $line !~ /for\s*\(.*;\s+\)/ &&
2863 $line !~ /:\s+\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002864 ERROR("SPACING",
2865 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002866 }
2867
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002868#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002869 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002870 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002871 WARN("INDENTED_LABEL",
2872 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002873 }
2874
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002875# Return is not a function.
2876 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2877 my $spacing = $1;
2878 my $value = $2;
2879
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002880 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002881 $value =~ s/\(/ \(/g;
2882 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002883 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002884 $value !~ /(?:$Ident|-?$Constant)\s*
2885 $Compare\s*
2886 (?:$Ident|-?$Constant)/x &&
2887 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002888 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002889#print "value<$value>\n";
2890 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002891 ERROR("RETURN_PARENTHESES",
2892 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002893
2894 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002895 ERROR("SPACING",
2896 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002897 }
2898 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002899# Return of what appears to be an errno should normally be -'ve
2900 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2901 my $name = $1;
2902 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002903 WARN("USE_NEGATIVE_ERRNO",
2904 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002905 }
2906 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002907
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002908# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002909 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002910 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002911 }
2912
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002913# Check for illegal assignment in if conditional -- and check for trailing
2914# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002915 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002916 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2917 ctx_statement_block($linenr, $realcnt, 0)
2918 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002919 my ($stat_next) = ctx_statement_block($line_nr_next,
2920 $remain_next, $off_next);
2921 $stat_next =~ s/\n./\n /g;
2922 ##print "stat<$stat> stat_next<$stat_next>\n";
2923
2924 if ($stat_next =~ /^\s*while\b/) {
2925 # If the statement carries leading newlines,
2926 # then count those as offsets.
2927 my ($whitespace) =
2928 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2929 my $offset =
2930 statement_rawlines($whitespace) - 1;
2931
2932 $suppress_whiletrailers{$line_nr_next +
2933 $offset} = 1;
2934 }
2935 }
2936 if (!defined $suppress_whiletrailers{$linenr} &&
2937 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002938 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002939
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002940 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002941 ERROR("ASSIGN_IN_IF",
2942 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002943 }
2944
2945 # Find out what is on the end of the line after the
2946 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002947 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002948 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002949 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002950 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2951 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002952 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002953 # Find out how long the conditional actually is.
2954 my @newlines = ($c =~ /\n/gs);
2955 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002956 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002957
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002958 $stat_real = raw_line($linenr, $cond_lines)
2959 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002960 if (defined($stat_real) && $cond_lines > 1) {
2961 $stat_real = "[...]\n$stat_real";
2962 }
2963
Joe Perches000d1cc2011-07-25 17:13:25 -07002964 ERROR("TRAILING_STATEMENTS",
2965 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002966 }
2967 }
2968
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002969# Check for bitwise tests written as boolean
2970 if ($line =~ /
2971 (?:
2972 (?:\[|\(|\&\&|\|\|)
2973 \s*0[xX][0-9]+\s*
2974 (?:\&\&|\|\|)
2975 |
2976 (?:\&\&|\|\|)
2977 \s*0[xX][0-9]+\s*
2978 (?:\&\&|\|\||\)|\])
2979 )/x)
2980 {
Joe Perches000d1cc2011-07-25 17:13:25 -07002981 WARN("HEXADECIMAL_BOOLEAN_TEST",
2982 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002983 }
2984
Andy Whitcroft8905a672007-11-28 16:21:06 -08002985# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002986 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2987 my $s = $1;
2988 $s =~ s/$;//g; # Remove any comments
2989 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002990 ERROR("TRAILING_STATEMENTS",
2991 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002992 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002993 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002994# if should not continue a brace
2995 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002996 ERROR("TRAILING_STATEMENTS",
2997 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08002998 $herecurr);
2999 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003000# case and default should not have general statements after them
3001 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3002 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003003 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003004 \s*return\s+
3005 )/xg)
3006 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003007 ERROR("TRAILING_STATEMENTS",
3008 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003009 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003010
3011 # Check for }<nl>else {, these must be at the same
3012 # indent level to be relevant to each other.
3013 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3014 $previndent == $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003015 ERROR("ELSE_AFTER_BRACE",
3016 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003017 }
3018
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003019 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3020 $previndent == $indent) {
3021 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3022
3023 # Find out what is on the end of the line after the
3024 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003025 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003026 $s =~ s/\n.*//g;
3027
3028 if ($s =~ /^\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003029 ERROR("WHILE_AFTER_BRACE",
3030 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003031 }
3032 }
3033
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003034#studly caps, commented out until figure out how to distinguish between use of existing and adding new
3035# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
3036# print "No studly caps, use _\n";
3037# print "$herecurr";
3038# $clean = 0;
3039# }
3040
3041#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003042 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003043 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3044 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003045 }
3046
Andy Whitcroft653d4872007-06-23 17:16:34 -07003047#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003048 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003049 my $file = "$1.h";
3050 my $checkfile = "include/linux/$file";
3051 if (-f "$root/$checkfile" &&
3052 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003053 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003054 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003055 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003056 CHK("ARCH_INCLUDE_LINUX",
3057 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003058 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003059 WARN("INCLUDE_LINUX",
3060 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003061 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003062 }
3063 }
3064
Andy Whitcroft653d4872007-06-23 17:16:34 -07003065# multi-statement macros should be enclosed in a do while loop, grab the
3066# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003067# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003068 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3069 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003070 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003071 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003072 my ($off, $dstat, $dcond, $rest);
3073 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003074 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003075 ctx_statement_block($linenr, $realcnt, 0);
3076 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003077 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003078 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003079
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003080 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003081 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003082 $dstat =~ s/\\\n.//g;
3083 $dstat =~ s/^\s*//s;
3084 $dstat =~ s/\s*$//s;
3085
3086 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003087 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3088 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003089 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003090 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003091 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003092
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003093 # Extremely long macros may fall off the end of the
3094 # available context without closing. Give a dangling
3095 # backslash the benefit of the doubt and allow it
3096 # to gobble any hanging open-parens.
3097 $dstat =~ s/\(.+\\$/1/;
3098
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003099 # Flatten any obvious string concatentation.
3100 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3101 $dstat =~ s/$Ident\s*("X*")/$1/)
3102 {
3103 }
3104
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003105 my $exceptions = qr{
3106 $Declare|
3107 module_param_named|
3108 MODULE_PARAM_DESC|
3109 DECLARE_PER_CPU|
3110 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003111 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003112 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003113 union|
3114 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003115 \.$Ident\s*=\s*|
3116 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003117 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003118 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003119 if ($dstat ne '' &&
3120 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3121 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Andy Whitcroftfd1b57a2012-03-23 15:02:18 -07003122 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003123 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003124 $dstat !~ /$exceptions/ &&
3125 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003126 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003127 $dstat !~ /^for\s*$Constant$/ && # for (...)
3128 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3129 $dstat !~ /^do\s*{/ && # do {...
3130 $dstat !~ /^\({/) # ({...
3131 {
3132 $ctx =~ s/\n*$//;
3133 my $herectx = $here . "\n";
3134 my $cnt = statement_rawlines($ctx);
3135
3136 for (my $n = 0; $n < $cnt; $n++) {
3137 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003138 }
3139
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003140 if ($dstat =~ /;/) {
3141 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3142 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3143 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003144 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003145 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003146 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003147 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003148 }
3149
Mike Frysinger080ba922009-01-06 14:41:25 -08003150# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3151# all assignments may have only one of the following with an assignment:
3152# .
3153# ALIGN(...)
3154# VMLINUX_SYMBOL(...)
3155 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003156 WARN("MISSING_VMLINUX_SYMBOL",
3157 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003158 }
3159
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003160# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003161 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3162 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003163 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003164 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003165 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3166 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003167 my @allowed = ();
3168 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003169 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003170 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003171 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003172 for my $chunk (@chunks) {
3173 my ($cond, $block) = @{$chunk};
3174
Andy Whitcroft773647a2008-03-28 14:15:58 -07003175 # If the condition carries leading newlines, then count those as offsets.
3176 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3177 my $offset = statement_rawlines($whitespace) - 1;
3178
Joe Perchesaad4f612012-03-23 15:02:19 -07003179 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003180 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3181
3182 # We have looked at and allowed this specific line.
3183 $suppress_ifbraces{$ln + $offset} = 1;
3184
3185 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003186 $ln += statement_rawlines($block) - 1;
3187
Andy Whitcroft773647a2008-03-28 14:15:58 -07003188 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003189
3190 $seen++ if ($block =~ /^\s*{/);
3191
Joe Perchesaad4f612012-03-23 15:02:19 -07003192 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003193 if (statement_lines($cond) > 1) {
3194 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003195 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003196 }
3197 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003198 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003199 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003200 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003201 if (statement_block_size($block) > 1) {
3202 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003203 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003204 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003205 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003206 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003207 if ($seen) {
3208 my $sum_allowed = 0;
3209 foreach (@allowed) {
3210 $sum_allowed += $_;
3211 }
3212 if ($sum_allowed == 0) {
3213 WARN("BRACES",
3214 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3215 } elsif ($sum_allowed != $allow &&
3216 $seen != $allow) {
3217 CHK("BRACES",
3218 "braces {} should be used on all arms of this statement\n" . $herectx);
3219 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003220 }
3221 }
3222 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003223 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003224 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003225 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003226
Andy Whitcroftcf655042008-03-04 14:28:20 -08003227 # Check the pre-context.
3228 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3229 #print "APW: ALLOWED: pre<$1>\n";
3230 $allowed = 1;
3231 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003232
3233 my ($level, $endln, @chunks) =
3234 ctx_statement_full($linenr, $realcnt, $-[0]);
3235
Andy Whitcroftcf655042008-03-04 14:28:20 -08003236 # Check the condition.
3237 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003238 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003239 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003240 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003241 }
3242 if (statement_lines($cond) > 1) {
3243 #print "APW: ALLOWED: cond<$cond>\n";
3244 $allowed = 1;
3245 }
3246 if ($block =~/\b(?:if|for|while)\b/) {
3247 #print "APW: ALLOWED: block<$block>\n";
3248 $allowed = 1;
3249 }
3250 if (statement_block_size($block) > 1) {
3251 #print "APW: ALLOWED: lines block<$block>\n";
3252 $allowed = 1;
3253 }
3254 # Check the post-context.
3255 if (defined $chunks[1]) {
3256 my ($cond, $block) = @{$chunks[1]};
3257 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003258 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003259 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003260 if ($block =~ /^\s*\{/) {
3261 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3262 $allowed = 1;
3263 }
3264 }
3265 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003266 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003267 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003268
Andy Whitcroftf0556632008-10-15 22:02:23 -07003269 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003270 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003271 }
3272
Joe Perches000d1cc2011-07-25 17:13:25 -07003273 WARN("BRACES",
3274 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003275 }
3276 }
3277
Andy Whitcroft653d4872007-06-23 17:16:34 -07003278# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003279 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003280 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003281 ERROR("DEPRECATED_INCLUDE",
3282 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003283 }
3284 }
3285
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003286# don't use deprecated functions
3287 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003288 if ($line =~ /\b$func\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003289 ERROR("DEPRECATED_FUNCTION",
3290 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003291 }
3292 }
3293
3294# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003295 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3296 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003297 WARN("VOLATILE",
3298 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003299 }
3300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003301# sys_open/read/write/close are not allowed in the kernel
3302 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003303 ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003304 $herecurr);
3305 }
3306
3307# filp_open is a backdoor for sys_open
3308 if ($line =~ /\b(filp_open)\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003309 ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003310 $herecurr);
3311 }
3312
3313# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
3314 if ($line =~ /\b((?:read|write)[bwl])\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003315 ERROR("NON_RELAXED_IO",
3316 "Use of $1 is deprecated: use $1_relaxed\n\t" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003317 "with appropriate memory barriers instead.\n" .
3318 $herecurr);
3319 }
3320
3321# likewise, in/out[bwl] should be __raw_read/write[bwl]...
3322 if ($line =~ /\b((in|out)([bwl]))\b/) {
3323 my ($all, $pref, $suf) = ($1, $2, $3);
3324 $pref =~ s/in/read/;
3325 $pref =~ s/out/write/;
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003326 ERROR("NON_RELAXED_IO",
3327 "Use of $all is deprecated: use " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003328 "__raw_$pref$suf\n\t" .
3329 "with appropriate memory barriers instead.\n" .
3330 $herecurr);
3331 }
3332
3333# dsb is too ARMish, and should usually be mb.
3334 if ($line =~ /\bdsb\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003335 WARN("ARM_BARRIER",
3336 "Use of dsb is discouranged: prefer mb.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003337 $herecurr);
3338 }
3339
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08003340# MSM - check if a non board-gpiomux file has any gpiomux declarations
3341 if ($realfile =~ /\/mach-msm\/board-[0-9]+/ &&
3342 $realfile !~ /camera/ && $realfile !~ /gpiomux/ &&
3343 $line =~ /\s*struct msm_gpiomux_config\s*/ ) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003344 WARN("GPIOMUX_IN_BOARD",
3345 "Non gpiomux board file cannot have a gpiomux config declarations. Please declare gpiomux configs in board-*-gpiomux.c file.\n" . $herecurr);
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08003346 }
3347
Pankaj Kumaradfb9332012-01-06 15:02:19 +05303348# MSM - check if vreg_xxx function are used
3349 if ($line =~ /\b(vreg_(get|put|set_level|enable|disable))\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003350 WARN("DEPRECATED_VREG_APIS", "Use of $1 API is deprecated: " .
Pankaj Kumaradfb9332012-01-06 15:02:19 +05303351 "use regulator APIs\n" . $herecurr);
3352 }
3353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003354# unbounded string functions are overflow risks
3355 my %str_fns = (
3356 "sprintf" => "snprintf",
Olav Haugan665be0d2012-02-06 09:42:18 -08003357 "strcpy" => "strlcpy",
3358 "strncpy" => "strlcpy",
3359 "strcat" => "strlcat",
3360 "strncat" => "strlcat",
3361 "vsprintf" => "vsnprintf",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003362 "strchr" => "strnchr",
3363 "strstr" => "strnstr",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003364 );
3365 foreach my $k (keys %str_fns) {
3366 if ($line =~ /\b$k\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003367 ERROR("UNBOUNDED_STRING_FNS",
3368 "Use of $k is deprecated: " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003369 "use $str_fns{$k} instead.\n" .
3370 $herecurr);
3371 }
3372 }
3373
Andy Whitcroft00df3442007-06-08 13:47:06 -07003374# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003375 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003376 WARN("IF_0",
3377 "if this code is redundant consider removing it\n"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003378 . $herecurr);
3379 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07003380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003381# warn about #if 1
3382 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003383 WARN("IF_1",
3384 "if this code is required consider removing"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003385 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003386 }
3387
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003388# check for needless kfree() checks
3389 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3390 my $expr = $1;
3391 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003392 WARN("NEEDLESS_KFREE",
3393 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003394 }
3395 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003396# check for needless usb_free_urb() checks
3397 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3398 my $expr = $1;
3399 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003400 WARN("NEEDLESS_USB_FREE_URB",
3401 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003402 }
3403 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003404
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003405# prefer usleep_range over udelay
3406 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3407 # ignore udelay's < 10, however
3408 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003409 CHK("USLEEP_RANGE",
3410 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003411 }
3412 }
3413
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003414# warn about unexpectedly long msleep's
3415 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3416 if ($1 < 20) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003417 WARN("MSLEEP",
3418 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003419 }
3420 }
3421
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003422# check the patch for use of mdelay
3423 if ($line =~ /\bmdelay\s*\(/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003424 WARN("MDELAY",
3425 "use of mdelay() found: msleep() is the preferred API.\n" . $line );
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003426 }
3427
Andy Whitcroft00df3442007-06-08 13:47:06 -07003428# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003429# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003430# print "#ifdef in C files should be avoided\n";
3431# print "$herecurr";
3432# $clean = 0;
3433# }
3434
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003435# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003436 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003437 ERROR("SPACING",
3438 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003439 }
3440
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003441# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003442 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3443 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003444 my $which = $1;
3445 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003446 CHK("UNCOMMENTED_DEFINITION",
3447 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003448 }
3449 }
3450# check for memory barriers without a comment.
3451 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3452 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003453 CHK("MEMORY_BARRIER",
3454 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003455 }
3456 }
3457# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003458 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003459 CHK("ARCH_DEFINES",
3460 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003461 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003462
Tobias Klauserd4977c72010-05-24 14:33:30 -07003463# Check that the storage class is at the beginning of a declaration
3464 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003465 WARN("STORAGE_CLASS",
3466 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003467 }
3468
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003469# check the location of the inline attribute, that it is between
3470# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003471 if ($line =~ /\b$Type\s+$Inline\b/ ||
3472 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003473 ERROR("INLINE_LOCATION",
3474 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003475 }
3476
Andy Whitcroft8905a672007-11-28 16:21:06 -08003477# Check for __inline__ and __inline, prefer inline
3478 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003479 WARN("INLINE",
3480 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003481 }
3482
Joe Perches3d130fd2011-01-12 17:00:00 -08003483# Check for __attribute__ packed, prefer __packed
3484 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003485 WARN("PREFER_PACKED",
3486 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003487 }
3488
Joe Perches39b7e282011-07-25 17:13:24 -07003489# Check for __attribute__ aligned, prefer __aligned
3490 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003491 WARN("PREFER_ALIGNED",
3492 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003493 }
3494
Joe Perches5f14d3b2012-01-10 15:09:52 -08003495# Check for __attribute__ format(printf, prefer __printf
3496 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3497 WARN("PREFER_PRINTF",
3498 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3499 }
3500
Joe Perches6061d942012-03-23 15:02:16 -07003501# Check for __attribute__ format(scanf, prefer __scanf
3502 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3503 WARN("PREFER_SCANF",
3504 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3505 }
3506
Joe Perches8f53a9b2010-03-05 13:43:48 -08003507# check for sizeof(&)
3508 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003509 WARN("SIZEOF_ADDRESS",
3510 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003511 }
3512
Joe Perches428e2fd2011-05-24 17:13:39 -07003513# check for line continuations in quoted strings with odd counts of "
3514 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003515 WARN("LINE_CONTINUATIONS",
3516 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003517 }
3518
Andy Whitcroft554e1652012-01-10 15:09:57 -08003519# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003520 if ($^V && $^V ge 5.10.0 &&
3521 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003522 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003523
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003524 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003525 my $ms_val = $7;
3526 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003527
Andy Whitcroft554e1652012-01-10 15:09:57 -08003528 if ($ms_size =~ /^(0x|)0$/i) {
3529 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003530 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003531 } elsif ($ms_size =~ /^(0x|)1$/i) {
3532 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003533 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3534 }
3535 }
3536
3537# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003538 if ($^V && $^V ge 5.10.0 &&
3539 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003540 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003541 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003542 my $call = $1;
3543 my $cast1 = deparenthesize($2);
3544 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003545 my $cast2 = deparenthesize($7);
3546 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003547 my $cast;
3548
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003549 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003550 $cast = "$cast1 or $cast2";
3551 } elsif ($cast1 ne "") {
3552 $cast = $cast1;
3553 } else {
3554 $cast = $cast2;
3555 }
3556 WARN("MINMAX",
3557 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003558 }
3559 }
3560
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003561# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003562 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003563 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003564 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003565 my $function_name = $1;
3566 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003567
3568 my $s = $stat;
3569 if (defined $cond) {
3570 substr($s, 0, length($cond), '');
3571 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003572 if ($s =~ /^\s*;/ &&
3573 $function_name ne 'uninitialized_var')
3574 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003575 WARN("AVOID_EXTERNS",
3576 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003577 }
3578
3579 if ($paren_space =~ /\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003580 WARN("FUNCTION_ARGUMENTS",
3581 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003582 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003583
3584 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3585 $stat =~ /^.\s*extern\s+/)
3586 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003587 WARN("AVOID_EXTERNS",
3588 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003589 }
3590
3591# checks for new __setup's
3592 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3593 my $name = $1;
3594
3595 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003596 CHK("UNDOCUMENTED_SETUP",
3597 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003598 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003599 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003600
3601# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003602 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003603 WARN("UNNECESSARY_CASTS",
3604 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003605 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003606
Joe Perchescaf2a542011-01-12 16:59:56 -08003607# check for multiple semicolons
3608 if ($line =~ /;\s*;\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003609 WARN("ONE_SEMICOLON",
3610 "Statements terminations use 1 semicolon\n" . $herecurr);
Joe Perchescaf2a542011-01-12 16:59:56 -08003611 }
3612
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003613# check for return codes on error paths
3614 if ($line =~ /\breturn\s+-\d+/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003615 ERROR("NO_ERROR_CODE",
3616 "illegal return value, please use an error code\n" . $herecurr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003617 }
3618
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003619# check for gcc specific __FUNCTION__
3620 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003621 WARN("USE_FUNC",
3622 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003623 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003624
Joe Perches2c924882012-03-23 15:02:20 -07003625# check for use of yield()
3626 if ($line =~ /\byield\s*\(\s*\)/) {
3627 WARN("YIELD",
3628 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3629 }
3630
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003631# check for semaphores initialized locked
3632 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003633 WARN("CONSIDER_COMPLETION",
3634 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003635 }
Joe Perches6712d852012-03-23 15:02:20 -07003636
Joe Perches67d0a072011-10-31 17:13:10 -07003637# recommend kstrto* over simple_strto* and strict_strto*
3638 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003639 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003640 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003641 }
Joe Perches6712d852012-03-23 15:02:20 -07003642
Michael Ellermanf3db6632008-07-23 21:28:57 -07003643# check for __initcall(), use device_initcall() explicitly please
3644 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003645 WARN("USE_DEVICE_INITCALL",
3646 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003647 }
Joe Perches6712d852012-03-23 15:02:20 -07003648
Emese Revfy79404842010-03-05 13:43:53 -08003649# check for various ops structs, ensure they are const.
3650 my $struct_ops = qr{acpi_dock_ops|
3651 address_space_operations|
3652 backlight_ops|
3653 block_device_operations|
3654 dentry_operations|
3655 dev_pm_ops|
3656 dma_map_ops|
3657 extent_io_ops|
3658 file_lock_operations|
3659 file_operations|
3660 hv_ops|
3661 ide_dma_ops|
3662 intel_dvo_dev_ops|
3663 item_operations|
3664 iwl_ops|
3665 kgdb_arch|
3666 kgdb_io|
3667 kset_uevent_ops|
3668 lock_manager_operations|
3669 microcode_ops|
3670 mtrr_ops|
3671 neigh_ops|
3672 nlmsvc_binding|
3673 pci_raw_ops|
3674 pipe_buf_operations|
3675 platform_hibernation_ops|
3676 platform_suspend_ops|
3677 proto_ops|
3678 rpc_pipe_ops|
3679 seq_operations|
3680 snd_ac97_build_ops|
3681 soc_pcmcia_socket_ops|
3682 stacktrace_ops|
3683 sysfs_ops|
3684 tty_operations|
3685 usb_mon_operations|
3686 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003687 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003688 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003689 WARN("CONST_STRUCT",
3690 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003691 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003692 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003693
3694# use of NR_CPUS is usually wrong
3695# ignore definitions of NR_CPUS and usage to define arrays as likely right
3696 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003697 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3698 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003699 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3700 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3701 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003702 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003703 WARN("NR_CPUS",
3704 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003705 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003706
3707# check for %L{u,d,i} in strings
3708 my $string;
3709 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3710 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003711 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003712 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003713 WARN("PRINTF_L",
3714 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003715 last;
3716 }
3717 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003718
3719# whine mightly about in_atomic
3720 if ($line =~ /\bin_atomic\s*\(/) {
3721 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003722 ERROR("IN_ATOMIC",
3723 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003724 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003725 WARN("IN_ATOMIC",
3726 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003727 }
3728 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003729
3730# check for lockdep_set_novalidate_class
3731 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3732 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3733 if ($realfile !~ m@^kernel/lockdep@ &&
3734 $realfile !~ m@^include/linux/lockdep@ &&
3735 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003736 ERROR("LOCKDEP",
3737 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003738 }
3739 }
Dave Jones88f88312011-01-12 16:59:59 -08003740
3741 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3742 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003743 WARN("EXPORTED_WORLD_WRITABLE",
3744 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003745 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003746 }
3747
3748 # If we have no input at all, then there is nothing to report on
3749 # so just keep quiet.
3750 if ($#rawlines == -1) {
3751 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003752 }
3753
Andy Whitcroft8905a672007-11-28 16:21:06 -08003754 # In mailback mode only produce a report in the negative, for
3755 # things that appear to be patches.
3756 if ($mailback && ($clean == 1 || !$is_patch)) {
3757 exit(0);
3758 }
3759
3760 # This is not a patch, and we are are in 'no-patch' mode so
3761 # just keep quiet.
3762 if (!$chk_patch && !$is_patch) {
3763 exit(0);
3764 }
3765
3766 if (!$is_patch) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003767 ERROR("NOT_UNIFIED_DIFF",
3768 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003769 }
3770 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003771 ERROR("MISSING_SIGN_OFF",
3772 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003773 }
3774
Andy Whitcroft8905a672007-11-28 16:21:06 -08003775 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003776 if ($summary && !($clean == 1 && $quiet == 1)) {
3777 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003778 print "total: $cnt_error errors, $cnt_warn warnings, " .
3779 (($check)? "$cnt_chk checks, " : "") .
3780 "$cnt_lines lines checked\n";
3781 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003782 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003783
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003784 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003785
3786 if ($^V lt 5.10.0) {
3787 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3788 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3789 }
3790
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003791 # If there were whitespace errors which cleanpatch can fix
3792 # then suggest that.
3793 if ($rpt_cleaners) {
3794 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3795 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003796 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003797 }
3798 }
3799
Artem Bityutskiy11232682012-03-23 15:02:17 -07003800 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003801 print "NOTE: Ignored message types:";
3802 foreach my $ignore (sort keys %ignore_type) {
3803 print " $ignore";
3804 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003805 print "\n\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07003806 }
3807
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003808 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003809 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003810 }
3811 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003812 print << "EOM";
3813$vname has style problems, please review.
3814
3815If any of these errors are false positives, please report
3816them to the maintainer, see CHECKPATCH in MAINTAINERS.
3817EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003818 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003819
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003820 return $clean;
3821}