pvrdma: add uar_read routine
[qemu/ar7.git] / scripts / checkpatch.pl
blob18e16b79dfc81b503c0d54c2bc88807f38b9e3ea
1 #!/usr/bin/env perl
2 # (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
8 use strict;
9 use warnings;
11 my $P = $0;
12 $P =~ s@.*/@@g;
14 our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
16 my $V = '0.31';
18 use Getopt::Long qw(:config no_auto_abbrev);
20 my $quiet = 0;
21 my $tree = 1;
22 my $chk_signoff = 1;
23 my $chk_patch = undef;
24 my $chk_branch = undef;
25 my $tst_only;
26 my $emacs = 0;
27 my $terse = 0;
28 my $file = undef;
29 my $no_warnings = 0;
30 my $summary = 1;
31 my $mailback = 0;
32 my $summary_file = 0;
33 my $root;
34 my %debug;
35 my $help = 0;
37 sub help {
38 my ($exitcode) = @_;
40 print << "EOM";
41 Usage:
43 $P [OPTION]... [FILE]...
44 $P [OPTION]... [GIT-REV-LIST]
46 Version: $V
48 Options:
49 -q, --quiet quiet
50 --no-tree run without a kernel tree
51 --no-signoff do not check for 'Signed-off-by' line
52 --patch treat FILE as patchfile
53 --branch treat args as GIT revision list
54 --emacs emacs compile window format
55 --terse one line per report
56 -f, --file treat FILE as regular source file
57 --strict fail if only warnings are found
58 --root=PATH PATH to the kernel tree root
59 --no-summary suppress the per-file summary
60 --mailback only produce a report in case of warnings/errors
61 --summary-file include the filename in summary
62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
64 is all off)
65 --test-only=WORD report only warnings/errors containing WORD
66 literally
67 -h, --help, --version display this help and exit
69 When FILE is - read standard input.
70 EOM
72 exit($exitcode);
75 GetOptions(
76 'q|quiet+' => \$quiet,
77 'tree!' => \$tree,
78 'signoff!' => \$chk_signoff,
79 'patch!' => \$chk_patch,
80 'branch!' => \$chk_branch,
81 'emacs!' => \$emacs,
82 'terse!' => \$terse,
83 'f|file!' => \$file,
84 'strict!' => \$no_warnings,
85 'root=s' => \$root,
86 'summary!' => \$summary,
87 'mailback!' => \$mailback,
88 'summary-file!' => \$summary_file,
90 'debug=s' => \%debug,
91 'test-only=s' => \$tst_only,
92 'h|help' => \$help,
93 'version' => \$help
94 ) or help(1);
96 help(0) if ($help);
98 my $exit = 0;
100 if ($#ARGV < 0) {
101 print "$P: no input files\n";
102 exit(1);
105 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
106 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
107 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
108 $chk_patch = $chk_branch || $file ? 0 : 1;
109 } elsif (!defined $chk_branch && !defined $chk_patch) {
110 if ($file) {
111 $chk_branch = $chk_patch = 0;
112 } else {
113 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
114 $chk_patch = $chk_branch ? 0 : 1;
116 } elsif (!defined $chk_branch && !defined $file) {
117 if ($chk_patch) {
118 $chk_branch = $file = 0;
119 } else {
120 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
121 $file = $chk_branch ? 0 : 1;
123 } elsif (!defined $chk_patch && !defined $file) {
124 if ($chk_branch) {
125 $chk_patch = $file = 0;
126 } else {
127 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
128 $chk_patch = $file ? 0 : 1;
130 } elsif (!defined $chk_branch) {
131 $chk_branch = $chk_patch || $file ? 0 : 1;
132 } elsif (!defined $chk_patch) {
133 $chk_patch = $chk_branch || $file ? 0 : 1;
134 } elsif (!defined $file) {
135 $file = $chk_patch || $chk_branch ? 0 : 1;
138 if (($chk_patch && $chk_branch) ||
139 ($chk_patch && $file) ||
140 ($chk_branch && $file)) {
141 die "Only one of --file, --branch, --patch is permitted\n";
143 if (!$chk_patch && !$chk_branch && !$file) {
144 die "One of --file, --branch, --patch is required\n";
147 my $dbg_values = 0;
148 my $dbg_possible = 0;
149 my $dbg_type = 0;
150 my $dbg_attr = 0;
151 my $dbg_adv_dcs = 0;
152 my $dbg_adv_checking = 0;
153 my $dbg_adv_apw = 0;
154 for my $key (keys %debug) {
155 ## no critic
156 eval "\${dbg_$key} = '$debug{$key}';";
157 die "$@" if ($@);
160 my $rpt_cleaners = 0;
162 if ($terse) {
163 $emacs = 1;
164 $quiet++;
167 if ($tree) {
168 if (defined $root) {
169 if (!top_of_kernel_tree($root)) {
170 die "$P: $root: --root does not point at a valid tree\n";
172 } else {
173 if (top_of_kernel_tree('.')) {
174 $root = '.';
175 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
176 top_of_kernel_tree($1)) {
177 $root = $1;
181 if (!defined $root) {
182 print "Must be run from the top-level dir. of a kernel tree\n";
183 exit(2);
187 my $emitted_corrupt = 0;
189 our $Ident = qr{
190 [A-Za-z_][A-Za-z\d_]*
191 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
193 our $Storage = qr{extern|static|asmlinkage};
194 our $Sparse = qr{
195 __force
198 # Notes to $Attribute:
199 our $Attribute = qr{
200 const|
201 volatile|
202 QEMU_NORETURN|
203 QEMU_WARN_UNUSED_RESULT|
204 QEMU_SENTINEL|
205 QEMU_PACKED|
206 GCC_FMT_ATTR
208 our $Modifier;
209 our $Inline = qr{inline};
210 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
211 our $Lval = qr{$Ident(?:$Member)*};
213 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
214 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
215 our $Compare = qr{<=|>=|==|!=|<|>};
216 our $Operators = qr{
217 <=|>=|==|!=|
218 =>|->|<<|>>|<|>|!|~|
219 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
222 our $NonptrType;
223 our $Type;
224 our $Declare;
226 our $NON_ASCII_UTF8 = qr{
227 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
228 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
229 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
230 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
231 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
232 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
233 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
236 our $UTF8 = qr{
237 [\x09\x0A\x0D\x20-\x7E] # ASCII
238 | $NON_ASCII_UTF8
241 # There are still some false positives, but this catches most
242 # common cases.
243 our $typeTypedefs = qr{(?x:
244 (?![KMGTPE]iB) # IEC binary prefix (do not match)
245 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
246 | [A-Z][A-Z\d_]*AIOCB # all uppercase
247 | [A-Z][A-Z\d_]*CPU # all uppercase
248 | QEMUBH # all uppercase
251 our @typeList = (
252 qr{void},
253 qr{(?:unsigned\s+)?char},
254 qr{(?:unsigned\s+)?short},
255 qr{(?:unsigned\s+)?int},
256 qr{(?:unsigned\s+)?long},
257 qr{(?:unsigned\s+)?long\s+int},
258 qr{(?:unsigned\s+)?long\s+long},
259 qr{(?:unsigned\s+)?long\s+long\s+int},
260 qr{unsigned},
261 qr{float},
262 qr{double},
263 qr{bool},
264 qr{struct\s+$Ident},
265 qr{union\s+$Ident},
266 qr{enum\s+$Ident},
267 qr{${Ident}_t},
268 qr{${Ident}_handler},
269 qr{${Ident}_handler_fn},
270 qr{target_(?:u)?long},
271 qr{hwaddr},
272 # external libraries
273 qr{xml${Ident}},
274 qr{xen\w+_handle},
275 # Glib definitions
276 qr{gchar},
277 qr{gshort},
278 qr{glong},
279 qr{gint},
280 qr{gboolean},
281 qr{guchar},
282 qr{gushort},
283 qr{gulong},
284 qr{guint},
285 qr{gfloat},
286 qr{gdouble},
287 qr{gpointer},
288 qr{gconstpointer},
289 qr{gint8},
290 qr{guint8},
291 qr{gint16},
292 qr{guint16},
293 qr{gint32},
294 qr{guint32},
295 qr{gint64},
296 qr{guint64},
297 qr{gsize},
298 qr{gssize},
299 qr{goffset},
300 qr{gintptr},
301 qr{guintptr},
304 # This can be modified by sub possible. Since it can be empty, be careful
305 # about regexes that always match, because they can cause infinite loops.
306 our @modifierList = (
309 sub build_types {
310 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
311 if (@modifierList > 0) {
312 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
313 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
314 } else {
315 $Modifier = qr{(?:$Attribute|$Sparse)};
317 $NonptrType = qr{
318 (?:$Modifier\s+|const\s+)*
320 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
321 (?:$typeTypedefs\b)|
322 (?:${all}\b)
324 (?:\s+$Modifier|\s+const)*
326 $Type = qr{
327 $NonptrType
328 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
329 (?:\s+$Inline|\s+$Modifier)*
331 $Declare = qr{(?:$Storage\s+)?$Type};
333 build_types();
335 $chk_signoff = 0 if ($file);
337 my @rawlines = ();
338 my @lines = ();
339 my $vname;
340 if ($chk_branch) {
341 my @patches;
342 my $HASH;
343 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) ||
344 die "$P: git log --format=%H $ARGV[0] failed - $!\n";
346 while (<$HASH>) {
347 chomp;
348 push @patches, $_;
351 close $HASH;
353 die "$P: no revisions returned for revlist '$chk_branch'\n"
354 unless @patches;
356 for my $hash (@patches) {
357 my $FILE;
358 open($FILE, '-|', "git", "show", $hash) ||
359 die "$P: git show $hash - $!\n";
360 $vname = $hash;
361 while (<$FILE>) {
362 chomp;
363 push(@rawlines, $_);
365 close($FILE);
366 if (!process($hash)) {
367 $exit = 1;
369 @rawlines = ();
370 @lines = ();
372 } else {
373 for my $filename (@ARGV) {
374 my $FILE;
375 if ($file) {
376 open($FILE, '-|', "diff -u /dev/null $filename") ||
377 die "$P: $filename: diff failed - $!\n";
378 } elsif ($filename eq '-') {
379 open($FILE, '<&STDIN');
380 } else {
381 open($FILE, '<', "$filename") ||
382 die "$P: $filename: open failed - $!\n";
384 if ($filename eq '-') {
385 $vname = 'Your patch';
386 } else {
387 $vname = $filename;
389 while (<$FILE>) {
390 chomp;
391 push(@rawlines, $_);
393 close($FILE);
394 if (!process($filename)) {
395 $exit = 1;
397 @rawlines = ();
398 @lines = ();
402 exit($exit);
404 sub top_of_kernel_tree {
405 my ($root) = @_;
407 my @tree_check = (
408 "COPYING", "MAINTAINERS", "Makefile",
409 "README", "docs", "VERSION",
410 "vl.c"
413 foreach my $check (@tree_check) {
414 if (! -e $root . '/' . $check) {
415 return 0;
418 return 1;
421 sub expand_tabs {
422 my ($str) = @_;
424 my $res = '';
425 my $n = 0;
426 for my $c (split(//, $str)) {
427 if ($c eq "\t") {
428 $res .= ' ';
429 $n++;
430 for (; ($n % 8) != 0; $n++) {
431 $res .= ' ';
433 next;
435 $res .= $c;
436 $n++;
439 return $res;
441 sub copy_spacing {
442 (my $res = shift) =~ tr/\t/ /c;
443 return $res;
446 sub line_stats {
447 my ($line) = @_;
449 # Drop the diff line leader and expand tabs
450 $line =~ s/^.//;
451 $line = expand_tabs($line);
453 # Pick the indent from the front of the line.
454 my ($white) = ($line =~ /^(\s*)/);
456 return (length($line), length($white));
459 my $sanitise_quote = '';
461 sub sanitise_line_reset {
462 my ($in_comment) = @_;
464 if ($in_comment) {
465 $sanitise_quote = '*/';
466 } else {
467 $sanitise_quote = '';
470 sub sanitise_line {
471 my ($line) = @_;
473 my $res = '';
474 my $l = '';
476 my $qlen = 0;
477 my $off = 0;
478 my $c;
480 # Always copy over the diff marker.
481 $res = substr($line, 0, 1);
483 for ($off = 1; $off < length($line); $off++) {
484 $c = substr($line, $off, 1);
486 # Comments we are wacking completely including the begin
487 # and end, all to $;.
488 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
489 $sanitise_quote = '*/';
491 substr($res, $off, 2, "$;$;");
492 $off++;
493 next;
495 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
496 $sanitise_quote = '';
497 substr($res, $off, 2, "$;$;");
498 $off++;
499 next;
501 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
502 $sanitise_quote = '//';
504 substr($res, $off, 2, $sanitise_quote);
505 $off++;
506 next;
509 # A \ in a string means ignore the next character.
510 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
511 $c eq "\\") {
512 substr($res, $off, 2, 'XX');
513 $off++;
514 next;
516 # Regular quotes.
517 if ($c eq "'" || $c eq '"') {
518 if ($sanitise_quote eq '') {
519 $sanitise_quote = $c;
521 substr($res, $off, 1, $c);
522 next;
523 } elsif ($sanitise_quote eq $c) {
524 $sanitise_quote = '';
528 #print "c<$c> SQ<$sanitise_quote>\n";
529 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
530 substr($res, $off, 1, $;);
531 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
532 substr($res, $off, 1, $;);
533 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
534 substr($res, $off, 1, 'X');
535 } else {
536 substr($res, $off, 1, $c);
540 if ($sanitise_quote eq '//') {
541 $sanitise_quote = '';
544 # The pathname on a #include may be surrounded by '<' and '>'.
545 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
546 my $clean = 'X' x length($1);
547 $res =~ s@\<.*\>@<$clean>@;
549 # The whole of a #error is a string.
550 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
551 my $clean = 'X' x length($1);
552 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
555 return $res;
558 sub ctx_statement_block {
559 my ($linenr, $remain, $off) = @_;
560 my $line = $linenr - 1;
561 my $blk = '';
562 my $soff = $off;
563 my $coff = $off - 1;
564 my $coff_set = 0;
566 my $loff = 0;
568 my $type = '';
569 my $level = 0;
570 my @stack = ();
571 my $p;
572 my $c;
573 my $len = 0;
575 my $remainder;
576 while (1) {
577 @stack = (['', 0]) if ($#stack == -1);
579 #warn "CSB: blk<$blk> remain<$remain>\n";
580 # If we are about to drop off the end, pull in more
581 # context.
582 if ($off >= $len) {
583 for (; $remain > 0; $line++) {
584 last if (!defined $lines[$line]);
585 next if ($lines[$line] =~ /^-/);
586 $remain--;
587 $loff = $len;
588 $blk .= $lines[$line] . "\n";
589 $len = length($blk);
590 $line++;
591 last;
593 # Bail if there is no further context.
594 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
595 if ($off >= $len) {
596 last;
599 $p = $c;
600 $c = substr($blk, $off, 1);
601 $remainder = substr($blk, $off);
603 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
605 # Handle nested #if/#else.
606 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
607 push(@stack, [ $type, $level ]);
608 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
609 ($type, $level) = @{$stack[$#stack - 1]};
610 } elsif ($remainder =~ /^#\s*endif\b/) {
611 ($type, $level) = @{pop(@stack)};
614 # Statement ends at the ';' or a close '}' at the
615 # outermost level.
616 if ($level == 0 && $c eq ';') {
617 last;
620 # An else is really a conditional as long as its not else if
621 if ($level == 0 && $coff_set == 0 &&
622 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
623 $remainder =~ /^(else)(?:\s|{)/ &&
624 $remainder !~ /^else\s+if\b/) {
625 $coff = $off + length($1) - 1;
626 $coff_set = 1;
627 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
628 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
631 if (($type eq '' || $type eq '(') && $c eq '(') {
632 $level++;
633 $type = '(';
635 if ($type eq '(' && $c eq ')') {
636 $level--;
637 $type = ($level != 0)? '(' : '';
639 if ($level == 0 && $coff < $soff) {
640 $coff = $off;
641 $coff_set = 1;
642 #warn "CSB: mark coff<$coff>\n";
645 if (($type eq '' || $type eq '{') && $c eq '{') {
646 $level++;
647 $type = '{';
649 if ($type eq '{' && $c eq '}') {
650 $level--;
651 $type = ($level != 0)? '{' : '';
653 if ($level == 0) {
654 if (substr($blk, $off + 1, 1) eq ';') {
655 $off++;
657 last;
660 $off++;
662 # We are truly at the end, so shuffle to the next line.
663 if ($off == $len) {
664 $loff = $len + 1;
665 $line++;
666 $remain--;
669 my $statement = substr($blk, $soff, $off - $soff + 1);
670 my $condition = substr($blk, $soff, $coff - $soff + 1);
672 #warn "STATEMENT<$statement>\n";
673 #warn "CONDITION<$condition>\n";
675 #print "coff<$coff> soff<$off> loff<$loff>\n";
677 return ($statement, $condition,
678 $line, $remain + 1, $off - $loff + 1, $level);
681 sub statement_lines {
682 my ($stmt) = @_;
684 # Strip the diff line prefixes and rip blank lines at start and end.
685 $stmt =~ s/(^|\n)./$1/g;
686 $stmt =~ s/^\s*//;
687 $stmt =~ s/\s*$//;
689 my @stmt_lines = ($stmt =~ /\n/g);
691 return $#stmt_lines + 2;
694 sub statement_rawlines {
695 my ($stmt) = @_;
697 my @stmt_lines = ($stmt =~ /\n/g);
699 return $#stmt_lines + 2;
702 sub statement_block_size {
703 my ($stmt) = @_;
705 $stmt =~ s/(^|\n)./$1/g;
706 $stmt =~ s/^\s*\{//;
707 $stmt =~ s/}\s*$//;
708 $stmt =~ s/^\s*//;
709 $stmt =~ s/\s*$//;
711 my @stmt_lines = ($stmt =~ /\n/g);
712 my @stmt_statements = ($stmt =~ /;/g);
714 my $stmt_lines = $#stmt_lines + 2;
715 my $stmt_statements = $#stmt_statements + 1;
717 if ($stmt_lines > $stmt_statements) {
718 return $stmt_lines;
719 } else {
720 return $stmt_statements;
724 sub ctx_statement_full {
725 my ($linenr, $remain, $off) = @_;
726 my ($statement, $condition, $level);
728 my (@chunks);
730 # Grab the first conditional/block pair.
731 ($statement, $condition, $linenr, $remain, $off, $level) =
732 ctx_statement_block($linenr, $remain, $off);
733 #print "F: c<$condition> s<$statement> remain<$remain>\n";
734 push(@chunks, [ $condition, $statement ]);
735 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
736 return ($level, $linenr, @chunks);
739 # Pull in the following conditional/block pairs and see if they
740 # could continue the statement.
741 for (;;) {
742 ($statement, $condition, $linenr, $remain, $off, $level) =
743 ctx_statement_block($linenr, $remain, $off);
744 #print "C: c<$condition> s<$statement> remain<$remain>\n";
745 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
746 #print "C: push\n";
747 push(@chunks, [ $condition, $statement ]);
750 return ($level, $linenr, @chunks);
753 sub ctx_block_get {
754 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
755 my $line;
756 my $start = $linenr - 1;
757 my $blk = '';
758 my @o;
759 my @c;
760 my @res = ();
762 my $level = 0;
763 my @stack = ($level);
764 for ($line = $start; $remain > 0; $line++) {
765 next if ($rawlines[$line] =~ /^-/);
766 $remain--;
768 $blk .= $rawlines[$line];
770 # Handle nested #if/#else.
771 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
772 push(@stack, $level);
773 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
774 $level = $stack[$#stack - 1];
775 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
776 $level = pop(@stack);
779 foreach my $c (split(//, $lines[$line])) {
780 ##print "C<$c>L<$level><$open$close>O<$off>\n";
781 if ($off > 0) {
782 $off--;
783 next;
786 if ($c eq $close && $level > 0) {
787 $level--;
788 last if ($level == 0);
789 } elsif ($c eq $open) {
790 $level++;
794 if (!$outer || $level <= 1) {
795 push(@res, $rawlines[$line]);
798 last if ($level == 0);
801 return ($level, @res);
803 sub ctx_block_outer {
804 my ($linenr, $remain) = @_;
806 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
807 return @r;
809 sub ctx_block {
810 my ($linenr, $remain) = @_;
812 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
813 return @r;
815 sub ctx_statement {
816 my ($linenr, $remain, $off) = @_;
818 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
819 return @r;
821 sub ctx_block_level {
822 my ($linenr, $remain) = @_;
824 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
826 sub ctx_statement_level {
827 my ($linenr, $remain, $off) = @_;
829 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
832 sub ctx_locate_comment {
833 my ($first_line, $end_line) = @_;
835 # Catch a comment on the end of the line itself.
836 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
837 return $current_comment if (defined $current_comment);
839 # Look through the context and try and figure out if there is a
840 # comment.
841 my $in_comment = 0;
842 $current_comment = '';
843 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
844 my $line = $rawlines[$linenr - 1];
845 #warn " $line\n";
846 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
847 $in_comment = 1;
849 if ($line =~ m@/\*@) {
850 $in_comment = 1;
852 if (!$in_comment && $current_comment ne '') {
853 $current_comment = '';
855 $current_comment .= $line . "\n" if ($in_comment);
856 if ($line =~ m@\*/@) {
857 $in_comment = 0;
861 chomp($current_comment);
862 return($current_comment);
864 sub ctx_has_comment {
865 my ($first_line, $end_line) = @_;
866 my $cmt = ctx_locate_comment($first_line, $end_line);
868 ##print "LINE: $rawlines[$end_line - 1 ]\n";
869 ##print "CMMT: $cmt\n";
871 return ($cmt ne '');
874 sub raw_line {
875 my ($linenr, $cnt) = @_;
877 my $offset = $linenr - 1;
878 $cnt++;
880 my $line;
881 while ($cnt) {
882 $line = $rawlines[$offset++];
883 next if (defined($line) && $line =~ /^-/);
884 $cnt--;
887 return $line;
890 sub cat_vet {
891 my ($vet) = @_;
892 my ($res, $coded);
894 $res = '';
895 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
896 $res .= $1;
897 if ($2 ne '') {
898 $coded = sprintf("^%c", unpack('C', $2) + 64);
899 $res .= $coded;
902 $res =~ s/$/\$/;
904 return $res;
907 my $av_preprocessor = 0;
908 my $av_pending;
909 my @av_paren_type;
910 my $av_pend_colon;
912 sub annotate_reset {
913 $av_preprocessor = 0;
914 $av_pending = '_';
915 @av_paren_type = ('E');
916 $av_pend_colon = 'O';
919 sub annotate_values {
920 my ($stream, $type) = @_;
922 my $res;
923 my $var = '_' x length($stream);
924 my $cur = $stream;
926 print "$stream\n" if ($dbg_values > 1);
928 while (length($cur)) {
929 @av_paren_type = ('E') if ($#av_paren_type < 0);
930 print " <" . join('', @av_paren_type) .
931 "> <$type> <$av_pending>" if ($dbg_values > 1);
932 if ($cur =~ /^(\s+)/o) {
933 print "WS($1)\n" if ($dbg_values > 1);
934 if ($1 =~ /\n/ && $av_preprocessor) {
935 $type = pop(@av_paren_type);
936 $av_preprocessor = 0;
939 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
940 print "CAST($1)\n" if ($dbg_values > 1);
941 push(@av_paren_type, $type);
942 $type = 'C';
944 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
945 print "DECLARE($1)\n" if ($dbg_values > 1);
946 $type = 'T';
948 } elsif ($cur =~ /^($Modifier)\s*/) {
949 print "MODIFIER($1)\n" if ($dbg_values > 1);
950 $type = 'T';
952 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
953 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
954 $av_preprocessor = 1;
955 push(@av_paren_type, $type);
956 if ($2 ne '') {
957 $av_pending = 'N';
959 $type = 'E';
961 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
962 print "UNDEF($1)\n" if ($dbg_values > 1);
963 $av_preprocessor = 1;
964 push(@av_paren_type, $type);
966 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
967 print "PRE_START($1)\n" if ($dbg_values > 1);
968 $av_preprocessor = 1;
970 push(@av_paren_type, $type);
971 push(@av_paren_type, $type);
972 $type = 'E';
974 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
975 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
976 $av_preprocessor = 1;
978 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
980 $type = 'E';
982 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
983 print "PRE_END($1)\n" if ($dbg_values > 1);
985 $av_preprocessor = 1;
987 # Assume all arms of the conditional end as this
988 # one does, and continue as if the #endif was not here.
989 pop(@av_paren_type);
990 push(@av_paren_type, $type);
991 $type = 'E';
993 } elsif ($cur =~ /^(\\\n)/o) {
994 print "PRECONT($1)\n" if ($dbg_values > 1);
996 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
997 print "ATTR($1)\n" if ($dbg_values > 1);
998 $av_pending = $type;
999 $type = 'N';
1001 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1002 print "SIZEOF($1)\n" if ($dbg_values > 1);
1003 if (defined $2) {
1004 $av_pending = 'V';
1006 $type = 'N';
1008 } elsif ($cur =~ /^(if|while|for)\b/o) {
1009 print "COND($1)\n" if ($dbg_values > 1);
1010 $av_pending = 'E';
1011 $type = 'N';
1013 } elsif ($cur =~/^(case)/o) {
1014 print "CASE($1)\n" if ($dbg_values > 1);
1015 $av_pend_colon = 'C';
1016 $type = 'N';
1018 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1019 print "KEYWORD($1)\n" if ($dbg_values > 1);
1020 $type = 'N';
1022 } elsif ($cur =~ /^(\()/o) {
1023 print "PAREN('$1')\n" if ($dbg_values > 1);
1024 push(@av_paren_type, $av_pending);
1025 $av_pending = '_';
1026 $type = 'N';
1028 } elsif ($cur =~ /^(\))/o) {
1029 my $new_type = pop(@av_paren_type);
1030 if ($new_type ne '_') {
1031 $type = $new_type;
1032 print "PAREN('$1') -> $type\n"
1033 if ($dbg_values > 1);
1034 } else {
1035 print "PAREN('$1')\n" if ($dbg_values > 1);
1038 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1039 print "FUNC($1)\n" if ($dbg_values > 1);
1040 $type = 'V';
1041 $av_pending = 'V';
1043 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1044 if (defined $2 && $type eq 'C' || $type eq 'T') {
1045 $av_pend_colon = 'B';
1046 } elsif ($type eq 'E') {
1047 $av_pend_colon = 'L';
1049 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1050 $type = 'V';
1052 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1053 print "IDENT($1)\n" if ($dbg_values > 1);
1054 $type = 'V';
1056 } elsif ($cur =~ /^($Assignment)/o) {
1057 print "ASSIGN($1)\n" if ($dbg_values > 1);
1058 $type = 'N';
1060 } elsif ($cur =~/^(;|{|})/) {
1061 print "END($1)\n" if ($dbg_values > 1);
1062 $type = 'E';
1063 $av_pend_colon = 'O';
1065 } elsif ($cur =~/^(,)/) {
1066 print "COMMA($1)\n" if ($dbg_values > 1);
1067 $type = 'C';
1069 } elsif ($cur =~ /^(\?)/o) {
1070 print "QUESTION($1)\n" if ($dbg_values > 1);
1071 $type = 'N';
1073 } elsif ($cur =~ /^(:)/o) {
1074 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1076 substr($var, length($res), 1, $av_pend_colon);
1077 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1078 $type = 'E';
1079 } else {
1080 $type = 'N';
1082 $av_pend_colon = 'O';
1084 } elsif ($cur =~ /^(\[)/o) {
1085 print "CLOSE($1)\n" if ($dbg_values > 1);
1086 $type = 'N';
1088 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1089 my $variant;
1091 print "OPV($1)\n" if ($dbg_values > 1);
1092 if ($type eq 'V') {
1093 $variant = 'B';
1094 } else {
1095 $variant = 'U';
1098 substr($var, length($res), 1, $variant);
1099 $type = 'N';
1101 } elsif ($cur =~ /^($Operators)/o) {
1102 print "OP($1)\n" if ($dbg_values > 1);
1103 if ($1 ne '++' && $1 ne '--') {
1104 $type = 'N';
1107 } elsif ($cur =~ /(^.)/o) {
1108 print "C($1)\n" if ($dbg_values > 1);
1110 if (defined $1) {
1111 $cur = substr($cur, length($1));
1112 $res .= $type x length($1);
1116 return ($res, $var);
1119 sub possible {
1120 my ($possible, $line) = @_;
1121 my $notPermitted = qr{(?:
1122 ^(?:
1123 $Modifier|
1124 $Storage|
1125 $Type|
1126 DEFINE_\S+
1128 ^(?:
1129 goto|
1130 return|
1131 case|
1132 else|
1133 asm|__asm__|
1135 )(?:\s|$)|
1136 ^(?:typedef|struct|enum)\b|
1138 )}x;
1139 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1140 if ($possible !~ $notPermitted) {
1141 # Check for modifiers.
1142 $possible =~ s/\s*$Storage\s*//g;
1143 $possible =~ s/\s*$Sparse\s*//g;
1144 if ($possible =~ /^\s*$/) {
1146 } elsif ($possible =~ /\s/) {
1147 $possible =~ s/\s*(?:$Type|\#\#)\s*//g;
1148 for my $modifier (split(' ', $possible)) {
1149 if ($modifier !~ $notPermitted) {
1150 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1151 push(@modifierList, $modifier);
1155 } else {
1156 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1157 push(@typeList, $possible);
1159 build_types();
1160 } else {
1161 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1165 my $prefix = '';
1167 sub report {
1168 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1169 return 0;
1171 my $line = $prefix . $_[0];
1173 $line = (split('\n', $line))[0] . "\n" if ($terse);
1175 push(our @report, $line);
1177 return 1;
1179 sub report_dump {
1180 our @report;
1182 sub ERROR {
1183 if (report("ERROR: $_[0]\n")) {
1184 our $clean = 0;
1185 our $cnt_error++;
1188 sub WARN {
1189 if (report("WARNING: $_[0]\n")) {
1190 our $clean = 0;
1191 our $cnt_warn++;
1195 sub process {
1196 my $filename = shift;
1198 my $linenr=0;
1199 my $prevline="";
1200 my $prevrawline="";
1201 my $stashline="";
1202 my $stashrawline="";
1204 my $length;
1205 my $indent;
1206 my $previndent=0;
1207 my $stashindent=0;
1209 our $clean = 1;
1210 my $signoff = 0;
1211 my $is_patch = 0;
1213 my $in_header_lines = $file ? 0 : 1;
1214 my $in_commit_log = 0; #Scanning lines before patch
1215 my $reported_maintainer_file = 0;
1216 my $non_utf8_charset = 0;
1218 our @report = ();
1219 our $cnt_lines = 0;
1220 our $cnt_error = 0;
1221 our $cnt_warn = 0;
1222 our $cnt_chk = 0;
1224 # Trace the real file/line as we go.
1225 my $realfile = '';
1226 my $realline = 0;
1227 my $realcnt = 0;
1228 my $here = '';
1229 my $in_comment = 0;
1230 my $comment_edge = 0;
1231 my $first_line = 0;
1232 my $p1_prefix = '';
1234 my $prev_values = 'E';
1236 # suppression flags
1237 my %suppress_ifbraces;
1238 my %suppress_whiletrailers;
1239 my %suppress_export;
1241 # Pre-scan the patch sanitizing the lines.
1243 sanitise_line_reset();
1244 my $line;
1245 foreach my $rawline (@rawlines) {
1246 $linenr++;
1247 $line = $rawline;
1249 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1250 $realline=$1-1;
1251 if (defined $2) {
1252 $realcnt=$3+1;
1253 } else {
1254 $realcnt=1+1;
1256 $in_comment = 0;
1258 # Guestimate if this is a continuing comment. Run
1259 # the context looking for a comment "edge". If this
1260 # edge is a close comment then we must be in a comment
1261 # at context start.
1262 my $edge;
1263 my $cnt = $realcnt;
1264 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1265 next if (defined $rawlines[$ln - 1] &&
1266 $rawlines[$ln - 1] =~ /^-/);
1267 $cnt--;
1268 #print "RAW<$rawlines[$ln - 1]>\n";
1269 last if (!defined $rawlines[$ln - 1]);
1270 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1271 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1272 ($edge) = $1;
1273 last;
1276 if (defined $edge && $edge eq '*/') {
1277 $in_comment = 1;
1280 # Guestimate if this is a continuing comment. If this
1281 # is the start of a diff block and this line starts
1282 # ' *' then it is very likely a comment.
1283 if (!defined $edge &&
1284 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1286 $in_comment = 1;
1289 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1290 sanitise_line_reset($in_comment);
1292 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1293 # Standardise the strings and chars within the input to
1294 # simplify matching -- only bother with positive lines.
1295 $line = sanitise_line($rawline);
1297 push(@lines, $line);
1299 if ($realcnt > 1) {
1300 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1301 } else {
1302 $realcnt = 0;
1305 #print "==>$rawline\n";
1306 #print "-->$line\n";
1309 $prefix = '';
1311 $realcnt = 0;
1312 $linenr = 0;
1313 foreach my $line (@lines) {
1314 $linenr++;
1316 my $rawline = $rawlines[$linenr - 1];
1318 #extract the line range in the file after the patch is applied
1319 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1320 $is_patch = 1;
1321 $first_line = $linenr + 1;
1322 $realline=$1-1;
1323 if (defined $2) {
1324 $realcnt=$3+1;
1325 } else {
1326 $realcnt=1+1;
1328 annotate_reset();
1329 $prev_values = 'E';
1331 %suppress_ifbraces = ();
1332 %suppress_whiletrailers = ();
1333 %suppress_export = ();
1334 next;
1336 # track the line number as we move through the hunk, note that
1337 # new versions of GNU diff omit the leading space on completely
1338 # blank context lines so we need to count that too.
1339 } elsif ($line =~ /^( |\+|$)/) {
1340 $realline++;
1341 $realcnt-- if ($realcnt != 0);
1343 # Measure the line length and indent.
1344 ($length, $indent) = line_stats($rawline);
1346 # Track the previous line.
1347 ($prevline, $stashline) = ($stashline, $line);
1348 ($previndent, $stashindent) = ($stashindent, $indent);
1349 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1351 #warn "line<$line>\n";
1353 } elsif ($realcnt == 1) {
1354 $realcnt--;
1357 my $hunk_line = ($realcnt != 0);
1359 #make up the handle for any error we report on this line
1360 $prefix = "$filename:$realline: " if ($emacs && $file);
1361 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1363 $here = "#$linenr: " if (!$file);
1364 $here = "#$realline: " if ($file);
1366 # extract the filename as it passes
1367 if ($line =~ /^diff --git.*?(\S+)$/) {
1368 $realfile = $1;
1369 $realfile =~ s@^([^/]*)/@@ if (!$file);
1370 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1371 $realfile = $1;
1372 $realfile =~ s@^([^/]*)/@@ if (!$file);
1374 $p1_prefix = $1;
1375 if (!$file && $tree && $p1_prefix ne '' &&
1376 -e "$root/$p1_prefix") {
1377 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1380 next;
1383 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1385 my $hereline = "$here\n$rawline\n";
1386 my $herecurr = "$here\n$rawline\n";
1387 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1389 $cnt_lines++ if ($realcnt != 0);
1391 # Check for incorrect file permissions
1392 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1393 my $permhere = $here . "FILE: $realfile\n";
1394 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
1395 ERROR("do not set execute permissions for source files\n" . $permhere);
1399 # Accept git diff extended headers as valid patches
1400 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1401 $is_patch = 1;
1404 if ($line =~ /^Author: .*via Qemu-devel.*<qemu-devel\@nongnu.org>/) {
1405 ERROR("Author email address is mangled by the mailing list\n" . $herecurr);
1408 #check the patch for a signoff:
1409 if ($line =~ /^\s*signed-off-by:/i) {
1410 # This is a signoff, if ugly, so do not double report.
1411 $signoff++;
1412 $in_commit_log = 0;
1414 if (!($line =~ /^\s*Signed-off-by:/)) {
1415 ERROR("The correct form is \"Signed-off-by\"\n" .
1416 $herecurr);
1418 if ($line =~ /^\s*signed-off-by:\S/i) {
1419 ERROR("space required after Signed-off-by:\n" .
1420 $herecurr);
1424 # Check if MAINTAINERS is being updated. If so, there's probably no need to
1425 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
1426 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
1427 $reported_maintainer_file = 1;
1430 # Check for added, moved or deleted files
1431 if (!$reported_maintainer_file && !$in_commit_log &&
1432 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
1433 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
1434 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
1435 (defined($1) || defined($2))))) {
1436 $reported_maintainer_file = 1;
1437 WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
1440 # Check for wrappage within a valid hunk of the file
1441 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1442 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1443 $herecurr) if (!$emitted_corrupt++);
1446 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1447 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1448 $rawline !~ m/^$UTF8*$/) {
1449 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1451 my $blank = copy_spacing($rawline);
1452 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1453 my $hereptr = "$hereline$ptr\n";
1455 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1458 # Check if it's the start of a commit log
1459 # (not a header line and we haven't seen the patch filename)
1460 if ($in_header_lines && $realfile =~ /^$/ &&
1461 !($rawline =~ /^\s+\S/ ||
1462 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
1463 $in_header_lines = 0;
1464 $in_commit_log = 1;
1467 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1468 # declined it, i.e defined some charset where it is missing.
1469 if ($in_header_lines &&
1470 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1471 $1 !~ /utf-8/i) {
1472 $non_utf8_charset = 1;
1475 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1476 $rawline =~ /$NON_ASCII_UTF8/) {
1477 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr);
1480 # ignore non-hunk lines and lines being removed
1481 next if (!$hunk_line || $line =~ /^-/);
1483 # ignore files that are being periodically imported from Linux
1484 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1486 #trailing whitespace
1487 if ($line =~ /^\+.*\015/) {
1488 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1489 ERROR("DOS line endings\n" . $herevet);
1491 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1492 $realfile =~ /^docs\/.+\.md/) {
1493 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1494 # TODO: properly check we're in a code block
1495 # (surrounding text is 4-column aligned)
1496 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1497 ERROR("code blocks in documentation should have " .
1498 "empty lines with exactly 4 columns of " .
1499 "whitespace\n" . $herevet);
1501 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1502 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1503 ERROR("trailing whitespace\n" . $herevet);
1504 $rpt_cleaners = 1;
1507 # checks for trace-events files
1508 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1509 if ($rawline =~ /%[-+ 0]*#/) {
1510 ERROR("Don't use '#' flag of printf format ('%#') in " .
1511 "trace-events, use '0x' prefix instead\n" . $herecurr);
1512 } else {
1513 my $hex =
1514 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1516 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1517 my $tmpline = $rawline;
1518 $tmpline =~ s/($hex[.:\/ ])+$hex//g;
1520 if ($tmpline =~ /(?<!0x)$hex/) {
1521 ERROR("Hex numbers must be prefixed with '0x'\n" .
1522 $herecurr);
1527 # check we are in a valid source file if not then ignore this hunk
1528 next if ($realfile !~ /$SrcFile/);
1530 #90 column limit; exempt URLs, if no other words on line
1531 if ($line =~ /^\+/ &&
1532 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1533 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) &&
1534 $length > 80)
1536 if ($length > 90) {
1537 ERROR("line over 90 characters\n" . $herecurr);
1538 } else {
1539 WARN("line over 80 characters\n" . $herecurr);
1543 # check for spaces before a quoted newline
1544 if ($rawline =~ /^.*\".*\s\\n/) {
1545 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
1548 # check for adding lines without a newline.
1549 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1550 ERROR("adding a line without newline at end of file\n" . $herecurr);
1553 # check for RCS/CVS revision markers
1554 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
1555 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1558 # tabs are only allowed in assembly source code, and in
1559 # some scripts we imported from other projects.
1560 next if ($realfile =~ /\.(s|S)$/);
1561 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
1563 if ($rawline =~ /^\+.*\t/) {
1564 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1565 ERROR("code indent should never use tabs\n" . $herevet);
1566 $rpt_cleaners = 1;
1569 # check we are in a valid C source file if not then ignore this hunk
1570 next if ($realfile !~ /\.(h|c|cpp)$/);
1572 # Block comment styles
1574 # Block comments use /* on a line of its own
1575 if ($rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1576 $rawline =~ m@^\+.*/\*\*?[ \t]*.+[ \t]*$@) { # /* or /** non-blank
1577 WARN("Block comments use a leading /* on a separate line\n" . $herecurr);
1580 # Block comments use * on subsequent lines
1581 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1582 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
1583 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1584 $rawline =~ /^\+/ && #line is new
1585 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1586 WARN("Block comments use * on subsequent lines\n" . $hereprev);
1589 # Block comments use */ on trailing lines
1590 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1591 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1592 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1593 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1594 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr);
1597 # Block comment * alignment
1598 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
1599 $line =~ /^\+[ \t]*$;/ && #leading comment
1600 $rawline =~ /^\+[ \t]*\*/ && #leading *
1601 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
1602 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
1603 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
1604 my $oldindent;
1605 $prevrawline =~ m@^\+([ \t]*/?)\*@;
1606 if (defined($1)) {
1607 $oldindent = expand_tabs($1);
1608 } else {
1609 $prevrawline =~ m@^\+(.*/?)\*@;
1610 $oldindent = expand_tabs($1);
1612 $rawline =~ m@^\+([ \t]*)\*@;
1613 my $newindent = $1;
1614 $newindent = expand_tabs($newindent);
1615 if (length($oldindent) ne length($newindent)) {
1616 WARN("Block comments should align the * on each line\n" . $hereprev);
1620 # Check for potential 'bare' types
1621 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1622 $realline_next);
1623 if ($realcnt && $line =~ /.\s*\S/) {
1624 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1625 ctx_statement_block($linenr, $realcnt, 0);
1626 $stat =~ s/\n./\n /g;
1627 $cond =~ s/\n./\n /g;
1629 # Find the real next line.
1630 $realline_next = $line_nr_next;
1631 if (defined $realline_next &&
1632 (!defined $lines[$realline_next - 1] ||
1633 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1634 $realline_next++;
1637 my $s = $stat;
1638 $s =~ s/{.*$//s;
1640 # Ignore goto labels.
1641 if ($s =~ /$Ident:\*$/s) {
1643 # Ignore functions being called
1644 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1646 } elsif ($s =~ /^.\s*else\b/s) {
1648 # declarations always start with types
1649 } 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) {
1650 my $type = $1;
1651 $type =~ s/\s+/ /g;
1652 possible($type, "A:" . $s);
1654 # definitions in global scope can only start with types
1655 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1656 possible($1, "B:" . $s);
1659 # any (foo ... *) is a pointer cast, and foo is a type
1660 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1661 possible($1, "C:" . $s);
1664 # Check for any sort of function declaration.
1665 # int foo(something bar, other baz);
1666 # void (*store_gdt)(x86_descr_ptr *);
1667 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1668 my ($name_len) = length($1);
1670 my $ctx = $s;
1671 substr($ctx, 0, $name_len + 1, '');
1672 $ctx =~ s/\)[^\)]*$//;
1674 for my $arg (split(/\s*,\s*/, $ctx)) {
1675 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1677 possible($1, "D:" . $s);
1685 # Checks which may be anchored in the context.
1688 # Check for switch () and associated case and default
1689 # statements should be at the same indent.
1690 if ($line=~/\bswitch\s*\(.*\)/) {
1691 my $err = '';
1692 my $sep = '';
1693 my @ctx = ctx_block_outer($linenr, $realcnt);
1694 shift(@ctx);
1695 for my $ctx (@ctx) {
1696 my ($clen, $cindent) = line_stats($ctx);
1697 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1698 $indent != $cindent) {
1699 $err .= "$sep$ctx\n";
1700 $sep = '';
1701 } else {
1702 $sep = "[...]\n";
1705 if ($err ne '') {
1706 ERROR("switch and case should be at the same indent\n$hereline$err");
1710 # if/while/etc brace do not go on next line, unless defining a do while loop,
1711 # or if that brace on the next line is for something else
1712 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1713 my $pre_ctx = "$1$2";
1715 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1716 my $ctx_cnt = $realcnt - $#ctx - 1;
1717 my $ctx = join("\n", @ctx);
1719 my $ctx_ln = $linenr;
1720 my $ctx_skip = $realcnt;
1722 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1723 defined $lines[$ctx_ln - 1] &&
1724 $lines[$ctx_ln - 1] =~ /^-/)) {
1725 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1726 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1727 $ctx_ln++;
1730 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1731 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1733 # The length of the "previous line" is checked against 80 because it
1734 # includes the + at the beginning of the line (if the actual line has
1735 # 79 or 80 characters, it is no longer possible to add a space and an
1736 # opening brace there)
1737 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
1738 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
1739 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
1740 ERROR("that open brace { should be on the previous line\n" .
1741 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1743 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1744 $ctx =~ /\)\s*\;\s*$/ &&
1745 defined $lines[$ctx_ln - 1])
1747 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1748 if ($nindent > $indent) {
1749 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
1750 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1755 # 'do ... while (0/false)' only makes sense in macros, without trailing ';'
1756 if ($line =~ /while\s*\((0|false)\);/) {
1757 ERROR("suspicious ; after while (0)\n" . $herecurr);
1760 # Check relative indent for conditionals and blocks.
1761 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1762 my ($s, $c) = ($stat, $cond);
1764 substr($s, 0, length($c), '');
1766 # Make sure we remove the line prefixes as we have
1767 # none on the first line, and are going to readd them
1768 # where necessary.
1769 $s =~ s/\n./\n/gs;
1771 # Find out how long the conditional actually is.
1772 my @newlines = ($c =~ /\n/gs);
1773 my $cond_lines = 1 + $#newlines;
1775 # We want to check the first line inside the block
1776 # starting at the end of the conditional, so remove:
1777 # 1) any blank line termination
1778 # 2) any opening brace { on end of the line
1779 # 3) any do (...) {
1780 my $continuation = 0;
1781 my $check = 0;
1782 $s =~ s/^.*\bdo\b//;
1783 $s =~ s/^\s*\{//;
1784 if ($s =~ s/^\s*\\//) {
1785 $continuation = 1;
1787 if ($s =~ s/^\s*?\n//) {
1788 $check = 1;
1789 $cond_lines++;
1792 # Also ignore a loop construct at the end of a
1793 # preprocessor statement.
1794 if (($prevline =~ /^.\s*#\s*define\s/ ||
1795 $prevline =~ /\\\s*$/) && $continuation == 0) {
1796 $check = 0;
1799 my $cond_ptr = -1;
1800 $continuation = 0;
1801 while ($cond_ptr != $cond_lines) {
1802 $cond_ptr = $cond_lines;
1804 # If we see an #else/#elif then the code
1805 # is not linear.
1806 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1807 $check = 0;
1810 # Ignore:
1811 # 1) blank lines, they should be at 0,
1812 # 2) preprocessor lines, and
1813 # 3) labels.
1814 if ($continuation ||
1815 $s =~ /^\s*?\n/ ||
1816 $s =~ /^\s*#\s*?/ ||
1817 $s =~ /^\s*$Ident\s*:/) {
1818 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1819 if ($s =~ s/^.*?\n//) {
1820 $cond_lines++;
1825 my (undef, $sindent) = line_stats("+" . $s);
1826 my $stat_real = raw_line($linenr, $cond_lines);
1828 # Check if either of these lines are modified, else
1829 # this is not this patch's fault.
1830 if (!defined($stat_real) ||
1831 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1832 $check = 0;
1834 if (defined($stat_real) && $cond_lines > 1) {
1835 $stat_real = "[...]\n$stat_real";
1838 #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";
1840 if ($check && (($sindent % 4) != 0 ||
1841 ($sindent <= $indent && $s ne ''))) {
1842 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1846 # Track the 'values' across context and added lines.
1847 my $opline = $line; $opline =~ s/^./ /;
1848 my ($curr_values, $curr_vars) =
1849 annotate_values($opline . "\n", $prev_values);
1850 $curr_values = $prev_values . $curr_values;
1851 if ($dbg_values) {
1852 my $outline = $opline; $outline =~ s/\t/ /g;
1853 print "$linenr > .$outline\n";
1854 print "$linenr > $curr_values\n";
1855 print "$linenr > $curr_vars\n";
1857 $prev_values = substr($curr_values, -1);
1859 #ignore lines not being added
1860 if ($line=~/^[^\+]/) {next;}
1862 # TEST: allow direct testing of the type matcher.
1863 if ($dbg_type) {
1864 if ($line =~ /^.\s*$Declare\s*$/) {
1865 ERROR("TEST: is type\n" . $herecurr);
1866 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1867 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1869 next;
1871 # TEST: allow direct testing of the attribute matcher.
1872 if ($dbg_attr) {
1873 if ($line =~ /^.\s*$Modifier\s*$/) {
1874 ERROR("TEST: is attr\n" . $herecurr);
1875 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1876 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1878 next;
1881 # check for initialisation to aggregates open brace on the next line
1882 if ($line =~ /^.\s*\{/ &&
1883 $prevline =~ /(?:^|[^=])=\s*$/) {
1884 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1888 # Checks which are anchored on the added line.
1891 # check for malformed paths in #include statements (uses RAW line)
1892 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1893 my $path = $1;
1894 if ($path =~ m{//}) {
1895 ERROR("malformed #include filename\n" .
1896 $herecurr);
1900 # no C99 // comments
1901 if ($line =~ m{//}) {
1902 ERROR("do not use C99 // comments\n" . $herecurr);
1904 # Remove C99 comments.
1905 $line =~ s@//.*@@;
1906 $opline =~ s@//.*@@;
1908 # check for global initialisers.
1909 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1910 ERROR("do not initialise globals to 0 or NULL\n" .
1911 $herecurr);
1913 # check for static initialisers.
1914 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1915 ERROR("do not initialise statics to 0 or NULL\n" .
1916 $herecurr);
1919 # * goes on variable not on type
1920 # (char*[ const])
1921 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1922 my ($from, $to) = ($1, $1);
1924 # Should start with a space.
1925 $to =~ s/^(\S)/ $1/;
1926 # Should not end with a space.
1927 $to =~ s/\s+$//;
1928 # '*'s should not have spaces between.
1929 while ($to =~ s/\*\s+\*/\*\*/) {
1932 #print "from<$from> to<$to>\n";
1933 if ($from ne $to) {
1934 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1936 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1937 my ($from, $to, $ident) = ($1, $1, $2);
1939 # Should start with a space.
1940 $to =~ s/^(\S)/ $1/;
1941 # Should not end with a space.
1942 $to =~ s/\s+$//;
1943 # '*'s should not have spaces between.
1944 while ($to =~ s/\*\s+\*/\*\*/) {
1946 # Modifiers should have spaces.
1947 $to =~ s/(\b$Modifier$)/$1 /;
1949 #print "from<$from> to<$to> ident<$ident>\n";
1950 if ($from ne $to && $ident !~ /^$Modifier$/) {
1951 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1955 # function brace can't be on same line, except for #defines of do while,
1956 # or if closed on same line
1957 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1958 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
1959 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1962 # open braces for enum, union and struct go on the same line.
1963 if ($line =~ /^.\s*\{/ &&
1964 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1965 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1968 # missing space after union, struct or enum definition
1969 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1970 ERROR("missing space after $1 definition\n" . $herecurr);
1973 # check for spacing round square brackets; allowed:
1974 # 1. with a type on the left -- int [] a;
1975 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1976 # 3. inside a curly brace -- = { [0...10] = 5 }
1977 # 4. after a comma -- [1] = 5, [2] = 6
1978 # 5. in a macro definition -- #define abc(x) [x] = y
1979 while ($line =~ /(.*?\s)\[/g) {
1980 my ($where, $prefix) = ($-[1], $1);
1981 if ($prefix !~ /$Type\s+$/ &&
1982 ($where != 0 || $prefix !~ /^.\s+$/) &&
1983 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
1984 $prefix !~ /[,{:]\s+$/) {
1985 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1989 # check for spaces between functions and their parentheses.
1990 while ($line =~ /($Ident)\s+\(/g) {
1991 my $name = $1;
1992 my $ctx_before = substr($line, 0, $-[1]);
1993 my $ctx = "$ctx_before$name";
1995 # Ignore those directives where spaces _are_ permitted.
1996 if ($name =~ /^(?:
1997 if|for|while|switch|return|case|
1998 volatile|__volatile__|coroutine_fn|
1999 __attribute__|format|__extension__|
2000 asm|__asm__)$/x)
2003 # Ignore 'catch (...)' in C++
2004 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
2006 # cpp #define statements have non-optional spaces, ie
2007 # if there is a space between the name and the open
2008 # parenthesis it is simply not a parameter group.
2009 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2011 # cpp #elif statement condition may start with a (
2012 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2014 # If this whole things ends with a type its most
2015 # likely a typedef for a function.
2016 } elsif ($ctx =~ /$Type$/) {
2018 } else {
2019 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
2022 # Check operator spacing.
2023 if (!($line=~/\#\s*include/)) {
2024 my $ops = qr{
2025 <<=|>>=|<=|>=|==|!=|
2026 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2027 =>|->|<<|>>|<|>|=|!|~|
2028 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2029 \?|::|:
2031 my @elements = split(/($ops|;)/, $opline);
2032 my $off = 0;
2034 my $blank = copy_spacing($opline);
2036 for (my $n = 0; $n < $#elements; $n += 2) {
2037 $off += length($elements[$n]);
2039 # Pick up the preceding and succeeding characters.
2040 my $ca = substr($opline, 0, $off);
2041 my $cc = '';
2042 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2043 $cc = substr($opline, $off + length($elements[$n + 1]));
2045 my $cb = "$ca$;$cc";
2047 my $a = '';
2048 $a = 'V' if ($elements[$n] ne '');
2049 $a = 'W' if ($elements[$n] =~ /\s$/);
2050 $a = 'C' if ($elements[$n] =~ /$;$/);
2051 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2052 $a = 'O' if ($elements[$n] eq '');
2053 $a = 'E' if ($ca =~ /^\s*$/);
2055 my $op = $elements[$n + 1];
2057 my $c = '';
2058 if (defined $elements[$n + 2]) {
2059 $c = 'V' if ($elements[$n + 2] ne '');
2060 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2061 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2062 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2063 $c = 'O' if ($elements[$n + 2] eq '');
2064 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2065 } else {
2066 $c = 'E';
2069 my $ctx = "${a}x${c}";
2071 my $at = "(ctx:$ctx)";
2073 my $ptr = substr($blank, 0, $off) . "^";
2074 my $hereptr = "$hereline$ptr\n";
2076 # Pull out the value of this operator.
2077 my $op_type = substr($curr_values, $off + 1, 1);
2079 # Get the full operator variant.
2080 my $opv = $op . substr($curr_vars, $off, 1);
2082 # Ignore operators passed as parameters.
2083 if ($op_type ne 'V' &&
2084 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2086 # # Ignore comments
2087 # } elsif ($op =~ /^$;+$/) {
2089 # ; should have either the end of line or a space or \ after it
2090 } elsif ($op eq ';') {
2091 if ($ctx !~ /.x[WEBC]/ &&
2092 $cc !~ /^\\/ && $cc !~ /^;/) {
2093 ERROR("space required after that '$op' $at\n" . $hereptr);
2096 # // is a comment
2097 } elsif ($op eq '//') {
2099 # Ignore : used in class declaration in C++
2100 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
2101 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
2103 # No spaces for:
2104 # ->
2105 # : when part of a bitfield
2106 } elsif ($op eq '->' || $opv eq ':B') {
2107 if ($ctx =~ /Wx.|.xW/) {
2108 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
2111 # , must have a space on the right.
2112 # not required when having a single },{ on one line
2113 } elsif ($op eq ',') {
2114 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
2115 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
2116 ERROR("space required after that '$op' $at\n" . $hereptr);
2119 # '*' as part of a type definition -- reported already.
2120 } elsif ($opv eq '*_') {
2121 #warn "'*' is part of type\n";
2123 # unary operators should have a space before and
2124 # none after. May be left adjacent to another
2125 # unary operator, or a cast
2126 } elsif ($op eq '!' || $op eq '~' ||
2127 $opv eq '*U' || $opv eq '-U' ||
2128 $opv eq '&U' || $opv eq '&&U') {
2129 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
2130 # '~' used as a name of Destructor
2132 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2133 ERROR("space required before that '$op' $at\n" . $hereptr);
2135 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2136 # A unary '*' may be const
2138 } elsif ($ctx =~ /.xW/) {
2139 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2142 # unary ++ and unary -- are allowed no space on one side.
2143 } elsif ($op eq '++' or $op eq '--') {
2144 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2145 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2147 if ($ctx =~ /Wx[BE]/ ||
2148 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2149 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2151 if ($ctx =~ /ExW/) {
2152 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2155 # A colon needs no spaces before when it is
2156 # terminating a case value or a label.
2157 } elsif ($opv eq ':C' || $opv eq ':L') {
2158 if ($ctx =~ /Wx./) {
2159 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2162 # All the others need spaces both sides.
2163 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2164 my $ok = 0;
2166 if ($realfile =~ /\.cpp|\.h$/) {
2167 # Ignore template arguments <...> in C++
2168 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2169 $ok = 1;
2172 # Ignore :: in C++
2173 if ($op eq '::') {
2174 $ok = 1;
2178 # Ignore email addresses <foo@bar>
2179 if (($op eq '<' &&
2180 $cc =~ /^\S+\@\S+>/) ||
2181 ($op eq '>' &&
2182 $ca =~ /<\S+\@\S+$/))
2184 $ok = 1;
2187 # Ignore ?:
2188 if (($opv eq ':O' && $ca =~ /\?$/) ||
2189 ($op eq '?' && $cc =~ /^:/)) {
2190 $ok = 1;
2193 if ($ok == 0) {
2194 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2197 $off += length($elements[$n + 1]);
2201 #need space before brace following if, while, etc
2202 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2203 $line =~ /do\{/) {
2204 ERROR("space required before the open brace '{'\n" . $herecurr);
2207 # closing brace should have a space following it when it has anything
2208 # on the line
2209 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2210 ERROR("space required after that close brace '}'\n" . $herecurr);
2213 # check spacing on square brackets
2214 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2215 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2217 if ($line =~ /\s\]/) {
2218 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2221 # check spacing on parentheses
2222 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2223 $line !~ /for\s*\(\s+;/) {
2224 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2226 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2227 $line !~ /for\s*\(.*;\s+\)/ &&
2228 $line !~ /:\s+\)/) {
2229 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2232 # Return is not a function.
2233 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2234 my $spacing = $1;
2235 my $value = $2;
2237 # Flatten any parentheses
2238 $value =~ s/\(/ \(/g;
2239 $value =~ s/\)/\) /g;
2240 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2241 $value !~ /(?:$Ident|-?$Constant)\s*
2242 $Compare\s*
2243 (?:$Ident|-?$Constant)/x &&
2244 $value =~ s/\([^\(\)]*\)/1/) {
2246 #print "value<$value>\n";
2247 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2248 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2250 } elsif ($spacing !~ /\s+/) {
2251 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2254 # Return of what appears to be an errno should normally be -'ve
2255 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2256 my $name = $1;
2257 if ($name ne 'EOF' && $name ne 'ERROR') {
2258 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2262 # Need a space before open parenthesis after if, while etc
2263 if ($line=~/\b(if|while|for|switch)\(/) {
2264 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2267 # Check for illegal assignment in if conditional -- and check for trailing
2268 # statements after the conditional.
2269 if ($line =~ /do\s*(?!{)/) {
2270 my ($stat_next) = ctx_statement_block($line_nr_next,
2271 $remain_next, $off_next);
2272 $stat_next =~ s/\n./\n /g;
2273 ##print "stat<$stat> stat_next<$stat_next>\n";
2275 if ($stat_next =~ /^\s*while\b/) {
2276 # If the statement carries leading newlines,
2277 # then count those as offsets.
2278 my ($whitespace) =
2279 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2280 my $offset =
2281 statement_rawlines($whitespace) - 1;
2283 $suppress_whiletrailers{$line_nr_next +
2284 $offset} = 1;
2287 if (!defined $suppress_whiletrailers{$linenr} &&
2288 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2289 my ($s, $c) = ($stat, $cond);
2291 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2292 ERROR("do not use assignment in if condition\n" . $herecurr);
2295 # Find out what is on the end of the line after the
2296 # conditional.
2297 substr($s, 0, length($c), '');
2298 $s =~ s/\n.*//g;
2299 $s =~ s/$;//g; # Remove any comments
2300 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2301 $c !~ /}\s*while\s*/)
2303 # Find out how long the conditional actually is.
2304 my @newlines = ($c =~ /\n/gs);
2305 my $cond_lines = 1 + $#newlines;
2306 my $stat_real = '';
2308 $stat_real = raw_line($linenr, $cond_lines)
2309 . "\n" if ($cond_lines);
2310 if (defined($stat_real) && $cond_lines > 1) {
2311 $stat_real = "[...]\n$stat_real";
2314 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2318 # Check for bitwise tests written as boolean
2319 if ($line =~ /
2321 (?:\[|\(|\&\&|\|\|)
2322 \s*0[xX][0-9]+\s*
2323 (?:\&\&|\|\|)
2325 (?:\&\&|\|\|)
2326 \s*0[xX][0-9]+\s*
2327 (?:\&\&|\|\||\)|\])
2328 )/x)
2330 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2333 # if and else should not have general statements after it
2334 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2335 my $s = $1;
2336 $s =~ s/$;//g; # Remove any comments
2337 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2338 ERROR("trailing statements should be on next line\n" . $herecurr);
2341 # if should not continue a brace
2342 if ($line =~ /}\s*if\b/) {
2343 ERROR("trailing statements should be on next line\n" .
2344 $herecurr);
2346 # case and default should not have general statements after them
2347 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2348 $line !~ /\G(?:
2349 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2350 \s*return\s+
2351 )/xg)
2353 ERROR("trailing statements should be on next line\n" . $herecurr);
2356 # Check for }<nl>else {, these must be at the same
2357 # indent level to be relevant to each other.
2358 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2359 $previndent == $indent) {
2360 ERROR("else should follow close brace '}'\n" . $hereprev);
2363 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2364 $previndent == $indent) {
2365 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2367 # Find out what is on the end of the line after the
2368 # conditional.
2369 substr($s, 0, length($c), '');
2370 $s =~ s/\n.*//g;
2372 if ($s =~ /^\s*;/) {
2373 ERROR("while should follow close brace '}'\n" . $hereprev);
2377 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2378 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2379 # print "No studly caps, use _\n";
2380 # print "$herecurr";
2381 # $clean = 0;
2384 #no spaces allowed after \ in define
2385 if ($line=~/\#\s*define.*\\\s$/) {
2386 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
2389 # multi-statement macros should be enclosed in a do while loop, grab the
2390 # first statement and ensure its the whole macro if its not enclosed
2391 # in a known good container
2392 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2393 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2394 my $ln = $linenr;
2395 my $cnt = $realcnt;
2396 my ($off, $dstat, $dcond, $rest);
2397 my $ctx = '';
2399 my $args = defined($1);
2401 # Find the end of the macro and limit our statement
2402 # search to that.
2403 while ($cnt > 0 && defined $lines[$ln - 1] &&
2404 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2406 $ctx .= $rawlines[$ln - 1] . "\n";
2407 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2408 $ln++;
2410 $ctx .= $rawlines[$ln - 1];
2412 ($dstat, $dcond, $ln, $cnt, $off) =
2413 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2414 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2415 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2417 # Extract the remainder of the define (if any) and
2418 # rip off surrounding spaces, and trailing \'s.
2419 $rest = '';
2420 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2421 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2422 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2423 $rest .= substr($lines[$ln - 1], $off) . "\n";
2424 $cnt--;
2426 $ln++;
2427 $off = 0;
2429 $rest =~ s/\\\n.//g;
2430 $rest =~ s/^\s*//s;
2431 $rest =~ s/\s*$//s;
2433 # Clean up the original statement.
2434 if ($args) {
2435 substr($dstat, 0, length($dcond), '');
2436 } else {
2437 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2439 $dstat =~ s/$;//g;
2440 $dstat =~ s/\\\n.//g;
2441 $dstat =~ s/^\s*//s;
2442 $dstat =~ s/\s*$//s;
2444 # Flatten any parentheses and braces
2445 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2446 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2447 $dstat =~ s/\[[^\{\}]*\]/1/)
2451 my $exceptions = qr{
2452 $Declare|
2453 module_param_named|
2454 MODULE_PARAM_DESC|
2455 DECLARE_PER_CPU|
2456 DEFINE_PER_CPU|
2457 __typeof__\(|
2458 union|
2459 struct|
2460 \.$Ident\s*=\s*|
2461 ^\"|\"$
2463 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2464 if ($rest ne '' && $rest ne ',') {
2465 if ($rest !~ /while\s*\(/ &&
2466 $dstat !~ /$exceptions/)
2468 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2471 } elsif ($ctx !~ /;/) {
2472 if ($dstat ne '' &&
2473 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2474 $dstat !~ /$exceptions/ &&
2475 $dstat !~ /^\.$Ident\s*=/ &&
2476 $dstat =~ /$Operators/)
2478 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2483 # check for missing bracing around if etc
2484 if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
2485 $line !~ /\#\s*if/) {
2486 my $allowed = 0;
2488 # Check the pre-context.
2489 if ($line =~ /(\}.*?)$/) {
2490 my $pre = $1;
2492 if ($line !~ /else/) {
2493 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2494 if $dbg_adv_apw;
2495 $allowed = 1;
2498 my ($level, $endln, @chunks) =
2499 ctx_statement_full($linenr, $realcnt, 1);
2500 if ($dbg_adv_apw) {
2501 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2502 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2503 if $#chunks >= 1;
2505 if ($#chunks >= 0 && $level == 0) {
2506 my $seen = 0;
2507 my $herectx = $here . "\n";
2508 my $ln = $linenr - 1;
2509 for my $chunk (@chunks) {
2510 my ($cond, $block) = @{$chunk};
2512 # If the condition carries leading newlines, then count those as offsets.
2513 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2514 my $offset = statement_rawlines($whitespace) - 1;
2516 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2518 # We have looked at and allowed this specific line.
2519 $suppress_ifbraces{$ln + $offset} = 1;
2521 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2522 $ln += statement_rawlines($block) - 1;
2524 substr($block, 0, length($cond), '');
2526 my $spaced_block = $block;
2527 $spaced_block =~ s/\n\+/ /g;
2529 $seen++ if ($spaced_block =~ /^\s*\{/);
2531 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2532 if $dbg_adv_apw;
2533 if (statement_lines($cond) > 1) {
2534 print "APW: ALLOWED: cond<$cond>\n"
2535 if $dbg_adv_apw;
2536 $allowed = 1;
2538 if ($block =~/\b(?:if|for|while)\b/) {
2539 print "APW: ALLOWED: block<$block>\n"
2540 if $dbg_adv_apw;
2541 $allowed = 1;
2543 if (statement_block_size($block) > 1) {
2544 print "APW: ALLOWED: lines block<$block>\n"
2545 if $dbg_adv_apw;
2546 $allowed = 1;
2549 if ($seen != ($#chunks + 1) && !$allowed) {
2550 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
2554 if (!defined $suppress_ifbraces{$linenr - 1} &&
2555 $line =~ /\b(if|while|for|else)\b/ &&
2556 $line !~ /\#\s*if/ &&
2557 $line !~ /\#\s*else/) {
2558 my $allowed = 0;
2560 # Check the pre-context.
2561 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2562 my $pre = $1;
2564 if ($line !~ /else/) {
2565 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2566 if $dbg_adv_apw;
2567 $allowed = 1;
2571 my ($level, $endln, @chunks) =
2572 ctx_statement_full($linenr, $realcnt, $-[0]);
2574 # Check the condition.
2575 my ($cond, $block) = @{$chunks[0]};
2576 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2577 if $dbg_adv_checking;
2578 if (defined $cond) {
2579 substr($block, 0, length($cond), '');
2581 if (statement_lines($cond) > 1) {
2582 print "APW: ALLOWED: cond<$cond>\n"
2583 if $dbg_adv_apw;
2584 $allowed = 1;
2586 if ($block =~/\b(?:if|for|while)\b/) {
2587 print "APW: ALLOWED: block<$block>\n"
2588 if $dbg_adv_apw;
2589 $allowed = 1;
2591 if (statement_block_size($block) > 1) {
2592 print "APW: ALLOWED: lines block<$block>\n"
2593 if $dbg_adv_apw;
2594 $allowed = 1;
2596 # Check the post-context.
2597 if (defined $chunks[1]) {
2598 my ($cond, $block) = @{$chunks[1]};
2599 if (defined $cond) {
2600 substr($block, 0, length($cond), '');
2602 if ($block =~ /^\s*\{/) {
2603 print "APW: ALLOWED: chunk-1 block<$block>\n"
2604 if $dbg_adv_apw;
2605 $allowed = 1;
2608 print "DCS: level=$level block<$block> allowed=$allowed\n"
2609 if $dbg_adv_dcs;
2610 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
2611 my $herectx = $here . "\n";;
2612 my $cnt = statement_rawlines($block);
2614 for (my $n = 0; $n < $cnt; $n++) {
2615 $herectx .= raw_line($linenr, $n) . "\n";;
2618 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
2622 # no volatiles please
2623 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2624 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ &&
2625 $line !~ /sig_atomic_t/ &&
2626 !ctx_has_comment($first_line, $linenr)) {
2627 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr;
2628 ERROR($msg);
2631 # warn about #if 0
2632 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2633 ERROR("if this code is redundant consider removing it\n" .
2634 $herecurr);
2637 # check for needless g_free() checks
2638 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2639 my $expr = $1;
2640 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
2641 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
2645 # warn about #ifdefs in C files
2646 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2647 # print "#ifdef in C files should be avoided\n";
2648 # print "$herecurr";
2649 # $clean = 0;
2652 # warn about spacing in #ifdefs
2653 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2654 ERROR("exactly one space required after that #$1\n" . $herecurr);
2656 # check for memory barriers without a comment.
2657 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2658 if (!ctx_has_comment($first_line, $linenr)) {
2659 ERROR("memory barrier without comment\n" . $herecurr);
2662 # check of hardware specific defines
2663 # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2664 # where they might be necessary.
2665 if ($line =~ m@^.\s*\#\s*if.*\b__@) {
2666 WARN("architecture specific defines should be avoided\n" . $herecurr);
2669 # Check that the storage class is at the beginning of a declaration
2670 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2671 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
2674 # check the location of the inline attribute, that it is between
2675 # storage class and type.
2676 if ($line =~ /\b$Type\s+$Inline\b/ ||
2677 $line =~ /\b$Inline\s+$Storage\b/) {
2678 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2681 # check for sizeof(&)
2682 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2683 ERROR("sizeof(& should be avoided\n" . $herecurr);
2686 # check for new externs in .c files.
2687 if ($realfile =~ /\.c$/ && defined $stat &&
2688 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2690 my $function_name = $1;
2691 my $paren_space = $2;
2693 my $s = $stat;
2694 if (defined $cond) {
2695 substr($s, 0, length($cond), '');
2697 if ($s =~ /^\s*;/ &&
2698 $function_name ne 'uninitialized_var')
2700 ERROR("externs should be avoided in .c files\n" . $herecurr);
2703 if ($paren_space =~ /\n/) {
2704 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
2707 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2708 $stat =~ /^.\s*extern\s+/)
2710 ERROR("externs should be avoided in .c files\n" . $herecurr);
2713 # check for pointless casting of g_malloc return
2714 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2715 if ($2 == 'm') {
2716 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
2717 } else {
2718 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
2722 # check for gcc specific __FUNCTION__
2723 if ($line =~ /__FUNCTION__/) {
2724 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2727 # recommend g_path_get_* over g_strdup(basename/dirname(...))
2728 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) {
2729 WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
2732 # recommend qemu_strto* over strto* for numeric conversions
2733 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
2734 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
2736 # recommend sigaction over signal for portability, when establishing a handler
2737 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2738 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2740 # check for module_init(), use category-specific init macros explicitly please
2741 if ($line =~ /^module_init\s*\(/) {
2742 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
2744 # check for various ops structs, ensure they are const.
2745 my $struct_ops = qr{AIOCBInfo|
2746 BdrvActionOps|
2747 BlockDevOps|
2748 BlockJobDriver|
2749 DisplayChangeListenerOps|
2750 GraphicHwOps|
2751 IDEDMAOps|
2752 KVMCapabilityInfo|
2753 MemoryRegionIOMMUOps|
2754 MemoryRegionOps|
2755 MemoryRegionPortio|
2756 QEMUFileOps|
2757 SCSIBusInfo|
2758 SCSIReqOps|
2759 Spice[A-Z][a-zA-Z0-9]*Interface|
2760 USBDesc[A-Z][a-zA-Z0-9]*|
2761 VhostOps|
2762 VMStateDescription|
2763 VMStateInfo}x;
2764 if ($line !~ /\bconst\b/ &&
2765 $line =~ /\b($struct_ops)\b.*=/) {
2766 ERROR("initializer for struct $1 should normally be const\n" .
2767 $herecurr);
2770 # check for %L{u,d,i} in strings
2771 my $string;
2772 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2773 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2774 $string =~ s/%%/__/g;
2775 if ($string =~ /(?<!%)%L[udi]/) {
2776 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2777 last;
2781 # QEMU specific tests
2782 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
2783 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
2786 # Qemu error function tests
2788 # Find newlines in error messages
2789 my $qemu_error_funcs = qr{error_setg|
2790 error_setg_errno|
2791 error_setg_win32|
2792 error_setg_file_open|
2793 error_set|
2794 error_prepend|
2795 warn_reportf_err|
2796 error_reportf_err|
2797 error_vreport|
2798 warn_vreport|
2799 info_vreport|
2800 error_report|
2801 warn_report|
2802 info_report|
2803 g_test_message}x;
2805 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
2806 ERROR("Error messages should not contain newlines\n" . $herecurr);
2809 # Continue checking for error messages that contains newlines. This
2810 # check handles cases where string literals are spread over multiple lines.
2811 # Example:
2812 # error_report("Error msg line #1"
2813 # "Error msg line #2\n");
2814 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2815 my $continued_str_literal = qr{\+\s*\".*\"};
2817 if ($rawline =~ /$quoted_newline_regex/) {
2818 # Backtrack to first line that does not contain only a quoted literal
2819 # and assume that it is the start of the statement.
2820 my $i = $linenr - 2;
2822 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2823 $i--;
2826 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
2827 ERROR("Error messages should not contain newlines\n" . $herecurr);
2831 # check for non-portable libc calls that have portable alternatives in QEMU
2832 if ($line =~ /\bffs\(/) {
2833 ERROR("use ctz32() instead of ffs()\n" . $herecurr);
2835 if ($line =~ /\bffsl\(/) {
2836 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2838 if ($line =~ /\bffsll\(/) {
2839 ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
2841 if ($line =~ /\bbzero\(/) {
2842 ERROR("use memset() instead of bzero()\n" . $herecurr);
2844 my $non_exit_glib_asserts = qr{g_assert_cmpstr|
2845 g_assert_cmpint|
2846 g_assert_cmpuint|
2847 g_assert_cmphex|
2848 g_assert_cmpfloat|
2849 g_assert_true|
2850 g_assert_false|
2851 g_assert_nonnull|
2852 g_assert_null|
2853 g_assert_no_error|
2854 g_assert_error|
2855 g_test_assert_expected_messages|
2856 g_test_trap_assert_passed|
2857 g_test_trap_assert_stdout|
2858 g_test_trap_assert_stdout_unmatched|
2859 g_test_trap_assert_stderr|
2860 g_test_trap_assert_stderr_unmatched}x;
2861 if ($realfile !~ /^tests\// &&
2862 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2863 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
2867 # If we have no input at all, then there is nothing to report on
2868 # so just keep quiet.
2869 if ($#rawlines == -1) {
2870 exit(0);
2873 # In mailback mode only produce a report in the negative, for
2874 # things that appear to be patches.
2875 if ($mailback && ($clean == 1 || !$is_patch)) {
2876 exit(0);
2879 # This is not a patch, and we are are in 'no-patch' mode so
2880 # just keep quiet.
2881 if (!$chk_patch && !$is_patch) {
2882 exit(0);
2885 if (!$is_patch) {
2886 ERROR("Does not appear to be a unified-diff format patch\n");
2888 if ($is_patch && $chk_signoff && $signoff == 0) {
2889 ERROR("Missing Signed-off-by: line(s)\n");
2892 print report_dump();
2893 if ($summary && !($clean == 1 && $quiet == 1)) {
2894 print "$filename " if ($summary_file);
2895 print "total: $cnt_error errors, $cnt_warn warnings, " .
2896 "$cnt_lines lines checked\n";
2897 print "\n" if ($quiet == 0);
2900 if ($quiet == 0) {
2901 # If there were whitespace errors which cleanpatch can fix
2902 # then suggest that.
2903 # if ($rpt_cleaners) {
2904 # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2905 # print " scripts/cleanfile\n\n";
2909 if ($clean == 1 && $quiet == 0) {
2910 print "$vname has no obvious style problems and is ready for submission.\n"
2912 if ($clean == 0 && $quiet == 0) {
2913 print "$vname has style problems, please review. If any of these errors\n";
2914 print "are false positives report them to the maintainer, see\n";
2915 print "CHECKPATCH in MAINTAINERS.\n";
2918 return ($no_warnings ? $clean : $cnt_error == 0);