blob: c3d08aa99f13e9651e5f5cbb490d7114ab40f019 [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;
Paolo Bonzini1db42692018-11-29 09:52:58 +010010use Term::ANSIColor qw(:constants);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000011
12my $P = $0;
13$P =~ s@.*/@@g;
14
Paolo Bonzini777d05b2017-10-04 16:35:53 +020015our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
16
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000017my $V = '0.31';
18
19use Getopt::Long qw(:config no_auto_abbrev);
20
21my $quiet = 0;
22my $tree = 1;
23my $chk_signoff = 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010024my $chk_patch = undef;
25my $chk_branch = undef;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000026my $tst_only;
27my $emacs = 0;
28my $terse = 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010029my $file = undef;
Paolo Bonzini1db42692018-11-29 09:52:58 +010030my $color = "auto";
Paolo Bonzini141de882016-08-09 17:47:44 +020031my $no_warnings = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000032my $summary = 1;
33my $mailback = 0;
34my $summary_file = 0;
35my $root;
36my %debug;
37my $help = 0;
38
39sub help {
40 my ($exitcode) = @_;
41
42 print << "EOM";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010043Usage:
44
45 $P [OPTION]... [FILE]...
46 $P [OPTION]... [GIT-REV-LIST]
47
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000048Version: $V
49
50Options:
51 -q, --quiet quiet
52 --no-tree run without a kernel tree
53 --no-signoff do not check for 'Signed-off-by' line
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010054 --patch treat FILE as patchfile
55 --branch treat args as GIT revision list
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000056 --emacs emacs compile window format
57 --terse one line per report
58 -f, --file treat FILE as regular source file
Paolo Bonzini141de882016-08-09 17:47:44 +020059 --strict fail if only warnings are found
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000060 --root=PATH PATH to the kernel tree root
61 --no-summary suppress the per-file summary
62 --mailback only produce a report in case of warnings/errors
63 --summary-file include the filename in summary
64 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
65 'values', 'possible', 'type', and 'attr' (default
66 is all off)
67 --test-only=WORD report only warnings/errors containing WORD
68 literally
Paolo Bonzini1db42692018-11-29 09:52:58 +010069 --color[=WHEN] Use colors 'always', 'never', or only when output
70 is a terminal ('auto'). Default is 'auto'.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000071 -h, --help, --version display this help and exit
72
73When FILE is - read standard input.
74EOM
75
76 exit($exitcode);
77}
78
Paolo Bonzini1db42692018-11-29 09:52:58 +010079# Perl's Getopt::Long allows options to take optional arguments after a space.
80# Prevent --color by itself from consuming other arguments
81foreach (@ARGV) {
82 if ($_ eq "--color" || $_ eq "-color") {
83 $_ = "--color=$color";
84 }
85}
86
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000087GetOptions(
88 'q|quiet+' => \$quiet,
89 'tree!' => \$tree,
90 'signoff!' => \$chk_signoff,
91 'patch!' => \$chk_patch,
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010092 'branch!' => \$chk_branch,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000093 'emacs!' => \$emacs,
94 'terse!' => \$terse,
95 'f|file!' => \$file,
Paolo Bonzini141de882016-08-09 17:47:44 +020096 'strict!' => \$no_warnings,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000097 'root=s' => \$root,
98 'summary!' => \$summary,
99 'mailback!' => \$mailback,
100 'summary-file!' => \$summary_file,
101
102 'debug=s' => \%debug,
103 'test-only=s' => \$tst_only,
Paolo Bonzini1db42692018-11-29 09:52:58 +0100104 'color=s' => \$color,
105 'no-color' => sub { $color = 'never'; },
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000106 'h|help' => \$help,
107 'version' => \$help
108) or help(1);
109
110help(0) if ($help);
111
112my $exit = 0;
113
114if ($#ARGV < 0) {
115 print "$P: no input files\n";
116 exit(1);
117}
118
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100119if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200120 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
121 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
122 $chk_patch = $chk_branch || $file ? 0 : 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100123} elsif (!defined $chk_branch && !defined $chk_patch) {
124 if ($file) {
125 $chk_branch = $chk_patch = 0;
126 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200127 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100128 $chk_patch = $chk_branch ? 0 : 1;
129 }
130} elsif (!defined $chk_branch && !defined $file) {
131 if ($chk_patch) {
132 $chk_branch = $file = 0;
133 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200134 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100135 $file = $chk_branch ? 0 : 1;
136 }
137} elsif (!defined $chk_patch && !defined $file) {
138 if ($chk_branch) {
139 $chk_patch = $file = 0;
140 } else {
Paolo Bonzini777d05b2017-10-04 16:35:53 +0200141 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
142 $chk_patch = $file ? 0 : 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100143 }
144} elsif (!defined $chk_branch) {
145 $chk_branch = $chk_patch || $file ? 0 : 1;
146} elsif (!defined $chk_patch) {
147 $chk_patch = $chk_branch || $file ? 0 : 1;
148} elsif (!defined $file) {
149 $file = $chk_patch || $chk_branch ? 0 : 1;
150}
151
152if (($chk_patch && $chk_branch) ||
153 ($chk_patch && $file) ||
154 ($chk_branch && $file)) {
155 die "Only one of --file, --branch, --patch is permitted\n";
156}
157if (!$chk_patch && !$chk_branch && !$file) {
158 die "One of --file, --branch, --patch is required\n";
159}
160
Paolo Bonzini1db42692018-11-29 09:52:58 +0100161if ($color =~ /^always$/i) {
162 $color = 1;
163} elsif ($color =~ /^never$/i) {
164 $color = 0;
165} elsif ($color =~ /^auto$/i) {
166 $color = (-t STDOUT);
167} else {
168 die "Invalid color mode: $color\n";
169}
170
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000171my $dbg_values = 0;
172my $dbg_possible = 0;
173my $dbg_type = 0;
174my $dbg_attr = 0;
Don Slutza99ac042012-09-02 19:22:35 -0400175my $dbg_adv_dcs = 0;
Don Slutz54243022012-09-02 19:22:36 -0400176my $dbg_adv_checking = 0;
Don Slutz69402a62012-09-02 19:22:37 -0400177my $dbg_adv_apw = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000178for my $key (keys %debug) {
179 ## no critic
180 eval "\${dbg_$key} = '$debug{$key}';";
181 die "$@" if ($@);
182}
183
184my $rpt_cleaners = 0;
185
186if ($terse) {
187 $emacs = 1;
188 $quiet++;
189}
190
191if ($tree) {
192 if (defined $root) {
193 if (!top_of_kernel_tree($root)) {
194 die "$P: $root: --root does not point at a valid tree\n";
195 }
196 } else {
197 if (top_of_kernel_tree('.')) {
198 $root = '.';
199 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
200 top_of_kernel_tree($1)) {
201 $root = $1;
202 }
203 }
204
205 if (!defined $root) {
206 print "Must be run from the top-level dir. of a kernel tree\n";
207 exit(2);
208 }
209}
210
211my $emitted_corrupt = 0;
212
213our $Ident = qr{
214 [A-Za-z_][A-Za-z\d_]*
215 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
216 }x;
217our $Storage = qr{extern|static|asmlinkage};
218our $Sparse = qr{
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200219 __force
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000220 }x;
221
222# Notes to $Attribute:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000223our $Attribute = qr{
224 const|
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200225 volatile|
226 QEMU_NORETURN|
227 QEMU_WARN_UNUSED_RESULT|
228 QEMU_SENTINEL|
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200229 QEMU_PACKED|
230 GCC_FMT_ATTR
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000231 }x;
232our $Modifier;
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200233our $Inline = qr{inline};
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000234our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
235our $Lval = qr{$Ident(?:$Member)*};
236
237our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
238our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
239our $Compare = qr{<=|>=|==|!=|<|>};
240our $Operators = qr{
241 <=|>=|==|!=|
242 =>|->|<<|>>|<|>|!|~|
243 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
244 }x;
245
246our $NonptrType;
247our $Type;
248our $Declare;
249
Joe Perchesb37c4aa2018-04-30 13:46:47 +0100250our $NON_ASCII_UTF8 = qr{
251 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000252 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
253 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
254 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
255 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
256 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
257 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
258}x;
259
Joe Perchesb37c4aa2018-04-30 13:46:47 +0100260our $UTF8 = qr{
261 [\x09\x0A\x0D\x20-\x7E] # ASCII
262 | $NON_ASCII_UTF8
263}x;
264
Paolo Bonzini874acb62019-05-17 15:19:00 +0200265# some readers default to ISO-8859-1 when showing email source. detect
266# when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back.
267# False positives are possible but very unlikely.
268our $UTF8_MOJIBAKE = qr{
269 \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf
270 | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf
271 | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf
272 | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf
273 | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf
274 | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf
275 | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf
276}x;
277
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200278# There are still some false positives, but this catches most
279# common cases.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000280our $typeTypedefs = qr{(?x:
Philippe Mathieu-Daudé5fa96ca2018-06-25 09:41:56 -0300281 (?![KMGTPE]iB) # IEC binary prefix (do not match)
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200282 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
283 | [A-Z][A-Z\d_]*AIOCB # all uppercase
284 | [A-Z][A-Z\d_]*CPU # all uppercase
285 | QEMUBH # all uppercase
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000286)};
287
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000288our @typeList = (
289 qr{void},
290 qr{(?:unsigned\s+)?char},
291 qr{(?:unsigned\s+)?short},
292 qr{(?:unsigned\s+)?int},
293 qr{(?:unsigned\s+)?long},
294 qr{(?:unsigned\s+)?long\s+int},
295 qr{(?:unsigned\s+)?long\s+long},
296 qr{(?:unsigned\s+)?long\s+long\s+int},
297 qr{unsigned},
298 qr{float},
299 qr{double},
300 qr{bool},
301 qr{struct\s+$Ident},
302 qr{union\s+$Ident},
303 qr{enum\s+$Ident},
304 qr{${Ident}_t},
305 qr{${Ident}_handler},
306 qr{${Ident}_handler_fn},
Cédric Le Goaterf0707d22016-04-01 11:40:06 +0200307 qr{target_(?:u)?long},
Greg Kurz825bfa02017-09-14 11:12:07 +0200308 qr{hwaddr},
Peter Xu82870f32018-04-25 15:01:03 +0800309 # external libraries
Klim Kireeved279a02018-01-12 12:01:19 +0300310 qr{xml${Ident}},
Paul Durrant8d0f08e2018-05-22 11:42:50 -0700311 qr{xen\w+_handle},
Peter Xu82870f32018-04-25 15:01:03 +0800312 # Glib definitions
313 qr{gchar},
314 qr{gshort},
315 qr{glong},
316 qr{gint},
317 qr{gboolean},
318 qr{guchar},
319 qr{gushort},
320 qr{gulong},
321 qr{guint},
322 qr{gfloat},
323 qr{gdouble},
324 qr{gpointer},
325 qr{gconstpointer},
326 qr{gint8},
327 qr{guint8},
328 qr{gint16},
329 qr{guint16},
330 qr{gint32},
331 qr{guint32},
332 qr{gint64},
333 qr{guint64},
334 qr{gsize},
335 qr{gssize},
336 qr{goffset},
337 qr{gintptr},
338 qr{guintptr},
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000339);
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200340
341# This can be modified by sub possible. Since it can be empty, be careful
342# about regexes that always match, because they can cause infinite loops.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000343our @modifierList = (
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000344);
345
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000346sub build_types {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000347 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200348 if (@modifierList > 0) {
349 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
350 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
351 } else {
352 $Modifier = qr{(?:$Attribute|$Sparse)};
353 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000354 $NonptrType = qr{
355 (?:$Modifier\s+|const\s+)*
356 (?:
357 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
358 (?:$typeTypedefs\b)|
359 (?:${all}\b)
360 )
361 (?:\s+$Modifier|\s+const)*
362 }x;
363 $Type = qr{
364 $NonptrType
365 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
366 (?:\s+$Inline|\s+$Modifier)*
367 }x;
368 $Declare = qr{(?:$Storage\s+)?$Type};
369}
370build_types();
371
372$chk_signoff = 0 if ($file);
373
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000374my @rawlines = ();
375my @lines = ();
376my $vname;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100377if ($chk_branch) {
378 my @patches;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100379 my %git_commits = ();
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100380 my $HASH;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100381 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) ||
382 die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100383
Paolo Bonzinic182b612018-11-29 09:35:47 +0100384 for my $line (<$HASH>) {
385 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
386 next if (!defined($1) || !defined($2));
387 my $sha1 = $1;
388 my $subject = $2;
389 push(@patches, $sha1);
390 $git_commits{$sha1} = $subject;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000391 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100392
393 close $HASH;
394
395 die "$P: no revisions returned for revlist '$chk_branch'\n"
396 unless @patches;
397
Paolo Bonzinic182b612018-11-29 09:35:47 +0100398 my $i = 1;
399 my $num_patches = @patches;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100400 for my $hash (@patches) {
401 my $FILE;
402 open($FILE, '-|', "git", "show", $hash) ||
403 die "$P: git show $hash - $!\n";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100404 while (<$FILE>) {
405 chomp;
406 push(@rawlines, $_);
407 }
408 close($FILE);
Paolo Bonzinic182b612018-11-29 09:35:47 +0100409 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')';
410 if ($num_patches > 1 && $quiet == 0) {
Paolo Bonzini1db42692018-11-29 09:52:58 +0100411 my $prefix = "$i/$num_patches";
412 $prefix = BLUE . BOLD . $prefix . RESET if $color;
413 print "$prefix Checking commit $vname\n";
Paolo Bonzinic182b612018-11-29 09:35:47 +0100414 $vname = "Patch $i/$num_patches";
415 } else {
416 $vname = "Commit " . $vname;
417 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100418 if (!process($hash)) {
419 $exit = 1;
Paolo Bonzinic182b612018-11-29 09:35:47 +0100420 print "\n" if ($num_patches > 1 && $quiet == 0);
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100421 }
422 @rawlines = ();
423 @lines = ();
Paolo Bonzinic182b612018-11-29 09:35:47 +0100424 $i++;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000425 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100426} else {
427 for my $filename (@ARGV) {
428 my $FILE;
429 if ($file) {
430 open($FILE, '-|', "diff -u /dev/null $filename") ||
431 die "$P: $filename: diff failed - $!\n";
432 } elsif ($filename eq '-') {
433 open($FILE, '<&STDIN');
434 } else {
435 open($FILE, '<', "$filename") ||
436 die "$P: $filename: open failed - $!\n";
437 }
438 if ($filename eq '-') {
439 $vname = 'Your patch';
440 } else {
441 $vname = $filename;
442 }
Paolo Bonzinic182b612018-11-29 09:35:47 +0100443 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100444 while (<$FILE>) {
445 chomp;
446 push(@rawlines, $_);
447 }
448 close($FILE);
449 if (!process($filename)) {
450 $exit = 1;
451 }
452 @rawlines = ();
453 @lines = ();
454 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000455}
456
457exit($exit);
458
459sub top_of_kernel_tree {
460 my ($root) = @_;
461
462 my @tree_check = (
Blue Swirlb6469682011-01-20 20:58:56 +0000463 "COPYING", "MAINTAINERS", "Makefile",
Daniel P. Berrangé336a7452019-08-23 17:09:24 +0100464 "README.rst", "docs", "VERSION",
Alexander Bulekovbac068e2020-02-19 23:10:57 -0500465 "linux-user", "softmmu"
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000466 );
467
468 foreach my $check (@tree_check) {
469 if (! -e $root . '/' . $check) {
470 return 0;
471 }
472 }
473 return 1;
474}
475
476sub expand_tabs {
477 my ($str) = @_;
478
479 my $res = '';
480 my $n = 0;
481 for my $c (split(//, $str)) {
482 if ($c eq "\t") {
483 $res .= ' ';
484 $n++;
485 for (; ($n % 8) != 0; $n++) {
486 $res .= ' ';
487 }
488 next;
489 }
490 $res .= $c;
491 $n++;
492 }
493
494 return $res;
495}
496sub copy_spacing {
497 (my $res = shift) =~ tr/\t/ /c;
498 return $res;
499}
500
501sub line_stats {
502 my ($line) = @_;
503
504 # Drop the diff line leader and expand tabs
505 $line =~ s/^.//;
506 $line = expand_tabs($line);
507
508 # Pick the indent from the front of the line.
509 my ($white) = ($line =~ /^(\s*)/);
510
511 return (length($line), length($white));
512}
513
514my $sanitise_quote = '';
515
516sub sanitise_line_reset {
517 my ($in_comment) = @_;
518
519 if ($in_comment) {
520 $sanitise_quote = '*/';
521 } else {
522 $sanitise_quote = '';
523 }
524}
525sub sanitise_line {
526 my ($line) = @_;
527
528 my $res = '';
529 my $l = '';
530
531 my $qlen = 0;
532 my $off = 0;
533 my $c;
534
535 # Always copy over the diff marker.
536 $res = substr($line, 0, 1);
537
538 for ($off = 1; $off < length($line); $off++) {
539 $c = substr($line, $off, 1);
540
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100541 # Comments we are wacking completely including the begin
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000542 # and end, all to $;.
543 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
544 $sanitise_quote = '*/';
545
546 substr($res, $off, 2, "$;$;");
547 $off++;
548 next;
549 }
550 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
551 $sanitise_quote = '';
552 substr($res, $off, 2, "$;$;");
553 $off++;
554 next;
555 }
556 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
557 $sanitise_quote = '//';
558
559 substr($res, $off, 2, $sanitise_quote);
560 $off++;
561 next;
562 }
563
564 # A \ in a string means ignore the next character.
565 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
566 $c eq "\\") {
567 substr($res, $off, 2, 'XX');
568 $off++;
569 next;
570 }
571 # Regular quotes.
572 if ($c eq "'" || $c eq '"') {
573 if ($sanitise_quote eq '') {
574 $sanitise_quote = $c;
575
576 substr($res, $off, 1, $c);
577 next;
578 } elsif ($sanitise_quote eq $c) {
579 $sanitise_quote = '';
580 }
581 }
582
583 #print "c<$c> SQ<$sanitise_quote>\n";
584 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
585 substr($res, $off, 1, $;);
586 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
587 substr($res, $off, 1, $;);
588 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
589 substr($res, $off, 1, 'X');
590 } else {
591 substr($res, $off, 1, $c);
592 }
593 }
594
595 if ($sanitise_quote eq '//') {
596 $sanitise_quote = '';
597 }
598
599 # The pathname on a #include may be surrounded by '<' and '>'.
600 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
601 my $clean = 'X' x length($1);
602 $res =~ s@\<.*\>@<$clean>@;
603
604 # The whole of a #error is a string.
605 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
606 my $clean = 'X' x length($1);
607 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
608 }
609
610 return $res;
611}
612
613sub ctx_statement_block {
614 my ($linenr, $remain, $off) = @_;
615 my $line = $linenr - 1;
616 my $blk = '';
617 my $soff = $off;
618 my $coff = $off - 1;
619 my $coff_set = 0;
620
621 my $loff = 0;
622
623 my $type = '';
624 my $level = 0;
625 my @stack = ();
626 my $p;
627 my $c;
628 my $len = 0;
629
630 my $remainder;
631 while (1) {
632 @stack = (['', 0]) if ($#stack == -1);
633
634 #warn "CSB: blk<$blk> remain<$remain>\n";
635 # If we are about to drop off the end, pull in more
636 # context.
637 if ($off >= $len) {
638 for (; $remain > 0; $line++) {
639 last if (!defined $lines[$line]);
640 next if ($lines[$line] =~ /^-/);
641 $remain--;
642 $loff = $len;
643 $blk .= $lines[$line] . "\n";
644 $len = length($blk);
645 $line++;
646 last;
647 }
648 # Bail if there is no further context.
649 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
650 if ($off >= $len) {
651 last;
652 }
653 }
654 $p = $c;
655 $c = substr($blk, $off, 1);
656 $remainder = substr($blk, $off);
657
658 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
659
660 # Handle nested #if/#else.
661 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
662 push(@stack, [ $type, $level ]);
663 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
664 ($type, $level) = @{$stack[$#stack - 1]};
665 } elsif ($remainder =~ /^#\s*endif\b/) {
666 ($type, $level) = @{pop(@stack)};
667 }
668
669 # Statement ends at the ';' or a close '}' at the
670 # outermost level.
671 if ($level == 0 && $c eq ';') {
672 last;
673 }
674
675 # An else is really a conditional as long as its not else if
676 if ($level == 0 && $coff_set == 0 &&
677 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
678 $remainder =~ /^(else)(?:\s|{)/ &&
679 $remainder !~ /^else\s+if\b/) {
680 $coff = $off + length($1) - 1;
681 $coff_set = 1;
682 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
683 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
684 }
685
686 if (($type eq '' || $type eq '(') && $c eq '(') {
687 $level++;
688 $type = '(';
689 }
690 if ($type eq '(' && $c eq ')') {
691 $level--;
692 $type = ($level != 0)? '(' : '';
693
694 if ($level == 0 && $coff < $soff) {
695 $coff = $off;
696 $coff_set = 1;
697 #warn "CSB: mark coff<$coff>\n";
698 }
699 }
700 if (($type eq '' || $type eq '{') && $c eq '{') {
701 $level++;
702 $type = '{';
703 }
704 if ($type eq '{' && $c eq '}') {
705 $level--;
706 $type = ($level != 0)? '{' : '';
707
708 if ($level == 0) {
709 if (substr($blk, $off + 1, 1) eq ';') {
710 $off++;
711 }
712 last;
713 }
714 }
715 $off++;
716 }
717 # We are truly at the end, so shuffle to the next line.
718 if ($off == $len) {
719 $loff = $len + 1;
720 $line++;
721 $remain--;
722 }
723
724 my $statement = substr($blk, $soff, $off - $soff + 1);
725 my $condition = substr($blk, $soff, $coff - $soff + 1);
726
727 #warn "STATEMENT<$statement>\n";
728 #warn "CONDITION<$condition>\n";
729
730 #print "coff<$coff> soff<$off> loff<$loff>\n";
731
732 return ($statement, $condition,
733 $line, $remain + 1, $off - $loff + 1, $level);
734}
735
736sub statement_lines {
737 my ($stmt) = @_;
738
739 # Strip the diff line prefixes and rip blank lines at start and end.
740 $stmt =~ s/(^|\n)./$1/g;
741 $stmt =~ s/^\s*//;
742 $stmt =~ s/\s*$//;
743
744 my @stmt_lines = ($stmt =~ /\n/g);
745
746 return $#stmt_lines + 2;
747}
748
749sub statement_rawlines {
750 my ($stmt) = @_;
751
752 my @stmt_lines = ($stmt =~ /\n/g);
753
754 return $#stmt_lines + 2;
755}
756
757sub statement_block_size {
758 my ($stmt) = @_;
759
760 $stmt =~ s/(^|\n)./$1/g;
Fam Zheng04f25622015-09-11 19:07:36 +0800761 $stmt =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000762 $stmt =~ s/}\s*$//;
763 $stmt =~ s/^\s*//;
764 $stmt =~ s/\s*$//;
765
766 my @stmt_lines = ($stmt =~ /\n/g);
767 my @stmt_statements = ($stmt =~ /;/g);
768
769 my $stmt_lines = $#stmt_lines + 2;
770 my $stmt_statements = $#stmt_statements + 1;
771
772 if ($stmt_lines > $stmt_statements) {
773 return $stmt_lines;
774 } else {
775 return $stmt_statements;
776 }
777}
778
779sub ctx_statement_full {
780 my ($linenr, $remain, $off) = @_;
781 my ($statement, $condition, $level);
782
783 my (@chunks);
784
785 # Grab the first conditional/block pair.
786 ($statement, $condition, $linenr, $remain, $off, $level) =
787 ctx_statement_block($linenr, $remain, $off);
788 #print "F: c<$condition> s<$statement> remain<$remain>\n";
789 push(@chunks, [ $condition, $statement ]);
790 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
791 return ($level, $linenr, @chunks);
792 }
793
794 # Pull in the following conditional/block pairs and see if they
795 # could continue the statement.
796 for (;;) {
797 ($statement, $condition, $linenr, $remain, $off, $level) =
798 ctx_statement_block($linenr, $remain, $off);
799 #print "C: c<$condition> s<$statement> remain<$remain>\n";
800 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
801 #print "C: push\n";
802 push(@chunks, [ $condition, $statement ]);
803 }
804
805 return ($level, $linenr, @chunks);
806}
807
808sub ctx_block_get {
809 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
810 my $line;
811 my $start = $linenr - 1;
812 my $blk = '';
813 my @o;
814 my @c;
815 my @res = ();
816
817 my $level = 0;
818 my @stack = ($level);
819 for ($line = $start; $remain > 0; $line++) {
820 next if ($rawlines[$line] =~ /^-/);
821 $remain--;
822
823 $blk .= $rawlines[$line];
824
825 # Handle nested #if/#else.
826 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
827 push(@stack, $level);
828 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
829 $level = $stack[$#stack - 1];
830 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
831 $level = pop(@stack);
832 }
833
834 foreach my $c (split(//, $lines[$line])) {
835 ##print "C<$c>L<$level><$open$close>O<$off>\n";
836 if ($off > 0) {
837 $off--;
838 next;
839 }
840
841 if ($c eq $close && $level > 0) {
842 $level--;
843 last if ($level == 0);
844 } elsif ($c eq $open) {
845 $level++;
846 }
847 }
848
849 if (!$outer || $level <= 1) {
850 push(@res, $rawlines[$line]);
851 }
852
853 last if ($level == 0);
854 }
855
856 return ($level, @res);
857}
858sub ctx_block_outer {
859 my ($linenr, $remain) = @_;
860
861 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
862 return @r;
863}
864sub ctx_block {
865 my ($linenr, $remain) = @_;
866
867 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
868 return @r;
869}
870sub ctx_statement {
871 my ($linenr, $remain, $off) = @_;
872
873 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
874 return @r;
875}
876sub ctx_block_level {
877 my ($linenr, $remain) = @_;
878
879 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
880}
881sub ctx_statement_level {
882 my ($linenr, $remain, $off) = @_;
883
884 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
885}
886
887sub ctx_locate_comment {
888 my ($first_line, $end_line) = @_;
889
890 # Catch a comment on the end of the line itself.
891 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
892 return $current_comment if (defined $current_comment);
893
894 # Look through the context and try and figure out if there is a
895 # comment.
896 my $in_comment = 0;
897 $current_comment = '';
898 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
899 my $line = $rawlines[$linenr - 1];
900 #warn " $line\n";
901 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
902 $in_comment = 1;
903 }
904 if ($line =~ m@/\*@) {
905 $in_comment = 1;
906 }
907 if (!$in_comment && $current_comment ne '') {
908 $current_comment = '';
909 }
910 $current_comment .= $line . "\n" if ($in_comment);
911 if ($line =~ m@\*/@) {
912 $in_comment = 0;
913 }
914 }
915
916 chomp($current_comment);
917 return($current_comment);
918}
919sub ctx_has_comment {
920 my ($first_line, $end_line) = @_;
921 my $cmt = ctx_locate_comment($first_line, $end_line);
922
923 ##print "LINE: $rawlines[$end_line - 1 ]\n";
924 ##print "CMMT: $cmt\n";
925
926 return ($cmt ne '');
927}
928
929sub raw_line {
930 my ($linenr, $cnt) = @_;
931
932 my $offset = $linenr - 1;
933 $cnt++;
934
935 my $line;
936 while ($cnt) {
937 $line = $rawlines[$offset++];
938 next if (defined($line) && $line =~ /^-/);
939 $cnt--;
940 }
941
942 return $line;
943}
944
945sub cat_vet {
946 my ($vet) = @_;
947 my ($res, $coded);
948
949 $res = '';
950 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
951 $res .= $1;
952 if ($2 ne '') {
953 $coded = sprintf("^%c", unpack('C', $2) + 64);
954 $res .= $coded;
955 }
956 }
957 $res =~ s/$/\$/;
958
959 return $res;
960}
961
962my $av_preprocessor = 0;
963my $av_pending;
964my @av_paren_type;
965my $av_pend_colon;
966
967sub annotate_reset {
968 $av_preprocessor = 0;
969 $av_pending = '_';
970 @av_paren_type = ('E');
971 $av_pend_colon = 'O';
972}
973
974sub annotate_values {
975 my ($stream, $type) = @_;
976
977 my $res;
978 my $var = '_' x length($stream);
979 my $cur = $stream;
980
981 print "$stream\n" if ($dbg_values > 1);
982
983 while (length($cur)) {
984 @av_paren_type = ('E') if ($#av_paren_type < 0);
985 print " <" . join('', @av_paren_type) .
986 "> <$type> <$av_pending>" if ($dbg_values > 1);
987 if ($cur =~ /^(\s+)/o) {
988 print "WS($1)\n" if ($dbg_values > 1);
989 if ($1 =~ /\n/ && $av_preprocessor) {
990 $type = pop(@av_paren_type);
991 $av_preprocessor = 0;
992 }
993
Florian Mickler61669f92011-11-25 10:24:16 +0100994 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000995 print "CAST($1)\n" if ($dbg_values > 1);
996 push(@av_paren_type, $type);
997 $type = 'C';
998
999 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1000 print "DECLARE($1)\n" if ($dbg_values > 1);
1001 $type = 'T';
1002
1003 } elsif ($cur =~ /^($Modifier)\s*/) {
1004 print "MODIFIER($1)\n" if ($dbg_values > 1);
1005 $type = 'T';
1006
1007 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1008 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1009 $av_preprocessor = 1;
1010 push(@av_paren_type, $type);
1011 if ($2 ne '') {
1012 $av_pending = 'N';
1013 }
1014 $type = 'E';
1015
1016 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1017 print "UNDEF($1)\n" if ($dbg_values > 1);
1018 $av_preprocessor = 1;
1019 push(@av_paren_type, $type);
1020
1021 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1022 print "PRE_START($1)\n" if ($dbg_values > 1);
1023 $av_preprocessor = 1;
1024
1025 push(@av_paren_type, $type);
1026 push(@av_paren_type, $type);
1027 $type = 'E';
1028
1029 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1030 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1031 $av_preprocessor = 1;
1032
1033 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1034
1035 $type = 'E';
1036
1037 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1038 print "PRE_END($1)\n" if ($dbg_values > 1);
1039
1040 $av_preprocessor = 1;
1041
1042 # Assume all arms of the conditional end as this
1043 # one does, and continue as if the #endif was not here.
1044 pop(@av_paren_type);
1045 push(@av_paren_type, $type);
1046 $type = 'E';
1047
1048 } elsif ($cur =~ /^(\\\n)/o) {
1049 print "PRECONT($1)\n" if ($dbg_values > 1);
1050
1051 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1052 print "ATTR($1)\n" if ($dbg_values > 1);
1053 $av_pending = $type;
1054 $type = 'N';
1055
1056 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1057 print "SIZEOF($1)\n" if ($dbg_values > 1);
1058 if (defined $2) {
1059 $av_pending = 'V';
1060 }
1061 $type = 'N';
1062
1063 } elsif ($cur =~ /^(if|while|for)\b/o) {
1064 print "COND($1)\n" if ($dbg_values > 1);
1065 $av_pending = 'E';
1066 $type = 'N';
1067
1068 } elsif ($cur =~/^(case)/o) {
1069 print "CASE($1)\n" if ($dbg_values > 1);
1070 $av_pend_colon = 'C';
1071 $type = 'N';
1072
1073 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1074 print "KEYWORD($1)\n" if ($dbg_values > 1);
1075 $type = 'N';
1076
1077 } elsif ($cur =~ /^(\()/o) {
1078 print "PAREN('$1')\n" if ($dbg_values > 1);
1079 push(@av_paren_type, $av_pending);
1080 $av_pending = '_';
1081 $type = 'N';
1082
1083 } elsif ($cur =~ /^(\))/o) {
1084 my $new_type = pop(@av_paren_type);
1085 if ($new_type ne '_') {
1086 $type = $new_type;
1087 print "PAREN('$1') -> $type\n"
1088 if ($dbg_values > 1);
1089 } else {
1090 print "PAREN('$1')\n" if ($dbg_values > 1);
1091 }
1092
1093 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1094 print "FUNC($1)\n" if ($dbg_values > 1);
1095 $type = 'V';
1096 $av_pending = 'V';
1097
1098 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1099 if (defined $2 && $type eq 'C' || $type eq 'T') {
1100 $av_pend_colon = 'B';
1101 } elsif ($type eq 'E') {
1102 $av_pend_colon = 'L';
1103 }
1104 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1105 $type = 'V';
1106
1107 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1108 print "IDENT($1)\n" if ($dbg_values > 1);
1109 $type = 'V';
1110
1111 } elsif ($cur =~ /^($Assignment)/o) {
1112 print "ASSIGN($1)\n" if ($dbg_values > 1);
1113 $type = 'N';
1114
1115 } elsif ($cur =~/^(;|{|})/) {
1116 print "END($1)\n" if ($dbg_values > 1);
1117 $type = 'E';
1118 $av_pend_colon = 'O';
1119
1120 } elsif ($cur =~/^(,)/) {
1121 print "COMMA($1)\n" if ($dbg_values > 1);
1122 $type = 'C';
1123
1124 } elsif ($cur =~ /^(\?)/o) {
1125 print "QUESTION($1)\n" if ($dbg_values > 1);
1126 $type = 'N';
1127
1128 } elsif ($cur =~ /^(:)/o) {
1129 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1130
1131 substr($var, length($res), 1, $av_pend_colon);
1132 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1133 $type = 'E';
1134 } else {
1135 $type = 'N';
1136 }
1137 $av_pend_colon = 'O';
1138
1139 } elsif ($cur =~ /^(\[)/o) {
1140 print "CLOSE($1)\n" if ($dbg_values > 1);
1141 $type = 'N';
1142
1143 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1144 my $variant;
1145
1146 print "OPV($1)\n" if ($dbg_values > 1);
1147 if ($type eq 'V') {
1148 $variant = 'B';
1149 } else {
1150 $variant = 'U';
1151 }
1152
1153 substr($var, length($res), 1, $variant);
1154 $type = 'N';
1155
1156 } elsif ($cur =~ /^($Operators)/o) {
1157 print "OP($1)\n" if ($dbg_values > 1);
1158 if ($1 ne '++' && $1 ne '--') {
1159 $type = 'N';
1160 }
1161
1162 } elsif ($cur =~ /(^.)/o) {
1163 print "C($1)\n" if ($dbg_values > 1);
1164 }
1165 if (defined $1) {
1166 $cur = substr($cur, length($1));
1167 $res .= $type x length($1);
1168 }
1169 }
1170
1171 return ($res, $var);
1172}
1173
1174sub possible {
1175 my ($possible, $line) = @_;
1176 my $notPermitted = qr{(?:
1177 ^(?:
1178 $Modifier|
1179 $Storage|
1180 $Type|
1181 DEFINE_\S+
1182 )$|
1183 ^(?:
1184 goto|
1185 return|
1186 case|
1187 else|
1188 asm|__asm__|
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001189 do
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001190 )(?:\s|$)|
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001191 ^(?:typedef|struct|enum)\b|
1192 ^\#
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001193 )}x;
1194 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1195 if ($possible !~ $notPermitted) {
1196 # Check for modifiers.
1197 $possible =~ s/\s*$Storage\s*//g;
1198 $possible =~ s/\s*$Sparse\s*//g;
1199 if ($possible =~ /^\s*$/) {
1200
1201 } elsif ($possible =~ /\s/) {
Paolo Bonzinie20122f2018-07-04 18:05:43 +02001202 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001203 for my $modifier (split(' ', $possible)) {
1204 if ($modifier !~ $notPermitted) {
1205 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1206 push(@modifierList, $modifier);
1207 }
1208 }
1209
1210 } else {
1211 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1212 push(@typeList, $possible);
1213 }
1214 build_types();
1215 } else {
1216 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1217 }
1218}
1219
1220my $prefix = '';
1221
1222sub report {
Paolo Bonzini1db42692018-11-29 09:52:58 +01001223 my ($level, $msg) = @_;
1224 if (defined $tst_only && $msg !~ /\Q$tst_only\E/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001225 return 0;
1226 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001227
Paolo Bonzini1db42692018-11-29 09:52:58 +01001228 my $output = '';
1229 $output .= BOLD if $color;
1230 $output .= $prefix;
1231 $output .= RED if $color && $level eq 'ERROR';
1232 $output .= MAGENTA if $color && $level eq 'WARNING';
1233 $output .= $level . ':';
1234 $output .= RESET if $color;
1235 $output .= ' ' . $msg . "\n";
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001236
Paolo Bonzini1db42692018-11-29 09:52:58 +01001237 $output = (split('\n', $output))[0] . "\n" if ($terse);
1238
1239 push(our @report, $output);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001240
1241 return 1;
1242}
1243sub report_dump {
1244 our @report;
1245}
1246sub ERROR {
Paolo Bonzini1db42692018-11-29 09:52:58 +01001247 if (report("ERROR", $_[0])) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001248 our $clean = 0;
1249 our $cnt_error++;
1250 }
1251}
1252sub WARN {
Paolo Bonzini1db42692018-11-29 09:52:58 +01001253 if (report("WARNING", $_[0])) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001254 our $clean = 0;
1255 our $cnt_warn++;
1256 }
1257}
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001258
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001259# According to tests/qtest/bios-tables-test.c: do not
1260# change expected file in the same commit with adding test
1261sub checkfilename {
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001262 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_;
1263
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001264 if ($name =~ m#^tests/data/acpi/# and
1265 # make exception for a shell script that rebuilds the files
1266 not $name =~ m#^\.sh$# or
1267 $name =~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) {
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001268 $$acpi_testexpected = $name;
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001269 } else {
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001270 $$acpi_nontestexpected = $name;
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001271 }
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001272 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) {
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001273 ERROR("Do not add expected files together with tests, " .
1274 "follow instructions in " .
1275 "tests/qtest/bios-tables-test.c: both " .
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001276 $$acpi_testexpected . " and " .
1277 $$acpi_nontestexpected . " found\n");
Michael S. Tsirkin1c3c9a82020-03-15 07:31:38 -04001278 }
1279}
1280
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001281sub process {
1282 my $filename = shift;
1283
1284 my $linenr=0;
1285 my $prevline="";
1286 my $prevrawline="";
1287 my $stashline="";
1288 my $stashrawline="";
1289
1290 my $length;
1291 my $indent;
1292 my $previndent=0;
1293 my $stashindent=0;
1294
1295 our $clean = 1;
1296 my $signoff = 0;
1297 my $is_patch = 0;
1298
Joe Perches5fc7e402018-04-30 13:46:49 +01001299 my $in_header_lines = $file ? 0 : 1;
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001300 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches4be61312018-04-30 13:46:50 +01001301 my $reported_maintainer_file = 0;
Pasi Savanainenbf139a02018-04-30 13:46:48 +01001302 my $non_utf8_charset = 0;
1303
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001304 our @report = ();
1305 our $cnt_lines = 0;
1306 our $cnt_error = 0;
1307 our $cnt_warn = 0;
1308 our $cnt_chk = 0;
1309
1310 # Trace the real file/line as we go.
1311 my $realfile = '';
1312 my $realline = 0;
1313 my $realcnt = 0;
1314 my $here = '';
1315 my $in_comment = 0;
1316 my $comment_edge = 0;
1317 my $first_line = 0;
1318 my $p1_prefix = '';
1319
1320 my $prev_values = 'E';
1321
1322 # suppression flags
1323 my %suppress_ifbraces;
1324 my %suppress_whiletrailers;
1325 my %suppress_export;
1326
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001327 my $acpi_testexpected;
1328 my $acpi_nontestexpected;
1329
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001330 # Pre-scan the patch sanitizing the lines.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001331
1332 sanitise_line_reset();
1333 my $line;
1334 foreach my $rawline (@rawlines) {
1335 $linenr++;
1336 $line = $rawline;
1337
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001338 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1339 $realline=$1-1;
1340 if (defined $2) {
1341 $realcnt=$3+1;
1342 } else {
1343 $realcnt=1+1;
1344 }
1345 $in_comment = 0;
1346
1347 # Guestimate if this is a continuing comment. Run
1348 # the context looking for a comment "edge". If this
1349 # edge is a close comment then we must be in a comment
1350 # at context start.
1351 my $edge;
1352 my $cnt = $realcnt;
1353 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1354 next if (defined $rawlines[$ln - 1] &&
1355 $rawlines[$ln - 1] =~ /^-/);
1356 $cnt--;
1357 #print "RAW<$rawlines[$ln - 1]>\n";
1358 last if (!defined $rawlines[$ln - 1]);
1359 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1360 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1361 ($edge) = $1;
1362 last;
1363 }
1364 }
1365 if (defined $edge && $edge eq '*/') {
1366 $in_comment = 1;
1367 }
1368
1369 # Guestimate if this is a continuing comment. If this
1370 # is the start of a diff block and this line starts
1371 # ' *' then it is very likely a comment.
1372 if (!defined $edge &&
1373 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1374 {
1375 $in_comment = 1;
1376 }
1377
1378 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1379 sanitise_line_reset($in_comment);
1380
1381 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1382 # Standardise the strings and chars within the input to
1383 # simplify matching -- only bother with positive lines.
1384 $line = sanitise_line($rawline);
1385 }
1386 push(@lines, $line);
1387
1388 if ($realcnt > 1) {
1389 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1390 } else {
1391 $realcnt = 0;
1392 }
1393
1394 #print "==>$rawline\n";
1395 #print "-->$line\n";
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001396 }
1397
1398 $prefix = '';
1399
1400 $realcnt = 0;
1401 $linenr = 0;
1402 foreach my $line (@lines) {
1403 $linenr++;
1404
1405 my $rawline = $rawlines[$linenr - 1];
1406
1407#extract the line range in the file after the patch is applied
1408 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1409 $is_patch = 1;
1410 $first_line = $linenr + 1;
1411 $realline=$1-1;
1412 if (defined $2) {
1413 $realcnt=$3+1;
1414 } else {
1415 $realcnt=1+1;
1416 }
1417 annotate_reset();
1418 $prev_values = 'E';
1419
1420 %suppress_ifbraces = ();
1421 %suppress_whiletrailers = ();
1422 %suppress_export = ();
1423 next;
1424
1425# track the line number as we move through the hunk, note that
1426# new versions of GNU diff omit the leading space on completely
1427# blank context lines so we need to count that too.
1428 } elsif ($line =~ /^( |\+|$)/) {
1429 $realline++;
1430 $realcnt-- if ($realcnt != 0);
1431
1432 # Measure the line length and indent.
1433 ($length, $indent) = line_stats($rawline);
1434
1435 # Track the previous line.
1436 ($prevline, $stashline) = ($stashline, $line);
1437 ($previndent, $stashindent) = ($stashindent, $indent);
1438 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1439
1440 #warn "line<$line>\n";
1441
1442 } elsif ($realcnt == 1) {
1443 $realcnt--;
1444 }
1445
1446 my $hunk_line = ($realcnt != 0);
1447
1448#make up the handle for any error we report on this line
1449 $prefix = "$filename:$realline: " if ($emacs && $file);
1450 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1451
1452 $here = "#$linenr: " if (!$file);
1453 $here = "#$realline: " if ($file);
1454
1455 # extract the filename as it passes
1456 if ($line =~ /^diff --git.*?(\S+)$/) {
1457 $realfile = $1;
Paolo Bonzini1a5c63c2018-08-10 16:35:19 +02001458 $realfile =~ s@^([^/]*)/@@ if (!$file);
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001459 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001460 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1461 $realfile = $1;
Paolo Bonzini1a5c63c2018-08-10 16:35:19 +02001462 $realfile =~ s@^([^/]*)/@@ if (!$file);
Michael S. Tsirkine625ba22020-05-04 07:22:49 -04001463 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001464
1465 $p1_prefix = $1;
1466 if (!$file && $tree && $p1_prefix ne '' &&
1467 -e "$root/$p1_prefix") {
1468 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1469 }
1470
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001471 next;
1472 }
1473
1474 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1475
1476 my $hereline = "$here\n$rawline\n";
1477 my $herecurr = "$here\n$rawline\n";
1478 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1479
1480 $cnt_lines++ if ($realcnt != 0);
1481
1482# Check for incorrect file permissions
1483 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1484 my $permhere = $here . "FILE: $realfile\n";
Paolo Bonzini71c47b02015-08-16 23:15:46 +02001485 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001486 ERROR("do not set execute permissions for source files\n" . $permhere);
1487 }
1488 }
1489
Philippe Mathieu-Daudébc7f3b02020-01-30 17:32:21 +01001490# Only allow Python 3 interpreter
1491 if ($realline == 1 &&
1492 $line =~ /^\+#!\ *\/usr\/bin\/(?:env )?python$/) {
1493 ERROR("please use python3 interpreter\n" . $herecurr);
1494 }
1495
Stefan Hajnoczif8dccbb2016-07-15 10:46:54 +01001496# Accept git diff extended headers as valid patches
1497 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1498 $is_patch = 1;
1499 }
1500
Daniel P. Berrangéf5177792018-08-23 11:21:43 +01001501 if ($line =~ /^Author: .*via Qemu-devel.*<qemu-devel\@nongnu.org>/) {
1502 ERROR("Author email address is mangled by the mailing list\n" . $herecurr);
1503 }
1504
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001505#check the patch for a signoff:
1506 if ($line =~ /^\s*signed-off-by:/i) {
1507 # This is a signoff, if ugly, so do not double report.
1508 $signoff++;
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001509 $in_commit_log = 0;
1510
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001511 if (!($line =~ /^\s*Signed-off-by:/)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001512 ERROR("The correct form is \"Signed-off-by\"\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001513 $herecurr);
1514 }
1515 if ($line =~ /^\s*signed-off-by:\S/i) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001516 ERROR("space required after Signed-off-by:\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001517 $herecurr);
1518 }
1519 }
1520
Joe Perches1a6fad02018-04-30 13:46:51 +01001521# Check if MAINTAINERS is being updated. If so, there's probably no need to
1522# emit the "does MAINTAINERS need updating?" message on file add/move/delete
1523 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
1524 $reported_maintainer_file = 1;
1525 }
1526
Joe Perches4be61312018-04-30 13:46:50 +01001527# Check for added, moved or deleted files
1528 if (!$reported_maintainer_file && !$in_commit_log &&
1529 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
1530 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
1531 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
1532 (defined($1) || defined($2))))) {
1533 $reported_maintainer_file = 1;
1534 WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
1535 }
1536
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001537# Check for wrappage within a valid hunk of the file
1538 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1539 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1540 $herecurr) if (!$emitted_corrupt++);
1541 }
1542
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001543# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1544 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1545 $rawline !~ m/^$UTF8*$/) {
1546 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1547
1548 my $blank = copy_spacing($rawline);
1549 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1550 my $hereptr = "$hereline$ptr\n";
1551
1552 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1553 }
1554
Paolo Bonzini874acb62019-05-17 15:19:00 +02001555 if ($rawline =~ m/$UTF8_MOJIBAKE/) {
1556 ERROR("Doubly-encoded UTF-8\n" . $herecurr);
1557 }
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001558# Check if it's the start of a commit log
1559# (not a header line and we haven't seen the patch filename)
1560 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches5fc7e402018-04-30 13:46:49 +01001561 !($rawline =~ /^\s+\S/ ||
1562 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001563 $in_header_lines = 0;
1564 $in_commit_log = 1;
1565 }
1566
Pasi Savanainenbf139a02018-04-30 13:46:48 +01001567# Check if there is UTF-8 in a commit log when a mail header has explicitly
1568# declined it, i.e defined some charset where it is missing.
1569 if ($in_header_lines &&
1570 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1571 $1 !~ /utf-8/i) {
1572 $non_utf8_charset = 1;
1573 }
1574
1575 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perchesb37c4aa2018-04-30 13:46:47 +01001576 $rawline =~ /$NON_ASCII_UTF8/) {
1577 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1578 }
1579
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001580# ignore non-hunk lines and lines being removed
1581 next if (!$hunk_line || $line =~ /^-/);
1582
Radim Krčmář93bf13c2016-08-09 19:38:41 +02001583# ignore files that are being periodically imported from Linux
1584 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1585
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001586#trailing whitespace
1587 if ($line =~ /^\+.*\015/) {
1588 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1589 ERROR("DOS line endings\n" . $herevet);
1590
Lluís Vilanova0cebabd2016-09-07 14:49:04 +02001591 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1592 $realfile =~ /^docs\/.+\.md/) {
1593 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1594 # TODO: properly check we're in a code block
1595 # (surrounding text is 4-column aligned)
1596 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1597 ERROR("code blocks in documentation should have " .
1598 "empty lines with exactly 4 columns of " .
1599 "whitespace\n" . $herevet);
1600 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001601 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1602 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1603 ERROR("trailing whitespace\n" . $herevet);
1604 $rpt_cleaners = 1;
1605 }
1606
Vladimir Sementsov-Ogievskiyc3e58752017-07-31 19:01:34 +03001607# checks for trace-events files
1608 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1609 if ($rawline =~ /%[-+ 0]*#/) {
1610 ERROR("Don't use '#' flag of printf format ('%#') in " .
1611 "trace-events, use '0x' prefix instead\n" . $herecurr);
1612 } else {
1613 my $hex =
1614 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1615
1616 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
Vladimir Sementsov-Ogievskiy45042732017-10-04 18:44:20 +03001617 my $tmpline = $rawline;
1618 $tmpline =~ s/($hex[.:\/ ])+$hex//g;
Vladimir Sementsov-Ogievskiyc3e58752017-07-31 19:01:34 +03001619
1620 if ($tmpline =~ /(?<!0x)$hex/) {
1621 ERROR("Hex numbers must be prefixed with '0x'\n" .
1622 $herecurr);
1623 }
1624 }
1625 }
1626
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001627# check we are in a valid source file if not then ignore this hunk
Paolo Bonzini777d05b2017-10-04 16:35:53 +02001628 next if ($realfile !~ /$SrcFile/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001629
Eric Blake205f31a2018-02-22 15:58:38 -06001630#90 column limit; exempt URLs, if no other words on line
Paolo Bonzinif1e155b2015-08-16 23:01:19 +02001631 if ($line =~ /^\+/ &&
1632 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Eric Blake205f31a2018-02-22 15:58:38 -06001633 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001634 $length > 80)
1635 {
Paolo Bonzini8fbe3d12015-09-07 11:53:02 +02001636 if ($length > 90) {
1637 ERROR("line over 90 characters\n" . $herecurr);
1638 } else {
1639 WARN("line over 80 characters\n" . $herecurr);
1640 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001641 }
1642
1643# check for spaces before a quoted newline
1644 if ($rawline =~ /^.*\".*\s\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001645 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001646 }
1647
1648# check for adding lines without a newline.
1649 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001650 ERROR("adding a line without newline at end of file\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001651 }
1652
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001653# check for RCS/CVS revision markers
1654 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001655 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001656 }
1657
Paolo Bonzini906fb132016-08-09 17:47:42 +02001658# tabs are only allowed in assembly source code, and in
1659# some scripts we imported from other projects.
1660 next if ($realfile =~ /\.(s|S)$/);
1661 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001662
Blue Swirlad36ce82011-02-05 13:18:20 +00001663 if ($rawline =~ /^\+.*\t/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001664 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Blue Swirlb6469682011-01-20 20:58:56 +00001665 ERROR("code indent should never use tabs\n" . $herevet);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001666 $rpt_cleaners = 1;
1667 }
1668
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001669# check we are in a valid C source file if not then ignore this hunk
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001670 next if ($realfile !~ /\.(h|c|cpp)$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001671
Peter Maydell8c06fbd2018-12-14 13:30:48 +00001672# Block comment styles
1673
1674 # Block comments use /* on a line of its own
Alex Bennéeca686682019-10-17 12:58:17 +01001675 if ($rawline !~ m@^\+.*/\*.*\*/[ \t)}]*$@ && #inline /*...*/
Peter Maydellb94e8092019-01-29 11:46:05 +00001676 $rawline =~ m@^\+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non-blank
Peter Maydell8c06fbd2018-12-14 13:30:48 +00001677 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1678 }
1679
1680# Block comments use * on subsequent lines
1681 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1682 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1683 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1684 $rawline =~ /^\+/ && #line is new
1685 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1686 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1687 }
1688
1689# Block comments use */ on trailing lines
1690 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1691 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1692 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1693 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1694 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1695 }
1696
1697# Block comment * alignment
1698 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1699 $line =~ /^\+[ \t]*$;/ && #leading comment
1700 $rawline =~ /^\+[ \t]*\*/ && #leading *
1701 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1702 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1703 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1704 my $oldindent;
1705 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1706 if (defined($1)) {
1707 $oldindent = expand_tabs($1);
1708 } else {
1709 $prevrawline =~ m@^\+(.*/?)\*@;
1710 $oldindent = expand_tabs($1);
1711 }
1712 $rawline =~ m@^\+([ \t]*)\*@;
1713 my $newindent = $1;
1714 $newindent = expand_tabs($newindent);
1715 if (length($oldindent) ne length($newindent)) {
1716 WARN("Block comments should align the * on each line\n" . $hereprev);
1717 }
1718 }
1719
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001720# Check for potential 'bare' types
1721 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1722 $realline_next);
1723 if ($realcnt && $line =~ /.\s*\S/) {
1724 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1725 ctx_statement_block($linenr, $realcnt, 0);
1726 $stat =~ s/\n./\n /g;
1727 $cond =~ s/\n./\n /g;
1728
1729 # Find the real next line.
1730 $realline_next = $line_nr_next;
1731 if (defined $realline_next &&
1732 (!defined $lines[$realline_next - 1] ||
1733 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1734 $realline_next++;
1735 }
1736
1737 my $s = $stat;
1738 $s =~ s/{.*$//s;
1739
1740 # Ignore goto labels.
1741 if ($s =~ /$Ident:\*$/s) {
1742
1743 # Ignore functions being called
1744 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1745
1746 } elsif ($s =~ /^.\s*else\b/s) {
1747
1748 # declarations always start with types
1749 } 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) {
1750 my $type = $1;
1751 $type =~ s/\s+/ /g;
1752 possible($type, "A:" . $s);
1753
1754 # definitions in global scope can only start with types
1755 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1756 possible($1, "B:" . $s);
1757 }
1758
1759 # any (foo ... *) is a pointer cast, and foo is a type
1760 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1761 possible($1, "C:" . $s);
1762 }
1763
1764 # Check for any sort of function declaration.
1765 # int foo(something bar, other baz);
1766 # void (*store_gdt)(x86_descr_ptr *);
1767 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1768 my ($name_len) = length($1);
1769
1770 my $ctx = $s;
1771 substr($ctx, 0, $name_len + 1, '');
1772 $ctx =~ s/\)[^\)]*$//;
1773
1774 for my $arg (split(/\s*,\s*/, $ctx)) {
1775 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1776
1777 possible($1, "D:" . $s);
1778 }
1779 }
1780 }
1781
1782 }
1783
1784#
1785# Checks which may be anchored in the context.
1786#
1787
1788# Check for switch () and associated case and default
1789# statements should be at the same indent.
1790 if ($line=~/\bswitch\s*\(.*\)/) {
1791 my $err = '';
1792 my $sep = '';
1793 my @ctx = ctx_block_outer($linenr, $realcnt);
1794 shift(@ctx);
1795 for my $ctx (@ctx) {
1796 my ($clen, $cindent) = line_stats($ctx);
1797 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1798 $indent != $cindent) {
1799 $err .= "$sep$ctx\n";
1800 $sep = '';
1801 } else {
1802 $sep = "[...]\n";
1803 }
1804 }
1805 if ($err ne '') {
1806 ERROR("switch and case should be at the same indent\n$hereline$err");
1807 }
1808 }
1809
1810# if/while/etc brace do not go on next line, unless defining a do while loop,
1811# or if that brace on the next line is for something else
1812 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1813 my $pre_ctx = "$1$2";
1814
1815 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1816 my $ctx_cnt = $realcnt - $#ctx - 1;
1817 my $ctx = join("\n", @ctx);
1818
1819 my $ctx_ln = $linenr;
1820 my $ctx_skip = $realcnt;
1821
1822 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1823 defined $lines[$ctx_ln - 1] &&
1824 $lines[$ctx_ln - 1] =~ /^-/)) {
1825 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1826 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1827 $ctx_ln++;
1828 }
1829
1830 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1831 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1832
Max Reitza97ceca2014-11-26 17:20:24 +01001833 # The length of the "previous line" is checked against 80 because it
1834 # includes the + at the beginning of the line (if the actual line has
1835 # 79 or 80 characters, it is no longer possible to add a space and an
1836 # opening brace there)
1837 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08001838 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
Max Reitza97ceca2014-11-26 17:20:24 +01001839 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001840 ERROR("that open brace { should be on the previous line\n" .
1841 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1842 }
1843 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1844 $ctx =~ /\)\s*\;\s*$/ &&
1845 defined $lines[$ctx_ln - 1])
1846 {
1847 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1848 if ($nindent > $indent) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001849 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001850 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1851 }
1852 }
1853 }
1854
Eric Blakef4bdc132017-12-01 17:24:33 -06001855# 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1856 if ($line =~ /while\s*\((0|false)\);/) {
1857 ERROR("suspicious ; after while (0)\n" . $herecurr);
1858 }
1859
Philippe Mathieu-Daudéee0f3c02020-02-18 10:43:50 +01001860# Check superfluous trailing ';'
1861 if ($line =~ /;;$/) {
1862 ERROR("superfluous trailing semicolon\n" . $herecurr);
1863 }
1864
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001865# Check relative indent for conditionals and blocks.
1866 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1867 my ($s, $c) = ($stat, $cond);
1868
1869 substr($s, 0, length($c), '');
1870
1871 # Make sure we remove the line prefixes as we have
1872 # none on the first line, and are going to readd them
1873 # where necessary.
1874 $s =~ s/\n./\n/gs;
1875
1876 # Find out how long the conditional actually is.
1877 my @newlines = ($c =~ /\n/gs);
1878 my $cond_lines = 1 + $#newlines;
1879
1880 # We want to check the first line inside the block
1881 # starting at the end of the conditional, so remove:
1882 # 1) any blank line termination
1883 # 2) any opening brace { on end of the line
1884 # 3) any do (...) {
1885 my $continuation = 0;
1886 my $check = 0;
1887 $s =~ s/^.*\bdo\b//;
Fam Zheng04f25622015-09-11 19:07:36 +08001888 $s =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001889 if ($s =~ s/^\s*\\//) {
1890 $continuation = 1;
1891 }
1892 if ($s =~ s/^\s*?\n//) {
1893 $check = 1;
1894 $cond_lines++;
1895 }
1896
1897 # Also ignore a loop construct at the end of a
1898 # preprocessor statement.
1899 if (($prevline =~ /^.\s*#\s*define\s/ ||
1900 $prevline =~ /\\\s*$/) && $continuation == 0) {
1901 $check = 0;
1902 }
1903
1904 my $cond_ptr = -1;
1905 $continuation = 0;
1906 while ($cond_ptr != $cond_lines) {
1907 $cond_ptr = $cond_lines;
1908
1909 # If we see an #else/#elif then the code
1910 # is not linear.
1911 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1912 $check = 0;
1913 }
1914
1915 # Ignore:
1916 # 1) blank lines, they should be at 0,
1917 # 2) preprocessor lines, and
1918 # 3) labels.
1919 if ($continuation ||
1920 $s =~ /^\s*?\n/ ||
1921 $s =~ /^\s*#\s*?/ ||
1922 $s =~ /^\s*$Ident\s*:/) {
1923 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1924 if ($s =~ s/^.*?\n//) {
1925 $cond_lines++;
1926 }
1927 }
1928 }
1929
1930 my (undef, $sindent) = line_stats("+" . $s);
1931 my $stat_real = raw_line($linenr, $cond_lines);
1932
1933 # Check if either of these lines are modified, else
1934 # this is not this patch's fault.
1935 if (!defined($stat_real) ||
1936 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1937 $check = 0;
1938 }
1939 if (defined($stat_real) && $cond_lines > 1) {
1940 $stat_real = "[...]\n$stat_real";
1941 }
1942
1943 #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";
1944
Blue Swirlb6469682011-01-20 20:58:56 +00001945 if ($check && (($sindent % 4) != 0 ||
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001946 ($sindent <= $indent && $s ne ''))) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001947 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001948 }
1949 }
1950
1951 # Track the 'values' across context and added lines.
1952 my $opline = $line; $opline =~ s/^./ /;
1953 my ($curr_values, $curr_vars) =
1954 annotate_values($opline . "\n", $prev_values);
1955 $curr_values = $prev_values . $curr_values;
1956 if ($dbg_values) {
1957 my $outline = $opline; $outline =~ s/\t/ /g;
1958 print "$linenr > .$outline\n";
1959 print "$linenr > $curr_values\n";
1960 print "$linenr > $curr_vars\n";
1961 }
1962 $prev_values = substr($curr_values, -1);
1963
1964#ignore lines not being added
1965 if ($line=~/^[^\+]/) {next;}
1966
1967# TEST: allow direct testing of the type matcher.
1968 if ($dbg_type) {
1969 if ($line =~ /^.\s*$Declare\s*$/) {
1970 ERROR("TEST: is type\n" . $herecurr);
1971 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1972 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1973 }
1974 next;
1975 }
1976# TEST: allow direct testing of the attribute matcher.
1977 if ($dbg_attr) {
1978 if ($line =~ /^.\s*$Modifier\s*$/) {
1979 ERROR("TEST: is attr\n" . $herecurr);
1980 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1981 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1982 }
1983 next;
1984 }
1985
1986# check for initialisation to aggregates open brace on the next line
Fam Zheng04f25622015-09-11 19:07:36 +08001987 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001988 $prevline =~ /(?:^|[^=])=\s*$/) {
1989 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1990 }
1991
1992#
1993# Checks which are anchored on the added line.
1994#
1995
1996# check for malformed paths in #include statements (uses RAW line)
1997 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1998 my $path = $1;
1999 if ($path =~ m{//}) {
2000 ERROR("malformed #include filename\n" .
2001 $herecurr);
2002 }
2003 }
2004
2005# no C99 // comments
Peter Xu8d061272019-04-26 14:27:05 +08002006 if ($line =~ m{//} &&
2007 $rawline !~ m{// SPDX-License-Identifier: }) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002008 ERROR("do not use C99 // comments\n" . $herecurr);
2009 }
2010 # Remove C99 comments.
2011 $line =~ s@//.*@@;
2012 $opline =~ s@//.*@@;
2013
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002014# check for global initialisers.
2015 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2016 ERROR("do not initialise globals to 0 or NULL\n" .
2017 $herecurr);
2018 }
2019# check for static initialisers.
2020 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2021 ERROR("do not initialise statics to 0 or NULL\n" .
2022 $herecurr);
2023 }
2024
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002025# * goes on variable not on type
2026 # (char*[ const])
2027 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
2028 my ($from, $to) = ($1, $1);
2029
2030 # Should start with a space.
2031 $to =~ s/^(\S)/ $1/;
2032 # Should not end with a space.
2033 $to =~ s/\s+$//;
2034 # '*'s should not have spaces between.
2035 while ($to =~ s/\*\s+\*/\*\*/) {
2036 }
2037
2038 #print "from<$from> to<$to>\n";
2039 if ($from ne $to) {
2040 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2041 }
2042 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
2043 my ($from, $to, $ident) = ($1, $1, $2);
2044
2045 # Should start with a space.
2046 $to =~ s/^(\S)/ $1/;
2047 # Should not end with a space.
2048 $to =~ s/\s+$//;
2049 # '*'s should not have spaces between.
2050 while ($to =~ s/\*\s+\*/\*\*/) {
2051 }
2052 # Modifiers should have spaces.
2053 $to =~ s/(\b$Modifier$)/$1 /;
2054
2055 #print "from<$from> to<$to> ident<$ident>\n";
2056 if ($from ne $to && $ident !~ /^$Modifier$/) {
2057 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2058 }
2059 }
2060
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002061# function brace can't be on same line, except for #defines of do while,
2062# or if closed on same line
Fam Zheng04f25622015-09-11 19:07:36 +08002063 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
2064 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002065 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
2066 }
2067
2068# open braces for enum, union and struct go on the same line.
Fam Zheng04f25622015-09-11 19:07:36 +08002069 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002070 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2071 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2072 }
2073
2074# missing space after union, struct or enum definition
2075 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002076 ERROR("missing space after $1 definition\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002077 }
2078
2079# check for spacing round square brackets; allowed:
2080# 1. with a type on the left -- int [] a;
2081# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2082# 3. inside a curly brace -- = { [0...10] = 5 }
Leonid Bloch409db6e2015-10-29 11:48:37 +02002083# 4. after a comma -- [1] = 5, [2] = 6
Leonid Bloch8800cf02015-10-29 11:48:38 +02002084# 5. in a macro definition -- #define abc(x) [x] = y
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002085 while ($line =~ /(.*?\s)\[/g) {
2086 my ($where, $prefix) = ($-[1], $1);
2087 if ($prefix !~ /$Type\s+$/ &&
2088 ($where != 0 || $prefix !~ /^.\s+$/) &&
Leonid Bloch8800cf02015-10-29 11:48:38 +02002089 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
Heinrich Schuchardt66e9d202018-04-10 16:34:14 -07002090 $prefix !~ /[,{:]\s+$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002091 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2092 }
2093 }
2094
2095# check for spaces between functions and their parentheses.
2096 while ($line =~ /($Ident)\s+\(/g) {
2097 my $name = $1;
2098 my $ctx_before = substr($line, 0, $-[1]);
2099 my $ctx = "$ctx_before$name";
2100
2101 # Ignore those directives where spaces _are_ permitted.
2102 if ($name =~ /^(?:
2103 if|for|while|switch|return|case|
Jeff Cody000980c2016-11-01 10:38:35 -04002104 volatile|__volatile__|coroutine_fn|
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002105 __attribute__|format|__extension__|
2106 asm|__asm__)$/x)
2107 {
2108
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002109 # Ignore 'catch (...)' in C++
2110 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
2111
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002112 # cpp #define statements have non-optional spaces, ie
2113 # if there is a space between the name and the open
2114 # parenthesis it is simply not a parameter group.
2115 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2116
2117 # cpp #elif statement condition may start with a (
2118 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2119
2120 # If this whole things ends with a type its most
2121 # likely a typedef for a function.
2122 } elsif ($ctx =~ /$Type$/) {
2123
2124 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002125 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002126 }
2127 }
2128# Check operator spacing.
2129 if (!($line=~/\#\s*include/)) {
2130 my $ops = qr{
2131 <<=|>>=|<=|>=|==|!=|
2132 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2133 =>|->|<<|>>|<|>|=|!|~|
2134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002135 \?|::|:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002136 }x;
2137 my @elements = split(/($ops|;)/, $opline);
2138 my $off = 0;
2139
2140 my $blank = copy_spacing($opline);
2141
2142 for (my $n = 0; $n < $#elements; $n += 2) {
2143 $off += length($elements[$n]);
2144
Stefan Weile7d81002011-12-10 00:19:46 +01002145 # Pick up the preceding and succeeding characters.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002146 my $ca = substr($opline, 0, $off);
2147 my $cc = '';
2148 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2149 $cc = substr($opline, $off + length($elements[$n + 1]));
2150 }
2151 my $cb = "$ca$;$cc";
2152
2153 my $a = '';
2154 $a = 'V' if ($elements[$n] ne '');
2155 $a = 'W' if ($elements[$n] =~ /\s$/);
2156 $a = 'C' if ($elements[$n] =~ /$;$/);
2157 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2158 $a = 'O' if ($elements[$n] eq '');
2159 $a = 'E' if ($ca =~ /^\s*$/);
2160
2161 my $op = $elements[$n + 1];
2162
2163 my $c = '';
2164 if (defined $elements[$n + 2]) {
2165 $c = 'V' if ($elements[$n + 2] ne '');
2166 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2167 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2168 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2169 $c = 'O' if ($elements[$n + 2] eq '');
2170 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2171 } else {
2172 $c = 'E';
2173 }
2174
2175 my $ctx = "${a}x${c}";
2176
2177 my $at = "(ctx:$ctx)";
2178
2179 my $ptr = substr($blank, 0, $off) . "^";
2180 my $hereptr = "$hereline$ptr\n";
2181
2182 # Pull out the value of this operator.
2183 my $op_type = substr($curr_values, $off + 1, 1);
2184
2185 # Get the full operator variant.
2186 my $opv = $op . substr($curr_vars, $off, 1);
2187
2188 # Ignore operators passed as parameters.
2189 if ($op_type ne 'V' &&
2190 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2191
2192# # Ignore comments
2193# } elsif ($op =~ /^$;+$/) {
2194
2195 # ; should have either the end of line or a space or \ after it
2196 } elsif ($op eq ';') {
2197 if ($ctx !~ /.x[WEBC]/ &&
2198 $cc !~ /^\\/ && $cc !~ /^;/) {
2199 ERROR("space required after that '$op' $at\n" . $hereptr);
2200 }
2201
2202 # // is a comment
2203 } elsif ($op eq '//') {
2204
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002205 # Ignore : used in class declaration in C++
2206 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2207 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2208
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002209 # No spaces for:
2210 # ->
2211 # : when part of a bitfield
2212 } elsif ($op eq '->' || $opv eq ':B') {
2213 if ($ctx =~ /Wx.|.xW/) {
2214 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2215 }
2216
2217 # , must have a space on the right.
Alexander Graf9fbe4782011-06-29 08:04:27 +02002218 # not required when having a single },{ on one line
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002219 } elsif ($op eq ',') {
Alexander Graf9fbe4782011-06-29 08:04:27 +02002220 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08002221 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002222 ERROR("space required after that '$op' $at\n" . $hereptr);
2223 }
2224
2225 # '*' as part of a type definition -- reported already.
2226 } elsif ($opv eq '*_') {
2227 #warn "'*' is part of type\n";
2228
2229 # unary operators should have a space before and
2230 # none after. May be left adjacent to another
2231 # unary operator, or a cast
2232 } elsif ($op eq '!' || $op eq '~' ||
2233 $opv eq '*U' || $opv eq '-U' ||
2234 $opv eq '&U' || $opv eq '&&U') {
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002235 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2236 # '~' used as a name of Destructor
2237
2238 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002239 ERROR("space required before that '$op' $at\n" . $hereptr);
2240 }
2241 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2242 # A unary '*' may be const
2243
2244 } elsif ($ctx =~ /.xW/) {
2245 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2246 }
2247
2248 # unary ++ and unary -- are allowed no space on one side.
2249 } elsif ($op eq '++' or $op eq '--') {
2250 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2251 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2252 }
2253 if ($ctx =~ /Wx[BE]/ ||
2254 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2255 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2256 }
2257 if ($ctx =~ /ExW/) {
2258 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2259 }
2260
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002261 # A colon needs no spaces before when it is
2262 # terminating a case value or a label.
2263 } elsif ($opv eq ':C' || $opv eq ':L') {
2264 if ($ctx =~ /Wx./) {
2265 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2266 }
2267
2268 # All the others need spaces both sides.
2269 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2270 my $ok = 0;
2271
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002272 if ($realfile =~ /\.cpp|\.h$/) {
2273 # Ignore template arguments <...> in C++
2274 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2275 $ok = 1;
2276 }
2277
2278 # Ignore :: in C++
2279 if ($op eq '::') {
2280 $ok = 1;
2281 }
2282 }
2283
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002284 # Ignore email addresses <foo@bar>
2285 if (($op eq '<' &&
2286 $cc =~ /^\S+\@\S+>/) ||
2287 ($op eq '>' &&
2288 $ca =~ /<\S+\@\S+$/))
2289 {
2290 $ok = 1;
2291 }
2292
2293 # Ignore ?:
2294 if (($opv eq ':O' && $ca =~ /\?$/) ||
2295 ($op eq '?' && $cc =~ /^:/)) {
2296 $ok = 1;
2297 }
2298
2299 if ($ok == 0) {
2300 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2301 }
2302 }
2303 $off += length($elements[$n + 1]);
2304 }
2305 }
2306
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002307#need space before brace following if, while, etc
Fam Zheng04f25622015-09-11 19:07:36 +08002308 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2309 $line =~ /do\{/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002310 ERROR("space required before the open brace '{'\n" . $herecurr);
2311 }
2312
2313# closing brace should have a space following it when it has anything
2314# on the line
2315 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2316 ERROR("space required after that close brace '}'\n" . $herecurr);
2317 }
2318
2319# check spacing on square brackets
2320 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2321 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2322 }
2323 if ($line =~ /\s\]/) {
2324 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2325 }
2326
2327# check spacing on parentheses
2328 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2329 $line !~ /for\s*\(\s+;/) {
2330 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2331 }
2332 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2333 $line !~ /for\s*\(.*;\s+\)/ &&
2334 $line !~ /:\s+\)/) {
2335 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2336 }
2337
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002338# Return is not a function.
2339 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2340 my $spacing = $1;
2341 my $value = $2;
2342
2343 # Flatten any parentheses
2344 $value =~ s/\(/ \(/g;
2345 $value =~ s/\)/\) /g;
2346 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2347 $value !~ /(?:$Ident|-?$Constant)\s*
2348 $Compare\s*
2349 (?:$Ident|-?$Constant)/x &&
2350 $value =~ s/\([^\(\)]*\)/1/) {
2351 }
2352#print "value<$value>\n";
Paolo Bonzinic20b1392019-06-21 13:28:54 +02002353 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ &&
2354 $line =~ /;$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002355 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2356
2357 } elsif ($spacing !~ /\s+/) {
2358 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2359 }
2360 }
2361# Return of what appears to be an errno should normally be -'ve
2362 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2363 my $name = $1;
2364 if ($name ne 'EOF' && $name ne 'ERROR') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002365 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002366 }
2367 }
2368
Paolo Bonzini50db69a2018-12-06 12:01:40 +01002369 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ &&
2370 $line !~ /^.typedef/) {
2371 ERROR("named $1 should be typedefed separately\n" . $herecurr);
2372 }
2373
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002374# Need a space before open parenthesis after if, while etc
2375 if ($line=~/\b(if|while|for|switch)\(/) {
2376 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2377 }
2378
2379# Check for illegal assignment in if conditional -- and check for trailing
2380# statements after the conditional.
2381 if ($line =~ /do\s*(?!{)/) {
2382 my ($stat_next) = ctx_statement_block($line_nr_next,
2383 $remain_next, $off_next);
2384 $stat_next =~ s/\n./\n /g;
2385 ##print "stat<$stat> stat_next<$stat_next>\n";
2386
2387 if ($stat_next =~ /^\s*while\b/) {
2388 # If the statement carries leading newlines,
2389 # then count those as offsets.
2390 my ($whitespace) =
2391 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2392 my $offset =
2393 statement_rawlines($whitespace) - 1;
2394
2395 $suppress_whiletrailers{$line_nr_next +
2396 $offset} = 1;
2397 }
2398 }
2399 if (!defined $suppress_whiletrailers{$linenr} &&
2400 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2401 my ($s, $c) = ($stat, $cond);
2402
2403 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2404 ERROR("do not use assignment in if condition\n" . $herecurr);
2405 }
2406
2407 # Find out what is on the end of the line after the
2408 # conditional.
2409 substr($s, 0, length($c), '');
2410 $s =~ s/\n.*//g;
2411 $s =~ s/$;//g; # Remove any comments
2412 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2413 $c !~ /}\s*while\s*/)
2414 {
2415 # Find out how long the conditional actually is.
2416 my @newlines = ($c =~ /\n/gs);
2417 my $cond_lines = 1 + $#newlines;
2418 my $stat_real = '';
2419
2420 $stat_real = raw_line($linenr, $cond_lines)
2421 . "\n" if ($cond_lines);
2422 if (defined($stat_real) && $cond_lines > 1) {
2423 $stat_real = "[...]\n$stat_real";
2424 }
2425
2426 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2427 }
2428 }
2429
2430# Check for bitwise tests written as boolean
2431 if ($line =~ /
2432 (?:
2433 (?:\[|\(|\&\&|\|\|)
2434 \s*0[xX][0-9]+\s*
2435 (?:\&\&|\|\|)
2436 |
2437 (?:\&\&|\|\|)
2438 \s*0[xX][0-9]+\s*
2439 (?:\&\&|\|\||\)|\])
2440 )/x)
2441 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002442 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002443 }
2444
2445# if and else should not have general statements after it
2446 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2447 my $s = $1;
2448 $s =~ s/$;//g; # Remove any comments
2449 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2450 ERROR("trailing statements should be on next line\n" . $herecurr);
2451 }
2452 }
2453# if should not continue a brace
2454 if ($line =~ /}\s*if\b/) {
2455 ERROR("trailing statements should be on next line\n" .
2456 $herecurr);
2457 }
2458# case and default should not have general statements after them
2459 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2460 $line !~ /\G(?:
2461 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2462 \s*return\s+
2463 )/xg)
2464 {
2465 ERROR("trailing statements should be on next line\n" . $herecurr);
2466 }
2467
2468 # Check for }<nl>else {, these must be at the same
2469 # indent level to be relevant to each other.
2470 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2471 $previndent == $indent) {
2472 ERROR("else should follow close brace '}'\n" . $hereprev);
2473 }
2474
2475 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2476 $previndent == $indent) {
2477 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2478
2479 # Find out what is on the end of the line after the
2480 # conditional.
2481 substr($s, 0, length($c), '');
2482 $s =~ s/\n.*//g;
2483
2484 if ($s =~ /^\s*;/) {
2485 ERROR("while should follow close brace '}'\n" . $hereprev);
2486 }
2487 }
2488
2489#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2490# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2491# print "No studly caps, use _\n";
2492# print "$herecurr";
2493# $clean = 0;
2494# }
2495
2496#no spaces allowed after \ in define
2497 if ($line=~/\#\s*define.*\\\s$/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002498 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002499 }
2500
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002501# multi-statement macros should be enclosed in a do while loop, grab the
2502# first statement and ensure its the whole macro if its not enclosed
2503# in a known good container
2504 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2505 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2506 my $ln = $linenr;
2507 my $cnt = $realcnt;
2508 my ($off, $dstat, $dcond, $rest);
2509 my $ctx = '';
2510
2511 my $args = defined($1);
2512
2513 # Find the end of the macro and limit our statement
2514 # search to that.
2515 while ($cnt > 0 && defined $lines[$ln - 1] &&
2516 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2517 {
2518 $ctx .= $rawlines[$ln - 1] . "\n";
2519 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2520 $ln++;
2521 }
2522 $ctx .= $rawlines[$ln - 1];
2523
2524 ($dstat, $dcond, $ln, $cnt, $off) =
2525 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2526 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2527 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2528
2529 # Extract the remainder of the define (if any) and
2530 # rip off surrounding spaces, and trailing \'s.
2531 $rest = '';
2532 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2533 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2534 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2535 $rest .= substr($lines[$ln - 1], $off) . "\n";
2536 $cnt--;
2537 }
2538 $ln++;
2539 $off = 0;
2540 }
2541 $rest =~ s/\\\n.//g;
2542 $rest =~ s/^\s*//s;
2543 $rest =~ s/\s*$//s;
2544
2545 # Clean up the original statement.
2546 if ($args) {
2547 substr($dstat, 0, length($dcond), '');
2548 } else {
2549 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2550 }
2551 $dstat =~ s/$;//g;
2552 $dstat =~ s/\\\n.//g;
2553 $dstat =~ s/^\s*//s;
2554 $dstat =~ s/\s*$//s;
2555
2556 # Flatten any parentheses and braces
2557 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2558 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2559 $dstat =~ s/\[[^\{\}]*\]/1/)
2560 {
2561 }
2562
2563 my $exceptions = qr{
2564 $Declare|
2565 module_param_named|
2566 MODULE_PARAM_DESC|
2567 DECLARE_PER_CPU|
2568 DEFINE_PER_CPU|
2569 __typeof__\(|
2570 union|
2571 struct|
2572 \.$Ident\s*=\s*|
2573 ^\"|\"$
2574 }x;
2575 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2576 if ($rest ne '' && $rest ne ',') {
2577 if ($rest !~ /while\s*\(/ &&
2578 $dstat !~ /$exceptions/)
2579 {
2580 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2581 }
2582
2583 } elsif ($ctx !~ /;/) {
2584 if ($dstat ne '' &&
2585 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2586 $dstat !~ /$exceptions/ &&
2587 $dstat !~ /^\.$Ident\s*=/ &&
2588 $dstat =~ /$Operators/)
2589 {
2590 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2591 }
2592 }
2593 }
2594
Su Hang2b9aef62018-03-06 15:04:50 +08002595# check for missing bracing around if etc
2596 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2597 $line !~ /\#\s*if/) {
Su Hang053e45d2018-03-26 10:06:22 +08002598 my $allowed = 0;
2599
2600 # Check the pre-context.
2601 if ($line =~ /(\}.*?)$/) {
2602 my $pre = $1;
2603
2604 if ($line !~ /else/) {
2605 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2606 if $dbg_adv_apw;
2607 $allowed = 1;
2608 }
2609 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002610 my ($level, $endln, @chunks) =
2611 ctx_statement_full($linenr, $realcnt, 1);
Don Slutz69402a62012-09-02 19:22:37 -04002612 if ($dbg_adv_apw) {
2613 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2614 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2615 if $#chunks >= 1;
2616 }
Blue Swirlb6469682011-01-20 20:58:56 +00002617 if ($#chunks >= 0 && $level == 0) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002618 my $seen = 0;
2619 my $herectx = $here . "\n";
2620 my $ln = $linenr - 1;
2621 for my $chunk (@chunks) {
2622 my ($cond, $block) = @{$chunk};
2623
2624 # If the condition carries leading newlines, then count those as offsets.
2625 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2626 my $offset = statement_rawlines($whitespace) - 1;
2627
2628 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2629
2630 # We have looked at and allowed this specific line.
2631 $suppress_ifbraces{$ln + $offset} = 1;
2632
2633 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2634 $ln += statement_rawlines($block) - 1;
2635
2636 substr($block, 0, length($cond), '');
2637
Max Reitza97ceca2014-11-26 17:20:24 +01002638 my $spaced_block = $block;
2639 $spaced_block =~ s/\n\+/ /g;
2640
Fam Zheng04f25622015-09-11 19:07:36 +08002641 $seen++ if ($spaced_block =~ /^\s*\{/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002642
Don Slutz69402a62012-09-02 19:22:37 -04002643 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2644 if $dbg_adv_apw;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002645 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002646 print "APW: ALLOWED: cond<$cond>\n"
2647 if $dbg_adv_apw;
2648 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002649 }
2650 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002651 print "APW: ALLOWED: block<$block>\n"
2652 if $dbg_adv_apw;
2653 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002654 }
2655 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002656 print "APW: ALLOWED: lines block<$block>\n"
2657 if $dbg_adv_apw;
2658 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002659 }
2660 }
Su Hang053e45d2018-03-26 10:06:22 +08002661 if ($seen != ($#chunks + 1) && !$allowed) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002662 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002663 }
2664 }
2665 }
2666 if (!defined $suppress_ifbraces{$linenr - 1} &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002667 $line =~ /\b(if|while|for|else)\b/ &&
Blue Swirld0510af2011-07-15 20:09:10 +00002668 $line !~ /\#\s*if/ &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002669 $line !~ /\#\s*else/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002670 my $allowed = 0;
2671
Don Slutzdfe70532012-09-02 19:22:38 -04002672 # Check the pre-context.
2673 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2674 my $pre = $1;
2675
2676 if ($line !~ /else/) {
2677 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2678 if $dbg_adv_apw;
2679 $allowed = 1;
2680 }
2681 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002682
2683 my ($level, $endln, @chunks) =
2684 ctx_statement_full($linenr, $realcnt, $-[0]);
2685
2686 # Check the condition.
2687 my ($cond, $block) = @{$chunks[0]};
Don Slutz54243022012-09-02 19:22:36 -04002688 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2689 if $dbg_adv_checking;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002690 if (defined $cond) {
2691 substr($block, 0, length($cond), '');
2692 }
2693 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002694 print "APW: ALLOWED: cond<$cond>\n"
2695 if $dbg_adv_apw;
2696 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002697 }
2698 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002699 print "APW: ALLOWED: block<$block>\n"
2700 if $dbg_adv_apw;
2701 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002702 }
2703 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002704 print "APW: ALLOWED: lines block<$block>\n"
2705 if $dbg_adv_apw;
2706 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002707 }
2708 # Check the post-context.
2709 if (defined $chunks[1]) {
2710 my ($cond, $block) = @{$chunks[1]};
2711 if (defined $cond) {
2712 substr($block, 0, length($cond), '');
2713 }
2714 if ($block =~ /^\s*\{/) {
Don Slutz69402a62012-09-02 19:22:37 -04002715 print "APW: ALLOWED: chunk-1 block<$block>\n"
2716 if $dbg_adv_apw;
2717 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002718 }
2719 }
Don Slutza99ac042012-09-02 19:22:35 -04002720 print "DCS: level=$level block<$block> allowed=$allowed\n"
2721 if $dbg_adv_dcs;
Blue Swirlb6469682011-01-20 20:58:56 +00002722 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002723 my $herectx = $here . "\n";;
2724 my $cnt = statement_rawlines($block);
2725
2726 for (my $n = 0; $n < $cnt; $n++) {
2727 $herectx .= raw_line($linenr, $n) . "\n";;
2728 }
2729
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002730 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002731 }
2732 }
2733
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002734# no volatiles please
2735 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
Marc-André Lureau6b012d22017-12-15 19:18:10 +01002736 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2737 $line !~ /sig_atomic_t/ &&
2738 !ctx_has_comment($first_line, $linenr)) {
2739 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2740 ERROR($msg);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002741 }
2742
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002743# warn about #if 0
2744 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002745 ERROR("if this code is redundant consider removing it\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002746 $herecurr);
2747 }
2748
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002749# check for needless g_free() checks
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002750 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2751 my $expr = $1;
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002752 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002753 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002754 }
2755 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002756
2757# warn about #ifdefs in C files
2758# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2759# print "#ifdef in C files should be avoided\n";
2760# print "$herecurr";
2761# $clean = 0;
2762# }
2763
2764# warn about spacing in #ifdefs
2765 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2766 ERROR("exactly one space required after that #$1\n" . $herecurr);
2767 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002768# check for memory barriers without a comment.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002769 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002770 if (!ctx_has_comment($first_line, $linenr)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002771 ERROR("memory barrier without comment\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002772 }
2773 }
2774# check of hardware specific defines
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002775# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2776# where they might be necessary.
2777 if ($line =~ m@^.\s*\#\s*if.*\b__@) {
Paolo Bonzini63ae8b92016-09-21 18:49:07 +02002778 WARN("architecture specific defines should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002779 }
2780
2781# Check that the storage class is at the beginning of a declaration
2782 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002783 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002784 }
2785
2786# check the location of the inline attribute, that it is between
2787# storage class and type.
2788 if ($line =~ /\b$Type\s+$Inline\b/ ||
2789 $line =~ /\b$Inline\s+$Storage\b/) {
2790 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2791 }
2792
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002793# check for sizeof(&)
2794 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002795 ERROR("sizeof(& should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002796 }
2797
2798# check for new externs in .c files.
2799 if ($realfile =~ /\.c$/ && defined $stat &&
2800 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2801 {
2802 my $function_name = $1;
2803 my $paren_space = $2;
2804
2805 my $s = $stat;
2806 if (defined $cond) {
2807 substr($s, 0, length($cond), '');
2808 }
2809 if ($s =~ /^\s*;/ &&
2810 $function_name ne 'uninitialized_var')
2811 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002812 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002813 }
2814
2815 if ($paren_space =~ /\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002816 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002817 }
2818
2819 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2820 $stat =~ /^.\s*extern\s+/)
2821 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002822 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002823 }
2824
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002825# check for pointless casting of g_malloc return
2826 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2827 if ($2 == 'm') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002828 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002829 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002830 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002831 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002832 }
2833
2834# check for gcc specific __FUNCTION__
2835 if ($line =~ /__FUNCTION__/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002836 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002837 }
2838
Julia Suvorovafb8446d2018-03-02 13:43:19 +03002839# recommend g_path_get_* over g_strdup(basename/dirname(...))
2840 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2841 WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2842 }
2843
Paolo Bonzini5e43efb2015-09-16 18:35:09 +02002844# recommend qemu_strto* over strto* for numeric conversions
Eric Blake01fb8e12016-06-09 20:48:07 -06002845 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002846 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002847 }
Paolo Bonzinie8c20912017-07-04 11:26:37 +02002848# recommend sigaction over signal for portability, when establishing a handler
2849 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2850 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2851 }
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002852# check for module_init(), use category-specific init macros explicitly please
2853 if ($line =~ /^module_init\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002854 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002855 }
2856# check for various ops structs, ensure they are const.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002857 my $struct_ops = qr{AIOCBInfo|
2858 BdrvActionOps|
2859 BlockDevOps|
2860 BlockJobDriver|
2861 DisplayChangeListenerOps|
2862 GraphicHwOps|
2863 IDEDMAOps|
2864 KVMCapabilityInfo|
2865 MemoryRegionIOMMUOps|
2866 MemoryRegionOps|
2867 MemoryRegionPortio|
2868 QEMUFileOps|
2869 SCSIBusInfo|
2870 SCSIReqOps|
2871 Spice[A-Z][a-zA-Z0-9]*Interface|
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002872 USBDesc[A-Z][a-zA-Z0-9]*|
2873 VhostOps|
2874 VMStateDescription|
2875 VMStateInfo}x;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002876 if ($line !~ /\bconst\b/ &&
Paolo Bonzinie20e7182016-10-14 10:45:45 +02002877 $line =~ /\b($struct_ops)\b.*=/) {
2878 ERROR("initializer for struct $1 should normally be const\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002879 $herecurr);
2880 }
2881
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002882# check for %L{u,d,i} in strings
2883 my $string;
2884 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2885 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2886 $string =~ s/%%/__/g;
2887 if ($string =~ /(?<!%)%L[udi]/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002888 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002889 last;
2890 }
2891 }
2892
Stefan Weil9964d8f2012-06-17 06:57:41 +02002893# QEMU specific tests
2894 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002895 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
Stefan Weil9964d8f2012-06-17 06:57:41 +02002896 }
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002897
Jason J. Herne5d596c22015-12-11 13:30:42 -05002898# Qemu error function tests
2899
2900 # Find newlines in error messages
2901 my $qemu_error_funcs = qr{error_setg|
2902 error_setg_errno|
2903 error_setg_win32|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002904 error_setg_file_open|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002905 error_set|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002906 error_prepend|
Alistair Francise43ead12017-07-12 06:57:52 -07002907 warn_reportf_err|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002908 error_reportf_err|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002909 error_vreport|
Alistair Francis97f40302017-07-12 06:57:36 -07002910 warn_vreport|
2911 info_vreport|
2912 error_report|
2913 warn_report|
Paolo Bonzinif1e35ac2018-11-21 19:27:20 +01002914 info_report|
2915 g_test_message}x;
Jason J. Herne5d596c22015-12-11 13:30:42 -05002916
Markus Armbrustera47eb012016-08-03 13:37:52 +02002917 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002918 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002919 }
2920
2921 # Continue checking for error messages that contains newlines. This
2922 # check handles cases where string literals are spread over multiple lines.
2923 # Example:
2924 # error_report("Error msg line #1"
2925 # "Error msg line #2\n");
2926 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2927 my $continued_str_literal = qr{\+\s*\".*\"};
2928
2929 if ($rawline =~ /$quoted_newline_regex/) {
2930 # Backtrack to first line that does not contain only a quoted literal
2931 # and assume that it is the start of the statement.
2932 my $i = $linenr - 2;
2933
2934 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2935 $i--;
2936 }
2937
2938 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002939 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002940 }
2941 }
2942
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002943# check for non-portable libc calls that have portable alternatives in QEMU
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002944 if ($line =~ /\bffs\(/) {
2945 ERROR("use ctz32() instead of ffs()\n" . $herecurr);
2946 }
2947 if ($line =~ /\bffsl\(/) {
2948 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2949 }
2950 if ($line =~ /\bffsll\(/) {
2951 ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
2952 }
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002953 if ($line =~ /\bbzero\(/) {
2954 ERROR("use memset() instead of bzero()\n" . $herecurr);
2955 }
Wei Yange31db8a2019-10-17 08:46:33 +08002956 if ($line =~ /\bgetpagesize\(\)/) {
2957 ERROR("use qemu_real_host_page_size instead of getpagesize()\n" . $herecurr);
2958 }
2959 if ($line =~ /\bsysconf\(_SC_PAGESIZE\)/) {
2960 ERROR("use qemu_real_host_page_size instead of sysconf(_SC_PAGESIZE)\n" . $herecurr);
2961 }
Dr. David Alan Gilbert6e938952017-04-27 17:55:26 +01002962 my $non_exit_glib_asserts = qr{g_assert_cmpstr|
2963 g_assert_cmpint|
2964 g_assert_cmpuint|
2965 g_assert_cmphex|
2966 g_assert_cmpfloat|
2967 g_assert_true|
2968 g_assert_false|
2969 g_assert_nonnull|
2970 g_assert_null|
2971 g_assert_no_error|
2972 g_assert_error|
2973 g_test_assert_expected_messages|
2974 g_test_trap_assert_passed|
2975 g_test_trap_assert_stdout|
2976 g_test_trap_assert_stdout_unmatched|
2977 g_test_trap_assert_stderr|
2978 g_test_trap_assert_stderr_unmatched}x;
2979 if ($realfile !~ /^tests\// &&
2980 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2981 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
2982 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002983 }
2984
Paolo Bonzinifd9c0cf2018-11-29 09:31:41 +01002985 if ($is_patch && $chk_signoff && $signoff == 0) {
2986 ERROR("Missing Signed-off-by: line(s)\n");
2987 }
2988
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002989 # If we have no input at all, then there is nothing to report on
2990 # so just keep quiet.
2991 if ($#rawlines == -1) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01002992 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002993 }
2994
2995 # In mailback mode only produce a report in the negative, for
2996 # things that appear to be patches.
2997 if ($mailback && ($clean == 1 || !$is_patch)) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01002998 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002999 }
3000
3001 # This is not a patch, and we are are in 'no-patch' mode so
3002 # just keep quiet.
3003 if (!$chk_patch && !$is_patch) {
Paolo Bonzini1ff7ebf2018-11-29 09:30:59 +01003004 return 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00003005 }
3006
3007 if (!$is_patch) {
3008 ERROR("Does not appear to be a unified-diff format patch\n");
3009 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00003010
3011 print report_dump();
3012 if ($summary && !($clean == 1 && $quiet == 1)) {
3013 print "$filename " if ($summary_file);
3014 print "total: $cnt_error errors, $cnt_warn warnings, " .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00003015 "$cnt_lines lines checked\n";
3016 print "\n" if ($quiet == 0);
3017 }
3018
3019 if ($quiet == 0) {
3020 # If there were whitespace errors which cleanpatch can fix
3021 # then suggest that.
Blue Swirlb6469682011-01-20 20:58:56 +00003022# if ($rpt_cleaners) {
3023# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3024# print " scripts/cleanfile\n\n";
3025# }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00003026 }
3027
3028 if ($clean == 1 && $quiet == 0) {
3029 print "$vname has no obvious style problems and is ready for submission.\n"
3030 }
3031 if ($clean == 0 && $quiet == 0) {
3032 print "$vname has style problems, please review. If any of these errors\n";
3033 print "are false positives report them to the maintainer, see\n";
3034 print "CHECKPATCH in MAINTAINERS.\n";
3035 }
3036
Paolo Bonzini141de882016-08-09 17:47:44 +02003037 return ($no_warnings ? $clean : $cnt_error == 0);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00003038}