blob: 58036487c67800216917ec962dc12c6ca3b46a43 [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
Steve Muckle8e7509d2009-03-13 15:46:36 -070010use constant BEFORE_SHORTTEXT => 0;
11use constant IN_SHORTTEXT => 1;
12use constant AFTER_SHORTTEXT => 2;
Richard Adamsb548e3e2010-01-04 13:06:09 -080013use constant CHECK_NEXT_SHORTTEXT => 3;
Steve Muckle8e7509d2009-03-13 15:46:36 -070014use constant SHORTTEXT_LIMIT => 75;
15
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070017$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070018
Joe Perches000d1cc2011-07-25 17:13:25 -070019my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070020
21use Getopt::Long qw(:config no_auto_abbrev);
22
23my $quiet = 0;
24my $tree = 1;
25my $chk_signoff = 1;
26my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070027my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070028my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080029my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $file = 0;
31my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080032my $summary = 1;
33my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080034my $summary_file = 0;
Joe Perches000d1cc2011-07-25 17:13:25 -070035my $show_types = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches000d1cc2011-07-25 17:13:25 -070038my %ignore_type = ();
39my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070040my $help = 0;
Joe Perches000d1cc2011-07-25 17:13:25 -070041my $configuration_file = ".checkpatch.conf";
Hannes Eder77f5b102009-09-21 17:04:37 -070042
43sub help {
44 my ($exitcode) = @_;
45
46 print << "EOM";
47Usage: $P [OPTION]... [FILE]...
48Version: $V
49
50Options:
51 -q, --quiet quiet
52 --no-tree run without a kernel tree
53 --no-signoff do not check for 'Signed-off-by' line
54 --patch treat FILE as patchfile (default)
55 --emacs emacs compile window format
56 --terse one line per report
57 -f, --file treat FILE as regular source file
58 --subjective, --strict enable more subjective tests
Joe Perches000d1cc2011-07-25 17:13:25 -070059 --ignore TYPE(,TYPE2...) ignore various comma separated message types
60 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070061 --root=PATH PATH to the kernel tree root
62 --no-summary suppress the per-file summary
63 --mailback only produce a report in case of warnings/errors
64 --summary-file include the filename in summary
65 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
66 'values', 'possible', 'type', and 'attr' (default
67 is all off)
68 --test-only=WORD report only warnings/errors containing WORD
69 literally
70 -h, --help, --version display this help and exit
71
72When FILE is - read standard input.
73EOM
74
75 exit($exitcode);
76}
77
Joe Perches000d1cc2011-07-25 17:13:25 -070078my $conf = which_conf($configuration_file);
79if (-f $conf) {
80 my @conf_args;
81 open(my $conffile, '<', "$conf")
82 or warn "$P: Can't find a readable $configuration_file file $!\n";
83
84 while (<$conffile>) {
85 my $line = $_;
86
87 $line =~ s/\s*\n?$//g;
88 $line =~ s/^\s*//g;
89 $line =~ s/\s+/ /g;
90
91 next if ($line =~ m/^\s*#/);
92 next if ($line =~ m/^\s*$/);
93
94 my @words = split(" ", $line);
95 foreach my $word (@words) {
96 last if ($word =~ m/^#/);
97 push (@conf_args, $word);
98 }
99 }
100 close($conffile);
101 unshift(@ARGV, @conf_args) if @conf_args;
102}
103
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700104GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700105 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700106 'tree!' => \$tree,
107 'signoff!' => \$chk_signoff,
108 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700109 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800110 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700111 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700112 'subjective!' => \$check,
113 'strict!' => \$check,
Joe Perches000d1cc2011-07-25 17:13:25 -0700114 'ignore=s' => \@ignore,
115 'show-types!' => \$show_types,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700116 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800117 'summary!' => \$summary,
118 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800119 'summary-file!' => \$summary_file,
120
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800121 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700122 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700123 'h|help' => \$help,
124 'version' => \$help
125) or help(1);
126
127help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700128
129my $exit = 0;
130
131if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700132 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700133 exit(1);
134}
135
Joe Perches000d1cc2011-07-25 17:13:25 -0700136@ignore = split(/,/, join(',',@ignore));
137foreach my $word (@ignore) {
138 $word =~ s/\s*\n?$//g;
139 $word =~ s/^\s*//g;
140 $word =~ s/\s+/ /g;
141 $word =~ tr/[a-z]/[A-Z]/;
142
143 next if ($word =~ m/^\s*#/);
144 next if ($word =~ m/^\s*$/);
145
146 $ignore_type{$word}++;
147}
148
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800149my $dbg_values = 0;
150my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700151my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700152my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800153for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800154 ## no critic
155 eval "\${dbg_$key} = '$debug{$key}';";
156 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800157}
158
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700159my $rpt_cleaners = 0;
160
Andy Whitcroft8905a672007-11-28 16:21:06 -0800161if ($terse) {
162 $emacs = 1;
163 $quiet++;
164}
165
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700166if ($tree) {
167 if (defined $root) {
168 if (!top_of_kernel_tree($root)) {
169 die "$P: $root: --root does not point at a valid tree\n";
170 }
171 } else {
172 if (top_of_kernel_tree('.')) {
173 $root = '.';
174 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
175 top_of_kernel_tree($1)) {
176 $root = $1;
177 }
178 }
179
180 if (!defined $root) {
181 print "Must be run from the top-level dir. of a kernel tree\n";
182 exit(2);
183 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700184}
185
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700186my $emitted_corrupt = 0;
187
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700188our $Ident = qr{
189 [A-Za-z_][A-Za-z\d_]*
190 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
191 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700192our $Storage = qr{extern|static|asmlinkage};
193our $Sparse = qr{
194 __user|
195 __kernel|
196 __force|
197 __iomem|
198 __must_check|
199 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800200 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700201 __ref|
202 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800204
205# Notes to $Attribute:
206# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207our $Attribute = qr{
208 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700209 __percpu|
210 __nocast|
211 __safe|
212 __bitwise__|
213 __packed__|
214 __packed2__|
215 __naked|
216 __maybe_unused|
217 __always_unused|
218 __noreturn|
219 __used|
220 __cold|
221 __noclone|
222 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700223 __read_mostly|
224 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800225 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700226 ____cacheline_aligned|
227 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800228 ____cacheline_internodealigned_in_smp|
229 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700230 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700231our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700232our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
234our $Lval = qr{$Ident(?:$Member)*};
235
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800236our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700237our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800238our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700239our $Operators = qr{
240 <=|>=|==|!=|
241 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800242 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700243 }x;
244
Andy Whitcroft8905a672007-11-28 16:21:06 -0800245our $NonptrType;
246our $Type;
247our $Declare;
248
Joe Perches15662b32011-10-31 17:13:12 -0700249our $NON_ASCII_UTF8 = qr{
250 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700251 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
252 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
253 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
254 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
255 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
256 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
257}x;
258
Joe Perches15662b32011-10-31 17:13:12 -0700259our $UTF8 = qr{
260 [\x09\x0A\x0D\x20-\x7E] # ASCII
261 | $NON_ASCII_UTF8
262}x;
263
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700264our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700265 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700266 atomic_t
267)};
268
Joe Perches691e6692010-03-05 13:43:51 -0800269our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700270 printk(?:_ratelimited|_once|)|
271 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
272 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700273 panic|
274 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800275)};
276
Joe Perches20112472011-07-25 17:13:23 -0700277our $signature_tags = qr{(?xi:
278 Signed-off-by:|
279 Acked-by:|
280 Tested-by:|
281 Reviewed-by:|
282 Reported-by:|
283 To:|
284 Cc:
285)};
286
Andy Whitcroft8905a672007-11-28 16:21:06 -0800287our @typeList = (
288 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700289 qr{(?:unsigned\s+)?char},
290 qr{(?:unsigned\s+)?short},
291 qr{(?:unsigned\s+)?int},
292 qr{(?:unsigned\s+)?long},
293 qr{(?:unsigned\s+)?long\s+int},
294 qr{(?:unsigned\s+)?long\s+long},
295 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800296 qr{unsigned},
297 qr{float},
298 qr{double},
299 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800300 qr{struct\s+$Ident},
301 qr{union\s+$Ident},
302 qr{enum\s+$Ident},
303 qr{${Ident}_t},
304 qr{${Ident}_handler},
305 qr{${Ident}_handler_fn},
306);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700307our @modifierList = (
308 qr{fastcall},
309);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800310
Wolfram Sang7840a942010-08-09 17:20:57 -0700311our $allowed_asm_includes = qr{(?x:
312 irq|
313 memory
314)};
315# memory.h: ARM has a custom one
316
Andy Whitcroft8905a672007-11-28 16:21:06 -0800317sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700318 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
319 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700320 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800321 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700322 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800323 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800324 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700325 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700326 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800327 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700328 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800329 }x;
330 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700331 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700332 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700333 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334 }x;
335 $Declare = qr{(?:$Storage\s+)?$Type};
336}
337build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700338
Joe Perches7d2367a2011-07-25 17:13:22 -0700339
340our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700341
342# Using $balanced_parens, $LvalOrFunc, or $FuncArg
343# requires at least perl version v5.10.0
344# Any use must be runtime checked with $^V
345
346our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
347our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800348our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700349
350sub deparenthesize {
351 my ($string) = @_;
352 return "" if (!defined($string));
353 $string =~ s@^\s*\(\s*@@g;
354 $string =~ s@\s*\)\s*$@@g;
355 $string =~ s@\s+@ @g;
356 return $string;
357}
358
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700359$chk_signoff = 0 if ($file);
360
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700361my @dep_includes = ();
362my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363my $removal = "Documentation/feature-removal-schedule.txt";
364if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800365 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700366 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800367 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700368 if (/^Check:\s+(.*\S)/) {
369 for my $entry (split(/[, ]+/, $1)) {
370 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700371 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700372
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700373 } elsif ($entry !~ m@/@) {
374 push(@dep_functions, $entry);
375 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700376 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700377 }
378 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800379 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700380}
381
Andy Whitcroft00df3442007-06-08 13:47:06 -0700382my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800383my @lines = ();
384my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700385for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800386 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800388 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800390 } elsif ($filename eq '-') {
391 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800393 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700394 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700395 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800396 if ($filename eq '-') {
397 $vname = 'Your patch';
398 } else {
399 $vname = $filename;
400 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800401 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 chomp;
403 push(@rawlines, $_);
404 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800405 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800406 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700407 $exit = 1;
408 }
409 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800410 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700411}
412
413exit($exit);
414
415sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700416 my ($root) = @_;
417
418 my @tree_check = (
419 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
420 "README", "Documentation", "arch", "include", "drivers",
421 "fs", "init", "ipc", "kernel", "lib", "scripts",
422 );
423
424 foreach my $check (@tree_check) {
425 if (! -e $root . '/' . $check) {
426 return 0;
427 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700428 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700429 return 1;
Joe Perches000d1cc2011-07-25 17:13:25 -0700430 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700431
Joe Perches20112472011-07-25 17:13:23 -0700432sub parse_email {
433 my ($formatted_email) = @_;
434
435 my $name = "";
436 my $address = "";
437 my $comment = "";
438
439 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
440 $name = $1;
441 $address = $2;
442 $comment = $3 if defined $3;
443 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
444 $address = $1;
445 $comment = $2 if defined $2;
446 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
447 $address = $1;
448 $comment = $2 if defined $2;
449 $formatted_email =~ s/$address.*$//;
450 $name = $formatted_email;
451 $name =~ s/^\s+|\s+$//g;
452 $name =~ s/^\"|\"$//g;
453 # If there's a name left after stripping spaces and
454 # leading quotes, and the address doesn't have both
455 # leading and trailing angle brackets, the address
456 # is invalid. ie:
457 # "joe smith joe@smith.com" bad
458 # "joe smith <joe@smith.com" bad
459 if ($name ne "" && $address !~ /^<[^>]+>$/) {
460 $name = "";
461 $address = "";
462 $comment = "";
463 }
464 }
465
466 $name =~ s/^\s+|\s+$//g;
467 $name =~ s/^\"|\"$//g;
468 $address =~ s/^\s+|\s+$//g;
469 $address =~ s/^\<|\>$//g;
470
471 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
472 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
473 $name = "\"$name\"";
474 }
475
476 return ($name, $address, $comment);
477}
478
479sub format_email {
480 my ($name, $address) = @_;
481
482 my $formatted_email;
483
484 $name =~ s/^\s+|\s+$//g;
485 $name =~ s/^\"|\"$//g;
486 $address =~ s/^\s+|\s+$//g;
487
488 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
489 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
490 $name = "\"$name\"";
491 }
492
493 if ("$name" eq "") {
494 $formatted_email = "$address";
495 } else {
496 $formatted_email = "$name <$address>";
497 }
498
499 return $formatted_email;
500}
501
Joe Perches000d1cc2011-07-25 17:13:25 -0700502sub which_conf {
503 my ($conf) = @_;
504
505 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
506 if (-e "$path/$conf") {
507 return "$path/$conf";
508 }
509 }
510
511 return "";
512}
513
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700514sub expand_tabs {
515 my ($str) = @_;
516
517 my $res = '';
518 my $n = 0;
519 for my $c (split(//, $str)) {
520 if ($c eq "\t") {
521 $res .= ' ';
522 $n++;
523 for (; ($n % 8) != 0; $n++) {
524 $res .= ' ';
525 }
526 next;
527 }
528 $res .= $c;
529 $n++;
530 }
531
532 return $res;
533}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700534sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700535 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700536 return $res;
537}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700538
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700539sub line_stats {
540 my ($line) = @_;
541
542 # Drop the diff line leader and expand tabs
543 $line =~ s/^.//;
544 $line = expand_tabs($line);
545
546 # Pick the indent from the front of the line.
547 my ($white) = ($line =~ /^(\s*)/);
548
549 return (length($line), length($white));
550}
551
Andy Whitcroft773647a2008-03-28 14:15:58 -0700552my $sanitise_quote = '';
553
554sub sanitise_line_reset {
555 my ($in_comment) = @_;
556
557 if ($in_comment) {
558 $sanitise_quote = '*/';
559 } else {
560 $sanitise_quote = '';
561 }
562}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700563sub sanitise_line {
564 my ($line) = @_;
565
566 my $res = '';
567 my $l = '';
568
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800569 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700570 my $off = 0;
571 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700572
Andy Whitcroft773647a2008-03-28 14:15:58 -0700573 # Always copy over the diff marker.
574 $res = substr($line, 0, 1);
575
576 for ($off = 1; $off < length($line); $off++) {
577 $c = substr($line, $off, 1);
578
579 # Comments we are wacking completly including the begin
580 # and end, all to $;.
581 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
582 $sanitise_quote = '*/';
583
584 substr($res, $off, 2, "$;$;");
585 $off++;
586 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800587 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700588 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700589 $sanitise_quote = '';
590 substr($res, $off, 2, "$;$;");
591 $off++;
592 next;
593 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700594 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
595 $sanitise_quote = '//';
596
597 substr($res, $off, 2, $sanitise_quote);
598 $off++;
599 next;
600 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700601
602 # A \ in a string means ignore the next character.
603 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
604 $c eq "\\") {
605 substr($res, $off, 2, 'XX');
606 $off++;
607 next;
608 }
609 # Regular quotes.
610 if ($c eq "'" || $c eq '"') {
611 if ($sanitise_quote eq '') {
612 $sanitise_quote = $c;
613
614 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700615 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700616 } elsif ($sanitise_quote eq $c) {
617 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700618 }
619 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700620
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800621 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700622 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
623 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700624 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
625 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700626 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
627 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700628 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700629 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700630 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800631 }
632
Daniel Walker113f04a2009-09-21 17:04:35 -0700633 if ($sanitise_quote eq '//') {
634 $sanitise_quote = '';
635 }
636
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800637 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700638 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800639 my $clean = 'X' x length($1);
640 $res =~ s@\<.*\>@<$clean>@;
641
642 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700643 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800644 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700645 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800646 }
647
Andy Whitcroft00df3442007-06-08 13:47:06 -0700648 return $res;
649}
650
Andy Whitcroft8905a672007-11-28 16:21:06 -0800651sub ctx_statement_block {
652 my ($linenr, $remain, $off) = @_;
653 my $line = $linenr - 1;
654 my $blk = '';
655 my $soff = $off;
656 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700657 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800658
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800659 my $loff = 0;
660
Andy Whitcroft8905a672007-11-28 16:21:06 -0800661 my $type = '';
662 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800663 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800664 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800665 my $c;
666 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800667
668 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800669 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800670 @stack = (['', 0]) if ($#stack == -1);
671
Andy Whitcroft773647a2008-03-28 14:15:58 -0700672 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800673 # If we are about to drop off the end, pull in more
674 # context.
675 if ($off >= $len) {
676 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700677 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800678 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800679 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800680 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800681 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800682 $len = length($blk);
683 $line++;
684 last;
685 }
686 # Bail if there is no further context.
687 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800688 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800689 last;
690 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800691 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
692 $level++;
693 $type = '#';
694 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800695 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800696 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800697 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800698 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800699
Andy Whitcroft773647a2008-03-28 14:15:58 -0700700 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800701
702 # Handle nested #if/#else.
703 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
704 push(@stack, [ $type, $level ]);
705 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
706 ($type, $level) = @{$stack[$#stack - 1]};
707 } elsif ($remainder =~ /^#\s*endif\b/) {
708 ($type, $level) = @{pop(@stack)};
709 }
710
Andy Whitcroft8905a672007-11-28 16:21:06 -0800711 # Statement ends at the ';' or a close '}' at the
712 # outermost level.
713 if ($level == 0 && $c eq ';') {
714 last;
715 }
716
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800717 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700718 if ($level == 0 && $coff_set == 0 &&
719 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
720 $remainder =~ /^(else)(?:\s|{)/ &&
721 $remainder !~ /^else\s+if\b/) {
722 $coff = $off + length($1) - 1;
723 $coff_set = 1;
724 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
725 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800726 }
727
Andy Whitcroft8905a672007-11-28 16:21:06 -0800728 if (($type eq '' || $type eq '(') && $c eq '(') {
729 $level++;
730 $type = '(';
731 }
732 if ($type eq '(' && $c eq ')') {
733 $level--;
734 $type = ($level != 0)? '(' : '';
735
736 if ($level == 0 && $coff < $soff) {
737 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700738 $coff_set = 1;
739 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800740 }
741 }
742 if (($type eq '' || $type eq '{') && $c eq '{') {
743 $level++;
744 $type = '{';
745 }
746 if ($type eq '{' && $c eq '}') {
747 $level--;
748 $type = ($level != 0)? '{' : '';
749
750 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700751 if (substr($blk, $off + 1, 1) eq ';') {
752 $off++;
753 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800754 last;
755 }
756 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800757 # Preprocessor commands end at the newline unless escaped.
758 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
759 $level--;
760 $type = '';
761 $off++;
762 last;
763 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800764 $off++;
765 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700766 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800767 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700768 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800769 $line++;
770 $remain--;
771 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800772
773 my $statement = substr($blk, $soff, $off - $soff + 1);
774 my $condition = substr($blk, $soff, $coff - $soff + 1);
775
776 #warn "STATEMENT<$statement>\n";
777 #warn "CONDITION<$condition>\n";
778
Andy Whitcroft773647a2008-03-28 14:15:58 -0700779 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800780
781 return ($statement, $condition,
782 $line, $remain + 1, $off - $loff + 1, $level);
783}
784
Andy Whitcroftcf655042008-03-04 14:28:20 -0800785sub statement_lines {
786 my ($stmt) = @_;
787
788 # Strip the diff line prefixes and rip blank lines at start and end.
789 $stmt =~ s/(^|\n)./$1/g;
790 $stmt =~ s/^\s*//;
791 $stmt =~ s/\s*$//;
792
793 my @stmt_lines = ($stmt =~ /\n/g);
794
795 return $#stmt_lines + 2;
796}
797
798sub statement_rawlines {
799 my ($stmt) = @_;
800
801 my @stmt_lines = ($stmt =~ /\n/g);
802
803 return $#stmt_lines + 2;
804}
805
806sub statement_block_size {
807 my ($stmt) = @_;
808
809 $stmt =~ s/(^|\n)./$1/g;
810 $stmt =~ s/^\s*{//;
811 $stmt =~ s/}\s*$//;
812 $stmt =~ s/^\s*//;
813 $stmt =~ s/\s*$//;
814
815 my @stmt_lines = ($stmt =~ /\n/g);
816 my @stmt_statements = ($stmt =~ /;/g);
817
818 my $stmt_lines = $#stmt_lines + 2;
819 my $stmt_statements = $#stmt_statements + 1;
820
821 if ($stmt_lines > $stmt_statements) {
822 return $stmt_lines;
823 } else {
824 return $stmt_statements;
825 }
826}
827
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800828sub ctx_statement_full {
829 my ($linenr, $remain, $off) = @_;
830 my ($statement, $condition, $level);
831
832 my (@chunks);
833
Andy Whitcroftcf655042008-03-04 14:28:20 -0800834 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800835 ($statement, $condition, $linenr, $remain, $off, $level) =
836 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700837 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800838 push(@chunks, [ $condition, $statement ]);
839 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
840 return ($level, $linenr, @chunks);
841 }
842
843 # Pull in the following conditional/block pairs and see if they
844 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800845 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 ($statement, $condition, $linenr, $remain, $off, $level) =
847 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800848 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700849 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800850 #print "C: push\n";
851 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800852 }
853
854 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800855}
856
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700857sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700858 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700859 my $line;
860 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700861 my $blk = '';
862 my @o;
863 my @c;
864 my @res = ();
865
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700866 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800867 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700868 for ($line = $start; $remain > 0; $line++) {
869 next if ($rawlines[$line] =~ /^-/);
870 $remain--;
871
872 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800873
874 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700875 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800876 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700877 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800878 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700879 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800880 $level = pop(@stack);
881 }
882
Andy Whitcroft01464f32010-10-26 14:23:19 -0700883 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700884 ##print "C<$c>L<$level><$open$close>O<$off>\n";
885 if ($off > 0) {
886 $off--;
887 next;
888 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700889
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700890 if ($c eq $close && $level > 0) {
891 $level--;
892 last if ($level == 0);
893 } elsif ($c eq $open) {
894 $level++;
895 }
896 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700897
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700898 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700899 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700900 }
901
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700902 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700903 }
904
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700905 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700906}
907sub ctx_block_outer {
908 my ($linenr, $remain) = @_;
909
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700910 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
911 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700912}
913sub ctx_block {
914 my ($linenr, $remain) = @_;
915
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700916 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
917 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700918}
919sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700920 my ($linenr, $remain, $off) = @_;
921
922 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
923 return @r;
924}
925sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700926 my ($linenr, $remain) = @_;
927
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700928 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700929}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700930sub ctx_statement_level {
931 my ($linenr, $remain, $off) = @_;
932
933 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
934}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700935
936sub ctx_locate_comment {
937 my ($first_line, $end_line) = @_;
938
939 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700940 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700941 return $current_comment if (defined $current_comment);
942
943 # Look through the context and try and figure out if there is a
944 # comment.
945 my $in_comment = 0;
946 $current_comment = '';
947 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700948 my $line = $rawlines[$linenr - 1];
949 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700950 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
951 $in_comment = 1;
952 }
953 if ($line =~ m@/\*@) {
954 $in_comment = 1;
955 }
956 if (!$in_comment && $current_comment ne '') {
957 $current_comment = '';
958 }
959 $current_comment .= $line . "\n" if ($in_comment);
960 if ($line =~ m@\*/@) {
961 $in_comment = 0;
962 }
963 }
964
965 chomp($current_comment);
966 return($current_comment);
967}
968sub ctx_has_comment {
969 my ($first_line, $end_line) = @_;
970 my $cmt = ctx_locate_comment($first_line, $end_line);
971
Andy Whitcroft00df3442007-06-08 13:47:06 -0700972 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700973 ##print "CMMT: $cmt\n";
974
975 return ($cmt ne '');
976}
977
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700978sub raw_line {
979 my ($linenr, $cnt) = @_;
980
981 my $offset = $linenr - 1;
982 $cnt++;
983
984 my $line;
985 while ($cnt) {
986 $line = $rawlines[$offset++];
987 next if (defined($line) && $line =~ /^-/);
988 $cnt--;
989 }
990
991 return $line;
992}
993
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700994sub cat_vet {
995 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700996 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700997
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700998 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700999 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1000 $res .= $1;
1001 if ($2 ne '') {
1002 $coded = sprintf("^%c", unpack('C', $2) + 64);
1003 $res .= $coded;
1004 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001005 }
1006 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001007
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001008 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001009}
1010
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001011my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001012my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001014my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001015
1016sub annotate_reset {
1017 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001018 $av_pending = '_';
1019 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001020 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001021}
1022
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001023sub annotate_values {
1024 my ($stream, $type) = @_;
1025
1026 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001027 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001028 my $cur = $stream;
1029
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001030 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001033 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001034 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001035 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001036 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037 print "WS($1)\n" if ($dbg_values > 1);
1038 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001039 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001040 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041 }
1042
Florian Micklerc023e4732011-01-12 16:59:58 -08001043 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001044 print "CAST($1)\n" if ($dbg_values > 1);
1045 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001046 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001047
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001048 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001049 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001050 $type = 'T';
1051
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001052 } elsif ($cur =~ /^($Modifier)\s*/) {
1053 print "MODIFIER($1)\n" if ($dbg_values > 1);
1054 $type = 'T';
1055
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001056 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001057 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001058 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001059 push(@av_paren_type, $type);
1060 if ($2 ne '') {
1061 $av_pending = 'N';
1062 }
1063 $type = 'E';
1064
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001065 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001066 print "UNDEF($1)\n" if ($dbg_values > 1);
1067 $av_preprocessor = 1;
1068 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001069
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001070 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001071 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001072 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001073
1074 push(@av_paren_type, $type);
1075 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001076 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001077
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001078 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001079 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1080 $av_preprocessor = 1;
1081
1082 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1083
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001084 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001085
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001086 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001087 print "PRE_END($1)\n" if ($dbg_values > 1);
1088
1089 $av_preprocessor = 1;
1090
1091 # Assume all arms of the conditional end as this
1092 # one does, and continue as if the #endif was not here.
1093 pop(@av_paren_type);
1094 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001095 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001096
1097 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001098 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001099
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001100 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1101 print "ATTR($1)\n" if ($dbg_values > 1);
1102 $av_pending = $type;
1103 $type = 'N';
1104
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001105 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001106 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001107 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001108 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001109 }
1110 $type = 'N';
1111
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001112 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001113 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001114 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001115 $type = 'N';
1116
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001117 } elsif ($cur =~/^(case)/o) {
1118 print "CASE($1)\n" if ($dbg_values > 1);
1119 $av_pend_colon = 'C';
1120 $type = 'N';
1121
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001122 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001123 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001124 $type = 'N';
1125
1126 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001127 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001128 push(@av_paren_type, $av_pending);
1129 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001130 $type = 'N';
1131
1132 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001133 my $new_type = pop(@av_paren_type);
1134 if ($new_type ne '_') {
1135 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001136 print "PAREN('$1') -> $type\n"
1137 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001139 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001140 }
1141
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001142 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001143 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001144 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001145 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001146
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001147 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1148 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001149 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001150 } elsif ($type eq 'E') {
1151 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001152 }
1153 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1154 $type = 'V';
1155
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001156 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001158 $type = 'V';
1159
1160 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001162 $type = 'N';
1163
Andy Whitcroftcf655042008-03-04 14:28:20 -08001164 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001165 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001166 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001167 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001168
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001169 } elsif ($cur =~/^(,)/) {
1170 print "COMMA($1)\n" if ($dbg_values > 1);
1171 $type = 'C';
1172
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001173 } elsif ($cur =~ /^(\?)/o) {
1174 print "QUESTION($1)\n" if ($dbg_values > 1);
1175 $type = 'N';
1176
1177 } elsif ($cur =~ /^(:)/o) {
1178 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1179
1180 substr($var, length($res), 1, $av_pend_colon);
1181 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1182 $type = 'E';
1183 } else {
1184 $type = 'N';
1185 }
1186 $av_pend_colon = 'O';
1187
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001188 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001189 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001190 $type = 'N';
1191
Andy Whitcroft0d413862008-10-15 22:02:16 -07001192 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001193 my $variant;
1194
1195 print "OPV($1)\n" if ($dbg_values > 1);
1196 if ($type eq 'V') {
1197 $variant = 'B';
1198 } else {
1199 $variant = 'U';
1200 }
1201
1202 substr($var, length($res), 1, $variant);
1203 $type = 'N';
1204
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001205 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001206 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001207 if ($1 ne '++' && $1 ne '--') {
1208 $type = 'N';
1209 }
1210
1211 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001212 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213 }
1214 if (defined $1) {
1215 $cur = substr($cur, length($1));
1216 $res .= $type x length($1);
1217 }
1218 }
1219
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001220 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001221}
1222
Andy Whitcroft8905a672007-11-28 16:21:06 -08001223sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001224 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001225 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001226 ^(?:
1227 $Modifier|
1228 $Storage|
1229 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001230 DEFINE_\S+
1231 )$|
1232 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001233 goto|
1234 return|
1235 case|
1236 else|
1237 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001238 do|
1239 \#|
1240 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001241 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001242 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001243 )}x;
1244 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1245 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001246 # Check for modifiers.
1247 $possible =~ s/\s*$Storage\s*//g;
1248 $possible =~ s/\s*$Sparse\s*//g;
1249 if ($possible =~ /^\s*$/) {
1250
1251 } elsif ($possible =~ /\s/) {
1252 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001253 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001254 if ($modifier !~ $notPermitted) {
1255 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1256 push(@modifierList, $modifier);
1257 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001258 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001259
1260 } else {
1261 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1262 push(@typeList, $possible);
1263 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001264 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001265 } else {
1266 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001267 }
1268}
1269
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001270my $prefix = '';
1271
Joe Perches000d1cc2011-07-25 17:13:25 -07001272sub show_type {
1273 return !defined $ignore_type{$_[0]};
1274}
1275
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001276sub report {
Joe Perches000d1cc2011-07-25 17:13:25 -07001277 if (!show_type($_[1]) ||
1278 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279 return 0;
1280 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001281 my $line;
1282 if ($show_types) {
1283 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1284 } else {
1285 $line = "$prefix$_[0]: $_[2]\n";
1286 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001287 $line = (split('\n', $line))[0] . "\n" if ($terse);
1288
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001289 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001290
1291 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001292}
1293sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001294 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001295}
Joe Perches000d1cc2011-07-25 17:13:25 -07001296
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001297sub ERROR {
Joe Perches000d1cc2011-07-25 17:13:25 -07001298 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 our $clean = 0;
1300 our $cnt_error++;
1301 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001302}
1303sub WARN {
Joe Perches000d1cc2011-07-25 17:13:25 -07001304 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001305 our $clean = 0;
1306 our $cnt_warn++;
1307 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001308}
1309sub CHK {
Joe Perches000d1cc2011-07-25 17:13:25 -07001310 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001311 our $clean = 0;
1312 our $cnt_chk++;
1313 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001314}
1315
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001316sub check_absolute_file {
1317 my ($absolute, $herecurr) = @_;
1318 my $file = $absolute;
1319
1320 ##print "absolute<$absolute>\n";
1321
1322 # See if any suffix of this path is a path within the tree.
1323 while ($file =~ s@^[^/]*/@@) {
1324 if (-f "$root/$file") {
1325 ##print "file<$file>\n";
1326 last;
1327 }
1328 }
1329 if (! -f _) {
1330 return 0;
1331 }
1332
1333 # It is, so see if the prefix is acceptable.
1334 my $prefix = $absolute;
1335 substr($prefix, -length($file)) = '';
1336
1337 ##print "prefix<$prefix>\n";
1338 if ($prefix ne ".../") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001339 WARN("USE_RELATIVE_PATH",
1340 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001341 }
1342}
1343
Gregory Bean63081e12011-07-06 15:36:58 -07001344sub cleanup_continuation_headers {
1345 # Collapse any header-continuation lines into a single line so they
1346 # can be parsed meaningfully, as the parser only has one line
1347 # of context to work with.
1348 my $again;
1349 do {
1350 $again = 0;
1351 foreach my $n (0 .. scalar(@rawlines) - 2) {
1352 if ($rawlines[$n]=~/^\s*$/) {
1353 # A blank line means there's no more chance
1354 # of finding headers. Shortcut to done.
1355 return;
1356 }
1357 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
1358 $rawlines[$n+1]=~/^\s+/) {
1359 # Continuation header. Collapse it.
1360 my $line = splice @rawlines, $n+1, 1;
1361 $line=~s/^\s+/ /;
1362 $rawlines[$n] .= $line;
1363 # We've 'destabilized' the list, so restart.
1364 $again = 1;
1365 last;
1366 }
1367 }
1368 } while ($again);
1369}
1370
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001371sub pos_last_openparen {
1372 my ($line) = @_;
1373
1374 my $pos = 0;
1375
1376 my $opens = $line =~ tr/\(/\(/;
1377 my $closes = $line =~ tr/\)/\)/;
1378
1379 my $last_openparen = 0;
1380
1381 if (($opens == 0) || ($closes >= $opens)) {
1382 return -1;
1383 }
1384
1385 my $len = length($line);
1386
1387 for ($pos = 0; $pos < $len; $pos++) {
1388 my $string = substr($line, $pos);
1389 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1390 $pos += length($1) - 1;
1391 } elsif (substr($line, $pos, 1) eq '(') {
1392 $last_openparen = $pos;
1393 } elsif (index($string, '(') == -1) {
1394 last;
1395 }
1396 }
1397
1398 return $last_openparen + 1;
1399}
1400
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001401sub process {
1402 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001403
1404 my $linenr=0;
1405 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001406 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001407 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001408 my $stashrawline="";
Richard Adamsb548e3e2010-01-04 13:06:09 -08001409 my $subjectline="";
1410 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001411
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001412 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001413 my $indent;
1414 my $previndent=0;
1415 my $stashindent=0;
1416
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001417 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001418 my $signoff = 0;
1419 my $is_patch = 0;
1420
Joe Perches15662b32011-10-31 17:13:12 -07001421 my $in_header_lines = 1;
1422 my $in_commit_log = 0; #Scanning lines before patch
1423
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001425 our $cnt_lines = 0;
1426 our $cnt_error = 0;
1427 our $cnt_warn = 0;
1428 our $cnt_chk = 0;
1429
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001430 # Trace the real file/line as we go.
1431 my $realfile = '';
1432 my $realline = 0;
1433 my $realcnt = 0;
1434 my $here = '';
1435 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001436 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001437 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001438 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001439
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 my $prev_values = 'E';
1441
1442 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001444 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001445 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001446 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001447
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001448 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001449 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001450 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001451 my @setup_docs = ();
1452 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001453
Steve Muckle8e7509d2009-03-13 15:46:36 -07001454 my $in_code_block = 0;
1455 my $exec_file = "";
1456
1457 my $shorttext = BEFORE_SHORTTEXT;
1458 my $shorttext_exspc = 0;
1459
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 sanitise_line_reset();
Gregory Bean63081e12011-07-06 15:36:58 -07001461 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001462 my $line;
Gregory Bean63081e12011-07-06 15:36:58 -07001463
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001465 $linenr++;
1466 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001467
Andy Whitcroft773647a2008-03-28 14:15:58 -07001468 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001469 $setup_docs = 0;
1470 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1471 $setup_docs = 1;
1472 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001473 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001474 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001475 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1476 $realline=$1-1;
1477 if (defined $2) {
1478 $realcnt=$3+1;
1479 } else {
1480 $realcnt=1+1;
1481 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001482 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001483
1484 # Guestimate if this is a continuing comment. Run
1485 # the context looking for a comment "edge". If this
1486 # edge is a close comment then we must be in a comment
1487 # at context start.
1488 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001489 my $cnt = $realcnt;
1490 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1491 next if (defined $rawlines[$ln - 1] &&
1492 $rawlines[$ln - 1] =~ /^-/);
1493 $cnt--;
1494 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001495 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001496 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1497 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1498 ($edge) = $1;
1499 last;
1500 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001501 }
1502 if (defined $edge && $edge eq '*/') {
1503 $in_comment = 1;
1504 }
1505
1506 # Guestimate if this is a continuing comment. If this
1507 # is the start of a diff block and this line starts
1508 # ' *' then it is very likely a comment.
1509 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001510 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001511 {
1512 $in_comment = 1;
1513 }
1514
1515 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1516 sanitise_line_reset($in_comment);
1517
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001518 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001519 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001520 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001521 $line = sanitise_line($rawline);
1522 }
1523 push(@lines, $line);
1524
1525 if ($realcnt > 1) {
1526 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1527 } else {
1528 $realcnt = 0;
1529 }
1530
1531 #print "==>$rawline\n";
1532 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001533
1534 if ($setup_docs && $line =~ /^\+/) {
1535 push(@setup_docs, $line);
1536 }
1537 }
1538
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001539 $prefix = '';
1540
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541 $realcnt = 0;
1542 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001543 foreach my $line (@lines) {
1544 $linenr++;
1545
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001546 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001547
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001548#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001549 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001550 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001551 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001552 $realline=$1-1;
1553 if (defined $2) {
1554 $realcnt=$3+1;
1555 } else {
1556 $realcnt=1+1;
1557 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001558 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001559 $prev_values = 'E';
1560
Andy Whitcroft773647a2008-03-28 14:15:58 -07001561 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001562 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001563 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001564 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001565 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001566
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001567# track the line number as we move through the hunk, note that
1568# new versions of GNU diff omit the leading space on completely
1569# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001570 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001571 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001572 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001573
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001574 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001575 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001576
1577 # Track the previous line.
1578 ($prevline, $stashline) = ($stashline, $line);
1579 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001580 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1581
Andy Whitcroft773647a2008-03-28 14:15:58 -07001582 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001583
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001584 } elsif ($realcnt == 1) {
1585 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001586 }
1587
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001588 my $hunk_line = ($realcnt != 0);
1589
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001590#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001591 $prefix = "$filename:$realline: " if ($emacs && $file);
1592 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1593
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001594 $here = "#$linenr: " if (!$file);
1595 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001596
1597 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001598 if ($line =~ /^diff --git.*?(\S+)$/) {
1599 $realfile = $1;
1600 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001601 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001602 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001603 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001604 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001605 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001606
1607 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001608 if (!$file && $tree && $p1_prefix ne '' &&
1609 -e "$root/$p1_prefix") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001610 WARN("PATCH_PREFIX",
1611 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001612 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001613
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001614 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001615 ERROR("MODIFIED_INCLUDE_ASM",
1616 "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 -07001617 }
Steve Muckle8e7509d2009-03-13 15:46:36 -07001618 $in_code_block = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001619 next;
1620 }
Steve Muckle8e7509d2009-03-13 15:46:36 -07001621 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1622 $exec_file = $1;
1623 $in_code_block = 0;
1624 }
1625 #Check state to make sure we aren't in code block.
1626 elsif (!$in_code_block &&
Abhijeet Dharmapurikarad7cbda2009-10-01 12:01:44 -07001627 ($exec_file =~ /^.+\.[chS]$/ or
1628 $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
Matt Wagantall7079b442010-09-18 14:45:56 -07001634 $exec_file =~ /^Kconfig$/) &&
Steve Muckle76138852009-04-03 16:19:20 -07001635 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1636 (oct($2) & 0111)) {
Steve Muckle8e7509d2009-03-13 15:46:36 -07001637 ERROR("Source file has +x permissions: " .
1638 "$exec_file\n");
1639 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001640 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001641
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001642 my $hereline = "$here\n$rawline\n";
1643 my $herecurr = "$here\n$rawline\n";
1644 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001645
Steve Muckle8e7509d2009-03-13 15:46:36 -07001646 if ($shorttext != AFTER_SHORTTEXT) {
1647 if ($shorttext == IN_SHORTTEXT) {
1648 if ($line=~/^---/ || $line=~/^diff.*/) {
1649 $shorttext = AFTER_SHORTTEXT;
1650 } elsif (length($line) > (SHORTTEXT_LIMIT +
1651 $shorttext_exspc)
1652 && $line !~ /^:([0-7]{6}\s){2}
1653 ([[:xdigit:]]+\.*
1654 \s){2}\w+\s\w+/xms) {
1655 WARN("commit text line over " .
1656 SHORTTEXT_LIMIT .
1657 " characters\n" . $herecurr);
1658 }
Richard Adamsb548e3e2010-01-04 13:06:09 -08001659 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Bean6c050d42011-03-03 13:46:41 -08001660# The Subject line doesn't have to be the last header in the patch.
1661# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1662# Per RFC5322, continuation lines must be folded, so any left-justified
1663# text which looks like a header is definitely a header.
1664 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1665 $shorttext = IN_SHORTTEXT;
Richard Adamsb548e3e2010-01-04 13:06:09 -08001666# Check for Subject line followed by a blank line.
Gregory Bean6c050d42011-03-03 13:46:41 -08001667 if (length($line) != 0) {
1668 WARN("non-blank line after " .
1669 "summary line\n" .
1670 $sublinenr . $here .
1671 "\n" . $subjectline .
1672 "\n" . $line . "\n");
1673 }
Richard Adamsb548e3e2010-01-04 13:06:09 -08001674 }
1675 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
1676 $shorttext = CHECK_NEXT_SHORTTEXT;
1677 $subjectline = $line;
1678 $sublinenr = "#$linenr & ";
1679# Check for Subject line less than line limit
Steve Muckle8e7509d2009-03-13 15:46:36 -07001680 if (length($1) > SHORTTEXT_LIMIT) {
1681 WARN("summary line over " .
1682 SHORTTEXT_LIMIT .
1683 " characters\n" . $herecurr);
1684 }
1685 } elsif ($line=~/^ (.*)/) {
1686 $shorttext = IN_SHORTTEXT;
1687 $shorttext_exspc = 4;
1688 if (length($1) > SHORTTEXT_LIMIT) {
1689 WARN("summary line over " .
1690 SHORTTEXT_LIMIT .
1691 " characters\n" . $herecurr);
1692 }
1693 }
1694 }
1695
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001696 $cnt_lines++ if ($realcnt != 0);
1697
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001698# Check for incorrect file permissions
1699 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1700 my $permhere = $here . "FILE: $realfile\n";
1701 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001702 ERROR("EXECUTE_PERMISSIONS",
1703 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001704 }
1705 }
1706
Joe Perches20112472011-07-25 17:13:23 -07001707# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001708 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001709 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001710 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001711 }
1712
1713# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001714 if (!$in_header_lines &&
1715 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
Joe Perches20112472011-07-25 17:13:23 -07001716 my $space_before = $1;
1717 my $sign_off = $2;
1718 my $space_after = $3;
1719 my $email = $4;
1720 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1721
1722 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001723 WARN("BAD_SIGN_OFF",
1724 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001725 }
Joe Perches20112472011-07-25 17:13:23 -07001726 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001727 WARN("BAD_SIGN_OFF",
1728 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001729 }
1730 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001731 WARN("BAD_SIGN_OFF",
1732 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001733 }
1734
1735 my ($email_name, $email_address, $comment) = parse_email($email);
1736 my $suggested_email = format_email(($email_name, $email_address));
1737 if ($suggested_email eq "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001738 ERROR("BAD_SIGN_OFF",
1739 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001740 } else {
1741 my $dequoted = $suggested_email;
1742 $dequoted =~ s/^"//;
1743 $dequoted =~ s/" </ </;
1744 # Don't force email to have quotes
1745 # Allow just an angle bracketed address
1746 if ("$dequoted$comment" ne $email &&
1747 "<$email_address>$comment" ne $email &&
1748 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001749 WARN("BAD_SIGN_OFF",
1750 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001751 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001752 }
Israel Schlesingerce682312010-07-21 11:44:20 -07001753 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsman99b5ef82010-05-04 17:31:32 -07001754 WARN("invalid Signed-off-by identity\n" . $line );
1755 }
1756 }
1757
1758#check the patch for invalid author credentials
Israel Schlesingerce682312010-07-21 11:44:20 -07001759 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsman99b5ef82010-05-04 17:31:32 -07001760 WARN("invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001761 }
1762
Andy Whitcroft00df3442007-06-08 13:47:06 -07001763# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001764 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001765 ERROR("CORRUPTED_PATCH",
1766 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001767 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001768 }
1769
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001770# Check for absolute kernel paths.
1771 if ($tree) {
1772 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1773 my $file = $1;
1774
1775 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1776 check_absolute_file($1, $herecurr)) {
1777 #
1778 } else {
1779 check_absolute_file($file, $herecurr);
1780 }
1781 }
1782 }
1783
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001784# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1785 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001786 $rawline !~ m/^$UTF8*$/) {
1787 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1788
1789 my $blank = copy_spacing($rawline);
1790 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1791 my $hereptr = "$hereline$ptr\n";
1792
Joe Perches34d99212011-07-25 17:13:26 -07001793 CHK("INVALID_UTF8",
1794 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001795 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001796
Joe Perches15662b32011-10-31 17:13:12 -07001797# Check if it's the start of a commit log
1798# (not a header line and we haven't seen the patch filename)
1799 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001800 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001801 $in_header_lines = 0;
1802 $in_commit_log = 1;
1803 }
1804
1805# Still not yet in a patch, check for any UTF-8
1806 if ($in_commit_log && $realfile =~ /^$/ &&
1807 $rawline =~ /$NON_ASCII_UTF8/) {
1808 CHK("UTF8_BEFORE_PATCH",
1809 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1810 }
1811
Andy Whitcroft306708542008-10-15 22:02:28 -07001812# ignore non-hunk lines and lines being removed
1813 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001814
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001815#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001816 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001817 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001818 ERROR("DOS_LINE_ENDINGS",
1819 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001820
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001821 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1822 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001823 ERROR("TRAILING_WHITESPACE",
1824 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001825 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001826 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001827
Andi Kleen33549572010-05-24 14:33:29 -07001828# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001829# Only applies when adding the entry originally, after that we do not have
1830# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001831 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001832 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001833 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001834 my $cnt = $realcnt;
1835 my $ln = $linenr + 1;
1836 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001837 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001838 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001839 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001840 $f = $lines[$ln - 1];
1841 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1842 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001843
1844 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001845
1846 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1847 $is_start = 1;
1848 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1849 $length = -1;
1850 }
1851
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001852 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001853 $f =~ s/#.*//;
1854 $f =~ s/^\s+//;
1855 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001856 if ($f =~ /^\s*config\s/) {
1857 $is_end = 1;
1858 last;
1859 }
Andi Kleen33549572010-05-24 14:33:29 -07001860 $length++;
1861 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001862 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001863 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1864 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001865 }
1866
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001867 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1868 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1869 my $flag = $1;
1870 my $replacement = {
1871 'EXTRA_AFLAGS' => 'asflags-y',
1872 'EXTRA_CFLAGS' => 'ccflags-y',
1873 'EXTRA_CPPFLAGS' => 'cppflags-y',
1874 'EXTRA_LDFLAGS' => 'ldflags-y',
1875 };
1876
1877 WARN("DEPRECATED_VARIABLE",
1878 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1879 }
1880
Andy Whitcroft5368df22008-10-15 22:02:27 -07001881# check we are in a valid source file if not then ignore this hunk
1882 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1883
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001884#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001885 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001886 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001887 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001888 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Israel Schlesingerc1813b62010-07-27 13:28:26 -07001889 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001890 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001891 {
Joe Perches000d1cc2011-07-25 17:13:25 -07001892 WARN("LONG_LINE",
1893 "line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001894 }
1895
Josh Triplettca56dc02012-03-23 15:02:21 -07001896# Check for user-visible strings broken across lines, which breaks the ability
1897# to grep for the string. Limited to strings used as parameters (those
1898# following an open parenthesis), which almost completely eliminates false
1899# positives, as well as warning only once per parameter rather than once per
1900# line of the string. Make an exception when the previous string ends in a
1901# newline (multiple lines in one string constant) or \n\t (common in inline
1902# assembly to indent the instruction on the following line).
1903 if ($line =~ /^\+\s*"/ &&
1904 $prevline =~ /"\s*$/ &&
1905 $prevline =~ /\(/ &&
1906 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1907 WARN("SPLIT_STRING",
1908 "quoted string split across lines\n" . $hereprev);
1909 }
1910
Joe Perches5e79d962010-03-05 13:43:55 -08001911# check for spaces before a quoted newline
1912 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001913 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1914 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001915 }
1916
Patrick Pannutoe48711e2010-07-27 17:15:07 -07001917# check for spaces before a quoted newline
1918 if ($rawline =~ /^.*\".*\s\\n/) {
1919 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1920 }
1921
Andy Whitcroft8905a672007-11-28 16:21:06 -08001922# check for adding lines without a newline.
1923 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001924 WARN("MISSING_EOF_NEWLINE",
1925 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001926 }
1927
Mike Frysinger42e41c52009-09-21 17:04:40 -07001928# Blackfin: use hi/lo macros
1929 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1930 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1931 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001932 ERROR("LO_MACRO",
1933 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001934 }
1935 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1936 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001937 ERROR("HI_MACRO",
1938 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001939 }
1940 }
1941
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001942# check we are in a valid source file C or perl if not then ignore this hunk
1943 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001944
1945# at the beginning of a line any tabs must come first and anything
1946# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1948 $rawline =~ /^\+\s* \s*/) {
1949 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001950 ERROR("CODE_INDENT",
1951 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001952 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001953 }
1954
Alberto Panizzo08e44362010-03-05 13:43:54 -08001955# check for space before tabs.
1956 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1957 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001958 WARN("SPACE_BEFORE_TAB",
1959 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08001960 }
1961
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001962# check for && or || at the start of a line
1963 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1964 CHK("LOGICAL_CONTINUATIONS",
1965 "Logical continuations should be on the previous line\n" . $hereprev);
1966 }
1967
1968# check multi-line statement indentation matches previous line
1969 if ($^V && $^V ge 5.10.0 &&
1970 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1971 $prevline =~ /^\+(\t*)(.*)$/;
1972 my $oldindent = $1;
1973 my $rest = $2;
1974
1975 my $pos = pos_last_openparen($rest);
1976 if ($pos >= 0) {
1977 $line =~ /^\+([ \t]*)/;
1978 my $newindent = $1;
1979
1980 my $goodtabindent = $oldindent .
1981 "\t" x ($pos / 8) .
1982 " " x ($pos % 8);
1983 my $goodspaceindent = $oldindent . " " x $pos;
1984
1985 if ($newindent ne $goodtabindent &&
1986 $newindent ne $goodspaceindent) {
1987 CHK("PARENTHESIS_ALIGNMENT",
1988 "Alignment should match open parenthesis\n" . $hereprev);
1989 }
1990 }
1991 }
1992
Joe Perchesaad4f612012-03-23 15:02:19 -07001993 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1994 CHK("SPACING",
1995 "No space is necessary after a cast\n" . $hereprev);
1996 }
1997
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001998# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001999# Exceptions:
2000# 1) within comments
2001# 2) indented preprocessor commands
2002# 3) hanging labels
2003 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002004 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002005 WARN("LEADING_SPACE",
2006 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002007 }
2008
Patrick Pannutoe48711e2010-07-27 17:15:07 -07002009# check for space before tabs.
2010 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2011 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2012 WARN("please, no space before tabs\n" . $herevet);
2013 }
2014
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002015# check we are in a valid C source file if not then ignore this hunk
2016 next if ($realfile !~ /\.(h|c)$/);
2017
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002018# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002019 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002020 WARN("CVS_KEYWORD",
2021 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002022 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002023
Mike Frysinger42e41c52009-09-21 17:04:40 -07002024# Blackfin: don't use __builtin_bfin_[cs]sync
2025 if ($line =~ /__builtin_bfin_csync/) {
2026 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002027 ERROR("CSYNC",
2028 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002029 }
2030 if ($line =~ /__builtin_bfin_ssync/) {
2031 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002032 ERROR("SSYNC",
2033 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002034 }
2035
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002036# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002037 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2038 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002039#print "LINE<$line>\n";
2040 if ($linenr >= $suppress_statement &&
2041 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002042 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002043 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002044 $stat =~ s/\n./\n /g;
2045 $cond =~ s/\n./\n /g;
2046
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002047#print "linenr<$linenr> <$stat>\n";
2048 # If this statement has no statement boundaries within
2049 # it there is no point in retrying a statement scan
2050 # until we hit end of it.
2051 my $frag = $stat; $frag =~ s/;+\s*$//;
2052 if ($frag !~ /(?:{|;)/) {
2053#print "skip<$line_nr_next>\n";
2054 $suppress_statement = $line_nr_next;
2055 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002056
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002057 # Find the real next line.
2058 $realline_next = $line_nr_next;
2059 if (defined $realline_next &&
2060 (!defined $lines[$realline_next - 1] ||
2061 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2062 $realline_next++;
2063 }
2064
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002065 my $s = $stat;
2066 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002067
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002069 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002070
2071 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002072 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002073
Andy Whitcroft463f2862009-09-21 17:04:34 -07002074 } elsif ($s =~ /^.\s*else\b/s) {
2075
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002076 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002077 } 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 -07002078 my $type = $1;
2079 $type =~ s/\s+/ /g;
2080 possible($type, "A:" . $s);
2081
Andy Whitcroft8905a672007-11-28 16:21:06 -08002082 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07002083 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002084 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002085 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002086
2087 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002088 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002089 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002090 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002091
2092 # Check for any sort of function declaration.
2093 # int foo(something bar, other baz);
2094 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002095 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 -08002096 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002097
Andy Whitcroftcf655042008-03-04 14:28:20 -08002098 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002099 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002100 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002101
Andy Whitcroft8905a672007-11-28 16:21:06 -08002102 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002103 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002104
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002105 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002106 }
2107 }
2108 }
2109
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002110 }
2111
Andy Whitcroft653d4872007-06-23 17:16:34 -07002112#
2113# Checks which may be anchored in the context.
2114#
2115
2116# Check for switch () and associated case and default
2117# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002118 if ($line=~/\bswitch\s*\(.*\)/) {
2119 my $err = '';
2120 my $sep = '';
2121 my @ctx = ctx_block_outer($linenr, $realcnt);
2122 shift(@ctx);
2123 for my $ctx (@ctx) {
2124 my ($clen, $cindent) = line_stats($ctx);
2125 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2126 $indent != $cindent) {
2127 $err .= "$sep$ctx\n";
2128 $sep = '';
2129 } else {
2130 $sep = "[...]\n";
2131 }
2132 }
2133 if ($err ne '') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002134 ERROR("SWITCH_CASE_INDENT_LEVEL",
2135 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002136 }
2137 }
2138
2139# if/while/etc brace do not go on next line, unless defining a do while loop,
2140# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002141 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002142 my $pre_ctx = "$1$2";
2143
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002144 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002145
2146 if ($line =~ /^\+\t{6,}/) {
2147 WARN("DEEP_INDENTATION",
2148 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2149 }
2150
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002151 my $ctx_cnt = $realcnt - $#ctx - 1;
2152 my $ctx = join("\n", @ctx);
2153
Andy Whitcroft548596d2008-07-23 21:29:01 -07002154 my $ctx_ln = $linenr;
2155 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002156
Andy Whitcroft548596d2008-07-23 21:29:01 -07002157 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2158 defined $lines[$ctx_ln - 1] &&
2159 $lines[$ctx_ln - 1] =~ /^-/)) {
2160 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2161 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002162 $ctx_ln++;
2163 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002164
Andy Whitcroft53210162008-07-23 21:29:03 -07002165 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2166 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002167
2168 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002169 ERROR("OPEN_BRACE",
2170 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002171 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002172 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002173 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2174 $ctx =~ /\)\s*\;\s*$/ &&
2175 defined $lines[$ctx_ln - 1])
2176 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002177 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2178 if ($nindent > $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002179 WARN("TRAILING_SEMICOLON",
2180 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002181 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002182 }
2183 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002184 }
2185
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002186# Check relative indent for conditionals and blocks.
2187 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002188 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2189 ctx_statement_block($linenr, $realcnt, 0)
2190 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002191 my ($s, $c) = ($stat, $cond);
2192
2193 substr($s, 0, length($c), '');
2194
2195 # Make sure we remove the line prefixes as we have
2196 # none on the first line, and are going to readd them
2197 # where necessary.
2198 $s =~ s/\n./\n/gs;
2199
2200 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002201 my @newlines = ($c =~ /\n/gs);
2202 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002203
2204 # We want to check the first line inside the block
2205 # starting at the end of the conditional, so remove:
2206 # 1) any blank line termination
2207 # 2) any opening brace { on end of the line
2208 # 3) any do (...) {
2209 my $continuation = 0;
2210 my $check = 0;
2211 $s =~ s/^.*\bdo\b//;
2212 $s =~ s/^\s*{//;
2213 if ($s =~ s/^\s*\\//) {
2214 $continuation = 1;
2215 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002216 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002217 $check = 1;
2218 $cond_lines++;
2219 }
2220
2221 # Also ignore a loop construct at the end of a
2222 # preprocessor statement.
2223 if (($prevline =~ /^.\s*#\s*define\s/ ||
2224 $prevline =~ /\\\s*$/) && $continuation == 0) {
2225 $check = 0;
2226 }
2227
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002228 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002229 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002230 while ($cond_ptr != $cond_lines) {
2231 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002232
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002233 # If we see an #else/#elif then the code
2234 # is not linear.
2235 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2236 $check = 0;
2237 }
2238
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002239 # Ignore:
2240 # 1) blank lines, they should be at 0,
2241 # 2) preprocessor lines, and
2242 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002243 if ($continuation ||
2244 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002245 $s =~ /^\s*#\s*?/ ||
2246 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002247 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002248 if ($s =~ s/^.*?\n//) {
2249 $cond_lines++;
2250 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002251 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002252 }
2253
2254 my (undef, $sindent) = line_stats("+" . $s);
2255 my $stat_real = raw_line($linenr, $cond_lines);
2256
2257 # Check if either of these lines are modified, else
2258 # this is not this patch's fault.
2259 if (!defined($stat_real) ||
2260 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2261 $check = 0;
2262 }
2263 if (defined($stat_real) && $cond_lines > 1) {
2264 $stat_real = "[...]\n$stat_real";
2265 }
2266
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002267 #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 -07002268
2269 if ($check && (($sindent % 8) != 0 ||
2270 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002271 WARN("SUSPECT_CODE_INDENT",
2272 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002273 }
2274 }
2275
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002276 # Track the 'values' across context and added lines.
2277 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002278 my ($curr_values, $curr_vars) =
2279 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002280 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002281 if ($dbg_values) {
2282 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002283 print "$linenr > .$outline\n";
2284 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002285 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002286 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002287 $prev_values = substr($curr_values, -1);
2288
Andy Whitcroft00df3442007-06-08 13:47:06 -07002289#ignore lines not being added
2290 if ($line=~/^[^\+]/) {next;}
2291
Andy Whitcroft653d4872007-06-23 17:16:34 -07002292# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002293 if ($dbg_type) {
2294 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002295 ERROR("TEST_TYPE",
2296 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002297 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002298 ERROR("TEST_NOT_TYPE",
2299 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002300 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002301 next;
2302 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002303# TEST: allow direct testing of the attribute matcher.
2304 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002305 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002306 ERROR("TEST_ATTR",
2307 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002308 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002309 ERROR("TEST_NOT_ATTR",
2310 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002311 }
2312 next;
2313 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002314
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002315# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002316 if ($line =~ /^.\s*{/ &&
2317 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002318 ERROR("OPEN_BRACE",
2319 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002320 }
2321
Andy Whitcroft653d4872007-06-23 17:16:34 -07002322#
2323# Checks which are anchored on the added line.
2324#
2325
2326# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002327 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002328 my $path = $1;
2329 if ($path =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002330 ERROR("MALFORMED_INCLUDE",
2331 "malformed #include filename\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002332 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002333 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002334 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002335
2336# no C99 // comments
2337 if ($line =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002338 ERROR("C99_COMMENTS",
2339 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002340 }
2341 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002343 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002344
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002345# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2346# the whole statement.
2347#print "APW <$lines[$realline_next - 1]>\n";
2348 if (defined $realline_next &&
2349 exists $lines[$realline_next - 1] &&
2350 !defined $suppress_export{$realline_next} &&
2351 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2352 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002353 # Handle definitions which produce identifiers with
2354 # a prefix:
2355 # XXX(foo);
2356 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002357 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002358 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002359 $name =~ /^${Ident}_$2/) {
2360#print "FOO C name<$name>\n";
2361 $suppress_export{$realline_next} = 1;
2362
2363 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002364 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002365 ^.DEFINE_$Ident\(\Q$name\E\)|
2366 ^.DECLARE_$Ident\(\Q$name\E\)|
2367 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002368 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2369 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002370 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002371#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2372 $suppress_export{$realline_next} = 2;
2373 } else {
2374 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002375 }
2376 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002377 if (!defined $suppress_export{$linenr} &&
2378 $prevline =~ /^.\s*$/ &&
2379 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2380 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2381#print "FOO B <$lines[$linenr - 1]>\n";
2382 $suppress_export{$linenr} = 2;
2383 }
2384 if (defined $suppress_export{$linenr} &&
2385 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002386 WARN("EXPORT_SYMBOL",
2387 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002388 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002389
Joe Eloff5150bda2010-08-09 17:21:00 -07002390# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002391 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002392 ERROR("GLOBAL_INITIALISERS",
2393 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002394 $herecurr);
2395 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002396# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002397 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002398 ERROR("INITIALISED_STATIC",
2399 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002400 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002401 }
2402
Joe Perchescb710ec2010-10-26 14:23:20 -07002403# check for static const char * arrays.
2404 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002405 WARN("STATIC_CONST_CHAR_ARRAY",
2406 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002407 $herecurr);
2408 }
2409
2410# check for static char foo[] = "bar" declarations.
2411 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002412 WARN("STATIC_CONST_CHAR_ARRAY",
2413 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002414 $herecurr);
2415 }
2416
Joe Perches93ed0e22010-10-26 14:23:21 -07002417# check for declarations of struct pci_device_id
2418 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002419 WARN("DEFINE_PCI_DEVICE_TABLE",
2420 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002421 }
2422
Andy Whitcroft653d4872007-06-23 17:16:34 -07002423# check for new typedefs, only function parameters and sparse annotations
2424# make sense.
2425 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002426 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002427 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002428 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002429 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002430 WARN("NEW_TYPEDEFS",
2431 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002432 }
2433
2434# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002435 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002436 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2437 #print "AA<$1>\n";
2438 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002439
Andy Whitcroft65863862009-01-06 14:41:21 -08002440 # Should start with a space.
2441 $to =~ s/^(\S)/ $1/;
2442 # Should not end with a space.
2443 $to =~ s/\s+$//;
2444 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002445 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002446 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002447
Andy Whitcroft65863862009-01-06 14:41:21 -08002448 #print "from<$from> to<$to>\n";
2449 if ($from ne $to) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002450 ERROR("POINTER_LOCATION",
2451 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002452 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002453 }
2454 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2455 #print "BB<$1>\n";
2456 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002457
Andy Whitcroft65863862009-01-06 14:41:21 -08002458 # Should start with a space.
2459 $to =~ s/^(\S)/ $1/;
2460 # Should not end with a space.
2461 $to =~ s/\s+$//;
2462 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002463 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002464 }
2465 # Modifiers should have spaces.
2466 $to =~ s/(\b$Modifier$)/$1 /;
2467
Andy Whitcroft667026e2009-02-27 14:03:08 -08002468 #print "from<$from> to<$to> ident<$ident>\n";
2469 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002470 ERROR("POINTER_LOCATION",
2471 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002472 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002473 }
2474
2475# # no BUG() or BUG_ON()
2476# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2477# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2478# print "$herecurr";
2479# $clean = 0;
2480# }
2481
Andy Whitcroft8905a672007-11-28 16:21:06 -08002482 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002483 WARN("LINUX_VERSION_CODE",
2484 "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 -08002485 }
2486
Joe Perches17441222011-06-15 15:08:17 -07002487# check for uses of printk_ratelimit
2488 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002489 WARN("PRINTK_RATELIMITED",
2490"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002491 }
2492
Andy Whitcroft00df3442007-06-08 13:47:06 -07002493# printk should use KERN_* levels. Note that follow on printk's on the
2494# same line do not need a level, so we use the current block context
2495# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002496# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002497# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002498 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002499 my $ok = 0;
2500 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2501 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002502 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002503 # with "\n" ignore it, else it is to blame
2504 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2505 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2506 $ok = 1;
2507 }
2508 last;
2509 }
2510 }
2511 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002512 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2513 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002514 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002515 }
2516
Andy Whitcroft653d4872007-06-23 17:16:34 -07002517# function brace can't be on same line, except for #defines of do while,
2518# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002519 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2520 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002521 ERROR("OPEN_BRACE",
2522 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002523 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002524
Andy Whitcroft8905a672007-11-28 16:21:06 -08002525# open braces for enum, union and struct go on the same line.
2526 if ($line =~ /^.\s*{/ &&
2527 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002528 ERROR("OPEN_BRACE",
2529 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002530 }
2531
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002532# missing space after union, struct or enum definition
2533 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002534 WARN("SPACING",
2535 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002536 }
2537
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002538# check for spacing round square brackets; allowed:
2539# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002540# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2541# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002542 while ($line =~ /(.*?\s)\[/g) {
2543 my ($where, $prefix) = ($-[1], $1);
2544 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002545 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002546 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002547 ERROR("BRACKET_SPACE",
2548 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002549 }
2550 }
2551
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002552# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002553 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002554 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002555 my $ctx_before = substr($line, 0, $-[1]);
2556 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002557
2558 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002559 if ($name =~ /^(?:
2560 if|for|while|switch|return|case|
2561 volatile|__volatile__|
2562 __attribute__|format|__extension__|
2563 asm|__asm__)$/x)
2564 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002565
2566 # cpp #define statements have non-optional spaces, ie
2567 # if there is a space between the name and the open
2568 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002569 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002570
2571 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002572 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002573
2574 # If this whole things ends with a type its most
2575 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002576 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002577
2578 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07002579 WARN("SPACING",
2580 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002581 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002582 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002583# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002584 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002585 my $ops = qr{
2586 <<=|>>=|<=|>=|==|!=|
2587 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2588 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002589 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2590 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002591 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002592 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002593 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002594
2595 my $blank = copy_spacing($opline);
2596
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002597 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002598 $off += length($elements[$n]);
2599
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002600 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002601 my $ca = substr($opline, 0, $off);
2602 my $cc = '';
2603 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2604 $cc = substr($opline, $off + length($elements[$n + 1]));
2605 }
2606 my $cb = "$ca$;$cc";
2607
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002608 my $a = '';
2609 $a = 'V' if ($elements[$n] ne '');
2610 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002611 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002612 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2613 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002614 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002615
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002616 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002617
2618 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002619 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002620 $c = 'V' if ($elements[$n + 2] ne '');
2621 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002622 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002623 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2624 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002625 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002626 } else {
2627 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002628 }
2629
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002630 my $ctx = "${a}x${c}";
2631
2632 my $at = "(ctx:$ctx)";
2633
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002634 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002635 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002636
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002637 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002638 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002639
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002640 # Get the full operator variant.
2641 my $opv = $op . substr($curr_vars, $off, 1);
2642
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002643 # Ignore operators passed as parameters.
2644 if ($op_type ne 'V' &&
2645 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2646
Andy Whitcroftcf655042008-03-04 14:28:20 -08002647# # Ignore comments
2648# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002649
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002650 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002651 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002652 if ($ctx !~ /.x[WEBC]/ &&
2653 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002654 ERROR("SPACING",
2655 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002656 }
2657
2658 # // is a comment
2659 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002660
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002661 # No spaces for:
2662 # ->
2663 # : when part of a bitfield
2664 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002665 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002666 ERROR("SPACING",
2667 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002668 }
2669
2670 # , must have a space on the right.
2671 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002672 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002673 ERROR("SPACING",
2674 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002675 }
2676
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002677 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002678 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002679 #warn "'*' is part of type\n";
2680
2681 # unary operators should have a space before and
2682 # none after. May be left adjacent to another
2683 # unary operator, or a cast
2684 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002685 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002686 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002687 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002688 ERROR("SPACING",
2689 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002690 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002691 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002692 # A unary '*' may be const
2693
2694 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002695 ERROR("SPACING",
2696 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002697 }
2698
2699 # unary ++ and unary -- are allowed no space on one side.
2700 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002701 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002702 ERROR("SPACING",
2703 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002704 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002705 if ($ctx =~ /Wx[BE]/ ||
2706 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002707 ERROR("SPACING",
2708 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002709 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002710 if ($ctx =~ /ExW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002711 ERROR("SPACING",
2712 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002713 }
2714
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002715
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002716 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002717 } elsif ($op eq '<<' or $op eq '>>' or
2718 $op eq '&' or $op eq '^' or $op eq '|' or
2719 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002720 $op eq '*' or $op eq '/' or
2721 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002722 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002723 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002724 ERROR("SPACING",
2725 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002726 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002727 }
2728
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002729 # A colon needs no spaces before when it is
2730 # terminating a case value or a label.
2731 } elsif ($opv eq ':C' || $opv eq ':L') {
2732 if ($ctx =~ /Wx./) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002733 ERROR("SPACING",
2734 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002735 }
2736
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002737 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002738 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002739 my $ok = 0;
2740
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002741 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002742 if (($op eq '<' &&
2743 $cc =~ /^\S+\@\S+>/) ||
2744 ($op eq '>' &&
2745 $ca =~ /<\S+\@\S+$/))
2746 {
2747 $ok = 1;
2748 }
2749
2750 # Ignore ?:
2751 if (($opv eq ':O' && $ca =~ /\?$/) ||
2752 ($op eq '?' && $cc =~ /^:/)) {
2753 $ok = 1;
2754 }
2755
2756 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002757 ERROR("SPACING",
2758 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002759 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002760 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002761 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002762 }
2763 }
2764
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002765# check for multiple assignments
2766 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002767 CHK("MULTIPLE_ASSIGNMENTS",
2768 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002769 }
2770
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002771## # check for multiple declarations, allowing for a function declaration
2772## # continuation.
2773## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2774## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2775##
2776## # Remove any bracketed sections to ensure we do not
2777## # falsly report the parameters of functions.
2778## my $ln = $line;
2779## while ($ln =~ s/\([^\(\)]*\)//g) {
2780## }
2781## if ($ln =~ /,/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002782## WARN("MULTIPLE_DECLARATION",
2783## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002784## }
2785## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002786
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002787#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002788 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2789 $line =~ /do{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002790 ERROR("SPACING",
2791 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002792 }
2793
2794# closing brace should have a space following it when it has anything
2795# on the line
2796 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002797 ERROR("SPACING",
2798 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002799 }
2800
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002801# check spacing on square brackets
2802 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002803 ERROR("SPACING",
2804 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002805 }
2806 if ($line =~ /\s\]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002807 ERROR("SPACING",
2808 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002809 }
2810
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002811# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002812 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall9bdb2022010-06-03 12:28:18 -07002813 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002814 ERROR("SPACING",
2815 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002816 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002817 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002818 $line !~ /for\s*\(.*;\s+\)/ &&
2819 $line !~ /:\s+\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002820 ERROR("SPACING",
2821 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002822 }
2823
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002824#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002825 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002826 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002827 WARN("INDENTED_LABEL",
2828 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002829 }
2830
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002831# Return is not a function.
2832 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2833 my $spacing = $1;
2834 my $value = $2;
2835
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002836 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002837 $value =~ s/\(/ \(/g;
2838 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002839 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002840 $value !~ /(?:$Ident|-?$Constant)\s*
2841 $Compare\s*
2842 (?:$Ident|-?$Constant)/x &&
2843 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002844 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002845#print "value<$value>\n";
2846 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002847 ERROR("RETURN_PARENTHESES",
2848 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002849
2850 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002851 ERROR("SPACING",
2852 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002853 }
2854 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002855# Return of what appears to be an errno should normally be -'ve
2856 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2857 my $name = $1;
2858 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002859 WARN("USE_NEGATIVE_ERRNO",
2860 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002861 }
2862 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002863
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002864# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002865 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002866 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002867 }
2868
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002869# Check for illegal assignment in if conditional -- and check for trailing
2870# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002871 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002872 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2873 ctx_statement_block($linenr, $realcnt, 0)
2874 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002875 my ($stat_next) = ctx_statement_block($line_nr_next,
2876 $remain_next, $off_next);
2877 $stat_next =~ s/\n./\n /g;
2878 ##print "stat<$stat> stat_next<$stat_next>\n";
2879
2880 if ($stat_next =~ /^\s*while\b/) {
2881 # If the statement carries leading newlines,
2882 # then count those as offsets.
2883 my ($whitespace) =
2884 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2885 my $offset =
2886 statement_rawlines($whitespace) - 1;
2887
2888 $suppress_whiletrailers{$line_nr_next +
2889 $offset} = 1;
2890 }
2891 }
2892 if (!defined $suppress_whiletrailers{$linenr} &&
2893 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002894 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002895
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002896 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002897 ERROR("ASSIGN_IN_IF",
2898 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002899 }
2900
2901 # Find out what is on the end of the line after the
2902 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002903 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002904 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002905 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002906 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2907 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002908 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002909 # Find out how long the conditional actually is.
2910 my @newlines = ($c =~ /\n/gs);
2911 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002912 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002913
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002914 $stat_real = raw_line($linenr, $cond_lines)
2915 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002916 if (defined($stat_real) && $cond_lines > 1) {
2917 $stat_real = "[...]\n$stat_real";
2918 }
2919
Joe Perches000d1cc2011-07-25 17:13:25 -07002920 ERROR("TRAILING_STATEMENTS",
2921 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002922 }
2923 }
2924
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002925# Check for bitwise tests written as boolean
2926 if ($line =~ /
2927 (?:
2928 (?:\[|\(|\&\&|\|\|)
2929 \s*0[xX][0-9]+\s*
2930 (?:\&\&|\|\|)
2931 |
2932 (?:\&\&|\|\|)
2933 \s*0[xX][0-9]+\s*
2934 (?:\&\&|\|\||\)|\])
2935 )/x)
2936 {
Joe Perches000d1cc2011-07-25 17:13:25 -07002937 WARN("HEXADECIMAL_BOOLEAN_TEST",
2938 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002939 }
2940
Andy Whitcroft8905a672007-11-28 16:21:06 -08002941# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002942 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2943 my $s = $1;
2944 $s =~ s/$;//g; # Remove any comments
2945 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002946 ERROR("TRAILING_STATEMENTS",
2947 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002948 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002949 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002950# if should not continue a brace
2951 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002952 ERROR("TRAILING_STATEMENTS",
2953 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08002954 $herecurr);
2955 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002956# case and default should not have general statements after them
2957 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2958 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002959 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002960 \s*return\s+
2961 )/xg)
2962 {
Joe Perches000d1cc2011-07-25 17:13:25 -07002963 ERROR("TRAILING_STATEMENTS",
2964 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002965 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002966
2967 # Check for }<nl>else {, these must be at the same
2968 # indent level to be relevant to each other.
2969 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2970 $previndent == $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002971 ERROR("ELSE_AFTER_BRACE",
2972 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002973 }
2974
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002975 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2976 $previndent == $indent) {
2977 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2978
2979 # Find out what is on the end of the line after the
2980 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002981 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002982 $s =~ s/\n.*//g;
2983
2984 if ($s =~ /^\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002985 ERROR("WHILE_AFTER_BRACE",
2986 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002987 }
2988 }
2989
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002990#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2991# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2992# print "No studly caps, use _\n";
2993# print "$herecurr";
2994# $clean = 0;
2995# }
2996
2997#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002998 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002999 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3000 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003001 }
3002
Andy Whitcroft653d4872007-06-23 17:16:34 -07003003#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003004 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003005 my $file = "$1.h";
3006 my $checkfile = "include/linux/$file";
3007 if (-f "$root/$checkfile" &&
3008 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003009 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003010 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003011 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003012 CHK("ARCH_INCLUDE_LINUX",
3013 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003014 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003015 WARN("INCLUDE_LINUX",
3016 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003017 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003018 }
3019 }
3020
Andy Whitcroft653d4872007-06-23 17:16:34 -07003021# multi-statement macros should be enclosed in a do while loop, grab the
3022# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003023# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003024 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3025 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003026 my $ln = $linenr;
3027 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003028 my ($off, $dstat, $dcond, $rest);
3029 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003030 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003031 ctx_statement_block($linenr, $realcnt, 0);
3032 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003033 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003034 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003035
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003036 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003037 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003038 $dstat =~ s/\\\n.//g;
3039 $dstat =~ s/^\s*//s;
3040 $dstat =~ s/\s*$//s;
3041
3042 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003043 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3044 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003045 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003046 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003047 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003048
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003049 # Flatten any obvious string concatentation.
3050 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3051 $dstat =~ s/$Ident\s*("X*")/$1/)
3052 {
3053 }
3054
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003055 my $exceptions = qr{
3056 $Declare|
3057 module_param_named|
3058 MODULE_PARAM_DESC|
3059 DECLARE_PER_CPU|
3060 DEFINE_PER_CPU|
Matt Wagantall9bdb2022010-06-03 12:28:18 -07003061 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003062 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003063 union|
3064 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003065 \.$Ident\s*=\s*|
3066 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003067 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003068 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003069 if ($dstat ne '' &&
3070 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3071 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Andy Whitcroftfd1b57a2012-03-23 15:02:18 -07003072 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003073 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003074 $dstat !~ /$exceptions/ &&
3075 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003076 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003077 $dstat !~ /^for\s*$Constant$/ && # for (...)
3078 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3079 $dstat !~ /^do\s*{/ && # do {...
3080 $dstat !~ /^\({/) # ({...
3081 {
3082 $ctx =~ s/\n*$//;
3083 my $herectx = $here . "\n";
3084 my $cnt = statement_rawlines($ctx);
3085
3086 for (my $n = 0; $n < $cnt; $n++) {
3087 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003088 }
3089
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003090 if ($dstat =~ /;/) {
3091 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3092 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3093 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003094 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003095 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003096 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003097 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003098 }
3099
Mike Frysinger080ba922009-01-06 14:41:25 -08003100# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3101# all assignments may have only one of the following with an assignment:
3102# .
3103# ALIGN(...)
3104# VMLINUX_SYMBOL(...)
3105 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003106 WARN("MISSING_VMLINUX_SYMBOL",
3107 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003108 }
3109
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003110# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003111 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3112 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003113 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003114 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003115 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3116 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003117 my @allowed = ();
3118 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003119 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003120 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003121 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003122 for my $chunk (@chunks) {
3123 my ($cond, $block) = @{$chunk};
3124
Andy Whitcroft773647a2008-03-28 14:15:58 -07003125 # If the condition carries leading newlines, then count those as offsets.
3126 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3127 my $offset = statement_rawlines($whitespace) - 1;
3128
Joe Perchesaad4f612012-03-23 15:02:19 -07003129 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003130 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3131
3132 # We have looked at and allowed this specific line.
3133 $suppress_ifbraces{$ln + $offset} = 1;
3134
3135 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003136 $ln += statement_rawlines($block) - 1;
3137
Andy Whitcroft773647a2008-03-28 14:15:58 -07003138 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003139
3140 $seen++ if ($block =~ /^\s*{/);
3141
Joe Perchesaad4f612012-03-23 15:02:19 -07003142 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003143 if (statement_lines($cond) > 1) {
3144 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003145 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003146 }
3147 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003148 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003149 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003150 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003151 if (statement_block_size($block) > 1) {
3152 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003153 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003154 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003155 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003156 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003157 if ($seen) {
3158 my $sum_allowed = 0;
3159 foreach (@allowed) {
3160 $sum_allowed += $_;
3161 }
3162 if ($sum_allowed == 0) {
3163 WARN("BRACES",
3164 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3165 } elsif ($sum_allowed != $allow &&
3166 $seen != $allow) {
3167 CHK("BRACES",
3168 "braces {} should be used on all arms of this statement\n" . $herectx);
3169 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003170 }
3171 }
3172 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003173 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003174 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003175 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003176
Andy Whitcroftcf655042008-03-04 14:28:20 -08003177 # Check the pre-context.
3178 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3179 #print "APW: ALLOWED: pre<$1>\n";
3180 $allowed = 1;
3181 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003182
3183 my ($level, $endln, @chunks) =
3184 ctx_statement_full($linenr, $realcnt, $-[0]);
3185
Andy Whitcroftcf655042008-03-04 14:28:20 -08003186 # Check the condition.
3187 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003188 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003189 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003190 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003191 }
3192 if (statement_lines($cond) > 1) {
3193 #print "APW: ALLOWED: cond<$cond>\n";
3194 $allowed = 1;
3195 }
3196 if ($block =~/\b(?:if|for|while)\b/) {
3197 #print "APW: ALLOWED: block<$block>\n";
3198 $allowed = 1;
3199 }
3200 if (statement_block_size($block) > 1) {
3201 #print "APW: ALLOWED: lines block<$block>\n";
3202 $allowed = 1;
3203 }
3204 # Check the post-context.
3205 if (defined $chunks[1]) {
3206 my ($cond, $block) = @{$chunks[1]};
3207 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003208 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003209 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003210 if ($block =~ /^\s*\{/) {
3211 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3212 $allowed = 1;
3213 }
3214 }
3215 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003216 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003217 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003218
Andy Whitcroftf0556632008-10-15 22:02:23 -07003219 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003220 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003221 }
3222
Joe Perches000d1cc2011-07-25 17:13:25 -07003223 WARN("BRACES",
3224 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003225 }
3226 }
3227
Andy Whitcroft653d4872007-06-23 17:16:34 -07003228# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003229 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003230 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003231 ERROR("DEPRECATED_INCLUDE",
3232 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003233 }
3234 }
3235
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003236# don't use deprecated functions
3237 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003238 if ($line =~ /\b$func\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003239 ERROR("DEPRECATED_FUNCTION",
3240 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003241 }
3242 }
3243
3244# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003245 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3246 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003247 WARN("VOLATILE",
3248 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003249 }
3250
Gregory Bean84407c92011-03-17 14:18:20 -07003251# sys_open/read/write/close are not allowed in the kernel
3252 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
3253 ERROR("$1 is inappropriate in kernel code.\n" .
3254 $herecurr);
3255 }
3256
Gregory Bean0f515702011-05-02 13:50:35 -07003257# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
3258 if ($line =~ /\b((?:read|write)[bwl])\b/) {
3259 ERROR("Use of $1 is deprecated: use $1_relaxed\n\t" .
3260 "with appropriate memory barriers instead.\n" .
3261 $herecurr);
3262 }
3263
3264# likewise, in/out[bwl] should be __raw_read/write[bwl]...
3265 if ($line =~ /\b((in|out)([bwl]))\b/) {
3266 my ($all, $pref, $suf) = ($1, $2, $3);
3267 $pref =~ s/in/read/;
3268 $pref =~ s/out/write/;
3269 ERROR("Use of $all is deprecated: use " .
3270 "__raw_$pref$suf\n\t" .
3271 "with appropriate memory barriers instead.\n" .
3272 $herecurr);
3273 }
3274
Andy Whitcroft00df3442007-06-08 13:47:06 -07003275# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003276 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikard8da6432009-10-01 12:01:44 -07003277 WARN("if this code is redundant consider removing it\n"
3278 . $herecurr);
3279 }
3280
3281# warn about #if 1
3282 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
3283 WARN("if this code is required consider removing"
3284 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003285 }
3286
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003287# check for needless kfree() checks
3288 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3289 my $expr = $1;
3290 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003291 WARN("NEEDLESS_KFREE",
3292 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003293 }
3294 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003295# check for needless usb_free_urb() checks
3296 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3297 my $expr = $1;
3298 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003299 WARN("NEEDLESS_USB_FREE_URB",
3300 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003301 }
3302 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003303
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003304# prefer usleep_range over udelay
3305 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3306 # ignore udelay's < 10, however
3307 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003308 CHK("USLEEP_RANGE",
3309 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003310 }
3311 }
3312
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003313# warn about unexpectedly long msleep's
3314 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3315 if ($1 < 20) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003316 WARN("MSLEEP",
3317 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003318 }
3319 }
3320
Israel Schlesinger20cf1852010-07-21 13:34:18 -07003321# check the patch for use of mdelay
Israel Schlesingerab131d72010-07-27 10:53:32 -07003322 if ($line =~ /\bmdelay\s*\(/) {
Israel Schlesinger20cf1852010-07-21 13:34:18 -07003323 WARN("use of mdelay() found: msleep() is the preferred API.\n" . $line );
3324 }
3325
Andy Whitcroft00df3442007-06-08 13:47:06 -07003326# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003327# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003328# print "#ifdef in C files should be avoided\n";
3329# print "$herecurr";
3330# $clean = 0;
3331# }
3332
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003333# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003334 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003335 ERROR("SPACING",
3336 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003337 }
3338
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003339# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003340 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3341 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003342 my $which = $1;
3343 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003344 CHK("UNCOMMENTED_DEFINITION",
3345 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003346 }
3347 }
3348# check for memory barriers without a comment.
3349 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3350 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003351 CHK("MEMORY_BARRIER",
3352 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003353 }
3354 }
3355# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003356 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003357 CHK("ARCH_DEFINES",
3358 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003359 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003360
Tobias Klauserd4977c72010-05-24 14:33:30 -07003361# Check that the storage class is at the beginning of a declaration
3362 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003363 WARN("STORAGE_CLASS",
3364 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003365 }
3366
Patrick Pannutoe48711e2010-07-27 17:15:07 -07003367# Check that the storage class is at the beginning of a declaration
3368 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3369 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
3370 }
3371
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003372# check the location of the inline attribute, that it is between
3373# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003374 if ($line =~ /\b$Type\s+$Inline\b/ ||
3375 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003376 ERROR("INLINE_LOCATION",
3377 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003378 }
3379
Andy Whitcroft8905a672007-11-28 16:21:06 -08003380# Check for __inline__ and __inline, prefer inline
3381 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003382 WARN("INLINE",
3383 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003384 }
3385
Joe Perches3d130fd2011-01-12 17:00:00 -08003386# Check for __attribute__ packed, prefer __packed
3387 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003388 WARN("PREFER_PACKED",
3389 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003390 }
3391
Joe Perches39b7e282011-07-25 17:13:24 -07003392# Check for __attribute__ aligned, prefer __aligned
3393 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003394 WARN("PREFER_ALIGNED",
3395 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003396 }
3397
Joe Perches5f14d3b2012-01-10 15:09:52 -08003398# Check for __attribute__ format(printf, prefer __printf
3399 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3400 WARN("PREFER_PRINTF",
3401 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3402 }
3403
Joe Perches6061d942012-03-23 15:02:16 -07003404# Check for __attribute__ format(scanf, prefer __scanf
3405 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3406 WARN("PREFER_SCANF",
3407 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3408 }
3409
Joe Perches8f53a9b2010-03-05 13:43:48 -08003410# check for sizeof(&)
3411 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003412 WARN("SIZEOF_ADDRESS",
3413 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003414 }
3415
Joe Perches428e2fd2011-05-24 17:13:39 -07003416# check for line continuations in quoted strings with odd counts of "
3417 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003418 WARN("LINE_CONTINUATIONS",
3419 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003420 }
3421
Andy Whitcroft554e1652012-01-10 15:09:57 -08003422# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003423 if ($^V && $^V ge 5.10.0 &&
3424 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003425 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003426
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003427 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003428 my $ms_val = $7;
3429 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003430
Andy Whitcroft554e1652012-01-10 15:09:57 -08003431 if ($ms_size =~ /^(0x|)0$/i) {
3432 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003433 "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 -08003434 } elsif ($ms_size =~ /^(0x|)1$/i) {
3435 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003436 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3437 }
3438 }
3439
3440# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003441 if ($^V && $^V ge 5.10.0 &&
3442 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003443 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003444 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003445 my $call = $1;
3446 my $cast1 = deparenthesize($2);
3447 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003448 my $cast2 = deparenthesize($7);
3449 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003450 my $cast;
3451
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003452 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003453 $cast = "$cast1 or $cast2";
3454 } elsif ($cast1 ne "") {
3455 $cast = $cast1;
3456 } else {
3457 $cast = $cast2;
3458 }
3459 WARN("MINMAX",
3460 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003461 }
3462 }
3463
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003464# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003465 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003466 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003467 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003468 my $function_name = $1;
3469 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003470
3471 my $s = $stat;
3472 if (defined $cond) {
3473 substr($s, 0, length($cond), '');
3474 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003475 if ($s =~ /^\s*;/ &&
3476 $function_name ne 'uninitialized_var')
3477 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003478 WARN("AVOID_EXTERNS",
3479 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003480 }
3481
3482 if ($paren_space =~ /\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003483 WARN("FUNCTION_ARGUMENTS",
3484 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003485 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003486
3487 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3488 $stat =~ /^.\s*extern\s+/)
3489 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003490 WARN("AVOID_EXTERNS",
3491 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003492 }
3493
3494# checks for new __setup's
3495 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3496 my $name = $1;
3497
3498 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003499 CHK("UNDOCUMENTED_SETUP",
3500 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003501 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003502 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003503
3504# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003505 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003506 WARN("UNNECESSARY_CASTS",
3507 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003508 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003509
Joe Perchescaf2a542011-01-12 16:59:56 -08003510# check for multiple semicolons
3511 if ($line =~ /;\s*;\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003512 WARN("ONE_SEMICOLON",
3513 "Statements terminations use 1 semicolon\n" . $herecurr);
Joe Perchescaf2a542011-01-12 16:59:56 -08003514 }
3515
Patrick Pannutof2b08d32010-07-23 13:34:18 -07003516# check for return codes on error paths
3517 if ($line =~ /\breturn\s+-\d+/) {
Patrick Pannutoedf25932010-08-04 14:30:59 -07003518 ERROR("illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof2b08d32010-07-23 13:34:18 -07003519 }
3520
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003521# check for gcc specific __FUNCTION__
3522 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003523 WARN("USE_FUNC",
3524 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003525 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003526
Joe Perches2c924882012-03-23 15:02:20 -07003527# check for use of yield()
3528 if ($line =~ /\byield\s*\(\s*\)/) {
3529 WARN("YIELD",
3530 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3531 }
3532
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003533# check for semaphores initialized locked
3534 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003535 WARN("CONSIDER_COMPLETION",
3536 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003537 }
Joe Perches6712d852012-03-23 15:02:20 -07003538
Joe Perches67d0a072011-10-31 17:13:10 -07003539# recommend kstrto* over simple_strto* and strict_strto*
3540 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003541 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003542 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003543 }
Joe Perches6712d852012-03-23 15:02:20 -07003544
Michael Ellermanf3db6632008-07-23 21:28:57 -07003545# check for __initcall(), use device_initcall() explicitly please
3546 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003547 WARN("USE_DEVICE_INITCALL",
3548 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003549 }
Joe Perches6712d852012-03-23 15:02:20 -07003550
Emese Revfy79404842010-03-05 13:43:53 -08003551# check for various ops structs, ensure they are const.
3552 my $struct_ops = qr{acpi_dock_ops|
3553 address_space_operations|
3554 backlight_ops|
3555 block_device_operations|
3556 dentry_operations|
3557 dev_pm_ops|
3558 dma_map_ops|
3559 extent_io_ops|
3560 file_lock_operations|
3561 file_operations|
3562 hv_ops|
3563 ide_dma_ops|
3564 intel_dvo_dev_ops|
3565 item_operations|
3566 iwl_ops|
3567 kgdb_arch|
3568 kgdb_io|
3569 kset_uevent_ops|
3570 lock_manager_operations|
3571 microcode_ops|
3572 mtrr_ops|
3573 neigh_ops|
3574 nlmsvc_binding|
3575 pci_raw_ops|
3576 pipe_buf_operations|
3577 platform_hibernation_ops|
3578 platform_suspend_ops|
3579 proto_ops|
3580 rpc_pipe_ops|
3581 seq_operations|
3582 snd_ac97_build_ops|
3583 soc_pcmcia_socket_ops|
3584 stacktrace_ops|
3585 sysfs_ops|
3586 tty_operations|
3587 usb_mon_operations|
3588 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003589 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003590 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003591 WARN("CONST_STRUCT",
3592 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003593 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003594 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003595
3596# use of NR_CPUS is usually wrong
3597# ignore definitions of NR_CPUS and usage to define arrays as likely right
3598 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003599 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3600 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003601 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3602 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3603 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003604 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003605 WARN("NR_CPUS",
3606 "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 -07003607 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003608
3609# check for %L{u,d,i} in strings
3610 my $string;
3611 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3612 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003613 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003614 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003615 WARN("PRINTF_L",
3616 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003617 last;
3618 }
3619 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003620
3621# whine mightly about in_atomic
3622 if ($line =~ /\bin_atomic\s*\(/) {
3623 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003624 ERROR("IN_ATOMIC",
3625 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003626 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003627 WARN("IN_ATOMIC",
3628 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003629 }
3630 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003631
3632# check for lockdep_set_novalidate_class
3633 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3634 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3635 if ($realfile !~ m@^kernel/lockdep@ &&
3636 $realfile !~ m@^include/linux/lockdep@ &&
3637 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003638 ERROR("LOCKDEP",
3639 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003640 }
3641 }
Dave Jones88f88312011-01-12 16:59:59 -08003642
3643 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3644 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003645 WARN("EXPORTED_WORLD_WRITABLE",
3646 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003647 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003648 }
3649
3650 # If we have no input at all, then there is nothing to report on
3651 # so just keep quiet.
3652 if ($#rawlines == -1) {
3653 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003654 }
3655
Andy Whitcroft8905a672007-11-28 16:21:06 -08003656 # In mailback mode only produce a report in the negative, for
3657 # things that appear to be patches.
3658 if ($mailback && ($clean == 1 || !$is_patch)) {
3659 exit(0);
3660 }
3661
3662 # This is not a patch, and we are are in 'no-patch' mode so
3663 # just keep quiet.
3664 if (!$chk_patch && !$is_patch) {
3665 exit(0);
3666 }
3667
3668 if (!$is_patch) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003669 ERROR("NOT_UNIFIED_DIFF",
3670 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003671 }
3672 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003673 ERROR("MISSING_SIGN_OFF",
3674 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003675 }
3676
Andy Whitcroft8905a672007-11-28 16:21:06 -08003677 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003678 if ($summary && !($clean == 1 && $quiet == 1)) {
3679 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003680 print "total: $cnt_error errors, $cnt_warn warnings, " .
3681 (($check)? "$cnt_chk checks, " : "") .
3682 "$cnt_lines lines checked\n";
3683 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003684 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003685
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003686 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003687
3688 if ($^V lt 5.10.0) {
3689 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3690 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3691 }
3692
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003693 # If there were whitespace errors which cleanpatch can fix
3694 # then suggest that.
3695 if ($rpt_cleaners) {
3696 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3697 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003698 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003699 }
3700 }
3701
Artem Bityutskiy11232682012-03-23 15:02:17 -07003702 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003703 print "NOTE: Ignored message types:";
3704 foreach my $ignore (sort keys %ignore_type) {
3705 print " $ignore";
3706 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003707 print "\n\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07003708 }
3709
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003710 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003711 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003712 }
3713 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003714 print << "EOM";
3715$vname has style problems, please review.
3716
3717If any of these errors are false positives, please report
3718them to the maintainer, see CHECKPATCH in MAINTAINERS.
3719EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003720 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003721
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003722 return $clean;
3723}