blob: b8e78d51c2aa64879efee4dcc88657ed868cefe5 [file] [log] [blame]
Kamil Rytarowskib7d5a9c2017-04-26 15:16:04 +02001#!/usr/bin/env perl
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Kamil Rytarowskib7d5a9c2017-04-26 15:16:04 +02009use warnings;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000010
11my $P = $0;
12$P =~ s@.*/@@g;
13
Paolo Bonzini777d05b2017-10-04 16:35:53 +020014our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
15
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000016my $V = '0.31';
17
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010023my $chk_patch = undef;
24my $chk_branch = undef;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000025my $tst_only;
26my $emacs = 0;
27my $terse = 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010028my $file = undef;
Paolo Bonzini141de882016-08-09 17:47:44 +020029my $no_warnings = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000030my $summary = 1;
31my $mailback = 0;
32my $summary_file = 0;
33my $root;
34my %debug;
35my $help = 0;
36
37sub help {
38 my ($exitcode) = @_;
39
40 print << "EOM";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010041Usage:
42
43 $P [OPTION]... [FILE]...
44 $P [OPTION]... [GIT-REV-LIST]
45
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000046Version: $V
47
48Options:
49 -q, --quiet quiet
50 --no-tree run without a kernel tree
51 --no-signoff do not check for 'Signed-off-by' line
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010052 --patch treat FILE as patchfile
53 --branch treat args as GIT revision list
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000054 --emacs emacs compile window format
55 --terse one line per report
56 -f, --file treat FILE as regular source file
Paolo Bonzini141de882016-08-09 17:47:44 +020057 --strict fail if only warnings are found
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000058 --root=PATH PATH to the kernel tree root
59 --no-summary suppress the per-file summary
60 --mailback only produce a report in case of warnings/errors
61 --summary-file include the filename in summary
62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
64 is all off)
65 --test-only=WORD report only warnings/errors containing WORD
66 literally
67 -h, --help, --version display this help and exit
68
69When FILE is - read standard input.
70EOM
71
72 exit($exitcode);
73}
74
75GetOptions(
76 'q|quiet+' => \$quiet,
77 'tree!' => \$tree,
78 'signoff!' => \$chk_signoff,
79 'patch!' => \$chk_patch,
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010080 'branch!' => \$chk_branch,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000081 'emacs!' => \$emacs,
82 'terse!' => \$terse,
83 'f|file!' => \$file,
Paolo Bonzini141de882016-08-09 17:47:44 +020084 'strict!' => \$no_warnings,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000085 'root=s' => \$root,
86 'summary!' => \$summary,
87 'mailback!' => \$mailback,
88 'summary-file!' => \$summary_file,
89
90 'debug=s' => \%debug,
91 'test-only=s' => \$tst_only,
92 'h|help' => \$help,
93 'version' => \$help
94) or help(1);
95
96help(0) if ($help);
97
98my $exit = 0;
99
100if ($#ARGV < 0) {
101 print "$P: no input files\n";
102 exit(1);
103}
104
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100105if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200106 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
107 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
108 $chk_patch = $chk_branch || $file ? 0 : 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100109} elsif (!defined $chk_branch && !defined $chk_patch) {
110 if ($file) {
111 $chk_branch = $chk_patch = 0;
112 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200113 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100114 $chk_patch = $chk_branch ? 0 : 1;
115 }
116} elsif (!defined $chk_branch && !defined $file) {
117 if ($chk_patch) {
118 $chk_branch = $file = 0;
119 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200120 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100121 $file = $chk_branch ? 0 : 1;
122 }
123} elsif (!defined $chk_patch && !defined $file) {
124 if ($chk_branch) {
125 $chk_patch = $file = 0;
126 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200127 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
128 $chk_patch = $file ? 0 : 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100129 }
130} elsif (!defined $chk_branch) {
131 $chk_branch = $chk_patch || $file ? 0 : 1;
132} elsif (!defined $chk_patch) {
133 $chk_patch = $chk_branch || $file ? 0 : 1;
134} elsif (!defined $file) {
135 $file = $chk_patch || $chk_branch ? 0 : 1;
136}
137
138if (($chk_patch && $chk_branch) ||
139 ($chk_patch && $file) ||
140 ($chk_branch && $file)) {
141 die "Only one of --file, --branch, --patch is permitted\n";
142}
143if (!$chk_patch && !$chk_branch && !$file) {
144 die "One of --file, --branch, --patch is required\n";
145}
146
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000147my $dbg_values = 0;
148my $dbg_possible = 0;
149my $dbg_type = 0;
150my $dbg_attr = 0;
Don Slutza99ac042012-09-02 19:22:35 -0400151my $dbg_adv_dcs = 0;
Don Slutz54243022012-09-02 19:22:36 -0400152my $dbg_adv_checking = 0;
Don Slutz69402a62012-09-02 19:22:37 -0400153my $dbg_adv_apw = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000154for my $key (keys %debug) {
155 ## no critic
156 eval "\${dbg_$key} = '$debug{$key}';";
157 die "$@" if ($@);
158}
159
160my $rpt_cleaners = 0;
161
162if ($terse) {
163 $emacs = 1;
164 $quiet++;
165}
166
167if ($tree) {
168 if (defined $root) {
169 if (!top_of_kernel_tree($root)) {
170 die "$P: $root: --root does not point at a valid tree\n";
171 }
172 } else {
173 if (top_of_kernel_tree('.')) {
174 $root = '.';
175 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
176 top_of_kernel_tree($1)) {
177 $root = $1;
178 }
179 }
180
181 if (!defined $root) {
182 print "Must be run from the top-level dir. of a kernel tree\n";
183 exit(2);
184 }
185}
186
187my $emitted_corrupt = 0;
188
189our $Ident = qr{
190 [A-Za-z_][A-Za-z\d_]*
191 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
192 }x;
193our $Storage = qr{extern|static|asmlinkage};
194our $Sparse = qr{
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200195 __force
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000196 }x;
197
198# Notes to $Attribute:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000199our $Attribute = qr{
200 const|
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200201 volatile|
202 QEMU_NORETURN|
203 QEMU_WARN_UNUSED_RESULT|
204 QEMU_SENTINEL|
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200205 QEMU_PACKED|
206 GCC_FMT_ATTR
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000207 }x;
208our $Modifier;
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200209our $Inline = qr{inline};
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000210our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
211our $Lval = qr{$Ident(?:$Member)*};
212
213our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
214our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
215our $Compare = qr{<=|>=|==|!=|<|>};
216our $Operators = qr{
217 <=|>=|==|!=|
218 =>|->|<<|>>|<|>|!|~|
219 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
220 }x;
221
222our $NonptrType;
223our $Type;
224our $Declare;
225
Joe Perchesb37c4aa2018-04-30 13:46:47 +0100226our $NON_ASCII_UTF8 = qr{
227 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000228 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
229 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
230 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
231 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
232 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
233 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
234}x;
235
Joe Perchesb37c4aa2018-04-30 13:46:47 +0100236our $UTF8 = qr{
237 [\x09\x0A\x0D\x20-\x7E] # ASCII
238 | $NON_ASCII_UTF8
239}x;
240
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200241# There are still some false positives, but this catches most
242# common cases.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000243our $typeTypedefs = qr{(?x:
Philippe Mathieu-Daudé5fa96ca2018-06-25 09:41:56 -0300244 (?![KMGTPE]iB) # IEC binary prefix (do not match)
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200245 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
246 | [A-Z][A-Z\d_]*AIOCB # all uppercase
247 | [A-Z][A-Z\d_]*CPU # all uppercase
248 | QEMUBH # all uppercase
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000249)};
250
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000251our @typeList = (
252 qr{void},
253 qr{(?:unsigned\s+)?char},
254 qr{(?:unsigned\s+)?short},
255 qr{(?:unsigned\s+)?int},
256 qr{(?:unsigned\s+)?long},
257 qr{(?:unsigned\s+)?long\s+int},
258 qr{(?:unsigned\s+)?long\s+long},
259 qr{(?:unsigned\s+)?long\s+long\s+int},
260 qr{unsigned},
261 qr{float},
262 qr{double},
263 qr{bool},
264 qr{struct\s+$Ident},
265 qr{union\s+$Ident},
266 qr{enum\s+$Ident},
267 qr{${Ident}_t},
268 qr{${Ident}_handler},
269 qr{${Ident}_handler_fn},
Cédric Le Goaterf0707d22016-04-01 11:40:06 +0200270 qr{target_(?:u)?long},
Greg Kurz825bfa02017-09-14 11:12:07 +0200271 qr{hwaddr},
Peter Xu82870f32018-04-25 15:01:03 +0800272 # external libraries
Klim Kireeved279a02018-01-12 12:01:19 +0300273 qr{xml${Ident}},
Paul Durrant8d0f08e2018-05-22 11:42:50 -0700274 qr{xen\w+_handle},
Peter Xu82870f32018-04-25 15:01:03 +0800275 # Glib definitions
276 qr{gchar},
277 qr{gshort},
278 qr{glong},
279 qr{gint},
280 qr{gboolean},
281 qr{guchar},
282 qr{gushort},
283 qr{gulong},
284 qr{guint},
285 qr{gfloat},
286 qr{gdouble},
287 qr{gpointer},
288 qr{gconstpointer},
289 qr{gint8},
290 qr{guint8},
291 qr{gint16},
292 qr{guint16},
293 qr{gint32},
294 qr{guint32},
295 qr{gint64},
296 qr{guint64},
297 qr{gsize},
298 qr{gssize},
299 qr{goffset},
300 qr{gintptr},
301 qr{guintptr},
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000302);
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200303
304# This can be modified by sub possible. Since it can be empty, be careful
305# about regexes that always match, because they can cause infinite loops.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000306our @modifierList = (
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000307);
308
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000309sub build_types {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000310 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200311 if (@modifierList > 0) {
312 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
313 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
314 } else {
315 $Modifier = qr{(?:$Attribute|$Sparse)};
316 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000317 $NonptrType = qr{
318 (?:$Modifier\s+|const\s+)*
319 (?:
320 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
321 (?:$typeTypedefs\b)|
322 (?:${all}\b)
323 )
324 (?:\s+$Modifier|\s+const)*
325 }x;
326 $Type = qr{
327 $NonptrType
328 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
329 (?:\s+$Inline|\s+$Modifier)*
330 }x;
331 $Declare = qr{(?:$Storage\s+)?$Type};
332}
333build_types();
334
335$chk_signoff = 0 if ($file);
336
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000337my @rawlines = ();
338my @lines = ();
339my $vname;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100340if ($chk_branch) {
341 my @patches;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100342 my %git_commits = ();
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100343 my $HASH;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100344 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) ||
345 die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100346
Paolo Bonzinic182b612018-11-29 09:35:47 +0100347 for my $line (<$HASH>) {
348 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
349 next if (!defined($1) || !defined($2));
350 my $sha1 = $1;
351 my $subject = $2;
352 push(@patches, $sha1);
353 $git_commits{$sha1} = $subject;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000354 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100355
356 close $HASH;
357
358 die "$P: no revisions returned for revlist '$chk_branch'\n"
359 unless @patches;
360
Paolo Bonzinic182b612018-11-29 09:35:47 +0100361 my $i = 1;
362 my $num_patches = @patches;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100363 for my $hash (@patches) {
364 my $FILE;
365 open($FILE, '-|', "git", "show", $hash) ||
366 die "$P: git show $hash - $!\n";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100367 while (<$FILE>) {
368 chomp;
369 push(@rawlines, $_);
370 }
371 close($FILE);
Paolo Bonzinic182b612018-11-29 09:35:47 +0100372 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')';
373 if ($num_patches > 1 && $quiet == 0) {
374 print "$i/$num_patches Checking commit $vname\n";
375 $vname = "Patch $i/$num_patches";
376 } else {
377 $vname = "Commit " . $vname;
378 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100379 if (!process($hash)) {
380 $exit = 1;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100381 print "\n" if ($num_patches > 1 && $quiet == 0);
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100382 }
383 @rawlines = ();
384 @lines = ();
Paolo Bonzinic182b612018-11-29 09:35:47 +0100385 $i++;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000386 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100387} else {
388 for my $filename (@ARGV) {
389 my $FILE;
390 if ($file) {
391 open($FILE, '-|', "diff -u /dev/null $filename") ||
392 die "$P: $filename: diff failed - $!\n";
393 } elsif ($filename eq '-') {
394 open($FILE, '<&STDIN');
395 } else {
396 open($FILE, '<', "$filename") ||
397 die "$P: $filename: open failed - $!\n";
398 }
399 if ($filename eq '-') {
400 $vname = 'Your patch';
401 } else {
402 $vname = $filename;
403 }
Paolo Bonzinic182b612018-11-29 09:35:47 +0100404 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100405 while (<$FILE>) {
406 chomp;
407 push(@rawlines, $_);
408 }
409 close($FILE);
410 if (!process($filename)) {
411 $exit = 1;
412 }
413 @rawlines = ();
414 @lines = ();
415 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000416}
417
418exit($exit);
419
420sub top_of_kernel_tree {
421 my ($root) = @_;
422
423 my @tree_check = (
Blue Swirlb6469682011-01-20 20:58:56 +0000424 "COPYING", "MAINTAINERS", "Makefile",
425 "README", "docs", "VERSION",
426 "vl.c"
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000427 );
428
429 foreach my $check (@tree_check) {
430 if (! -e $root . '/' . $check) {
431 return 0;
432 }
433 }
434 return 1;
435}
436
437sub expand_tabs {
438 my ($str) = @_;
439
440 my $res = '';
441 my $n = 0;
442 for my $c (split(//, $str)) {
443 if ($c eq "\t") {
444 $res .= ' ';
445 $n++;
446 for (; ($n % 8) != 0; $n++) {
447 $res .= ' ';
448 }
449 next;
450 }
451 $res .= $c;
452 $n++;
453 }
454
455 return $res;
456}
457sub copy_spacing {
458 (my $res = shift) =~ tr/\t/ /c;
459 return $res;
460}
461
462sub line_stats {
463 my ($line) = @_;
464
465 # Drop the diff line leader and expand tabs
466 $line =~ s/^.//;
467 $line = expand_tabs($line);
468
469 # Pick the indent from the front of the line.
470 my ($white) = ($line =~ /^(\s*)/);
471
472 return (length($line), length($white));
473}
474
475my $sanitise_quote = '';
476
477sub sanitise_line_reset {
478 my ($in_comment) = @_;
479
480 if ($in_comment) {
481 $sanitise_quote = '*/';
482 } else {
483 $sanitise_quote = '';
484 }
485}
486sub sanitise_line {
487 my ($line) = @_;
488
489 my $res = '';
490 my $l = '';
491
492 my $qlen = 0;
493 my $off = 0;
494 my $c;
495
496 # Always copy over the diff marker.
497 $res = substr($line, 0, 1);
498
499 for ($off = 1; $off < length($line); $off++) {
500 $c = substr($line, $off, 1);
501
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100502 # Comments we are wacking completely including the begin
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000503 # and end, all to $;.
504 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
505 $sanitise_quote = '*/';
506
507 substr($res, $off, 2, "$;$;");
508 $off++;
509 next;
510 }
511 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
512 $sanitise_quote = '';
513 substr($res, $off, 2, "$;$;");
514 $off++;
515 next;
516 }
517 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
518 $sanitise_quote = '//';
519
520 substr($res, $off, 2, $sanitise_quote);
521 $off++;
522 next;
523 }
524
525 # A \ in a string means ignore the next character.
526 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
527 $c eq "\\") {
528 substr($res, $off, 2, 'XX');
529 $off++;
530 next;
531 }
532 # Regular quotes.
533 if ($c eq "'" || $c eq '"') {
534 if ($sanitise_quote eq '') {
535 $sanitise_quote = $c;
536
537 substr($res, $off, 1, $c);
538 next;
539 } elsif ($sanitise_quote eq $c) {
540 $sanitise_quote = '';
541 }
542 }
543
544 #print "c<$c> SQ<$sanitise_quote>\n";
545 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
546 substr($res, $off, 1, $;);
547 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
548 substr($res, $off, 1, $;);
549 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
550 substr($res, $off, 1, 'X');
551 } else {
552 substr($res, $off, 1, $c);
553 }
554 }
555
556 if ($sanitise_quote eq '//') {
557 $sanitise_quote = '';
558 }
559
560 # The pathname on a #include may be surrounded by '<' and '>'.
561 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
562 my $clean = 'X' x length($1);
563 $res =~ s@\<.*\>@<$clean>@;
564
565 # The whole of a #error is a string.
566 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
567 my $clean = 'X' x length($1);
568 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
569 }
570
571 return $res;
572}
573
574sub ctx_statement_block {
575 my ($linenr, $remain, $off) = @_;
576 my $line = $linenr - 1;
577 my $blk = '';
578 my $soff = $off;
579 my $coff = $off - 1;
580 my $coff_set = 0;
581
582 my $loff = 0;
583
584 my $type = '';
585 my $level = 0;
586 my @stack = ();
587 my $p;
588 my $c;
589 my $len = 0;
590
591 my $remainder;
592 while (1) {
593 @stack = (['', 0]) if ($#stack == -1);
594
595 #warn "CSB: blk<$blk> remain<$remain>\n";
596 # If we are about to drop off the end, pull in more
597 # context.
598 if ($off >= $len) {
599 for (; $remain > 0; $line++) {
600 last if (!defined $lines[$line]);
601 next if ($lines[$line] =~ /^-/);
602 $remain--;
603 $loff = $len;
604 $blk .= $lines[$line] . "\n";
605 $len = length($blk);
606 $line++;
607 last;
608 }
609 # Bail if there is no further context.
610 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
611 if ($off >= $len) {
612 last;
613 }
614 }
615 $p = $c;
616 $c = substr($blk, $off, 1);
617 $remainder = substr($blk, $off);
618
619 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
620
621 # Handle nested #if/#else.
622 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
623 push(@stack, [ $type, $level ]);
624 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
625 ($type, $level) = @{$stack[$#stack - 1]};
626 } elsif ($remainder =~ /^#\s*endif\b/) {
627 ($type, $level) = @{pop(@stack)};
628 }
629
630 # Statement ends at the ';' or a close '}' at the
631 # outermost level.
632 if ($level == 0 && $c eq ';') {
633 last;
634 }
635
636 # An else is really a conditional as long as its not else if
637 if ($level == 0 && $coff_set == 0 &&
638 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
639 $remainder =~ /^(else)(?:\s|{)/ &&
640 $remainder !~ /^else\s+if\b/) {
641 $coff = $off + length($1) - 1;
642 $coff_set = 1;
643 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
644 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
645 }
646
647 if (($type eq '' || $type eq '(') && $c eq '(') {
648 $level++;
649 $type = '(';
650 }
651 if ($type eq '(' && $c eq ')') {
652 $level--;
653 $type = ($level != 0)? '(' : '';
654
655 if ($level == 0 && $coff < $soff) {
656 $coff = $off;
657 $coff_set = 1;
658 #warn "CSB: mark coff<$coff>\n";
659 }
660 }
661 if (($type eq '' || $type eq '{') && $c eq '{') {
662 $level++;
663 $type = '{';
664 }
665 if ($type eq '{' && $c eq '}') {
666 $level--;
667 $type = ($level != 0)? '{' : '';
668
669 if ($level == 0) {
670 if (substr($blk, $off + 1, 1) eq ';') {
671 $off++;
672 }
673 last;
674 }
675 }
676 $off++;
677 }
678 # We are truly at the end, so shuffle to the next line.
679 if ($off == $len) {
680 $loff = $len + 1;
681 $line++;
682 $remain--;
683 }
684
685 my $statement = substr($blk, $soff, $off - $soff + 1);
686 my $condition = substr($blk, $soff, $coff - $soff + 1);
687
688 #warn "STATEMENT<$statement>\n";
689 #warn "CONDITION<$condition>\n";
690
691 #print "coff<$coff> soff<$off> loff<$loff>\n";
692
693 return ($statement, $condition,
694 $line, $remain + 1, $off - $loff + 1, $level);
695}
696
697sub statement_lines {
698 my ($stmt) = @_;
699
700 # Strip the diff line prefixes and rip blank lines at start and end.
701 $stmt =~ s/(^|\n)./$1/g;
702 $stmt =~ s/^\s*//;
703 $stmt =~ s/\s*$//;
704
705 my @stmt_lines = ($stmt =~ /\n/g);
706
707 return $#stmt_lines + 2;
708}
709
710sub statement_rawlines {
711 my ($stmt) = @_;
712
713 my @stmt_lines = ($stmt =~ /\n/g);
714
715 return $#stmt_lines + 2;
716}
717
718sub statement_block_size {
719 my ($stmt) = @_;
720
721 $stmt =~ s/(^|\n)./$1/g;
Fam Zheng04f25622015-09-11 19:07:36 +0800722 $stmt =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000723 $stmt =~ s/}\s*$//;
724 $stmt =~ s/^\s*//;
725 $stmt =~ s/\s*$//;
726
727 my @stmt_lines = ($stmt =~ /\n/g);
728 my @stmt_statements = ($stmt =~ /;/g);
729
730 my $stmt_lines = $#stmt_lines + 2;
731 my $stmt_statements = $#stmt_statements + 1;
732
733 if ($stmt_lines > $stmt_statements) {
734 return $stmt_lines;
735 } else {
736 return $stmt_statements;
737 }
738}
739
740sub ctx_statement_full {
741 my ($linenr, $remain, $off) = @_;
742 my ($statement, $condition, $level);
743
744 my (@chunks);
745
746 # Grab the first conditional/block pair.
747 ($statement, $condition, $linenr, $remain, $off, $level) =
748 ctx_statement_block($linenr, $remain, $off);
749 #print "F: c<$condition> s<$statement> remain<$remain>\n";
750 push(@chunks, [ $condition, $statement ]);
751 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
752 return ($level, $linenr, @chunks);
753 }
754
755 # Pull in the following conditional/block pairs and see if they
756 # could continue the statement.
757 for (;;) {
758 ($statement, $condition, $linenr, $remain, $off, $level) =
759 ctx_statement_block($linenr, $remain, $off);
760 #print "C: c<$condition> s<$statement> remain<$remain>\n";
761 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
762 #print "C: push\n";
763 push(@chunks, [ $condition, $statement ]);
764 }
765
766 return ($level, $linenr, @chunks);
767}
768
769sub ctx_block_get {
770 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
771 my $line;
772 my $start = $linenr - 1;
773 my $blk = '';
774 my @o;
775 my @c;
776 my @res = ();
777
778 my $level = 0;
779 my @stack = ($level);
780 for ($line = $start; $remain > 0; $line++) {
781 next if ($rawlines[$line] =~ /^-/);
782 $remain--;
783
784 $blk .= $rawlines[$line];
785
786 # Handle nested #if/#else.
787 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
788 push(@stack, $level);
789 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
790 $level = $stack[$#stack - 1];
791 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
792 $level = pop(@stack);
793 }
794
795 foreach my $c (split(//, $lines[$line])) {
796 ##print "C<$c>L<$level><$open$close>O<$off>\n";
797 if ($off > 0) {
798 $off--;
799 next;
800 }
801
802 if ($c eq $close && $level > 0) {
803 $level--;
804 last if ($level == 0);
805 } elsif ($c eq $open) {
806 $level++;
807 }
808 }
809
810 if (!$outer || $level <= 1) {
811 push(@res, $rawlines[$line]);
812 }
813
814 last if ($level == 0);
815 }
816
817 return ($level, @res);
818}
819sub ctx_block_outer {
820 my ($linenr, $remain) = @_;
821
822 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
823 return @r;
824}
825sub ctx_block {
826 my ($linenr, $remain) = @_;
827
828 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
829 return @r;
830}
831sub ctx_statement {
832 my ($linenr, $remain, $off) = @_;
833
834 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
835 return @r;
836}
837sub ctx_block_level {
838 my ($linenr, $remain) = @_;
839
840 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
841}
842sub ctx_statement_level {
843 my ($linenr, $remain, $off) = @_;
844
845 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
846}
847
848sub ctx_locate_comment {
849 my ($first_line, $end_line) = @_;
850
851 # Catch a comment on the end of the line itself.
852 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
853 return $current_comment if (defined $current_comment);
854
855 # Look through the context and try and figure out if there is a
856 # comment.
857 my $in_comment = 0;
858 $current_comment = '';
859 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
860 my $line = $rawlines[$linenr - 1];
861 #warn " $line\n";
862 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
863 $in_comment = 1;
864 }
865 if ($line =~ m@/\*@) {
866 $in_comment = 1;
867 }
868 if (!$in_comment && $current_comment ne '') {
869 $current_comment = '';
870 }
871 $current_comment .= $line . "\n" if ($in_comment);
872 if ($line =~ m@\*/@) {
873 $in_comment = 0;
874 }
875 }
876
877 chomp($current_comment);
878 return($current_comment);
879}
880sub ctx_has_comment {
881 my ($first_line, $end_line) = @_;
882 my $cmt = ctx_locate_comment($first_line, $end_line);
883
884 ##print "LINE: $rawlines[$end_line - 1 ]\n";
885 ##print "CMMT: $cmt\n";
886
887 return ($cmt ne '');
888}
889
890sub raw_line {
891 my ($linenr, $cnt) = @_;
892
893 my $offset = $linenr - 1;
894 $cnt++;
895
896 my $line;
897 while ($cnt) {
898 $line = $rawlines[$offset++];
899 next if (defined($line) && $line =~ /^-/);
900 $cnt--;
901 }
902
903 return $line;
904}
905
906sub cat_vet {
907 my ($vet) = @_;
908 my ($res, $coded);
909
910 $res = '';
911 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
912 $res .= $1;
913 if ($2 ne '') {
914 $coded = sprintf("^%c", unpack('C', $2) + 64);
915 $res .= $coded;
916 }
917 }
918 $res =~ s/$/\$/;
919
920 return $res;
921}
922
923my $av_preprocessor = 0;
924my $av_pending;
925my @av_paren_type;
926my $av_pend_colon;
927
928sub annotate_reset {
929 $av_preprocessor = 0;
930 $av_pending = '_';
931 @av_paren_type = ('E');
932 $av_pend_colon = 'O';
933}
934
935sub annotate_values {
936 my ($stream, $type) = @_;
937
938 my $res;
939 my $var = '_' x length($stream);
940 my $cur = $stream;
941
942 print "$stream\n" if ($dbg_values > 1);
943
944 while (length($cur)) {
945 @av_paren_type = ('E') if ($#av_paren_type < 0);
946 print " <" . join('', @av_paren_type) .
947 "> <$type> <$av_pending>" if ($dbg_values > 1);
948 if ($cur =~ /^(\s+)/o) {
949 print "WS($1)\n" if ($dbg_values > 1);
950 if ($1 =~ /\n/ && $av_preprocessor) {
951 $type = pop(@av_paren_type);
952 $av_preprocessor = 0;
953 }
954
Florian Mickler61669f92011-11-25 10:24:16 +0100955 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000956 print "CAST($1)\n" if ($dbg_values > 1);
957 push(@av_paren_type, $type);
958 $type = 'C';
959
960 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
961 print "DECLARE($1)\n" if ($dbg_values > 1);
962 $type = 'T';
963
964 } elsif ($cur =~ /^($Modifier)\s*/) {
965 print "MODIFIER($1)\n" if ($dbg_values > 1);
966 $type = 'T';
967
968 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
969 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
970 $av_preprocessor = 1;
971 push(@av_paren_type, $type);
972 if ($2 ne '') {
973 $av_pending = 'N';
974 }
975 $type = 'E';
976
977 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
978 print "UNDEF($1)\n" if ($dbg_values > 1);
979 $av_preprocessor = 1;
980 push(@av_paren_type, $type);
981
982 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
983 print "PRE_START($1)\n" if ($dbg_values > 1);
984 $av_preprocessor = 1;
985
986 push(@av_paren_type, $type);
987 push(@av_paren_type, $type);
988 $type = 'E';
989
990 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
991 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
992 $av_preprocessor = 1;
993
994 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
995
996 $type = 'E';
997
998 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
999 print "PRE_END($1)\n" if ($dbg_values > 1);
1000
1001 $av_preprocessor = 1;
1002
1003 # Assume all arms of the conditional end as this
1004 # one does, and continue as if the #endif was not here.
1005 pop(@av_paren_type);
1006 push(@av_paren_type, $type);
1007 $type = 'E';
1008
1009 } elsif ($cur =~ /^(\\\n)/o) {
1010 print "PRECONT($1)\n" if ($dbg_values > 1);
1011
1012 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1013 print "ATTR($1)\n" if ($dbg_values > 1);
1014 $av_pending = $type;
1015 $type = 'N';
1016
1017 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1018 print "SIZEOF($1)\n" if ($dbg_values > 1);
1019 if (defined $2) {
1020 $av_pending = 'V';
1021 }
1022 $type = 'N';
1023
1024 } elsif ($cur =~ /^(if|while|for)\b/o) {
1025 print "COND($1)\n" if ($dbg_values > 1);
1026 $av_pending = 'E';
1027 $type = 'N';
1028
1029 } elsif ($cur =~/^(case)/o) {
1030 print "CASE($1)\n" if ($dbg_values > 1);
1031 $av_pend_colon = 'C';
1032 $type = 'N';
1033
1034 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1035 print "KEYWORD($1)\n" if ($dbg_values > 1);
1036 $type = 'N';
1037
1038 } elsif ($cur =~ /^(\()/o) {
1039 print "PAREN('$1')\n" if ($dbg_values > 1);
1040 push(@av_paren_type, $av_pending);
1041 $av_pending = '_';
1042 $type = 'N';
1043
1044 } elsif ($cur =~ /^(\))/o) {
1045 my $new_type = pop(@av_paren_type);
1046 if ($new_type ne '_') {
1047 $type = $new_type;
1048 print "PAREN('$1') -> $type\n"
1049 if ($dbg_values > 1);
1050 } else {
1051 print "PAREN('$1')\n" if ($dbg_values > 1);
1052 }
1053
1054 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1055 print "FUNC($1)\n" if ($dbg_values > 1);
1056 $type = 'V';
1057 $av_pending = 'V';
1058
1059 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1060 if (defined $2 && $type eq 'C' || $type eq 'T') {
1061 $av_pend_colon = 'B';
1062 } elsif ($type eq 'E') {
1063 $av_pend_colon = 'L';
1064 }
1065 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1066 $type = 'V';
1067
1068 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1069 print "IDENT($1)\n" if ($dbg_values > 1);
1070 $type = 'V';
1071
1072 } elsif ($cur =~ /^($Assignment)/o) {
1073 print "ASSIGN($1)\n" if ($dbg_values > 1);
1074 $type = 'N';
1075
1076 } elsif ($cur =~/^(;|{|})/) {
1077 print "END($1)\n" if ($dbg_values > 1);
1078 $type = 'E';
1079 $av_pend_colon = 'O';
1080
1081 } elsif ($cur =~/^(,)/) {
1082 print "COMMA($1)\n" if ($dbg_values > 1);
1083 $type = 'C';
1084
1085 } elsif ($cur =~ /^(\?)/o) {
1086 print "QUESTION($1)\n" if ($dbg_values > 1);
1087 $type = 'N';
1088
1089 } elsif ($cur =~ /^(:)/o) {
1090 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1091
1092 substr($var, length($res), 1, $av_pend_colon);
1093 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1094 $type = 'E';
1095 } else {
1096 $type = 'N';
1097 }
1098 $av_pend_colon = 'O';
1099
1100 } elsif ($cur =~ /^(\[)/o) {
1101 print "CLOSE($1)\n" if ($dbg_values > 1);
1102 $type = 'N';
1103
1104 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1105 my $variant;
1106
1107 print "OPV($1)\n" if ($dbg_values > 1);
1108 if ($type eq 'V') {
1109 $variant = 'B';
1110 } else {
1111 $variant = 'U';
1112 }
1113
1114 substr($var, length($res), 1, $variant);
1115 $type = 'N';
1116
1117 } elsif ($cur =~ /^($Operators)/o) {
1118 print "OP($1)\n" if ($dbg_values > 1);
1119 if ($1 ne '++' && $1 ne '--') {
1120 $type = 'N';
1121 }
1122
1123 } elsif ($cur =~ /(^.)/o) {
1124 print "C($1)\n" if ($dbg_values > 1);
1125 }
1126 if (defined $1) {
1127 $cur = substr($cur, length($1));
1128 $res .= $type x length($1);
1129 }
1130 }
1131
1132 return ($res, $var);
1133}
1134
1135sub possible {
1136 my ($possible, $line) = @_;
1137 my $notPermitted = qr{(?:
1138 ^(?:
1139 $Modifier|
1140 $Storage|
1141 $Type|
1142 DEFINE_\S+
1143 )$|
1144 ^(?:
1145 goto|
1146 return|
1147 case|
1148 else|
1149 asm|__asm__|
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001150 do
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001151 )(?:\s|$)|
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001152 ^(?:typedef|struct|enum)\b|
1153 ^\#
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001154 )}x;
1155 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1156 if ($possible !~ $notPermitted) {
1157 # Check for modifiers.
1158 $possible =~ s/\s*$Storage\s*//g;
1159 $possible =~ s/\s*$Sparse\s*//g;
1160 if ($possible =~ /^\s*$/) {
1161
1162 } elsif ($possible =~ /\s/) {
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001163 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001164 for my $modifier (split(' ', $possible)) {
1165 if ($modifier !~ $notPermitted) {
1166 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1167 push(@modifierList, $modifier);
1168 }
1169 }
1170
1171 } else {
1172 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1173 push(@typeList, $possible);
1174 }
1175 build_types();
1176 } else {
1177 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1178 }
1179}
1180
1181my $prefix = '';
1182
1183sub report {
1184 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1185 return 0;
1186 }
1187 my $line = $prefix . $_[0];
1188
1189 $line = (split('\n', $line))[0] . "\n" if ($terse);
1190
1191 push(our @report, $line);
1192
1193 return 1;
1194}
1195sub report_dump {
1196 our @report;
1197}
1198sub ERROR {
1199 if (report("ERROR: $_[0]\n")) {
1200 our $clean = 0;
1201 our $cnt_error++;
1202 }
1203}
1204sub WARN {
1205 if (report("WARNING: $_[0]\n")) {
1206 our $clean = 0;
1207 our $cnt_warn++;
1208 }
1209}
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001210
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001211sub process {
1212 my $filename = shift;
1213
1214 my $linenr=0;
1215 my $prevline="";
1216 my $prevrawline="";
1217 my $stashline="";
1218 my $stashrawline="";
1219
1220 my $length;
1221 my $indent;
1222 my $previndent=0;
1223 my $stashindent=0;
1224
1225 our $clean = 1;
1226 my $signoff = 0;
1227 my $is_patch = 0;
1228
Joe Perches5fc7e402018-04-30 13:46:49 +01001229 my $in_header_lines = $file ? 0 : 1;
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001230 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches4be61312018-04-30 13:46:50 +01001231 my $reported_maintainer_file = 0;
Pasi Savanainenbf139a02018-04-30 13:46:48 +01001232 my $non_utf8_charset = 0;
1233
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001234 our @report = ();
1235 our $cnt_lines = 0;
1236 our $cnt_error = 0;
1237 our $cnt_warn = 0;
1238 our $cnt_chk = 0;
1239
1240 # Trace the real file/line as we go.
1241 my $realfile = '';
1242 my $realline = 0;
1243 my $realcnt = 0;
1244 my $here = '';
1245 my $in_comment = 0;
1246 my $comment_edge = 0;
1247 my $first_line = 0;
1248 my $p1_prefix = '';
1249
1250 my $prev_values = 'E';
1251
1252 # suppression flags
1253 my %suppress_ifbraces;
1254 my %suppress_whiletrailers;
1255 my %suppress_export;
1256
1257 # Pre-scan the patch sanitizing the lines.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001258
1259 sanitise_line_reset();
1260 my $line;
1261 foreach my $rawline (@rawlines) {
1262 $linenr++;
1263 $line = $rawline;
1264
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001265 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1266 $realline=$1-1;
1267 if (defined $2) {
1268 $realcnt=$3+1;
1269 } else {
1270 $realcnt=1+1;
1271 }
1272 $in_comment = 0;
1273
1274 # Guestimate if this is a continuing comment. Run
1275 # the context looking for a comment "edge". If this
1276 # edge is a close comment then we must be in a comment
1277 # at context start.
1278 my $edge;
1279 my $cnt = $realcnt;
1280 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1281 next if (defined $rawlines[$ln - 1] &&
1282 $rawlines[$ln - 1] =~ /^-/);
1283 $cnt--;
1284 #print "RAW<$rawlines[$ln - 1]>\n";
1285 last if (!defined $rawlines[$ln - 1]);
1286 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1287 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1288 ($edge) = $1;
1289 last;
1290 }
1291 }
1292 if (defined $edge && $edge eq '*/') {
1293 $in_comment = 1;
1294 }
1295
1296 # Guestimate if this is a continuing comment. If this
1297 # is the start of a diff block and this line starts
1298 # ' *' then it is very likely a comment.
1299 if (!defined $edge &&
1300 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1301 {
1302 $in_comment = 1;
1303 }
1304
1305 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1306 sanitise_line_reset($in_comment);
1307
1308 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1309 # Standardise the strings and chars within the input to
1310 # simplify matching -- only bother with positive lines.
1311 $line = sanitise_line($rawline);
1312 }
1313 push(@lines, $line);
1314
1315 if ($realcnt > 1) {
1316 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1317 } else {
1318 $realcnt = 0;
1319 }
1320
1321 #print "==>$rawline\n";
1322 #print "-->$line\n";
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001323 }
1324
1325 $prefix = '';
1326
1327 $realcnt = 0;
1328 $linenr = 0;
1329 foreach my $line (@lines) {
1330 $linenr++;
1331
1332 my $rawline = $rawlines[$linenr - 1];
1333
1334#extract the line range in the file after the patch is applied
1335 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1336 $is_patch = 1;
1337 $first_line = $linenr + 1;
1338 $realline=$1-1;
1339 if (defined $2) {
1340 $realcnt=$3+1;
1341 } else {
1342 $realcnt=1+1;
1343 }
1344 annotate_reset();
1345 $prev_values = 'E';
1346
1347 %suppress_ifbraces = ();
1348 %suppress_whiletrailers = ();
1349 %suppress_export = ();
1350 next;
1351
1352# track the line number as we move through the hunk, note that
1353# new versions of GNU diff omit the leading space on completely
1354# blank context lines so we need to count that too.
1355 } elsif ($line =~ /^( |\+|$)/) {
1356 $realline++;
1357 $realcnt-- if ($realcnt != 0);
1358
1359 # Measure the line length and indent.
1360 ($length, $indent) = line_stats($rawline);
1361
1362 # Track the previous line.
1363 ($prevline, $stashline) = ($stashline, $line);
1364 ($previndent, $stashindent) = ($stashindent, $indent);
1365 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1366
1367 #warn "line<$line>\n";
1368
1369 } elsif ($realcnt == 1) {
1370 $realcnt--;
1371 }
1372
1373 my $hunk_line = ($realcnt != 0);
1374
1375#make up the handle for any error we report on this line
1376 $prefix = "$filename:$realline: " if ($emacs && $file);
1377 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1378
1379 $here = "#$linenr: " if (!$file);
1380 $here = "#$realline: " if ($file);
1381
1382 # extract the filename as it passes
1383 if ($line =~ /^diff --git.*?(\S+)$/) {
1384 $realfile = $1;
Paolo Bonzini1a5c63c2018-08-10 16:35:19 +02001385 $realfile =~ s@^([^/]*)/@@ if (!$file);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001386 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1387 $realfile = $1;
Paolo Bonzini1a5c63c2018-08-10 16:35:19 +02001388 $realfile =~ s@^([^/]*)/@@ if (!$file);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001389
1390 $p1_prefix = $1;
1391 if (!$file && $tree && $p1_prefix ne '' &&
1392 -e "$root/$p1_prefix") {
1393 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1394 }
1395
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001396 next;
1397 }
1398
1399 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1400
1401 my $hereline = "$here\n$rawline\n";
1402 my $herecurr = "$here\n$rawline\n";
1403 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1404
1405 $cnt_lines++ if ($realcnt != 0);
1406
1407# Check for incorrect file permissions
1408 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1409 my $permhere = $here . "FILE: $realfile\n";
Paolo Bonzini71c47b02015-08-16 23:15:46 +02001410 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001411 ERROR("do not set execute permissions for source files\n" . $permhere);
1412 }
1413 }
1414
Stefan Hajnoczif8dccbb2016-07-15 10:46:54 +01001415# Accept git diff extended headers as valid patches
1416 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1417 $is_patch = 1;
1418 }
1419
Daniel P. Berrangéf5177792018-08-23 11:21:43 +01001420 if ($line =~ /^Author: .*via Qemu-devel.*<qemu-devel\@nongnu.org>/) {
1421 ERROR("Author email address is mangled by the mailing list\n" . $herecurr);
1422 }
1423
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001424#check the patch for a signoff:
1425 if ($line =~ /^\s*signed-off-by:/i) {
1426 # This is a signoff, if ugly, so do not double report.
1427 $signoff++;
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001428 $in_commit_log = 0;
1429
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001430 if (!($line =~ /^\s*Signed-off-by:/)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001431 ERROR("The correct form is \"Signed-off-by\"\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001432 $herecurr);
1433 }
1434 if ($line =~ /^\s*signed-off-by:\S/i) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001435 ERROR("space required after Signed-off-by:\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001436 $herecurr);
1437 }
1438 }
1439
Joe Perches1a6fad02018-04-30 13:46:51 +01001440# Check if MAINTAINERS is being updated. If so, there's probably no need to
1441# emit the "does MAINTAINERS need updating?" message on file add/move/delete
1442 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
1443 $reported_maintainer_file = 1;
1444 }
1445
Joe Perches4be61312018-04-30 13:46:50 +01001446# Check for added, moved or deleted files
1447 if (!$reported_maintainer_file && !$in_commit_log &&
1448 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
1449 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
1450 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
1451 (defined($1) || defined($2))))) {
1452 $reported_maintainer_file = 1;
1453 WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
1454 }
1455
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001456# Check for wrappage within a valid hunk of the file
1457 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1458 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1459 $herecurr) if (!$emitted_corrupt++);
1460 }
1461
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001462# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1463 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1464 $rawline !~ m/^$UTF8*$/) {
1465 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1466
1467 my $blank = copy_spacing($rawline);
1468 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1469 my $hereptr = "$hereline$ptr\n";
1470
1471 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1472 }
1473
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001474# Check if it's the start of a commit log
1475# (not a header line and we haven't seen the patch filename)
1476 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches5fc7e402018-04-30 13:46:49 +01001477 !($rawline =~ /^\s+\S/ ||
1478 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001479 $in_header_lines = 0;
1480 $in_commit_log = 1;
1481 }
1482
Pasi Savanainenbf139a02018-04-30 13:46:48 +01001483# Check if there is UTF-8 in a commit log when a mail header has explicitly
1484# declined it, i.e defined some charset where it is missing.
1485 if ($in_header_lines &&
1486 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1487 $1 !~ /utf-8/i) {
1488 $non_utf8_charset = 1;
1489 }
1490
1491 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001492 $rawline =~ /$NON_ASCII_UTF8/) {
1493 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1494 }
1495
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001496# ignore non-hunk lines and lines being removed
1497 next if (!$hunk_line || $line =~ /^-/);
1498
Radim Krčmář93bf13c2016-08-09 19:38:41 +02001499# ignore files that are being periodically imported from Linux
1500 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1501
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001502#trailing whitespace
1503 if ($line =~ /^\+.*\015/) {
1504 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1505 ERROR("DOS line endings\n" . $herevet);
1506
Lluís Vilanova0cebabd2016-09-07 14:49:04 +02001507 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1508 $realfile =~ /^docs\/.+\.md/) {
1509 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1510 # TODO: properly check we're in a code block
1511 # (surrounding text is 4-column aligned)
1512 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1513 ERROR("code blocks in documentation should have " .
1514 "empty lines with exactly 4 columns of " .
1515 "whitespace\n" . $herevet);
1516 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001517 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1518 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1519 ERROR("trailing whitespace\n" . $herevet);
1520 $rpt_cleaners = 1;
1521 }
1522
Vladimir Sementsov-Ogievskiyc3e58752017-07-31 19:01:34 +03001523# checks for trace-events files
1524 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1525 if ($rawline =~ /%[-+ 0]*#/) {
1526 ERROR("Don't use '#' flag of printf format ('%#') in " .
1527 "trace-events, use '0x' prefix instead\n" . $herecurr);
1528 } else {
1529 my $hex =
1530 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1531
1532 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
Vladimir Sementsov-Ogievskiy45042732017-10-04 18:44:20 +03001533 my $tmpline = $rawline;
1534 $tmpline =~ s/($hex[.:\/ ])+$hex//g;
Vladimir Sementsov-Ogievskiyc3e58752017-07-31 19:01:34 +03001535
1536 if ($tmpline =~ /(?<!0x)$hex/) {
1537 ERROR("Hex numbers must be prefixed with '0x'\n" .
1538 $herecurr);
1539 }
1540 }
1541 }
1542
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001543# check we are in a valid source file if not then ignore this hunk
Paolo Bonzini777d05b2017-10-04 16:35:53 +02001544 next if ($realfile !~ /$SrcFile/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001545
Eric Blake205f31a2018-02-22 15:58:38 -06001546#90 column limit; exempt URLs, if no other words on line
Paolo Bonzinif1e155b2015-08-16 23:01:19 +02001547 if ($line =~ /^\+/ &&
1548 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Eric Blake205f31a2018-02-22 15:58:38 -06001549 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001550 $length > 80)
1551 {
Paolo Bonzini8fbe3d12015-09-07 11:53:02 +02001552 if ($length > 90) {
1553 ERROR("line over 90 characters\n" . $herecurr);
1554 } else {
1555 WARN("line over 80 characters\n" . $herecurr);
1556 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001557 }
1558
1559# check for spaces before a quoted newline
1560 if ($rawline =~ /^.*\".*\s\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001561 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001562 }
1563
1564# check for adding lines without a newline.
1565 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001566 ERROR("adding a line without newline at end of file\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001567 }
1568
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001569# check for RCS/CVS revision markers
1570 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001571 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001572 }
1573
Paolo Bonzini906fb132016-08-09 17:47:42 +02001574# tabs are only allowed in assembly source code, and in
1575# some scripts we imported from other projects.
1576 next if ($realfile =~ /\.(s|S)$/);
1577 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001578
Blue Swirlad36ce82011-02-05 13:18:20 +00001579 if ($rawline =~ /^\+.*\t/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001580 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Blue Swirlb6469682011-01-20 20:58:56 +00001581 ERROR("code indent should never use tabs\n" . $herevet);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001582 $rpt_cleaners = 1;
1583 }
1584
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001585# check we are in a valid C source file if not then ignore this hunk
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001586 next if ($realfile !~ /\.(h|c|cpp)$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001587
Peter Maydell8c06fbd2018-12-14 13:30:48 +00001588# Block comment styles
1589
1590 # Block comments use /* on a line of its own
1591 if ($rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1592 $rawline =~ m@^\+.*/\*\*?[ \t]*.+[ \t]*$@) { # /* or /** non-blank
1593 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1594 }
1595
1596# Block comments use * on subsequent lines
1597 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1598 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1599 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1600 $rawline =~ /^\+/ && #line is new
1601 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1602 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1603 }
1604
1605# Block comments use */ on trailing lines
1606 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1607 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1608 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1609 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1610 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1611 }
1612
1613# Block comment * alignment
1614 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1615 $line =~ /^\+[ \t]*$;/ && #leading comment
1616 $rawline =~ /^\+[ \t]*\*/ && #leading *
1617 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1618 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1619 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1620 my $oldindent;
1621 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1622 if (defined($1)) {
1623 $oldindent = expand_tabs($1);
1624 } else {
1625 $prevrawline =~ m@^\+(.*/?)\*@;
1626 $oldindent = expand_tabs($1);
1627 }
1628 $rawline =~ m@^\+([ \t]*)\*@;
1629 my $newindent = $1;
1630 $newindent = expand_tabs($newindent);
1631 if (length($oldindent) ne length($newindent)) {
1632 WARN("Block comments should align the * on each line\n" . $hereprev);
1633 }
1634 }
1635
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001636# Check for potential 'bare' types
1637 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1638 $realline_next);
1639 if ($realcnt && $line =~ /.\s*\S/) {
1640 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1641 ctx_statement_block($linenr, $realcnt, 0);
1642 $stat =~ s/\n./\n /g;
1643 $cond =~ s/\n./\n /g;
1644
1645 # Find the real next line.
1646 $realline_next = $line_nr_next;
1647 if (defined $realline_next &&
1648 (!defined $lines[$realline_next - 1] ||
1649 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1650 $realline_next++;
1651 }
1652
1653 my $s = $stat;
1654 $s =~ s/{.*$//s;
1655
1656 # Ignore goto labels.
1657 if ($s =~ /$Ident:\*$/s) {
1658
1659 # Ignore functions being called
1660 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1661
1662 } elsif ($s =~ /^.\s*else\b/s) {
1663
1664 # declarations always start with types
1665 } 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) {
1666 my $type = $1;
1667 $type =~ s/\s+/ /g;
1668 possible($type, "A:" . $s);
1669
1670 # definitions in global scope can only start with types
1671 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1672 possible($1, "B:" . $s);
1673 }
1674
1675 # any (foo ... *) is a pointer cast, and foo is a type
1676 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1677 possible($1, "C:" . $s);
1678 }
1679
1680 # Check for any sort of function declaration.
1681 # int foo(something bar, other baz);
1682 # void (*store_gdt)(x86_descr_ptr *);
1683 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1684 my ($name_len) = length($1);
1685
1686 my $ctx = $s;
1687 substr($ctx, 0, $name_len + 1, '');
1688 $ctx =~ s/\)[^\)]*$//;
1689
1690 for my $arg (split(/\s*,\s*/, $ctx)) {
1691 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1692
1693 possible($1, "D:" . $s);
1694 }
1695 }
1696 }
1697
1698 }
1699
1700#
1701# Checks which may be anchored in the context.
1702#
1703
1704# Check for switch () and associated case and default
1705# statements should be at the same indent.
1706 if ($line=~/\bswitch\s*\(.*\)/) {
1707 my $err = '';
1708 my $sep = '';
1709 my @ctx = ctx_block_outer($linenr, $realcnt);
1710 shift(@ctx);
1711 for my $ctx (@ctx) {
1712 my ($clen, $cindent) = line_stats($ctx);
1713 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1714 $indent != $cindent) {
1715 $err .= "$sep$ctx\n";
1716 $sep = '';
1717 } else {
1718 $sep = "[...]\n";
1719 }
1720 }
1721 if ($err ne '') {
1722 ERROR("switch and case should be at the same indent\n$hereline$err");
1723 }
1724 }
1725
1726# if/while/etc brace do not go on next line, unless defining a do while loop,
1727# or if that brace on the next line is for something else
1728 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1729 my $pre_ctx = "$1$2";
1730
1731 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1732 my $ctx_cnt = $realcnt - $#ctx - 1;
1733 my $ctx = join("\n", @ctx);
1734
1735 my $ctx_ln = $linenr;
1736 my $ctx_skip = $realcnt;
1737
1738 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1739 defined $lines[$ctx_ln - 1] &&
1740 $lines[$ctx_ln - 1] =~ /^-/)) {
1741 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1742 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1743 $ctx_ln++;
1744 }
1745
1746 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1747 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1748
Max Reitza97ceca2014-11-26 17:20:24 +01001749 # The length of the "previous line" is checked against 80 because it
1750 # includes the + at the beginning of the line (if the actual line has
1751 # 79 or 80 characters, it is no longer possible to add a space and an
1752 # opening brace there)
1753 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08001754 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
Max Reitza97ceca2014-11-26 17:20:24 +01001755 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001756 ERROR("that open brace { should be on the previous line\n" .
1757 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1758 }
1759 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1760 $ctx =~ /\)\s*\;\s*$/ &&
1761 defined $lines[$ctx_ln - 1])
1762 {
1763 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1764 if ($nindent > $indent) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001765 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001766 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1767 }
1768 }
1769 }
1770
Eric Blakef4bdc132017-12-01 17:24:33 -06001771# 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1772 if ($line =~ /while\s*\((0|false)\);/) {
1773 ERROR("suspicious ; after while (0)\n" . $herecurr);
1774 }
1775
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001776# Check relative indent for conditionals and blocks.
1777 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1778 my ($s, $c) = ($stat, $cond);
1779
1780 substr($s, 0, length($c), '');
1781
1782 # Make sure we remove the line prefixes as we have
1783 # none on the first line, and are going to readd them
1784 # where necessary.
1785 $s =~ s/\n./\n/gs;
1786
1787 # Find out how long the conditional actually is.
1788 my @newlines = ($c =~ /\n/gs);
1789 my $cond_lines = 1 + $#newlines;
1790
1791 # We want to check the first line inside the block
1792 # starting at the end of the conditional, so remove:
1793 # 1) any blank line termination
1794 # 2) any opening brace { on end of the line
1795 # 3) any do (...) {
1796 my $continuation = 0;
1797 my $check = 0;
1798 $s =~ s/^.*\bdo\b//;
Fam Zheng04f25622015-09-11 19:07:36 +08001799 $s =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001800 if ($s =~ s/^\s*\\//) {
1801 $continuation = 1;
1802 }
1803 if ($s =~ s/^\s*?\n//) {
1804 $check = 1;
1805 $cond_lines++;
1806 }
1807
1808 # Also ignore a loop construct at the end of a
1809 # preprocessor statement.
1810 if (($prevline =~ /^.\s*#\s*define\s/ ||
1811 $prevline =~ /\\\s*$/) && $continuation == 0) {
1812 $check = 0;
1813 }
1814
1815 my $cond_ptr = -1;
1816 $continuation = 0;
1817 while ($cond_ptr != $cond_lines) {
1818 $cond_ptr = $cond_lines;
1819
1820 # If we see an #else/#elif then the code
1821 # is not linear.
1822 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1823 $check = 0;
1824 }
1825
1826 # Ignore:
1827 # 1) blank lines, they should be at 0,
1828 # 2) preprocessor lines, and
1829 # 3) labels.
1830 if ($continuation ||
1831 $s =~ /^\s*?\n/ ||
1832 $s =~ /^\s*#\s*?/ ||
1833 $s =~ /^\s*$Ident\s*:/) {
1834 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1835 if ($s =~ s/^.*?\n//) {
1836 $cond_lines++;
1837 }
1838 }
1839 }
1840
1841 my (undef, $sindent) = line_stats("+" . $s);
1842 my $stat_real = raw_line($linenr, $cond_lines);
1843
1844 # Check if either of these lines are modified, else
1845 # this is not this patch's fault.
1846 if (!defined($stat_real) ||
1847 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1848 $check = 0;
1849 }
1850 if (defined($stat_real) && $cond_lines > 1) {
1851 $stat_real = "[...]\n$stat_real";
1852 }
1853
1854 #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";
1855
Blue Swirlb6469682011-01-20 20:58:56 +00001856 if ($check && (($sindent % 4) != 0 ||
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001857 ($sindent <= $indent && $s ne ''))) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001858 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001859 }
1860 }
1861
1862 # Track the 'values' across context and added lines.
1863 my $opline = $line; $opline =~ s/^./ /;
1864 my ($curr_values, $curr_vars) =
1865 annotate_values($opline . "\n", $prev_values);
1866 $curr_values = $prev_values . $curr_values;
1867 if ($dbg_values) {
1868 my $outline = $opline; $outline =~ s/\t/ /g;
1869 print "$linenr > .$outline\n";
1870 print "$linenr > $curr_values\n";
1871 print "$linenr > $curr_vars\n";
1872 }
1873 $prev_values = substr($curr_values, -1);
1874
1875#ignore lines not being added
1876 if ($line=~/^[^\+]/) {next;}
1877
1878# TEST: allow direct testing of the type matcher.
1879 if ($dbg_type) {
1880 if ($line =~ /^.\s*$Declare\s*$/) {
1881 ERROR("TEST: is type\n" . $herecurr);
1882 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1883 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1884 }
1885 next;
1886 }
1887# TEST: allow direct testing of the attribute matcher.
1888 if ($dbg_attr) {
1889 if ($line =~ /^.\s*$Modifier\s*$/) {
1890 ERROR("TEST: is attr\n" . $herecurr);
1891 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1892 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1893 }
1894 next;
1895 }
1896
1897# check for initialisation to aggregates open brace on the next line
Fam Zheng04f25622015-09-11 19:07:36 +08001898 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001899 $prevline =~ /(?:^|[^=])=\s*$/) {
1900 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1901 }
1902
1903#
1904# Checks which are anchored on the added line.
1905#
1906
1907# check for malformed paths in #include statements (uses RAW line)
1908 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1909 my $path = $1;
1910 if ($path =~ m{//}) {
1911 ERROR("malformed #include filename\n" .
1912 $herecurr);
1913 }
1914 }
1915
1916# no C99 // comments
1917 if ($line =~ m{//}) {
1918 ERROR("do not use C99 // comments\n" . $herecurr);
1919 }
1920 # Remove C99 comments.
1921 $line =~ s@//.*@@;
1922 $opline =~ s@//.*@@;
1923
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001924# check for global initialisers.
1925 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1926 ERROR("do not initialise globals to 0 or NULL\n" .
1927 $herecurr);
1928 }
1929# check for static initialisers.
1930 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1931 ERROR("do not initialise statics to 0 or NULL\n" .
1932 $herecurr);
1933 }
1934
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001935# * goes on variable not on type
1936 # (char*[ const])
1937 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1938 my ($from, $to) = ($1, $1);
1939
1940 # Should start with a space.
1941 $to =~ s/^(\S)/ $1/;
1942 # Should not end with a space.
1943 $to =~ s/\s+$//;
1944 # '*'s should not have spaces between.
1945 while ($to =~ s/\*\s+\*/\*\*/) {
1946 }
1947
1948 #print "from<$from> to<$to>\n";
1949 if ($from ne $to) {
1950 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1951 }
1952 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1953 my ($from, $to, $ident) = ($1, $1, $2);
1954
1955 # Should start with a space.
1956 $to =~ s/^(\S)/ $1/;
1957 # Should not end with a space.
1958 $to =~ s/\s+$//;
1959 # '*'s should not have spaces between.
1960 while ($to =~ s/\*\s+\*/\*\*/) {
1961 }
1962 # Modifiers should have spaces.
1963 $to =~ s/(\b$Modifier$)/$1 /;
1964
1965 #print "from<$from> to<$to> ident<$ident>\n";
1966 if ($from ne $to && $ident !~ /^$Modifier$/) {
1967 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1968 }
1969 }
1970
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001971# function brace can't be on same line, except for #defines of do while,
1972# or if closed on same line
Fam Zheng04f25622015-09-11 19:07:36 +08001973 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1974 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001975 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1976 }
1977
1978# open braces for enum, union and struct go on the same line.
Fam Zheng04f25622015-09-11 19:07:36 +08001979 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001980 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1981 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1982 }
1983
1984# missing space after union, struct or enum definition
1985 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Paolo Bonzini71c47b02015-08-16 23:15:46 +02001986 ERROR("missing space after $1 definition\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001987 }
1988
1989# check for spacing round square brackets; allowed:
1990# 1. with a type on the left -- int [] a;
1991# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1992# 3. inside a curly brace -- = { [0...10] = 5 }
Leonid Bloch409db6e2015-10-29 11:48:37 +02001993# 4. after a comma -- [1] = 5, [2] = 6
Leonid Bloch8800cf02015-10-29 11:48:38 +02001994# 5. in a macro definition -- #define abc(x) [x] = y
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001995 while ($line =~ /(.*?\s)\[/g) {
1996 my ($where, $prefix) = ($-[1], $1);
1997 if ($prefix !~ /$Type\s+$/ &&
1998 ($where != 0 || $prefix !~ /^.\s+$/) &&
Leonid Bloch8800cf02015-10-29 11:48:38 +02001999 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
Heinrich Schuchardt66e9d202018-04-10 16:34:14 -07002000 $prefix !~ /[,{:]\s+$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002001 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2002 }
2003 }
2004
2005# check for spaces between functions and their parentheses.
2006 while ($line =~ /($Ident)\s+\(/g) {
2007 my $name = $1;
2008 my $ctx_before = substr($line, 0, $-[1]);
2009 my $ctx = "$ctx_before$name";
2010
2011 # Ignore those directives where spaces _are_ permitted.
2012 if ($name =~ /^(?:
2013 if|for|while|switch|return|case|
Jeff Cody000980c2016-11-01 10:38:35 -04002014 volatile|__volatile__|coroutine_fn|
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002015 __attribute__|format|__extension__|
2016 asm|__asm__)$/x)
2017 {
2018
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002019 # Ignore 'catch (...)' in C++
2020 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
2021
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002022 # cpp #define statements have non-optional spaces, ie
2023 # if there is a space between the name and the open
2024 # parenthesis it is simply not a parameter group.
2025 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2026
2027 # cpp #elif statement condition may start with a (
2028 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2029
2030 # If this whole things ends with a type its most
2031 # likely a typedef for a function.
2032 } elsif ($ctx =~ /$Type$/) {
2033
2034 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002035 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002036 }
2037 }
2038# Check operator spacing.
2039 if (!($line=~/\#\s*include/)) {
2040 my $ops = qr{
2041 <<=|>>=|<=|>=|==|!=|
2042 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2043 =>|->|<<|>>|<|>|=|!|~|
2044 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002045 \?|::|:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002046 }x;
2047 my @elements = split(/($ops|;)/, $opline);
2048 my $off = 0;
2049
2050 my $blank = copy_spacing($opline);
2051
2052 for (my $n = 0; $n < $#elements; $n += 2) {
2053 $off += length($elements[$n]);
2054
Stefan Weile7d81002011-12-10 00:19:46 +01002055 # Pick up the preceding and succeeding characters.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002056 my $ca = substr($opline, 0, $off);
2057 my $cc = '';
2058 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2059 $cc = substr($opline, $off + length($elements[$n + 1]));
2060 }
2061 my $cb = "$ca$;$cc";
2062
2063 my $a = '';
2064 $a = 'V' if ($elements[$n] ne '');
2065 $a = 'W' if ($elements[$n] =~ /\s$/);
2066 $a = 'C' if ($elements[$n] =~ /$;$/);
2067 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2068 $a = 'O' if ($elements[$n] eq '');
2069 $a = 'E' if ($ca =~ /^\s*$/);
2070
2071 my $op = $elements[$n + 1];
2072
2073 my $c = '';
2074 if (defined $elements[$n + 2]) {
2075 $c = 'V' if ($elements[$n + 2] ne '');
2076 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2077 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2078 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2079 $c = 'O' if ($elements[$n + 2] eq '');
2080 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2081 } else {
2082 $c = 'E';
2083 }
2084
2085 my $ctx = "${a}x${c}";
2086
2087 my $at = "(ctx:$ctx)";
2088
2089 my $ptr = substr($blank, 0, $off) . "^";
2090 my $hereptr = "$hereline$ptr\n";
2091
2092 # Pull out the value of this operator.
2093 my $op_type = substr($curr_values, $off + 1, 1);
2094
2095 # Get the full operator variant.
2096 my $opv = $op . substr($curr_vars, $off, 1);
2097
2098 # Ignore operators passed as parameters.
2099 if ($op_type ne 'V' &&
2100 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2101
2102# # Ignore comments
2103# } elsif ($op =~ /^$;+$/) {
2104
2105 # ; should have either the end of line or a space or \ after it
2106 } elsif ($op eq ';') {
2107 if ($ctx !~ /.x[WEBC]/ &&
2108 $cc !~ /^\\/ && $cc !~ /^;/) {
2109 ERROR("space required after that '$op' $at\n" . $hereptr);
2110 }
2111
2112 # // is a comment
2113 } elsif ($op eq '//') {
2114
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002115 # Ignore : used in class declaration in C++
2116 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2117 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2118
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002119 # No spaces for:
2120 # ->
2121 # : when part of a bitfield
2122 } elsif ($op eq '->' || $opv eq ':B') {
2123 if ($ctx =~ /Wx.|.xW/) {
2124 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2125 }
2126
2127 # , must have a space on the right.
Alexander Graf9fbe4782011-06-29 08:04:27 +02002128 # not required when having a single },{ on one line
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002129 } elsif ($op eq ',') {
Alexander Graf9fbe4782011-06-29 08:04:27 +02002130 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08002131 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002132 ERROR("space required after that '$op' $at\n" . $hereptr);
2133 }
2134
2135 # '*' as part of a type definition -- reported already.
2136 } elsif ($opv eq '*_') {
2137 #warn "'*' is part of type\n";
2138
2139 # unary operators should have a space before and
2140 # none after. May be left adjacent to another
2141 # unary operator, or a cast
2142 } elsif ($op eq '!' || $op eq '~' ||
2143 $opv eq '*U' || $opv eq '-U' ||
2144 $opv eq '&U' || $opv eq '&&U') {
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002145 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2146 # '~' used as a name of Destructor
2147
2148 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002149 ERROR("space required before that '$op' $at\n" . $hereptr);
2150 }
2151 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2152 # A unary '*' may be const
2153
2154 } elsif ($ctx =~ /.xW/) {
2155 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2156 }
2157
2158 # unary ++ and unary -- are allowed no space on one side.
2159 } elsif ($op eq '++' or $op eq '--') {
2160 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2161 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2162 }
2163 if ($ctx =~ /Wx[BE]/ ||
2164 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2165 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2166 }
2167 if ($ctx =~ /ExW/) {
2168 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2169 }
2170
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002171 # A colon needs no spaces before when it is
2172 # terminating a case value or a label.
2173 } elsif ($opv eq ':C' || $opv eq ':L') {
2174 if ($ctx =~ /Wx./) {
2175 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2176 }
2177
2178 # All the others need spaces both sides.
2179 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2180 my $ok = 0;
2181
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002182 if ($realfile =~ /\.cpp|\.h$/) {
2183 # Ignore template arguments <...> in C++
2184 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2185 $ok = 1;
2186 }
2187
2188 # Ignore :: in C++
2189 if ($op eq '::') {
2190 $ok = 1;
2191 }
2192 }
2193
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002194 # Ignore email addresses <foo@bar>
2195 if (($op eq '<' &&
2196 $cc =~ /^\S+\@\S+>/) ||
2197 ($op eq '>' &&
2198 $ca =~ /<\S+\@\S+$/))
2199 {
2200 $ok = 1;
2201 }
2202
2203 # Ignore ?:
2204 if (($opv eq ':O' && $ca =~ /\?$/) ||
2205 ($op eq '?' && $cc =~ /^:/)) {
2206 $ok = 1;
2207 }
2208
2209 if ($ok == 0) {
2210 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2211 }
2212 }
2213 $off += length($elements[$n + 1]);
2214 }
2215 }
2216
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002217#need space before brace following if, while, etc
Fam Zheng04f25622015-09-11 19:07:36 +08002218 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2219 $line =~ /do\{/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002220 ERROR("space required before the open brace '{'\n" . $herecurr);
2221 }
2222
2223# closing brace should have a space following it when it has anything
2224# on the line
2225 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2226 ERROR("space required after that close brace '}'\n" . $herecurr);
2227 }
2228
2229# check spacing on square brackets
2230 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2231 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2232 }
2233 if ($line =~ /\s\]/) {
2234 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2235 }
2236
2237# check spacing on parentheses
2238 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2239 $line !~ /for\s*\(\s+;/) {
2240 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2241 }
2242 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2243 $line !~ /for\s*\(.*;\s+\)/ &&
2244 $line !~ /:\s+\)/) {
2245 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2246 }
2247
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002248# Return is not a function.
2249 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2250 my $spacing = $1;
2251 my $value = $2;
2252
2253 # Flatten any parentheses
2254 $value =~ s/\(/ \(/g;
2255 $value =~ s/\)/\) /g;
2256 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2257 $value !~ /(?:$Ident|-?$Constant)\s*
2258 $Compare\s*
2259 (?:$Ident|-?$Constant)/x &&
2260 $value =~ s/\([^\(\)]*\)/1/) {
2261 }
2262#print "value<$value>\n";
2263 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2264 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2265
2266 } elsif ($spacing !~ /\s+/) {
2267 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2268 }
2269 }
2270# Return of what appears to be an errno should normally be -'ve
2271 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2272 my $name = $1;
2273 if ($name ne 'EOF' && $name ne 'ERROR') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002274 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002275 }
2276 }
2277
2278# Need a space before open parenthesis after if, while etc
2279 if ($line=~/\b(if|while|for|switch)\(/) {
2280 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2281 }
2282
2283# Check for illegal assignment in if conditional -- and check for trailing
2284# statements after the conditional.
2285 if ($line =~ /do\s*(?!{)/) {
2286 my ($stat_next) = ctx_statement_block($line_nr_next,
2287 $remain_next, $off_next);
2288 $stat_next =~ s/\n./\n /g;
2289 ##print "stat<$stat> stat_next<$stat_next>\n";
2290
2291 if ($stat_next =~ /^\s*while\b/) {
2292 # If the statement carries leading newlines,
2293 # then count those as offsets.
2294 my ($whitespace) =
2295 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2296 my $offset =
2297 statement_rawlines($whitespace) - 1;
2298
2299 $suppress_whiletrailers{$line_nr_next +
2300 $offset} = 1;
2301 }
2302 }
2303 if (!defined $suppress_whiletrailers{$linenr} &&
2304 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2305 my ($s, $c) = ($stat, $cond);
2306
2307 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2308 ERROR("do not use assignment in if condition\n" . $herecurr);
2309 }
2310
2311 # Find out what is on the end of the line after the
2312 # conditional.
2313 substr($s, 0, length($c), '');
2314 $s =~ s/\n.*//g;
2315 $s =~ s/$;//g; # Remove any comments
2316 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2317 $c !~ /}\s*while\s*/)
2318 {
2319 # Find out how long the conditional actually is.
2320 my @newlines = ($c =~ /\n/gs);
2321 my $cond_lines = 1 + $#newlines;
2322 my $stat_real = '';
2323
2324 $stat_real = raw_line($linenr, $cond_lines)
2325 . "\n" if ($cond_lines);
2326 if (defined($stat_real) && $cond_lines > 1) {
2327 $stat_real = "[...]\n$stat_real";
2328 }
2329
2330 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2331 }
2332 }
2333
2334# Check for bitwise tests written as boolean
2335 if ($line =~ /
2336 (?:
2337 (?:\[|\(|\&\&|\|\|)
2338 \s*0[xX][0-9]+\s*
2339 (?:\&\&|\|\|)
2340 |
2341 (?:\&\&|\|\|)
2342 \s*0[xX][0-9]+\s*
2343 (?:\&\&|\|\||\)|\])
2344 )/x)
2345 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002346 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002347 }
2348
2349# if and else should not have general statements after it
2350 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2351 my $s = $1;
2352 $s =~ s/$;//g; # Remove any comments
2353 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2354 ERROR("trailing statements should be on next line\n" . $herecurr);
2355 }
2356 }
2357# if should not continue a brace
2358 if ($line =~ /}\s*if\b/) {
2359 ERROR("trailing statements should be on next line\n" .
2360 $herecurr);
2361 }
2362# case and default should not have general statements after them
2363 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2364 $line !~ /\G(?:
2365 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2366 \s*return\s+
2367 )/xg)
2368 {
2369 ERROR("trailing statements should be on next line\n" . $herecurr);
2370 }
2371
2372 # Check for }<nl>else {, these must be at the same
2373 # indent level to be relevant to each other.
2374 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2375 $previndent == $indent) {
2376 ERROR("else should follow close brace '}'\n" . $hereprev);
2377 }
2378
2379 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2380 $previndent == $indent) {
2381 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2382
2383 # Find out what is on the end of the line after the
2384 # conditional.
2385 substr($s, 0, length($c), '');
2386 $s =~ s/\n.*//g;
2387
2388 if ($s =~ /^\s*;/) {
2389 ERROR("while should follow close brace '}'\n" . $hereprev);
2390 }
2391 }
2392
2393#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2394# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2395# print "No studly caps, use _\n";
2396# print "$herecurr";
2397# $clean = 0;
2398# }
2399
2400#no spaces allowed after \ in define
2401 if ($line=~/\#\s*define.*\\\s$/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002402 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002403 }
2404
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002405# multi-statement macros should be enclosed in a do while loop, grab the
2406# first statement and ensure its the whole macro if its not enclosed
2407# in a known good container
2408 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2409 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2410 my $ln = $linenr;
2411 my $cnt = $realcnt;
2412 my ($off, $dstat, $dcond, $rest);
2413 my $ctx = '';
2414
2415 my $args = defined($1);
2416
2417 # Find the end of the macro and limit our statement
2418 # search to that.
2419 while ($cnt > 0 && defined $lines[$ln - 1] &&
2420 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2421 {
2422 $ctx .= $rawlines[$ln - 1] . "\n";
2423 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2424 $ln++;
2425 }
2426 $ctx .= $rawlines[$ln - 1];
2427
2428 ($dstat, $dcond, $ln, $cnt, $off) =
2429 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2430 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2431 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2432
2433 # Extract the remainder of the define (if any) and
2434 # rip off surrounding spaces, and trailing \'s.
2435 $rest = '';
2436 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2437 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2438 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2439 $rest .= substr($lines[$ln - 1], $off) . "\n";
2440 $cnt--;
2441 }
2442 $ln++;
2443 $off = 0;
2444 }
2445 $rest =~ s/\\\n.//g;
2446 $rest =~ s/^\s*//s;
2447 $rest =~ s/\s*$//s;
2448
2449 # Clean up the original statement.
2450 if ($args) {
2451 substr($dstat, 0, length($dcond), '');
2452 } else {
2453 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2454 }
2455 $dstat =~ s/$;//g;
2456 $dstat =~ s/\\\n.//g;
2457 $dstat =~ s/^\s*//s;
2458 $dstat =~ s/\s*$//s;
2459
2460 # Flatten any parentheses and braces
2461 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2462 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2463 $dstat =~ s/\[[^\{\}]*\]/1/)
2464 {
2465 }
2466
2467 my $exceptions = qr{
2468 $Declare|
2469 module_param_named|
2470 MODULE_PARAM_DESC|
2471 DECLARE_PER_CPU|
2472 DEFINE_PER_CPU|
2473 __typeof__\(|
2474 union|
2475 struct|
2476 \.$Ident\s*=\s*|
2477 ^\"|\"$
2478 }x;
2479 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2480 if ($rest ne '' && $rest ne ',') {
2481 if ($rest !~ /while\s*\(/ &&
2482 $dstat !~ /$exceptions/)
2483 {
2484 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2485 }
2486
2487 } elsif ($ctx !~ /;/) {
2488 if ($dstat ne '' &&
2489 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2490 $dstat !~ /$exceptions/ &&
2491 $dstat !~ /^\.$Ident\s*=/ &&
2492 $dstat =~ /$Operators/)
2493 {
2494 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2495 }
2496 }
2497 }
2498
Su Hang2b9aef62018-03-06 15:04:50 +08002499# check for missing bracing around if etc
2500 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2501 $line !~ /\#\s*if/) {
Su Hang053e45d2018-03-26 10:06:22 +08002502 my $allowed = 0;
2503
2504 # Check the pre-context.
2505 if ($line =~ /(\}.*?)$/) {
2506 my $pre = $1;
2507
2508 if ($line !~ /else/) {
2509 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2510 if $dbg_adv_apw;
2511 $allowed = 1;
2512 }
2513 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002514 my ($level, $endln, @chunks) =
2515 ctx_statement_full($linenr, $realcnt, 1);
Don Slutz69402a62012-09-02 19:22:37 -04002516 if ($dbg_adv_apw) {
2517 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2518 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2519 if $#chunks >= 1;
2520 }
Blue Swirlb6469682011-01-20 20:58:56 +00002521 if ($#chunks >= 0 && $level == 0) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002522 my $seen = 0;
2523 my $herectx = $here . "\n";
2524 my $ln = $linenr - 1;
2525 for my $chunk (@chunks) {
2526 my ($cond, $block) = @{$chunk};
2527
2528 # If the condition carries leading newlines, then count those as offsets.
2529 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2530 my $offset = statement_rawlines($whitespace) - 1;
2531
2532 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2533
2534 # We have looked at and allowed this specific line.
2535 $suppress_ifbraces{$ln + $offset} = 1;
2536
2537 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2538 $ln += statement_rawlines($block) - 1;
2539
2540 substr($block, 0, length($cond), '');
2541
Max Reitza97ceca2014-11-26 17:20:24 +01002542 my $spaced_block = $block;
2543 $spaced_block =~ s/\n\+/ /g;
2544
Fam Zheng04f25622015-09-11 19:07:36 +08002545 $seen++ if ($spaced_block =~ /^\s*\{/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002546
Don Slutz69402a62012-09-02 19:22:37 -04002547 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2548 if $dbg_adv_apw;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002549 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002550 print "APW: ALLOWED: cond<$cond>\n"
2551 if $dbg_adv_apw;
2552 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002553 }
2554 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002555 print "APW: ALLOWED: block<$block>\n"
2556 if $dbg_adv_apw;
2557 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002558 }
2559 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002560 print "APW: ALLOWED: lines block<$block>\n"
2561 if $dbg_adv_apw;
2562 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002563 }
2564 }
Su Hang053e45d2018-03-26 10:06:22 +08002565 if ($seen != ($#chunks + 1) && !$allowed) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002566 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002567 }
2568 }
2569 }
2570 if (!defined $suppress_ifbraces{$linenr - 1} &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002571 $line =~ /\b(if|while|for|else)\b/ &&
Blue Swirld0510af2011-07-15 20:09:10 +00002572 $line !~ /\#\s*if/ &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002573 $line !~ /\#\s*else/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002574 my $allowed = 0;
2575
Don Slutzdfe70532012-09-02 19:22:38 -04002576 # Check the pre-context.
2577 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2578 my $pre = $1;
2579
2580 if ($line !~ /else/) {
2581 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2582 if $dbg_adv_apw;
2583 $allowed = 1;
2584 }
2585 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002586
2587 my ($level, $endln, @chunks) =
2588 ctx_statement_full($linenr, $realcnt, $-[0]);
2589
2590 # Check the condition.
2591 my ($cond, $block) = @{$chunks[0]};
Don Slutz54243022012-09-02 19:22:36 -04002592 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2593 if $dbg_adv_checking;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002594 if (defined $cond) {
2595 substr($block, 0, length($cond), '');
2596 }
2597 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002598 print "APW: ALLOWED: cond<$cond>\n"
2599 if $dbg_adv_apw;
2600 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002601 }
2602 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002603 print "APW: ALLOWED: block<$block>\n"
2604 if $dbg_adv_apw;
2605 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002606 }
2607 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002608 print "APW: ALLOWED: lines block<$block>\n"
2609 if $dbg_adv_apw;
2610 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002611 }
2612 # Check the post-context.
2613 if (defined $chunks[1]) {
2614 my ($cond, $block) = @{$chunks[1]};
2615 if (defined $cond) {
2616 substr($block, 0, length($cond), '');
2617 }
2618 if ($block =~ /^\s*\{/) {
Don Slutz69402a62012-09-02 19:22:37 -04002619 print "APW: ALLOWED: chunk-1 block<$block>\n"
2620 if $dbg_adv_apw;
2621 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002622 }
2623 }
Don Slutza99ac042012-09-02 19:22:35 -04002624 print "DCS: level=$level block<$block> allowed=$allowed\n"
2625 if $dbg_adv_dcs;
Blue Swirlb6469682011-01-20 20:58:56 +00002626 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002627 my $herectx = $here . "\n";;
2628 my $cnt = statement_rawlines($block);
2629
2630 for (my $n = 0; $n < $cnt; $n++) {
2631 $herectx .= raw_line($linenr, $n) . "\n";;
2632 }
2633
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002634 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002635 }
2636 }
2637
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002638# no volatiles please
2639 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
Marc-André Lureau6b012d22017-12-15 19:18:10 +01002640 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2641 $line !~ /sig_atomic_t/ &&
2642 !ctx_has_comment($first_line, $linenr)) {
2643 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2644 ERROR($msg);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002645 }
2646
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002647# warn about #if 0
2648 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002649 ERROR("if this code is redundant consider removing it\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002650 $herecurr);
2651 }
2652
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002653# check for needless g_free() checks
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002654 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2655 my $expr = $1;
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002656 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002657 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002658 }
2659 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002660
2661# warn about #ifdefs in C files
2662# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2663# print "#ifdef in C files should be avoided\n";
2664# print "$herecurr";
2665# $clean = 0;
2666# }
2667
2668# warn about spacing in #ifdefs
2669 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2670 ERROR("exactly one space required after that #$1\n" . $herecurr);
2671 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002672# check for memory barriers without a comment.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002673 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002674 if (!ctx_has_comment($first_line, $linenr)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002675 ERROR("memory barrier without comment\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002676 }
2677 }
2678# check of hardware specific defines
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002679# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2680# where they might be necessary.
2681 if ($line =~ m@^.\s*\#\s*if.*\b__@) {
Paolo Bonzini63ae8b92016-09-21 18:49:07 +02002682 WARN("architecture specific defines should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002683 }
2684
2685# Check that the storage class is at the beginning of a declaration
2686 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002687 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002688 }
2689
2690# check the location of the inline attribute, that it is between
2691# storage class and type.
2692 if ($line =~ /\b$Type\s+$Inline\b/ ||
2693 $line =~ /\b$Inline\s+$Storage\b/) {
2694 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2695 }
2696
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002697# check for sizeof(&)
2698 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002699 ERROR("sizeof(& should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002700 }
2701
2702# check for new externs in .c files.
2703 if ($realfile =~ /\.c$/ && defined $stat &&
2704 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2705 {
2706 my $function_name = $1;
2707 my $paren_space = $2;
2708
2709 my $s = $stat;
2710 if (defined $cond) {
2711 substr($s, 0, length($cond), '');
2712 }
2713 if ($s =~ /^\s*;/ &&
2714 $function_name ne 'uninitialized_var')
2715 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002716 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002717 }
2718
2719 if ($paren_space =~ /\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002720 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002721 }
2722
2723 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2724 $stat =~ /^.\s*extern\s+/)
2725 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002726 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002727 }
2728
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002729# check for pointless casting of g_malloc return
2730 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2731 if ($2 == 'm') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002732 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002733 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002734 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002735 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002736 }
2737
2738# check for gcc specific __FUNCTION__
2739 if ($line =~ /__FUNCTION__/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002740 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002741 }
2742
Julia Suvorovafb8446d2018-03-02 13:43:19 +03002743# recommend g_path_get_* over g_strdup(basename/dirname(...))
2744 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2745 WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2746 }
2747
Paolo Bonzini5e43efb2015-09-16 18:35:09 +02002748# recommend qemu_strto* over strto* for numeric conversions
Eric Blake01fb8e12016-06-09 20:48:07 -06002749 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002750 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002751 }
Paolo Bonzinie8c20912017-07-04 11:26:37 +02002752# recommend sigaction over signal for portability, when establishing a handler
2753 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2754 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2755 }
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002756# check for module_init(), use category-specific init macros explicitly please
2757 if ($line =~ /^module_init\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002758 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002759 }
2760# check for various ops structs, ensure they are const.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002761 my $struct_ops = qr{AIOCBInfo|
2762 BdrvActionOps|
2763 BlockDevOps|
2764 BlockJobDriver|
2765 DisplayChangeListenerOps|
2766 GraphicHwOps|
2767 IDEDMAOps|
2768 KVMCapabilityInfo|
2769 MemoryRegionIOMMUOps|
2770 MemoryRegionOps|
2771 MemoryRegionPortio|
2772 QEMUFileOps|
2773 SCSIBusInfo|
2774 SCSIReqOps|
2775 Spice[A-Z][a-zA-Z0-9]*Interface|
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002776 USBDesc[A-Z][a-zA-Z0-9]*|
2777 VhostOps|
2778 VMStateDescription|
2779 VMStateInfo}x;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002780 if ($line !~ /\bconst\b/ &&
Paolo Bonzinie20e7182016-10-14 10:45:45 +02002781 $line =~ /\b($struct_ops)\b.*=/) {
2782 ERROR("initializer for struct $1 should normally be const\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002783 $herecurr);
2784 }
2785
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002786# check for %L{u,d,i} in strings
2787 my $string;
2788 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2789 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2790 $string =~ s/%%/__/g;
2791 if ($string =~ /(?<!%)%L[udi]/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002792 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002793 last;
2794 }
2795 }
2796
Stefan Weil9964d8f2012-06-17 06:57:41 +02002797# QEMU specific tests
2798 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002799 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
Stefan Weil9964d8f2012-06-17 06:57:41 +02002800 }
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002801
Jason J. Herne5d596c22015-12-11 13:30:42 -05002802# Qemu error function tests
2803
2804 # Find newlines in error messages
2805 my $qemu_error_funcs = qr{error_setg|
2806 error_setg_errno|
2807 error_setg_win32|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002808 error_setg_file_open|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002809 error_set|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002810 error_prepend|
Alistair Francise43ead12017-07-12 06:57:52 -07002811 warn_reportf_err|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002812 error_reportf_err|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002813 error_vreport|
Alistair Francis97f40302017-07-12 06:57:36 -07002814 warn_vreport|
2815 info_vreport|
2816 error_report|
2817 warn_report|
Paolo Bonzinif1e35ac2018-11-21 19:27:20 +01002818 info_report|
2819 g_test_message}x;
Jason J. Herne5d596c22015-12-11 13:30:42 -05002820
Markus Armbrustera47eb012016-08-03 13:37:52 +02002821 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002822 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002823 }
2824
2825 # Continue checking for error messages that contains newlines. This
2826 # check handles cases where string literals are spread over multiple lines.
2827 # Example:
2828 # error_report("Error msg line #1"
2829 # "Error msg line #2\n");
2830 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2831 my $continued_str_literal = qr{\+\s*\".*\"};
2832
2833 if ($rawline =~ /$quoted_newline_regex/) {
2834 # Backtrack to first line that does not contain only a quoted literal
2835 # and assume that it is the start of the statement.
2836 my $i = $linenr - 2;
2837
2838 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2839 $i--;
2840 }
2841
2842 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002843 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002844 }
2845 }
2846
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002847# check for non-portable libc calls that have portable alternatives in QEMU
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002848 if ($line =~ /\bffs\(/) {
2849 ERROR("use ctz32() instead of ffs()\n" . $herecurr);
2850 }
2851 if ($line =~ /\bffsl\(/) {
2852 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2853 }
2854 if ($line =~ /\bffsll\(/) {
2855 ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
2856 }
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002857 if ($line =~ /\bbzero\(/) {
2858 ERROR("use memset() instead of bzero()\n" . $herecurr);
2859 }
Dr. David Alan Gilbert6e938952017-04-27 17:55:26 +01002860 my $non_exit_glib_asserts = qr{g_assert_cmpstr|
2861 g_assert_cmpint|
2862 g_assert_cmpuint|
2863 g_assert_cmphex|
2864 g_assert_cmpfloat|
2865 g_assert_true|
2866 g_assert_false|
2867 g_assert_nonnull|
2868 g_assert_null|
2869 g_assert_no_error|
2870 g_assert_error|
2871 g_test_assert_expected_messages|
2872 g_test_trap_assert_passed|
2873 g_test_trap_assert_stdout|
2874 g_test_trap_assert_stdout_unmatched|
2875 g_test_trap_assert_stderr|
2876 g_test_trap_assert_stderr_unmatched}x;
2877 if ($realfile !~ /^tests\// &&
2878 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2879 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
2880 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002881 }
2882
Paolo Bonzinifd9c0cf2018-11-29 09:31:41 +01002883 if ($is_patch && $chk_signoff && $signoff == 0) {
2884 ERROR("Missing Signed-off-by: line(s)\n");
2885 }
2886
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002887 # If we have no input at all, then there is nothing to report on
2888 # so just keep quiet.
2889 if ($#rawlines == -1) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01002890 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002891 }
2892
2893 # In mailback mode only produce a report in the negative, for
2894 # things that appear to be patches.
2895 if ($mailback && ($clean == 1 || !$is_patch)) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01002896 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002897 }
2898
2899 # This is not a patch, and we are are in 'no-patch' mode so
2900 # just keep quiet.
2901 if (!$chk_patch && !$is_patch) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01002902 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002903 }
2904
2905 if (!$is_patch) {
2906 ERROR("Does not appear to be a unified-diff format patch\n");
2907 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002908
2909 print report_dump();
2910 if ($summary && !($clean == 1 && $quiet == 1)) {
2911 print "$filename " if ($summary_file);
2912 print "total: $cnt_error errors, $cnt_warn warnings, " .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002913 "$cnt_lines lines checked\n";
2914 print "\n" if ($quiet == 0);
2915 }
2916
2917 if ($quiet == 0) {
2918 # If there were whitespace errors which cleanpatch can fix
2919 # then suggest that.
Blue Swirlb6469682011-01-20 20:58:56 +00002920# if ($rpt_cleaners) {
2921# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2922# print " scripts/cleanfile\n\n";
2923# }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002924 }
2925
2926 if ($clean == 1 && $quiet == 0) {
2927 print "$vname has no obvious style problems and is ready for submission.\n"
2928 }
2929 if ($clean == 0 && $quiet == 0) {
2930 print "$vname has style problems, please review. If any of these errors\n";
2931 print "are false positives report them to the maintainer, see\n";
2932 print "CHECKPATCH in MAINTAINERS.\n";
2933 }
2934
Paolo Bonzini141de882016-08-09 17:47:44 +02002935 return ($no_warnings ? $clean : $cnt_error == 0);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002936}