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 7 8use strict; 9use warnings; 10use Term::ANSIColor qw(:constants); 11 12my $P = $0; 13$P =~ s@.*/@@g; 14 15our $SrcFile = qr{\.(?:(h|c)(\.inc)?|cpp|s|S|pl|py|sh)$}; 16 17my $V = '0.31'; 18 19use Getopt::Long qw(:config no_auto_abbrev); 20 21my $quiet = 0; 22my $tree = 1; 23my $chk_signoff = 1; 24my $chk_patch = undef; 25my $chk_branch = undef; 26my $tst_only; 27my $emacs = 0; 28my $terse = 0; 29my $file = undef; 30my $color = "auto"; 31my $no_warnings = 0; 32my $summary = 1; 33my $mailback = 0; 34my $summary_file = 0; 35my $root; 36my %debug; 37my $help = 0; 38my $codespell = 0; 39my $codespellfile = "/usr/share/codespell/dictionary.txt"; 40my $user_codespellfile = ""; 41 42sub help { 43 my ($exitcode) = @_; 44 45 print << "EOM"; 46Usage: 47 48 $P [OPTION]... [FILE]... 49 $P [OPTION]... [GIT-REV-LIST] 50 51Version: $V 52 53Options: 54 -q, --quiet quiet 55 --no-tree run without a qemu tree 56 --no-signoff do not check for 'Signed-off-by' line 57 --patch treat FILE as patchfile 58 --branch treat args as GIT revision list 59 --emacs emacs compile window format 60 --terse one line per report 61 -f, --file treat FILE as regular source file 62 --strict fail if only warnings are found 63 --root=PATH PATH to the qemu tree root 64 --no-summary suppress the per-file summary 65 --mailback only produce a report in case of warnings/errors 66 --summary-file include the filename in summary 67 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 68 'values', 'possible', 'type', and 'attr' (default 69 is all off) 70 --test-only=WORD report only warnings/errors containing WORD 71 literally 72 --codespell Use the codespell dictionary for spelling/typos 73 (default: $codespellfile) 74 --codespellfile Use this codespell dictionary 75 --color[=WHEN] Use colors 'always', 'never', or only when output 76 is a terminal ('auto'). Default is 'auto'. 77 -h, --help, --version display this help and exit 78 79When FILE is - read standard input. 80EOM 81 82 exit($exitcode); 83} 84 85# Perl's Getopt::Long allows options to take optional arguments after a space. 86# Prevent --color by itself from consuming other arguments 87foreach (@ARGV) { 88 if ($_ eq "--color" || $_ eq "-color") { 89 $_ = "--color=$color"; 90 } 91} 92 93GetOptions( 94 'q|quiet+' => \$quiet, 95 'tree!' => \$tree, 96 'signoff!' => \$chk_signoff, 97 'patch!' => \$chk_patch, 98 'branch!' => \$chk_branch, 99 'emacs!' => \$emacs, 100 'terse!' => \$terse, 101 'f|file!' => \$file, 102 'strict!' => \$no_warnings, 103 'root=s' => \$root, 104 'summary!' => \$summary, 105 'mailback!' => \$mailback, 106 'summary-file!' => \$summary_file, 107 'debug=s' => \%debug, 108 'test-only=s' => \$tst_only, 109 'codespell!' => \$codespell, 110 'codespellfile=s' => \$user_codespellfile, 111 'color=s' => \$color, 112 'no-color' => sub { $color = 'never'; }, 113 'h|help' => \$help, 114 'version' => \$help 115) or help(1); 116 117if ($user_codespellfile) { 118 # Use the user provided codespell file unconditionally 119 $codespellfile = $user_codespellfile; 120} elsif (!(-f $codespellfile)) { 121 # If /usr/share/codespell/dictionary.txt is not present, try to find it 122 # under codespell's install directory: <codespell_root>/data/dictionary.txt 123 if (($codespell || $help) && which("python3") ne "") { 124 my $python_codespell_dict = << "EOF"; 125 126import os.path as op 127import codespell_lib 128codespell_dir = op.dirname(codespell_lib.__file__) 129codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt') 130print(codespell_file, end='') 131EOF 132 133 my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`; 134 $codespellfile = $codespell_dict if (-f $codespell_dict); 135 } 136} 137 138help(0) if ($help); 139 140my $exit = 0; 141 142if ($#ARGV < 0) { 143 print "$P: no input files\n"; 144 exit(1); 145} 146 147if (!defined $chk_branch && !defined $chk_patch && !defined $file) { 148 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 149 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; 150 $chk_patch = $chk_branch || $file ? 0 : 1; 151} elsif (!defined $chk_branch && !defined $chk_patch) { 152 if ($file) { 153 $chk_branch = $chk_patch = 0; 154 } else { 155 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 156 $chk_patch = $chk_branch ? 0 : 1; 157 } 158} elsif (!defined $chk_branch && !defined $file) { 159 if ($chk_patch) { 160 $chk_branch = $file = 0; 161 } else { 162 $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; 163 $file = $chk_branch ? 0 : 1; 164 } 165} elsif (!defined $chk_patch && !defined $file) { 166 if ($chk_branch) { 167 $chk_patch = $file = 0; 168 } else { 169 $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; 170 $chk_patch = $file ? 0 : 1; 171 } 172} elsif (!defined $chk_branch) { 173 $chk_branch = $chk_patch || $file ? 0 : 1; 174} elsif (!defined $chk_patch) { 175 $chk_patch = $chk_branch || $file ? 0 : 1; 176} elsif (!defined $file) { 177 $file = $chk_patch || $chk_branch ? 0 : 1; 178} 179 180if (($chk_patch && $chk_branch) || 181 ($chk_patch && $file) || 182 ($chk_branch && $file)) { 183 die "Only one of --file, --branch, --patch is permitted\n"; 184} 185if (!$chk_patch && !$chk_branch && !$file) { 186 die "One of --file, --branch, --patch is required\n"; 187} 188 189if ($color =~ /^always$/i) { 190 $color = 1; 191} elsif ($color =~ /^never$/i) { 192 $color = 0; 193} elsif ($color =~ /^auto$/i) { 194 $color = (-t STDOUT); 195} else { 196 die "Invalid color mode: $color\n"; 197} 198 199my $dbg_values = 0; 200my $dbg_possible = 0; 201my $dbg_type = 0; 202my $dbg_attr = 0; 203my $dbg_adv_dcs = 0; 204my $dbg_adv_checking = 0; 205my $dbg_adv_apw = 0; 206for my $key (keys %debug) { 207 ## no critic 208 eval "\${dbg_$key} = '$debug{$key}';"; 209 die "$@" if ($@); 210} 211 212my $rpt_cleaners = 0; 213 214if ($terse) { 215 $emacs = 1; 216 $quiet++; 217} 218 219if ($tree) { 220 if (defined $root) { 221 if (!top_of_kernel_tree($root)) { 222 die "$P: $root: --root does not point at a valid tree\n"; 223 } 224 } else { 225 if (top_of_kernel_tree('.')) { 226 $root = '.'; 227 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 228 top_of_kernel_tree($1)) { 229 $root = $1; 230 } 231 } 232 233 if (!defined $root) { 234 print "Must be run from the top-level dir. of a qemu tree\n"; 235 exit(2); 236 } 237} 238 239my $emitted_corrupt = 0; 240 241our $Ident = qr{ 242 [A-Za-z_][A-Za-z\d_]* 243 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 244 }x; 245our $Storage = qr{extern|static|asmlinkage}; 246our $Sparse = qr{ 247 __force 248 }x; 249 250# Notes to $Attribute: 251our $Attribute = qr{ 252 const| 253 volatile| 254 G_NORETURN| 255 G_GNUC_WARN_UNUSED_RESULT| 256 G_GNUC_NULL_TERMINATED| 257 QEMU_PACKED| 258 G_GNUC_PRINTF 259 }x; 260our $Modifier; 261our $Inline = qr{inline}; 262our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 263our $Lval = qr{$Ident(?:$Member)*}; 264 265our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 266our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 267our $Compare = qr{<=|>=|==|!=|<|>}; 268our $Operators = qr{ 269 <=|>=|==|!=| 270 =>|->|<<|>>|<|>|!|~| 271 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 272 }x; 273 274our $NonptrType; 275our $Type; 276our $Declare; 277 278our $NON_ASCII_UTF8 = qr{ 279 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 280 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 281 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 282 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 283 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 284 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 285 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 286}x; 287 288our $UTF8 = qr{ 289 [\x09\x0A\x0D\x20-\x7E] # ASCII 290 | $NON_ASCII_UTF8 291}x; 292 293# some readers default to ISO-8859-1 when showing email source. detect 294# when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back. 295# False positives are possible but very unlikely. 296our $UTF8_MOJIBAKE = qr{ 297 \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf 298 | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf 299 | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf 300 | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf 301 | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf 302 | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf 303 | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf 304}x; 305 306# There are still some false positives, but this catches most 307# common cases. 308our $typeTypedefs = qr{(?x: 309 (?![KMGTPE]iB) # IEC binary prefix (do not match) 310 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase 311 | [A-Z][A-Z\d_]*AIOCB # all uppercase 312 | [A-Z][A-Z\d_]*CPU # all uppercase 313 | QEMUBH # all uppercase 314)}; 315 316our @typeList = ( 317 qr{void}, 318 qr{(?:unsigned\s+)?char}, 319 qr{(?:unsigned\s+)?short}, 320 qr{(?:unsigned\s+)?int}, 321 qr{(?:unsigned\s+)?long}, 322 qr{(?:unsigned\s+)?long\s+int}, 323 qr{(?:unsigned\s+)?long\s+long}, 324 qr{(?:unsigned\s+)?long\s+long\s+int}, 325 qr{unsigned}, 326 qr{float}, 327 qr{double}, 328 qr{bool}, 329 qr{struct\s+$Ident}, 330 qr{union\s+$Ident}, 331 qr{enum\s+$Ident}, 332 qr{${Ident}_t}, 333 qr{${Ident}_handler}, 334 qr{${Ident}_handler_fn}, 335 qr{target_(?:u)?long}, 336 qr{hwaddr}, 337 # external libraries 338 qr{xen\w+_handle}, 339 # Glib definitions 340 qr{gchar}, 341 qr{gshort}, 342 qr{glong}, 343 qr{gint}, 344 qr{gboolean}, 345 qr{guchar}, 346 qr{gushort}, 347 qr{gulong}, 348 qr{guint}, 349 qr{gfloat}, 350 qr{gdouble}, 351 qr{gpointer}, 352 qr{gconstpointer}, 353 qr{gint8}, 354 qr{guint8}, 355 qr{gint16}, 356 qr{guint16}, 357 qr{gint32}, 358 qr{guint32}, 359 qr{gint64}, 360 qr{guint64}, 361 qr{gsize}, 362 qr{gssize}, 363 qr{goffset}, 364 qr{gintptr}, 365 qr{guintptr}, 366); 367 368# Load common spelling mistakes and build regular expression list. 369my $misspellings; 370my %spelling_fix; 371 372if ($codespell) { 373 if (open(my $spelling, '<', $codespellfile)) { 374 while (<$spelling>) { 375 my $line = $_; 376 377 $line =~ s/\s*\n?$//g; 378 $line =~ s/^\s*//g; 379 380 next if ($line =~ m/^\s*#/); 381 next if ($line =~ m/^\s*$/); 382 next if ($line =~ m/, disabled/i); 383 384 $line =~ s/,.*$//; 385 386 my ($suspect, $fix) = split(/->/, $line); 387 388 $spelling_fix{$suspect} = $fix; 389 } 390 close($spelling); 391 } else { 392 warn "No codespell typos will be found - file '$codespellfile': $!\n"; 393 } 394} 395 396$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 397 398# This can be modified by sub possible. Since it can be empty, be careful 399# about regexes that always match, because they can cause infinite loops. 400our @modifierList = ( 401); 402 403sub build_types { 404 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 405 if (@modifierList > 0) { 406 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 407 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 408 } else { 409 $Modifier = qr{(?:$Attribute|$Sparse)}; 410 } 411 $NonptrType = qr{ 412 (?:$Modifier\s+|const\s+)* 413 (?: 414 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 415 (?:$typeTypedefs\b)| 416 (?:${all}\b) 417 ) 418 (?:\s+$Modifier|\s+const)* 419 }x; 420 $Type = qr{ 421 $NonptrType 422 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 423 (?:\s+$Inline|\s+$Modifier)* 424 }x; 425 $Declare = qr{(?:$Storage\s+)?$Type}; 426} 427build_types(); 428 429$chk_signoff = 0 if ($file); 430 431my @rawlines = (); 432my @lines = (); 433my $vname; 434if ($chk_branch) { 435 my @patches; 436 my %git_commits = (); 437 my $HASH; 438 open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--no-mailmap", "--format=%H %s", $ARGV[0]) || 439 die "$P: git log --reverse --no-merges --no-mailmap --format='%H %s' $ARGV[0] failed - $!\n"; 440 441 for my $line (<$HASH>) { 442 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 443 next if (!defined($1) || !defined($2)); 444 my $sha1 = $1; 445 my $subject = $2; 446 push(@patches, $sha1); 447 $git_commits{$sha1} = $subject; 448 } 449 450 close $HASH; 451 452 die "$P: no revisions returned for revlist '$ARGV[0]'\n" 453 unless @patches; 454 455 my $i = 1; 456 my $num_patches = @patches; 457 for my $hash (@patches) { 458 my $FILE; 459 open($FILE, '-|', "git", 460 "-c", "diff.renamelimit=0", 461 "-c", "diff.renames=True", 462 "-c", "diff.algorithm=histogram", 463 "show", "--no-mailmap", 464 "--patch-with-stat", $hash) || 465 die "$P: git show $hash - $!\n"; 466 while (<$FILE>) { 467 chomp; 468 push(@rawlines, $_); 469 } 470 close($FILE); 471 $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')'; 472 if ($num_patches > 1 && $quiet == 0) { 473 my $prefix = "$i/$num_patches"; 474 $prefix = BLUE . BOLD . $prefix . RESET if $color; 475 print "$prefix Checking commit $vname\n"; 476 $vname = "Patch $i/$num_patches"; 477 } else { 478 $vname = "Commit " . $vname; 479 } 480 if (!process($hash)) { 481 $exit = 1; 482 print "\n" if ($num_patches > 1 && $quiet == 0); 483 } 484 @rawlines = (); 485 @lines = (); 486 $i++; 487 } 488} else { 489 for my $filename (@ARGV) { 490 my $FILE; 491 if ($file) { 492 open($FILE, '-|', "diff -u /dev/null $filename") || 493 die "$P: $filename: diff failed - $!\n"; 494 } elsif ($filename eq '-') { 495 open($FILE, '<&STDIN'); 496 } else { 497 open($FILE, '<', "$filename") || 498 die "$P: $filename: open failed - $!\n"; 499 } 500 if ($filename eq '-') { 501 $vname = 'Your patch'; 502 } else { 503 $vname = $filename; 504 } 505 print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0; 506 while (<$FILE>) { 507 chomp; 508 push(@rawlines, $_); 509 } 510 close($FILE); 511 if (!process($filename)) { 512 $exit = 1; 513 } 514 @rawlines = (); 515 @lines = (); 516 } 517} 518 519exit($exit); 520 521sub top_of_kernel_tree { 522 my ($root) = @_; 523 524 my @tree_check = ( 525 "COPYING", "MAINTAINERS", "Makefile", 526 "README.rst", "docs", "VERSION", 527 "linux-user", "system" 528 ); 529 530 foreach my $check (@tree_check) { 531 if (! -e $root . '/' . $check) { 532 return 0; 533 } 534 } 535 return 1; 536} 537 538sub which { 539 my ($bin) = @_; 540 541 foreach my $path (split(/:/, $ENV{PATH})) { 542 if (-e "$path/$bin") { 543 return "$path/$bin"; 544 } 545 } 546 547 return ""; 548} 549 550sub expand_tabs { 551 my ($str) = @_; 552 553 my $res = ''; 554 my $n = 0; 555 for my $c (split(//, $str)) { 556 if ($c eq "\t") { 557 $res .= ' '; 558 $n++; 559 for (; ($n % 8) != 0; $n++) { 560 $res .= ' '; 561 } 562 next; 563 } 564 $res .= $c; 565 $n++; 566 } 567 568 return $res; 569} 570sub copy_spacing { 571 (my $res = shift) =~ tr/\t/ /c; 572 return $res; 573} 574 575sub line_stats { 576 my ($line) = @_; 577 578 # Drop the diff line leader and expand tabs 579 $line =~ s/^.//; 580 $line = expand_tabs($line); 581 582 # Pick the indent from the front of the line. 583 my ($white) = ($line =~ /^(\s*)/); 584 585 return (length($line), length($white)); 586} 587 588my $sanitise_quote = ''; 589 590sub sanitise_line_reset { 591 my ($in_comment) = @_; 592 593 if ($in_comment) { 594 $sanitise_quote = '*/'; 595 } else { 596 $sanitise_quote = ''; 597 } 598} 599sub sanitise_line { 600 my ($line) = @_; 601 602 my $res = ''; 603 my $l = ''; 604 605 my $qlen = 0; 606 my $off = 0; 607 my $c; 608 609 # Always copy over the diff marker. 610 $res = substr($line, 0, 1); 611 612 for ($off = 1; $off < length($line); $off++) { 613 $c = substr($line, $off, 1); 614 615 # Comments we are wacking completely including the begin 616 # and end, all to $;. 617 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 618 $sanitise_quote = '*/'; 619 620 substr($res, $off, 2, "$;$;"); 621 $off++; 622 next; 623 } 624 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 625 $sanitise_quote = ''; 626 substr($res, $off, 2, "$;$;"); 627 $off++; 628 next; 629 } 630 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 631 $sanitise_quote = '//'; 632 633 substr($res, $off, 2, $sanitise_quote); 634 $off++; 635 next; 636 } 637 638 # A \ in a string means ignore the next character. 639 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 640 $c eq "\\") { 641 substr($res, $off, 2, 'XX'); 642 $off++; 643 next; 644 } 645 # Regular quotes. 646 if ($c eq "'" || $c eq '"') { 647 if ($sanitise_quote eq '') { 648 $sanitise_quote = $c; 649 650 substr($res, $off, 1, $c); 651 next; 652 } elsif ($sanitise_quote eq $c) { 653 $sanitise_quote = ''; 654 } 655 } 656 657 #print "c<$c> SQ<$sanitise_quote>\n"; 658 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 659 substr($res, $off, 1, $;); 660 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 661 substr($res, $off, 1, $;); 662 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 663 substr($res, $off, 1, 'X'); 664 } else { 665 substr($res, $off, 1, $c); 666 } 667 } 668 669 if ($sanitise_quote eq '//') { 670 $sanitise_quote = ''; 671 } 672 673 # The pathname on a #include may be surrounded by '<' and '>'. 674 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 675 my $clean = 'X' x length($1); 676 $res =~ s@\<.*\>@<$clean>@; 677 678 # The whole of a #error is a string. 679 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 680 my $clean = 'X' x length($1); 681 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 682 } 683 684 return $res; 685} 686 687sub ctx_statement_block { 688 my ($linenr, $remain, $off) = @_; 689 my $line = $linenr - 1; 690 my $blk = ''; 691 my $soff = $off; 692 my $coff = $off - 1; 693 my $coff_set = 0; 694 695 my $loff = 0; 696 697 my $type = ''; 698 my $level = 0; 699 my @stack = (); 700 my $p; 701 my $c; 702 my $len = 0; 703 704 my $remainder; 705 while (1) { 706 @stack = (['', 0]) if ($#stack == -1); 707 708 #warn "CSB: blk<$blk> remain<$remain>\n"; 709 # If we are about to drop off the end, pull in more 710 # context. 711 if ($off >= $len) { 712 for (; $remain > 0; $line++) { 713 last if (!defined $lines[$line]); 714 next if ($lines[$line] =~ /^-/); 715 $remain--; 716 $loff = $len; 717 $blk .= $lines[$line] . "\n"; 718 $len = length($blk); 719 $line++; 720 last; 721 } 722 # Bail if there is no further context. 723 #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 724 if ($off >= $len) { 725 last; 726 } 727 } 728 $p = $c; 729 $c = substr($blk, $off, 1); 730 $remainder = substr($blk, $off); 731 732 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 733 734 # Handle nested #if/#else. 735 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 736 push(@stack, [ $type, $level ]); 737 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 738 ($type, $level) = @{$stack[$#stack - 1]}; 739 } elsif ($remainder =~ /^#\s*endif\b/) { 740 ($type, $level) = @{pop(@stack)}; 741 } 742 743 # Statement ends at the ';' or a close '}' at the 744 # outermost level. 745 if ($level == 0 && $c eq ';') { 746 last; 747 } 748 749 # An else is really a conditional as long as its not else if 750 if ($level == 0 && $coff_set == 0 && 751 (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 752 $remainder =~ /^(else)(?:\s|{)/ && 753 $remainder !~ /^else\s+if\b/) { 754 $coff = $off + length($1) - 1; 755 $coff_set = 1; 756 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 757 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 758 } 759 760 if (($type eq '' || $type eq '(') && $c eq '(') { 761 $level++; 762 $type = '('; 763 } 764 if ($type eq '(' && $c eq ')') { 765 $level--; 766 $type = ($level != 0)? '(' : ''; 767 768 if ($level == 0 && $coff < $soff) { 769 $coff = $off; 770 $coff_set = 1; 771 #warn "CSB: mark coff<$coff>\n"; 772 } 773 } 774 if (($type eq '' || $type eq '{') && $c eq '{') { 775 $level++; 776 $type = '{'; 777 } 778 if ($type eq '{' && $c eq '}') { 779 $level--; 780 $type = ($level != 0)? '{' : ''; 781 782 if ($level == 0) { 783 if (substr($blk, $off + 1, 1) eq ';') { 784 $off++; 785 } 786 last; 787 } 788 } 789 $off++; 790 } 791 # We are truly at the end, so shuffle to the next line. 792 if ($off == $len) { 793 $loff = $len + 1; 794 $line++; 795 $remain--; 796 } 797 798 my $statement = substr($blk, $soff, $off - $soff + 1); 799 my $condition = substr($blk, $soff, $coff - $soff + 1); 800 801 #warn "STATEMENT<$statement>\n"; 802 #warn "CONDITION<$condition>\n"; 803 804 #print "coff<$coff> soff<$off> loff<$loff>\n"; 805 806 return ($statement, $condition, 807 $line, $remain + 1, $off - $loff + 1, $level); 808} 809 810sub statement_lines { 811 my ($stmt) = @_; 812 813 # Strip the diff line prefixes and rip blank lines at start and end. 814 $stmt =~ s/(^|\n)./$1/g; 815 $stmt =~ s/^\s*//; 816 $stmt =~ s/\s*$//; 817 818 my @stmt_lines = ($stmt =~ /\n/g); 819 820 return $#stmt_lines + 2; 821} 822 823sub statement_rawlines { 824 my ($stmt) = @_; 825 826 my @stmt_lines = ($stmt =~ /\n/g); 827 828 return $#stmt_lines + 2; 829} 830 831sub statement_block_size { 832 my ($stmt) = @_; 833 834 $stmt =~ s/(^|\n)./$1/g; 835 $stmt =~ s/^\s*\{//; 836 $stmt =~ s/}\s*$//; 837 $stmt =~ s/^\s*//; 838 $stmt =~ s/\s*$//; 839 840 my @stmt_lines = ($stmt =~ /\n/g); 841 my @stmt_statements = ($stmt =~ /;/g); 842 843 my $stmt_lines = $#stmt_lines + 2; 844 my $stmt_statements = $#stmt_statements + 1; 845 846 if ($stmt_lines > $stmt_statements) { 847 return $stmt_lines; 848 } else { 849 return $stmt_statements; 850 } 851} 852 853sub ctx_statement_full { 854 my ($linenr, $remain, $off) = @_; 855 my ($statement, $condition, $level); 856 857 my (@chunks); 858 859 # Grab the first conditional/block pair. 860 ($statement, $condition, $linenr, $remain, $off, $level) = 861 ctx_statement_block($linenr, $remain, $off); 862 #print "F: c<$condition> s<$statement> remain<$remain>\n"; 863 push(@chunks, [ $condition, $statement ]); 864 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 865 return ($level, $linenr, @chunks); 866 } 867 868 # Pull in the following conditional/block pairs and see if they 869 # could continue the statement. 870 for (;;) { 871 ($statement, $condition, $linenr, $remain, $off, $level) = 872 ctx_statement_block($linenr, $remain, $off); 873 #print "C: c<$condition> s<$statement> remain<$remain>\n"; 874 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 875 #print "C: push\n"; 876 push(@chunks, [ $condition, $statement ]); 877 } 878 879 return ($level, $linenr, @chunks); 880} 881 882sub ctx_block_get { 883 my ($linenr, $remain, $outer, $open, $close, $off) = @_; 884 my $line; 885 my $start = $linenr - 1; 886 my $blk = ''; 887 my @o; 888 my @c; 889 my @res = (); 890 891 my $level = 0; 892 my @stack = ($level); 893 for ($line = $start; $remain > 0; $line++) { 894 next if ($rawlines[$line] =~ /^-/); 895 $remain--; 896 897 $blk .= $rawlines[$line]; 898 899 # Handle nested #if/#else. 900 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 901 push(@stack, $level); 902 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 903 $level = $stack[$#stack - 1]; 904 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 905 $level = pop(@stack); 906 } 907 908 foreach my $c (split(//, $lines[$line])) { 909 ##print "C<$c>L<$level><$open$close>O<$off>\n"; 910 if ($off > 0) { 911 $off--; 912 next; 913 } 914 915 if ($c eq $close && $level > 0) { 916 $level--; 917 last if ($level == 0); 918 } elsif ($c eq $open) { 919 $level++; 920 } 921 } 922 923 if (!$outer || $level <= 1) { 924 push(@res, $rawlines[$line]); 925 } 926 927 last if ($level == 0); 928 } 929 930 return ($level, @res); 931} 932sub ctx_block_outer { 933 my ($linenr, $remain) = @_; 934 935 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 936 return @r; 937} 938sub ctx_block { 939 my ($linenr, $remain) = @_; 940 941 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 942 return @r; 943} 944sub ctx_statement { 945 my ($linenr, $remain, $off) = @_; 946 947 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 948 return @r; 949} 950sub ctx_block_level { 951 my ($linenr, $remain) = @_; 952 953 return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 954} 955sub ctx_statement_level { 956 my ($linenr, $remain, $off) = @_; 957 958 return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 959} 960 961sub ctx_locate_comment { 962 my ($first_line, $end_line) = @_; 963 964 # Catch a comment on the end of the line itself. 965 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 966 return $current_comment if (defined $current_comment); 967 968 # Look through the context and try and figure out if there is a 969 # comment. 970 my $in_comment = 0; 971 $current_comment = ''; 972 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 973 my $line = $rawlines[$linenr - 1]; 974 #warn " $line\n"; 975 if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 976 $in_comment = 1; 977 } 978 if ($line =~ m@/\*@) { 979 $in_comment = 1; 980 } 981 if (!$in_comment && $current_comment ne '') { 982 $current_comment = ''; 983 } 984 $current_comment .= $line . "\n" if ($in_comment); 985 if ($line =~ m@\*/@) { 986 $in_comment = 0; 987 } 988 } 989 990 chomp($current_comment); 991 return($current_comment); 992} 993sub ctx_has_comment { 994 my ($first_line, $end_line) = @_; 995 my $cmt = ctx_locate_comment($first_line, $end_line); 996 997 ##print "LINE: $rawlines[$end_line - 1 ]\n"; 998 ##print "CMMT: $cmt\n"; 999 1000 return ($cmt ne ''); 1001} 1002 1003sub raw_line { 1004 my ($linenr, $cnt) = @_; 1005 1006 my $offset = $linenr - 1; 1007 $cnt++; 1008 1009 my $line; 1010 while ($cnt) { 1011 $line = $rawlines[$offset++]; 1012 next if (defined($line) && $line =~ /^-/); 1013 $cnt--; 1014 } 1015 1016 return $line; 1017} 1018 1019sub cat_vet { 1020 my ($vet) = @_; 1021 my ($res, $coded); 1022 1023 $res = ''; 1024 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 1025 $res .= $1; 1026 if ($2 ne '') { 1027 $coded = sprintf("^%c", unpack('C', $2) + 64); 1028 $res .= $coded; 1029 } 1030 } 1031 $res =~ s/$/\$/; 1032 1033 return $res; 1034} 1035 1036my $av_preprocessor = 0; 1037my $av_pending; 1038my @av_paren_type; 1039my $av_pend_colon; 1040 1041sub annotate_reset { 1042 $av_preprocessor = 0; 1043 $av_pending = '_'; 1044 @av_paren_type = ('E'); 1045 $av_pend_colon = 'O'; 1046} 1047 1048sub annotate_values { 1049 my ($stream, $type) = @_; 1050 1051 my $res; 1052 my $var = '_' x length($stream); 1053 my $cur = $stream; 1054 1055 print "$stream\n" if ($dbg_values > 1); 1056 1057 while (length($cur)) { 1058 @av_paren_type = ('E') if ($#av_paren_type < 0); 1059 print " <" . join('', @av_paren_type) . 1060 "> <$type> <$av_pending>" if ($dbg_values > 1); 1061 if ($cur =~ /^(\s+)/o) { 1062 print "WS($1)\n" if ($dbg_values > 1); 1063 if ($1 =~ /\n/ && $av_preprocessor) { 1064 $type = pop(@av_paren_type); 1065 $av_preprocessor = 0; 1066 } 1067 1068 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 1069 print "CAST($1)\n" if ($dbg_values > 1); 1070 push(@av_paren_type, $type); 1071 $type = 'C'; 1072 1073 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1074 print "DECLARE($1)\n" if ($dbg_values > 1); 1075 $type = 'T'; 1076 1077 } elsif ($cur =~ /^($Modifier)\s*/) { 1078 print "MODIFIER($1)\n" if ($dbg_values > 1); 1079 $type = 'T'; 1080 1081 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1082 print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1083 $av_preprocessor = 1; 1084 push(@av_paren_type, $type); 1085 if ($2 ne '') { 1086 $av_pending = 'N'; 1087 } 1088 $type = 'E'; 1089 1090 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1091 print "UNDEF($1)\n" if ($dbg_values > 1); 1092 $av_preprocessor = 1; 1093 push(@av_paren_type, $type); 1094 1095 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1096 print "PRE_START($1)\n" if ($dbg_values > 1); 1097 $av_preprocessor = 1; 1098 1099 push(@av_paren_type, $type); 1100 push(@av_paren_type, $type); 1101 $type = 'E'; 1102 1103 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1104 print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1105 $av_preprocessor = 1; 1106 1107 push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1108 1109 $type = 'E'; 1110 1111 } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1112 print "PRE_END($1)\n" if ($dbg_values > 1); 1113 1114 $av_preprocessor = 1; 1115 1116 # Assume all arms of the conditional end as this 1117 # one does, and continue as if the #endif was not here. 1118 pop(@av_paren_type); 1119 push(@av_paren_type, $type); 1120 $type = 'E'; 1121 1122 } elsif ($cur =~ /^(\\\n)/o) { 1123 print "PRECONT($1)\n" if ($dbg_values > 1); 1124 1125 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1126 print "ATTR($1)\n" if ($dbg_values > 1); 1127 $av_pending = $type; 1128 $type = 'N'; 1129 1130 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1131 print "SIZEOF($1)\n" if ($dbg_values > 1); 1132 if (defined $2) { 1133 $av_pending = 'V'; 1134 } 1135 $type = 'N'; 1136 1137 } elsif ($cur =~ /^(if|while|for)\b/o) { 1138 print "COND($1)\n" if ($dbg_values > 1); 1139 $av_pending = 'E'; 1140 $type = 'N'; 1141 1142 } elsif ($cur =~/^(case)/o) { 1143 print "CASE($1)\n" if ($dbg_values > 1); 1144 $av_pend_colon = 'C'; 1145 $type = 'N'; 1146 1147 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1148 print "KEYWORD($1)\n" if ($dbg_values > 1); 1149 $type = 'N'; 1150 1151 } elsif ($cur =~ /^(\()/o) { 1152 print "PAREN('$1')\n" if ($dbg_values > 1); 1153 push(@av_paren_type, $av_pending); 1154 $av_pending = '_'; 1155 $type = 'N'; 1156 1157 } elsif ($cur =~ /^(\))/o) { 1158 my $new_type = pop(@av_paren_type); 1159 if ($new_type ne '_') { 1160 $type = $new_type; 1161 print "PAREN('$1') -> $type\n" 1162 if ($dbg_values > 1); 1163 } else { 1164 print "PAREN('$1')\n" if ($dbg_values > 1); 1165 } 1166 1167 } elsif ($cur =~ /^($Ident)\s*\(/o) { 1168 print "FUNC($1)\n" if ($dbg_values > 1); 1169 $type = 'V'; 1170 $av_pending = 'V'; 1171 1172 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 1173 if (defined $2 && $type eq 'C' || $type eq 'T') { 1174 $av_pend_colon = 'B'; 1175 } elsif ($type eq 'E') { 1176 $av_pend_colon = 'L'; 1177 } 1178 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 1179 $type = 'V'; 1180 1181 } elsif ($cur =~ /^($Ident|$Constant)/o) { 1182 print "IDENT($1)\n" if ($dbg_values > 1); 1183 $type = 'V'; 1184 1185 } elsif ($cur =~ /^($Assignment)/o) { 1186 print "ASSIGN($1)\n" if ($dbg_values > 1); 1187 $type = 'N'; 1188 1189 } elsif ($cur =~/^(;|{|})/) { 1190 print "END($1)\n" if ($dbg_values > 1); 1191 $type = 'E'; 1192 $av_pend_colon = 'O'; 1193 1194 } elsif ($cur =~/^(,)/) { 1195 print "COMMA($1)\n" if ($dbg_values > 1); 1196 $type = 'C'; 1197 1198 } elsif ($cur =~ /^(\?)/o) { 1199 print "QUESTION($1)\n" if ($dbg_values > 1); 1200 $type = 'N'; 1201 1202 } elsif ($cur =~ /^(:)/o) { 1203 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 1204 1205 substr($var, length($res), 1, $av_pend_colon); 1206 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 1207 $type = 'E'; 1208 } else { 1209 $type = 'N'; 1210 } 1211 $av_pend_colon = 'O'; 1212 1213 } elsif ($cur =~ /^(\[)/o) { 1214 print "CLOSE($1)\n" if ($dbg_values > 1); 1215 $type = 'N'; 1216 1217 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 1218 my $variant; 1219 1220 print "OPV($1)\n" if ($dbg_values > 1); 1221 if ($type eq 'V') { 1222 $variant = 'B'; 1223 } else { 1224 $variant = 'U'; 1225 } 1226 1227 substr($var, length($res), 1, $variant); 1228 $type = 'N'; 1229 1230 } elsif ($cur =~ /^($Operators)/o) { 1231 print "OP($1)\n" if ($dbg_values > 1); 1232 if ($1 ne '++' && $1 ne '--') { 1233 $type = 'N'; 1234 } 1235 1236 } elsif ($cur =~ /(^.)/o) { 1237 print "C($1)\n" if ($dbg_values > 1); 1238 } 1239 if (defined $1) { 1240 $cur = substr($cur, length($1)); 1241 $res .= $type x length($1); 1242 } 1243 } 1244 1245 return ($res, $var); 1246} 1247 1248sub possible { 1249 my ($possible, $line) = @_; 1250 my $notPermitted = qr{(?: 1251 ^(?: 1252 $Modifier| 1253 $Storage| 1254 $Type| 1255 DEFINE_\S+ 1256 )$| 1257 ^(?: 1258 goto| 1259 return| 1260 case| 1261 else| 1262 asm|__asm__| 1263 do 1264 )(?:\s|$)| 1265 ^(?:typedef|struct|enum)\b| 1266 ^\# 1267 )}x; 1268 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 1269 if ($possible !~ $notPermitted) { 1270 # Check for modifiers. 1271 $possible =~ s/\s*$Storage\s*//g; 1272 $possible =~ s/\s*$Sparse\s*//g; 1273 if ($possible =~ /^\s*$/) { 1274 1275 } elsif ($possible =~ /\s/) { 1276 $possible =~ s/\s*(?:$Type|\#\#)\s*//g; 1277 for my $modifier (split(' ', $possible)) { 1278 if ($modifier !~ $notPermitted) { 1279 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1280 push(@modifierList, $modifier); 1281 } 1282 } 1283 1284 } else { 1285 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1286 push(@typeList, $possible); 1287 } 1288 build_types(); 1289 } else { 1290 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 1291 } 1292} 1293 1294my $prefix = ''; 1295 1296sub report { 1297 my ($level, $msg) = @_; 1298 if (defined $tst_only && $msg !~ /\Q$tst_only\E/) { 1299 return 0; 1300 } 1301 1302 my $output = ''; 1303 $output .= BOLD if $color; 1304 $output .= $prefix; 1305 $output .= RED if $color && $level eq 'ERROR'; 1306 $output .= MAGENTA if $color && $level eq 'WARNING'; 1307 $output .= $level . ':'; 1308 $output .= RESET if $color; 1309 $output .= ' ' . $msg . "\n"; 1310 1311 $output = (split('\n', $output))[0] . "\n" if ($terse); 1312 1313 push(our @report, $output); 1314 1315 return 1; 1316} 1317sub report_dump { 1318 our @report; 1319} 1320sub ERROR { 1321 if (report("ERROR", $_[0])) { 1322 our $clean = 0; 1323 our $cnt_error++; 1324 } 1325} 1326sub WARN { 1327 if (report("WARNING", $_[0])) { 1328 our $clean = 0; 1329 our $cnt_warn++; 1330 } 1331} 1332 1333# According to tests/qtest/bios-tables-test.c: do not 1334# change expected file in the same commit with adding test 1335sub checkfilename { 1336 my ($name, $acpi_testexpected, $acpi_nontestexpected) = @_; 1337 1338 # Note: shell script that rebuilds the expected files is in the same 1339 # directory as files themselves. 1340 # Note: allowed diff list can be changed both when changing expected 1341 # files and when changing tests. 1342 if ($name =~ m#^tests/data/acpi/# and not $name =~ m#^\.sh$#) { 1343 $$acpi_testexpected = $name; 1344 } elsif ($name !~ m#^tests/qtest/bios-tables-test-allowed-diff.h$#) { 1345 $$acpi_nontestexpected = $name; 1346 } 1347 if (defined $$acpi_testexpected and defined $$acpi_nontestexpected) { 1348 ERROR("Do not add expected files together with tests, " . 1349 "follow instructions in " . 1350 "tests/qtest/bios-tables-test.c: both " . 1351 $$acpi_testexpected . " and " . 1352 $$acpi_nontestexpected . " found\n"); 1353 } 1354} 1355 1356sub checkspdx { 1357 my ($file, $expr) = @_; 1358 1359 # Imported Linux headers probably have SPDX tags, but if they 1360 # don't we're not requiring contributors to fix this 1361 if ($file =~ m,include/standard-headers, || 1362 $file =~ m,linux-headers,) { 1363 return; 1364 } 1365 1366 my $origexpr = $expr; 1367 1368 # Flatten sub-expressions 1369 $expr =~ s/\(|\)/ /g; 1370 $expr =~ s/OR|AND/ /g; 1371 1372 # Merge WITH exceptions to the license 1373 $expr =~ s/\s+WITH\s+/-WITH-/g; 1374 1375 # Cull more leading/trailing whitespace 1376 $expr =~ s/^\s*//g; 1377 $expr =~ s/\s*$//g; 1378 1379 my @bits = split / +/, $expr; 1380 1381 my $prefer = "GPL-2.0-or-later"; 1382 my @valid = qw( 1383 LGPL-2.0-or-later 1384 LGPL-2.1-or-later 1385 GPL-2.0-only 1386 LGPL-2.0-only 1387 LGPL-2.0-only 1388 BSD-2-Clause 1389 BSD-3-Clause 1390 MIT 1391 ); 1392 1393 my $nonpreferred = 0; 1394 my @unknown = (); 1395 foreach my $bit (@bits) { 1396 if ($bit eq $prefer) { 1397 next; 1398 } 1399 if (grep /^$bit$/, @valid) { 1400 $nonpreferred = 1; 1401 } else { 1402 push @unknown, $bit; 1403 } 1404 } 1405 if (@unknown) { 1406 ERROR("Saw unacceptable licenses '" . join(',', @unknown) . 1407 "', valid choices for QEMU are:\n" . join("\n", $prefer, @valid)); 1408 } 1409 1410 if ($nonpreferred) { 1411 WARN("Saw acceptable license '$origexpr' but note '$prefer' is preferred " . 1412 "for new files unless the code is derived from a source with an" . 1413 "existed declared license that must be followed."); 1414 } 1415} 1416 1417sub process { 1418 my $filename = shift; 1419 1420 my $linenr=0; 1421 my $prevline=""; 1422 my $prevrawline=""; 1423 my $stashline=""; 1424 my $stashrawline=""; 1425 1426 my $length; 1427 my $indent; 1428 my $previndent=0; 1429 my $stashindent=0; 1430 1431 our $clean = 1; 1432 my $signoff = 0; 1433 my $is_patch = 0; 1434 1435 my $in_header_lines = $file ? 0 : 1; 1436 my $in_commit_log = 0; #Scanning lines before patch 1437 my $reported_maintainer_file = 0; 1438 my $reported_mixing_imported_file = 0; 1439 my $in_imported_file = 0; 1440 my $in_no_imported_file = 0; 1441 my $non_utf8_charset = 0; 1442 my $expect_spdx = 0; 1443 my $expect_spdx_file; 1444 1445 our @report = (); 1446 our $cnt_lines = 0; 1447 our $cnt_error = 0; 1448 our $cnt_warn = 0; 1449 our $cnt_chk = 0; 1450 1451 # Trace the real file/line as we go. 1452 my $realfile = ''; 1453 my $realline = 0; 1454 my $realcnt = 0; 1455 my $here = ''; 1456 my $in_comment = 0; 1457 my $comment_edge = 0; 1458 my $first_line = 0; 1459 my $p1_prefix = ''; 1460 1461 my $prev_values = 'E'; 1462 1463 # suppression flags 1464 my %suppress_ifbraces; 1465 my %suppress_whiletrailers; 1466 my %suppress_export; 1467 1468 my $acpi_testexpected; 1469 my $acpi_nontestexpected; 1470 1471 # Pre-scan the patch sanitizing the lines. 1472 1473 sanitise_line_reset(); 1474 my $line; 1475 foreach my $rawline (@rawlines) { 1476 $linenr++; 1477 $line = $rawline; 1478 1479 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1480 $realline=$1-1; 1481 if (defined $2) { 1482 $realcnt=$3+1; 1483 } else { 1484 $realcnt=1+1; 1485 } 1486 $in_comment = 0; 1487 1488 # Guestimate if this is a continuing comment. Run 1489 # the context looking for a comment "edge". If this 1490 # edge is a close comment then we must be in a comment 1491 # at context start. 1492 my $edge; 1493 my $cnt = $realcnt; 1494 for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 1495 next if (defined $rawlines[$ln - 1] && 1496 $rawlines[$ln - 1] =~ /^-/); 1497 $cnt--; 1498 #print "RAW<$rawlines[$ln - 1]>\n"; 1499 last if (!defined $rawlines[$ln - 1]); 1500 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1501 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1502 ($edge) = $1; 1503 last; 1504 } 1505 } 1506 if (defined $edge && $edge eq '*/') { 1507 $in_comment = 1; 1508 } 1509 1510 # Guestimate if this is a continuing comment. If this 1511 # is the start of a diff block and this line starts 1512 # ' *' then it is very likely a comment. 1513 if (!defined $edge && 1514 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1515 { 1516 $in_comment = 1; 1517 } 1518 1519 ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1520 sanitise_line_reset($in_comment); 1521 1522 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1523 # Standardise the strings and chars within the input to 1524 # simplify matching -- only bother with positive lines. 1525 $line = sanitise_line($rawline); 1526 } 1527 push(@lines, $line); 1528 1529 if ($realcnt > 1) { 1530 $realcnt-- if ($line =~ /^(?:\+| |$)/); 1531 } else { 1532 $realcnt = 0; 1533 } 1534 1535 #print "==>$rawline\n"; 1536 #print "-->$line\n"; 1537 } 1538 1539 $prefix = ''; 1540 1541 $realcnt = 0; 1542 $linenr = 0; 1543 foreach my $line (@lines) { 1544 $linenr++; 1545 1546 my $rawline = $rawlines[$linenr - 1]; 1547 1548#extract the line range in the file after the patch is applied 1549 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1550 $is_patch = 1; 1551 $first_line = $linenr + 1; 1552 $realline=$1-1; 1553 if (defined $2) { 1554 $realcnt=$3+1; 1555 } else { 1556 $realcnt=1+1; 1557 } 1558 annotate_reset(); 1559 $prev_values = 'E'; 1560 1561 %suppress_ifbraces = (); 1562 %suppress_whiletrailers = (); 1563 %suppress_export = (); 1564 next; 1565 1566# track the line number as we move through the hunk, note that 1567# new versions of GNU diff omit the leading space on completely 1568# blank context lines so we need to count that too. 1569 } elsif ($line =~ /^( |\+|$)/) { 1570 $realline++; 1571 $realcnt-- if ($realcnt != 0); 1572 1573 # Measure the line length and indent. 1574 ($length, $indent) = line_stats($rawline); 1575 1576 # Track the previous line. 1577 ($prevline, $stashline) = ($stashline, $line); 1578 ($previndent, $stashindent) = ($stashindent, $indent); 1579 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1580 1581 #warn "line<$line>\n"; 1582 1583 } elsif ($realcnt == 1) { 1584 $realcnt--; 1585 } 1586 1587 my $hunk_line = ($realcnt != 0); 1588 1589#make up the handle for any error we report on this line 1590 $prefix = "$filename:$realline: " if ($emacs && $file); 1591 $prefix = "$filename:$linenr: " if ($emacs && !$file); 1592 1593 $here = "#$linenr: " if (!$file); 1594 $here = "#$realline: " if ($file); 1595 1596 # extract the filename as it passes 1597 if ($line =~ /^diff --git.*?(\S+)$/) { 1598 $realfile = $1; 1599 $realfile =~ s@^([^/]*)/@@ if (!$file); 1600 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected); 1601 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1602 $realfile = $1; 1603 $realfile =~ s@^([^/]*)/@@ if (!$file); 1604 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected); 1605 1606 $p1_prefix = $1; 1607 if (!$file && $tree && $p1_prefix ne '' && 1608 -e "$root/$p1_prefix") { 1609 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 1610 } 1611 1612 next; 1613 } 1614 1615 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 1616 1617 my $hereline = "$here\n$rawline\n"; 1618 my $herecurr = "$here\n$rawline\n"; 1619 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 1620 1621 $cnt_lines++ if ($realcnt != 0); 1622 1623# Check for incorrect file permissions 1624 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 1625 my $permhere = $here . "FILE: $realfile\n"; 1626 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) { 1627 ERROR("do not set execute permissions for source files\n" . $permhere); 1628 } 1629 } 1630 1631# Only allow Python 3 interpreter 1632 if ($realline == 1 && 1633 $line =~ /^\+#!\ *\/usr\/bin\/(?:env )?python$/) { 1634 ERROR("please use python3 interpreter\n" . $herecurr); 1635 } 1636 1637# Accept git diff extended headers as valid patches 1638 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) { 1639 $is_patch = 1; 1640 } 1641 1642 if ($line =~ /^(Author|From): .* via .*<qemu-\w+\@nongnu\.org>/) { 1643 ERROR("Author email address is mangled by the mailing list\n" . $herecurr); 1644 } 1645 1646#check the patch for a signoff: 1647 if ($line =~ /^\s*signed-off-by:/i) { 1648 # This is a signoff, if ugly, so do not double report. 1649 $signoff++; 1650 $in_commit_log = 0; 1651 1652 if (!($line =~ /^\s*Signed-off-by:/)) { 1653 ERROR("The correct form is \"Signed-off-by\"\n" . 1654 $herecurr); 1655 } 1656 if ($line =~ /^\s*signed-off-by:\S/i) { 1657 ERROR("space required after Signed-off-by:\n" . 1658 $herecurr); 1659 } 1660 } 1661 1662# Check if MAINTAINERS is being updated. If so, there's probably no need to 1663# emit the "does MAINTAINERS need updating?" message on file add/move/delete 1664 if ($line =~ /^\s*MAINTAINERS\s*\|/) { 1665 $reported_maintainer_file = 1; 1666 } 1667 1668# Check for added, moved or deleted files 1669 if (!$reported_maintainer_file && !$in_commit_log && 1670 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 1671 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 1672 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 1673 (defined($1) || defined($2)))) && 1674 !(($realfile ne '') && 1675 defined($acpi_testexpected) && 1676 ($realfile eq $acpi_testexpected))) { 1677 $reported_maintainer_file = 1; 1678 WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 1679 } 1680 1681# All new files should have a SPDX-License-Identifier tag 1682 if ($line =~ /^new file mode\s*\d+\s*$/) { 1683 if ($expect_spdx) { 1684 if ($expect_spdx_file =~ /\.(c|h|py|pl|c\.inc)$/) { 1685 # source code files MUST have SPDX license declared 1686 ERROR("expected 'SPDX-License-Identifer' in new file $expect_spdx_file"); 1687 } else { 1688 # Other files MAY have SPDX license if appropriate 1689 WARNING("Does new file $expect_spdx_file need 'SPDX-License-Identifer'?"); 1690 } 1691 } 1692 $expect_spdx = 1; 1693 $expect_spdx_file = undef; 1694 } elsif ($expect_spdx) { 1695 $expect_spdx_file = $realfile unless defined $expect_spdx_file; 1696 1697 # SPDX tagsd may occurr in comments which were 1698 # stripped from '$line', so use '$rawline' 1699 if ($rawline =~ /SPDX-License-Identifier/) { 1700 $expect_spdx = 0; 1701 $expect_spdx_file = undef; 1702 } 1703 } 1704 1705# Check SPDX-License-Identifier references a permitted license 1706 if ($rawline =~ m,SPDX-License-Identifier: (.*?)(\*/)?\s*$,) { 1707 &checkspdx($realfile, $1); 1708 } 1709 1710 if ($rawline =~ m,(SPDX-[a-zA-Z0-9-_]+):,) { 1711 my $tag = $1; 1712 my @permitted = qw( 1713 SPDX-License-Identifier 1714 ); 1715 1716 unless (grep { /^$tag$/ } @permitted) { 1717 ERROR("Tag $tag not permitted in QEMU code, valid " . 1718 "choices are: " . join(", ", @permitted)); 1719 } 1720 } 1721 1722# Check for wrappage within a valid hunk of the file 1723 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1724 ERROR("patch seems to be corrupt (line wrapped?)\n" . 1725 $herecurr) if (!$emitted_corrupt++); 1726 } 1727 1728# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1729 if (($realfile =~ /^$/ || $line =~ /^\+/) && 1730 $rawline !~ m/^$UTF8*$/) { 1731 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1732 1733 my $blank = copy_spacing($rawline); 1734 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1735 my $hereptr = "$hereline$ptr\n"; 1736 1737 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 1738 } 1739 1740 if ($rawline =~ m/$UTF8_MOJIBAKE/) { 1741 ERROR("Doubly-encoded UTF-8\n" . $herecurr); 1742 } 1743# Check if it's the start of a commit log 1744# (not a header line and we haven't seen the patch filename) 1745 if ($in_header_lines && $realfile =~ /^$/ && 1746 !($rawline =~ /^\s+\S/ || 1747 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 1748 $in_header_lines = 0; 1749 $in_commit_log = 1; 1750 } 1751 1752# Check if there is UTF-8 in a commit log when a mail header has explicitly 1753# declined it, i.e defined some charset where it is missing. 1754 if ($in_header_lines && 1755 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1756 $1 !~ /utf-8/i) { 1757 $non_utf8_charset = 1; 1758 } 1759 1760 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 1761 $rawline =~ /$NON_ASCII_UTF8/) { 1762 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr); 1763 } 1764 1765# Check for various typo / spelling mistakes 1766 if (defined($misspellings) && 1767 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 1768 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { 1769 my $typo = $1; 1770 my $blank = copy_spacing($rawline); 1771 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); 1772 my $hereptr = "$hereline$ptr\n"; 1773 my $typo_fix = $spelling_fix{lc($typo)}; 1774 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 1775 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 1776 WARN("'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr); 1777 } 1778 } 1779 1780# ignore non-hunk lines and lines being removed 1781 next if (!$hunk_line || $line =~ /^-/); 1782 1783# Check that updating imported files from Linux are not mixed with other changes 1784 if ($realfile =~ /^(linux-headers|include\/standard-headers)\//) { 1785 if (!$in_imported_file) { 1786 WARN("added, moved or deleted file(s) " . 1787 "imported from Linux, are you using " . 1788 "scripts/update-linux-headers.sh?\n" . 1789 $herecurr); 1790 } 1791 $in_imported_file = 1; 1792 } else { 1793 $in_no_imported_file = 1; 1794 } 1795 1796 if (!$reported_mixing_imported_file && 1797 $in_imported_file && $in_no_imported_file) { 1798 ERROR("headers imported from Linux should be self-" . 1799 "contained in a patch with no other changes\n" . 1800 $herecurr); 1801 $reported_mixing_imported_file = 1; 1802 } 1803 1804# ignore files that are being periodically imported from Linux 1805 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//); 1806 1807#trailing whitespace 1808 if ($line =~ /^\+.*\015/) { 1809 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1810 ERROR("DOS line endings\n" . $herevet); 1811 1812 } elsif ($realfile =~ /^docs\/.+\.txt/ || 1813 $realfile =~ /^docs\/.+\.md/) { 1814 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) { 1815 # TODO: properly check we're in a code block 1816 # (surrounding text is 4-column aligned) 1817 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1818 ERROR("code blocks in documentation should have " . 1819 "empty lines with exactly 4 columns of " . 1820 "whitespace\n" . $herevet); 1821 } 1822 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1823 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1824 ERROR("trailing whitespace\n" . $herevet); 1825 $rpt_cleaners = 1; 1826 } 1827 1828# checks for trace-events files 1829 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) { 1830 if ($rawline =~ /%[-+ 0]*#/) { 1831 ERROR("Don't use '#' flag of printf format ('%#') in " . 1832 "trace-events, use '0x' prefix instead\n" . $herecurr); 1833 } else { 1834 my $hex = 1835 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/; 1836 1837 # don't consider groups split by [.:/ ], like 2A.20:12ab 1838 my $tmpline = $rawline; 1839 $tmpline =~ s/($hex[.:\/ ])+$hex//g; 1840 1841 if ($tmpline =~ /(?<!0x)$hex/) { 1842 ERROR("Hex numbers must be prefixed with '0x'\n" . 1843 $herecurr); 1844 } 1845 } 1846 } 1847 1848# check we are in a valid source file if not then ignore this hunk 1849 next if ($realfile !~ /$SrcFile/); 1850 1851#90 column limit; exempt URLs, if no other words on line 1852 if ($line =~ /^\+/ && 1853 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1854 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) && 1855 $length > 80) 1856 { 1857 if ($length > 90) { 1858 ERROR("line over 90 characters\n" . $herecurr); 1859 } else { 1860 WARN("line over 80 characters\n" . $herecurr); 1861 } 1862 } 1863 1864# check for spaces before a quoted newline 1865 if ($rawline =~ /^.*\".*\s\\n/) { 1866 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr); 1867 } 1868 1869# check for adding lines without a newline. 1870 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1871 ERROR("adding a line without newline at end of file\n" . $herecurr); 1872 } 1873 1874# check for RCS/CVS revision markers 1875 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) { 1876 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1877 } 1878 1879# tabs are only allowed in assembly source code, and in 1880# some scripts we imported from other projects. 1881 next if ($realfile =~ /\.(s|S)$/); 1882 next if ($realfile =~ /(checkpatch|get_maintainer)\.pl$/); 1883 next if ($realfile =~ /^target\/hexagon\/imported\/*/); 1884 1885 if ($rawline =~ /^\+.*\t/) { 1886 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1887 ERROR("code indent should never use tabs\n" . $herevet); 1888 $rpt_cleaners = 1; 1889 } 1890 1891# check we are in a valid C source file if not then ignore this hunk 1892 next if ($realfile !~ /\.((h|c)(\.inc)?|cpp)$/); 1893 1894# Block comment styles 1895 1896 # Block comments use /* on a line of its own 1897 my $commentline = $rawline; 1898 while ($commentline =~ s@^(\+.*)/\*.*\*/@$1@o) { # remove inline /*...*/ 1899 } 1900 if ($commentline =~ m@^\+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non-blank 1901 WARN("Block comments use a leading /* on a separate line\n" . $herecurr); 1902 } 1903 1904# Block comments use * on subsequent lines 1905 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 1906 $prevrawline =~ /^\+.*?\/\*/ && #starting /* 1907 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 1908 $rawline =~ /^\+/ && #line is new 1909 $rawline !~ /^\+[ \t]*\*/) { #no leading * 1910 WARN("Block comments use * on subsequent lines\n" . $hereprev); 1911 } 1912 1913# Block comments use */ on trailing lines 1914 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 1915 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 1916 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 1917 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 1918 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr); 1919 } 1920 1921# Block comment * alignment 1922 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 1923 $line =~ /^\+[ \t]*$;/ && #leading comment 1924 $rawline =~ /^\+[ \t]*\*/ && #leading * 1925 (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 1926 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 1927 $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 1928 my $oldindent; 1929 $prevrawline =~ m@^\+([ \t]*/?)\*@; 1930 if (defined($1)) { 1931 $oldindent = expand_tabs($1); 1932 } else { 1933 $prevrawline =~ m@^\+(.*/?)\*@; 1934 $oldindent = expand_tabs($1); 1935 } 1936 $rawline =~ m@^\+([ \t]*)\*@; 1937 my $newindent = $1; 1938 $newindent = expand_tabs($newindent); 1939 if (length($oldindent) ne length($newindent)) { 1940 WARN("Block comments should align the * on each line\n" . $hereprev); 1941 } 1942 } 1943 1944# Check for potential 'bare' types 1945 my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 1946 $realline_next); 1947 if ($realcnt && $line =~ /.\s*\S/) { 1948 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1949 ctx_statement_block($linenr, $realcnt, 0); 1950 $stat =~ s/\n./\n /g; 1951 $cond =~ s/\n./\n /g; 1952 1953 # Find the real next line. 1954 $realline_next = $line_nr_next; 1955 if (defined $realline_next && 1956 (!defined $lines[$realline_next - 1] || 1957 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 1958 $realline_next++; 1959 } 1960 1961 my $s = $stat; 1962 $s =~ s/{.*$//s; 1963 1964 # Ignore goto labels. 1965 if ($s =~ /$Ident:\*$/s) { 1966 1967 # Ignore functions being called 1968 } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1969 1970 } elsif ($s =~ /^.\s*else\b/s) { 1971 1972 # declarations always start with types 1973 } 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) { 1974 my $type = $1; 1975 $type =~ s/\s+/ /g; 1976 possible($type, "A:" . $s); 1977 1978 # definitions in global scope can only start with types 1979 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1980 possible($1, "B:" . $s); 1981 } 1982 1983 # any (foo ... *) is a pointer cast, and foo is a type 1984 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1985 possible($1, "C:" . $s); 1986 } 1987 1988 # Check for any sort of function declaration. 1989 # int foo(something bar, other baz); 1990 # void (*store_gdt)(x86_descr_ptr *); 1991 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 1992 my ($name_len) = length($1); 1993 1994 my $ctx = $s; 1995 substr($ctx, 0, $name_len + 1, ''); 1996 $ctx =~ s/\)[^\)]*$//; 1997 1998 for my $arg (split(/\s*,\s*/, $ctx)) { 1999 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 2000 2001 possible($1, "D:" . $s); 2002 } 2003 } 2004 } 2005 2006 } 2007 2008# 2009# Checks which may be anchored in the context. 2010# 2011 2012# Check for switch () and associated case and default 2013# statements should be at the same indent. 2014 if ($line=~/\bswitch\s*\(.*\)/) { 2015 my $err = ''; 2016 my $sep = ''; 2017 my @ctx = ctx_block_outer($linenr, $realcnt); 2018 shift(@ctx); 2019 for my $ctx (@ctx) { 2020 my ($clen, $cindent) = line_stats($ctx); 2021 if ($ctx =~ /^\+\s*(case\s+|default:)/ && 2022 $indent != $cindent) { 2023 $err .= "$sep$ctx\n"; 2024 $sep = ''; 2025 } else { 2026 $sep = "[...]\n"; 2027 } 2028 } 2029 if ($err ne '') { 2030 ERROR("switch and case should be at the same indent\n$hereline$err"); 2031 } 2032 } 2033 2034# if/while/etc brace do not go on next line, unless defining a do while loop, 2035# or if that brace on the next line is for something else 2036 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2037 my $pre_ctx = "$1$2"; 2038 2039 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 2040 my $ctx_cnt = $realcnt - $#ctx - 1; 2041 my $ctx = join("\n", @ctx); 2042 2043 my $ctx_ln = $linenr; 2044 my $ctx_skip = $realcnt; 2045 2046 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2047 defined $lines[$ctx_ln - 1] && 2048 $lines[$ctx_ln - 1] =~ /^-/)) { 2049 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2050 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2051 $ctx_ln++; 2052 } 2053 2054 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 2055 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2056 2057 # The length of the "previous line" is checked against 80 because it 2058 # includes the + at the beginning of the line (if the actual line has 2059 # 79 or 80 characters, it is no longer possible to add a space and an 2060 # opening brace there) 2061 if ($#ctx == 0 && $ctx !~ /{\s*/ && 2062 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ && 2063 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) { 2064 ERROR("that open brace { should be on the previous line\n" . 2065 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 2066 } 2067 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2068 $ctx =~ /\)\s*\;\s*$/ && 2069 defined $lines[$ctx_ln - 1]) 2070 { 2071 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 2072 if ($nindent > $indent) { 2073 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" . 2074 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 2075 } 2076 } 2077 } 2078 2079# 'do ... while (0/false)' only makes sense in macros, without trailing ';' 2080 if ($line =~ /while\s*\((0|false)\);/) { 2081 ERROR("suspicious ; after while (0)\n" . $herecurr); 2082 } 2083 2084# Check superfluous trailing ';' 2085 if ($line =~ /;;$/) { 2086 ERROR("superfluous trailing semicolon\n" . $herecurr); 2087 } 2088 2089# Check relative indent for conditionals and blocks. 2090 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 2091 my ($s, $c) = ($stat, $cond); 2092 2093 substr($s, 0, length($c), ''); 2094 2095 # Make sure we remove the line prefixes as we have 2096 # none on the first line, and are going to re-add them 2097 # where necessary. 2098 $s =~ s/\n./\n/gs; 2099 2100 # Find out how long the conditional actually is. 2101 my @newlines = ($c =~ /\n/gs); 2102 my $cond_lines = 1 + $#newlines; 2103 2104 # We want to check the first line inside the block 2105 # starting at the end of the conditional, so remove: 2106 # 1) any blank line termination 2107 # 2) any opening brace { on end of the line 2108 # 3) any do (...) { 2109 my $continuation = 0; 2110 my $check = 0; 2111 $s =~ s/^.*\bdo\b//; 2112 $s =~ s/^\s*\{//; 2113 if ($s =~ s/^\s*\\//) { 2114 $continuation = 1; 2115 } 2116 if ($s =~ s/^\s*?\n//) { 2117 $check = 1; 2118 $cond_lines++; 2119 } 2120 2121 # Also ignore a loop construct at the end of a 2122 # preprocessor statement. 2123 if (($prevline =~ /^.\s*#\s*define\s/ || 2124 $prevline =~ /\\\s*$/) && $continuation == 0) { 2125 $check = 0; 2126 } 2127 2128 my $cond_ptr = -1; 2129 $continuation = 0; 2130 while ($cond_ptr != $cond_lines) { 2131 $cond_ptr = $cond_lines; 2132 2133 # If we see an #else/#elif then the code 2134 # is not linear. 2135 if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2136 $check = 0; 2137 } 2138 2139 # Ignore: 2140 # 1) blank lines, they should be at 0, 2141 # 2) preprocessor lines, and 2142 # 3) labels. 2143 if ($continuation || 2144 $s =~ /^\s*?\n/ || 2145 $s =~ /^\s*#\s*?/ || 2146 $s =~ /^\s*$Ident\s*:/) { 2147 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 2148 if ($s =~ s/^.*?\n//) { 2149 $cond_lines++; 2150 } 2151 } 2152 } 2153 2154 my (undef, $sindent) = line_stats("+" . $s); 2155 my $stat_real = raw_line($linenr, $cond_lines); 2156 2157 # Check if either of these lines are modified, else 2158 # this is not this patch's fault. 2159 if (!defined($stat_real) || 2160 $stat !~ /^\+/ && $stat_real !~ /^\+/) { 2161 $check = 0; 2162 } 2163 if (defined($stat_real) && $cond_lines > 1) { 2164 $stat_real = "[...]\n$stat_real"; 2165 } 2166 2167 #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"; 2168 2169 if ($check && (($sindent % 4) != 0 || 2170 ($sindent <= $indent && $s ne ''))) { 2171 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 2172 } 2173 } 2174 2175 # Track the 'values' across context and added lines. 2176 my $opline = $line; $opline =~ s/^./ /; 2177 my ($curr_values, $curr_vars) = 2178 annotate_values($opline . "\n", $prev_values); 2179 $curr_values = $prev_values . $curr_values; 2180 if ($dbg_values) { 2181 my $outline = $opline; $outline =~ s/\t/ /g; 2182 print "$linenr > .$outline\n"; 2183 print "$linenr > $curr_values\n"; 2184 print "$linenr > $curr_vars\n"; 2185 } 2186 $prev_values = substr($curr_values, -1); 2187 2188#ignore lines not being added 2189 if ($line=~/^[^\+]/) {next;} 2190 2191# TEST: allow direct testing of the type matcher. 2192 if ($dbg_type) { 2193 if ($line =~ /^.\s*$Declare\s*$/) { 2194 ERROR("TEST: is type\n" . $herecurr); 2195 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2196 ERROR("TEST: is not type ($1 is)\n". $herecurr); 2197 } 2198 next; 2199 } 2200# TEST: allow direct testing of the attribute matcher. 2201 if ($dbg_attr) { 2202 if ($line =~ /^.\s*$Modifier\s*$/) { 2203 ERROR("TEST: is attr\n" . $herecurr); 2204 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2205 ERROR("TEST: is not attr ($1 is)\n". $herecurr); 2206 } 2207 next; 2208 } 2209 2210# check for initialisation to aggregates open brace on the next line 2211 if ($line =~ /^.\s*\{/ && 2212 $prevline =~ /(?:^|[^=])=\s*$/) { 2213 ERROR("that open brace { should be on the previous line\n" . $hereprev); 2214 } 2215 2216# 2217# Checks which are anchored on the added line. 2218# 2219 2220# check for malformed paths in #include statements (uses RAW line) 2221 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2222 my $path = $1; 2223 if ($path =~ m{//}) { 2224 ERROR("malformed #include filename\n" . 2225 $herecurr); 2226 } 2227 } 2228 2229# no C99 // comments 2230 if ($line =~ m{//} && 2231 $rawline !~ m{// SPDX-License-Identifier: }) { 2232 ERROR("do not use C99 // comments\n" . $herecurr); 2233 } 2234 # Remove C99 comments. 2235 $line =~ s@//.*@@; 2236 $opline =~ s@//.*@@; 2237 2238# check for global initialisers. 2239 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2240 ERROR("do not initialise globals to 0 or NULL\n" . 2241 $herecurr); 2242 } 2243# check for static initialisers. 2244 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2245 ERROR("do not initialise statics to 0 or NULL\n" . 2246 $herecurr); 2247 } 2248 2249# * goes on variable not on type 2250 # (char*[ const]) 2251 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 2252 my ($from, $to) = ($1, $1); 2253 2254 # Should start with a space. 2255 $to =~ s/^(\S)/ $1/; 2256 # Should not end with a space. 2257 $to =~ s/\s+$//; 2258 # '*'s should not have spaces between. 2259 while ($to =~ s/\*\s+\*/\*\*/) { 2260 } 2261 2262 #print "from<$from> to<$to>\n"; 2263 if ($from ne $to) { 2264 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 2265 } 2266 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 2267 my ($from, $to, $ident) = ($1, $1, $2); 2268 2269 # Should start with a space. 2270 $to =~ s/^(\S)/ $1/; 2271 # Should not end with a space. 2272 $to =~ s/\s+$//; 2273 # '*'s should not have spaces between. 2274 while ($to =~ s/\*\s+\*/\*\*/) { 2275 } 2276 # Modifiers should have spaces. 2277 $to =~ s/(\b$Modifier$)/$1 /; 2278 2279 #print "from<$from> to<$to> ident<$ident>\n"; 2280 if ($from ne $to && $ident !~ /^$Modifier$/) { 2281 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 2282 } 2283 } 2284 2285# function brace can't be on same line, except for #defines of do while, 2286# or if closed on same line 2287 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and 2288 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { 2289 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 2290 } 2291 2292# open braces for enum, union and struct go on the same line. 2293 if ($line =~ /^.\s*\{/ && 2294 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2295 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 2296 } 2297 2298# missing space after union, struct or enum definition 2299 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2300 ERROR("missing space after $1 definition\n" . $herecurr); 2301 } 2302 2303# check for spacing round square brackets; allowed: 2304# 1. with a type on the left -- int [] a; 2305# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2306# 3. inside a curly brace -- = { [0...10] = 5 } 2307# 4. after a comma -- [1] = 5, [2] = 6 2308# 5. in a macro definition -- #define abc(x) [x] = y 2309 while ($line =~ /(.*?\s)\[/g) { 2310 my ($where, $prefix) = ($-[1], $1); 2311 if ($prefix !~ /$Type\s+$/ && 2312 ($where != 0 || $prefix !~ /^.\s+$/) && 2313 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ && 2314 $prefix !~ /[,{:]\s+$/) { 2315 ERROR("space prohibited before open square bracket '['\n" . $herecurr); 2316 } 2317 } 2318 2319# check for spaces between functions and their parentheses. 2320 while ($line =~ /($Ident)\s+\(/g) { 2321 my $name = $1; 2322 my $ctx_before = substr($line, 0, $-[1]); 2323 my $ctx = "$ctx_before$name"; 2324 2325 # Ignore those directives where spaces _are_ permitted. 2326 if ($name =~ /^(?: 2327 if|for|while|switch|return|case| 2328 volatile|__volatile__|coroutine_fn| 2329 __attribute__|format|__extension__| 2330 asm|__asm__)$/x) 2331 { 2332 2333 # Ignore 'catch (...)' in C++ 2334 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) { 2335 2336 # cpp #define statements have non-optional spaces, ie 2337 # if there is a space between the name and the open 2338 # parenthesis it is simply not a parameter group. 2339 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2340 2341 # cpp #elif statement condition may start with a ( 2342 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2343 2344 # If this whole things ends with a type its most 2345 # likely a typedef for a function. 2346 } elsif ($ctx =~ /$Type$/) { 2347 2348 } else { 2349 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr); 2350 } 2351 } 2352# Check operator spacing. 2353 if (!($line=~/\#\s*(include|import)/)) { 2354 my $ops = qr{ 2355 <<=|>>=|<=|>=|==|!=| 2356 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 2357 =>|->|<<|>>|<|>|=|!|~| 2358 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 2359 \?|::|: 2360 }x; 2361 my @elements = split(/($ops|;)/, $opline); 2362 my $off = 0; 2363 2364 my $blank = copy_spacing($opline); 2365 2366 for (my $n = 0; $n < $#elements; $n += 2) { 2367 $off += length($elements[$n]); 2368 2369 # Pick up the preceding and succeeding characters. 2370 my $ca = substr($opline, 0, $off); 2371 my $cc = ''; 2372 if (length($opline) >= ($off + length($elements[$n + 1]))) { 2373 $cc = substr($opline, $off + length($elements[$n + 1])); 2374 } 2375 my $cb = "$ca$;$cc"; 2376 2377 my $a = ''; 2378 $a = 'V' if ($elements[$n] ne ''); 2379 $a = 'W' if ($elements[$n] =~ /\s$/); 2380 $a = 'C' if ($elements[$n] =~ /$;$/); 2381 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 2382 $a = 'O' if ($elements[$n] eq ''); 2383 $a = 'E' if ($ca =~ /^\s*$/); 2384 2385 my $op = $elements[$n + 1]; 2386 2387 my $c = ''; 2388 if (defined $elements[$n + 2]) { 2389 $c = 'V' if ($elements[$n + 2] ne ''); 2390 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2391 $c = 'C' if ($elements[$n + 2] =~ /^$;/); 2392 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 2393 $c = 'O' if ($elements[$n + 2] eq ''); 2394 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 2395 } else { 2396 $c = 'E'; 2397 } 2398 2399 my $ctx = "${a}x${c}"; 2400 2401 my $at = "(ctx:$ctx)"; 2402 2403 my $ptr = substr($blank, 0, $off) . "^"; 2404 my $hereptr = "$hereline$ptr\n"; 2405 2406 # Pull out the value of this operator. 2407 my $op_type = substr($curr_values, $off + 1, 1); 2408 2409 # Get the full operator variant. 2410 my $opv = $op . substr($curr_vars, $off, 1); 2411 2412 # Ignore operators passed as parameters. 2413 if ($op_type ne 'V' && 2414 $ca =~ /\s$/ && $cc =~ /^\s*,/) { 2415 2416# # Ignore comments 2417# } elsif ($op =~ /^$;+$/) { 2418 2419 # ; should have either the end of line or a space or \ after it 2420 } elsif ($op eq ';') { 2421 if ($ctx !~ /.x[WEBC]/ && 2422 $cc !~ /^\\/ && $cc !~ /^;/) { 2423 ERROR("space required after that '$op' $at\n" . $hereptr); 2424 } 2425 2426 # // is a comment 2427 } elsif ($op eq '//') { 2428 2429 # Ignore : used in class declaration in C++ 2430 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ && 2431 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) { 2432 2433 # No spaces for: 2434 # -> 2435 # : when part of a bitfield 2436 } elsif ($op eq '->' || $opv eq ':B') { 2437 if ($ctx =~ /Wx.|.xW/) { 2438 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 2439 } 2440 2441 # , must have a space on the right. 2442 # not required when having a single },{ on one line 2443 } elsif ($op eq ',') { 2444 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ && 2445 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") { 2446 ERROR("space required after that '$op' $at\n" . $hereptr); 2447 } 2448 2449 # '*' as part of a type definition -- reported already. 2450 } elsif ($opv eq '*_') { 2451 #warn "'*' is part of type\n"; 2452 2453 # unary operators should have a space before and 2454 # none after. May be left adjacent to another 2455 # unary operator, or a cast 2456 } elsif ($op eq '!' || $op eq '~' || 2457 $opv eq '*U' || $opv eq '-U' || 2458 $opv eq '&U' || $opv eq '&&U') { 2459 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) { 2460 # '~' used as a name of Destructor 2461 2462 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2463 ERROR("space required before that '$op' $at\n" . $hereptr); 2464 } 2465 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2466 # A unary '*' may be const 2467 2468 } elsif ($ctx =~ /.xW/) { 2469 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2470 } 2471 2472 # unary ++ and unary -- are allowed no space on one side. 2473 } elsif ($op eq '++' or $op eq '--') { 2474 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2475 ERROR("space required one side of that '$op' $at\n" . $hereptr); 2476 } 2477 if ($ctx =~ /Wx[BE]/ || 2478 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2479 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2480 } 2481 if ($ctx =~ /ExW/) { 2482 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2483 } 2484 2485 # A colon needs no spaces before when it is 2486 # terminating a case value or a label. 2487 } elsif ($opv eq ':C' || $opv eq ':L') { 2488 if ($ctx =~ /Wx./) { 2489 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2490 } 2491 2492 # All the others need spaces both sides. 2493 } elsif ($ctx !~ /[EWC]x[CWE]/) { 2494 my $ok = 0; 2495 2496 if ($realfile =~ /\.cpp|\.h$/) { 2497 # Ignore template arguments <...> in C++ 2498 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) { 2499 $ok = 1; 2500 } 2501 2502 # Ignore :: in C++ 2503 if ($op eq '::') { 2504 $ok = 1; 2505 } 2506 } 2507 2508 # Ignore email addresses <foo@bar> 2509 if (($op eq '<' && 2510 $cc =~ /^\S+\@\S+>/) || 2511 ($op eq '>' && 2512 $ca =~ /<\S+\@\S+$/)) 2513 { 2514 $ok = 1; 2515 } 2516 2517 # Ignore ?: 2518 if (($opv eq ':O' && $ca =~ /\?$/) || 2519 ($op eq '?' && $cc =~ /^:/)) { 2520 $ok = 1; 2521 } 2522 2523 if ($ok == 0) { 2524 ERROR("spaces required around that '$op' $at\n" . $hereptr); 2525 } 2526 } 2527 $off += length($elements[$n + 1]); 2528 } 2529 } 2530 2531#need space before brace following if, while, etc 2532 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 2533 $line =~ /do\{/) { 2534 ERROR("space required before the open brace '{'\n" . $herecurr); 2535 } 2536 2537# closing brace should have a space following it when it has anything 2538# on the line 2539 if ($line =~ /}(?!(?:,|;|\)))\S/) { 2540 ERROR("space required after that close brace '}'\n" . $herecurr); 2541 } 2542 2543# check spacing on square brackets 2544 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2545 ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 2546 } 2547 if ($line =~ /\s\]/) { 2548 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 2549 } 2550 2551# check spacing on parentheses 2552 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 2553 $line !~ /for\s*\(\s+;/) { 2554 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 2555 } 2556 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2557 $line !~ /for\s*\(.*;\s+\)/ && 2558 $line !~ /:\s+\)/) { 2559 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 2560 } 2561 2562# Return is not a function. 2563 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2564 my $spacing = $1; 2565 my $value = $2; 2566 2567 # Flatten any parentheses 2568 $value =~ s/\(/ \(/g; 2569 $value =~ s/\)/\) /g; 2570 while ($value =~ s/\[[^\{\}]*\]/1/ || 2571 $value !~ /(?:$Ident|-?$Constant)\s* 2572 $Compare\s* 2573 (?:$Ident|-?$Constant)/x && 2574 $value =~ s/\([^\(\)]*\)/1/) { 2575 } 2576#print "value<$value>\n"; 2577 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ && 2578 $line =~ /;$/) { 2579 ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2580 2581 } elsif ($spacing !~ /\s+/) { 2582 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2583 } 2584 } 2585# Return of what appears to be an errno should normally be -'ve 2586 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 2587 my $name = $1; 2588 if ($name ne 'EOF' && $name ne 'ERROR') { 2589 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr); 2590 } 2591 } 2592 2593 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ && 2594 $line !~ /^.typedef/) { 2595 ERROR("named $1 should be typedefed separately\n" . $herecurr); 2596 } 2597 2598# Need a space before open parenthesis after if, while etc 2599 if ($line=~/\b(if|while|for|switch)\(/) { 2600 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2601 } 2602 2603# Check for illegal assignment in if conditional -- and check for trailing 2604# statements after the conditional. 2605 if ($line =~ /do\s*(?!{)/) { 2606 my ($stat_next) = ctx_statement_block($line_nr_next, 2607 $remain_next, $off_next); 2608 $stat_next =~ s/\n./\n /g; 2609 ##print "stat<$stat> stat_next<$stat_next>\n"; 2610 2611 if ($stat_next =~ /^\s*while\b/) { 2612 # If the statement carries leading newlines, 2613 # then count those as offsets. 2614 my ($whitespace) = 2615 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2616 my $offset = 2617 statement_rawlines($whitespace) - 1; 2618 2619 $suppress_whiletrailers{$line_nr_next + 2620 $offset} = 1; 2621 } 2622 } 2623 if (!defined $suppress_whiletrailers{$linenr} && 2624 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2625 my ($s, $c) = ($stat, $cond); 2626 2627 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2628 ERROR("do not use assignment in if condition\n" . $herecurr); 2629 } 2630 2631 # Find out what is on the end of the line after the 2632 # conditional. 2633 substr($s, 0, length($c), ''); 2634 $s =~ s/\n.*//g; 2635 $s =~ s/$;//g; # Remove any comments 2636 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 2637 $c !~ /}\s*while\s*/) 2638 { 2639 # Find out how long the conditional actually is. 2640 my @newlines = ($c =~ /\n/gs); 2641 my $cond_lines = 1 + $#newlines; 2642 my $stat_real = ''; 2643 2644 $stat_real = raw_line($linenr, $cond_lines) 2645 . "\n" if ($cond_lines); 2646 if (defined($stat_real) && $cond_lines > 1) { 2647 $stat_real = "[...]\n$stat_real"; 2648 } 2649 2650 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 2651 } 2652 } 2653 2654# Check for bitwise tests written as boolean 2655 if ($line =~ / 2656 (?: 2657 (?:\[|\(|\&\&|\|\|) 2658 \s*0[xX][0-9]+\s* 2659 (?:\&\&|\|\|) 2660 | 2661 (?:\&\&|\|\|) 2662 \s*0[xX][0-9]+\s* 2663 (?:\&\&|\|\||\)|\]) 2664 )/x) 2665 { 2666 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 2667 } 2668 2669# if and else should not have general statements after it 2670 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 2671 my $s = $1; 2672 $s =~ s/$;//g; # Remove any comments 2673 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2674 ERROR("trailing statements should be on next line\n" . $herecurr); 2675 } 2676 } 2677# if should not continue a brace 2678 if ($line =~ /}\s*if\b/) { 2679 ERROR("trailing statements should be on next line\n" . 2680 $herecurr); 2681 } 2682# case and default should not have general statements after them 2683 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2684 $line !~ /\G(?: 2685 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2686 \s*return\s+ 2687 )/xg) 2688 { 2689 ERROR("trailing statements should be on next line\n" . $herecurr); 2690 } 2691 2692 # Check for }<nl>else {, these must be at the same 2693 # indent level to be relevant to each other. 2694 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 2695 $previndent == $indent) { 2696 ERROR("else should follow close brace '}'\n" . $hereprev); 2697 } 2698 2699 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2700 $previndent == $indent) { 2701 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2702 2703 # Find out what is on the end of the line after the 2704 # conditional. 2705 substr($s, 0, length($c), ''); 2706 $s =~ s/\n.*//g; 2707 2708 if ($s =~ /^\s*;/) { 2709 ERROR("while should follow close brace '}'\n" . $hereprev); 2710 } 2711 } 2712 2713#studly caps, commented out until figure out how to distinguish between use of existing and adding new 2714# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 2715# print "No studly caps, use _\n"; 2716# print "$herecurr"; 2717# $clean = 0; 2718# } 2719 2720#no spaces allowed after \ in define 2721 if ($line=~/\#\s*define.*\\\s$/) { 2722 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr); 2723 } 2724 2725# multi-statement macros should be enclosed in a do while loop, grab the 2726# first statement and ensure its the whole macro if its not enclosed 2727# in a known good container 2728 if ($realfile !~ m@/vmlinux.lds.h$@ && 2729 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2730 my $ln = $linenr; 2731 my $cnt = $realcnt; 2732 my ($off, $dstat, $dcond, $rest); 2733 my $ctx = ''; 2734 2735 my $args = defined($1); 2736 2737 # Find the end of the macro and limit our statement 2738 # search to that. 2739 while ($cnt > 0 && defined $lines[$ln - 1] && 2740 $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2741 { 2742 $ctx .= $rawlines[$ln - 1] . "\n"; 2743 $cnt-- if ($lines[$ln - 1] !~ /^-/); 2744 $ln++; 2745 } 2746 $ctx .= $rawlines[$ln - 1]; 2747 2748 ($dstat, $dcond, $ln, $cnt, $off) = 2749 ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2750 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2751 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2752 2753 # Extract the remainder of the define (if any) and 2754 # rip off surrounding spaces, and trailing \'s. 2755 $rest = ''; 2756 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2757 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2758 if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2759 $rest .= substr($lines[$ln - 1], $off) . "\n"; 2760 $cnt--; 2761 } 2762 $ln++; 2763 $off = 0; 2764 } 2765 $rest =~ s/\\\n.//g; 2766 $rest =~ s/^\s*//s; 2767 $rest =~ s/\s*$//s; 2768 2769 # Clean up the original statement. 2770 if ($args) { 2771 substr($dstat, 0, length($dcond), ''); 2772 } else { 2773 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2774 } 2775 $dstat =~ s/$;//g; 2776 $dstat =~ s/\\\n.//g; 2777 $dstat =~ s/^\s*//s; 2778 $dstat =~ s/\s*$//s; 2779 2780 # Flatten any parentheses and braces 2781 while ($dstat =~ s/\([^\(\)]*\)/1/ || 2782 $dstat =~ s/\{[^\{\}]*\}/1/ || 2783 $dstat =~ s/\[[^\{\}]*\]/1/) 2784 { 2785 } 2786 2787 my $exceptions = qr{ 2788 $Declare| 2789 module_param_named| 2790 MODULE_PARAM_DESC| 2791 DECLARE_PER_CPU| 2792 DEFINE_PER_CPU| 2793 __typeof__\(| 2794 union| 2795 struct| 2796 \.$Ident\s*=\s*| 2797 ^\"|\"$ 2798 }x; 2799 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2800 if ($rest ne '' && $rest ne ',') { 2801 if ($rest !~ /while\s*\(/ && 2802 $dstat !~ /$exceptions/) 2803 { 2804 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2805 } 2806 2807 } elsif ($ctx !~ /;/) { 2808 if ($dstat ne '' && 2809 $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2810 $dstat !~ /$exceptions/ && 2811 $dstat !~ /^\.$Ident\s*=/ && 2812 $dstat =~ /$Operators/) 2813 { 2814 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2815 } 2816 } 2817 } 2818 2819# check for missing bracing around if etc 2820 if ($line =~ /(^.*)\b(?:if|while|for)\b/ && 2821 $line !~ /\#\s*if/) { 2822 my $allowed = 0; 2823 2824 # Check the pre-context. 2825 if ($line =~ /(\}.*?)$/) { 2826 my $pre = $1; 2827 2828 if ($line !~ /else/) { 2829 print "APW: ALLOWED: pre<$pre> line<$line>\n" 2830 if $dbg_adv_apw; 2831 $allowed = 1; 2832 } 2833 } 2834 my ($level, $endln, @chunks) = 2835 ctx_statement_full($linenr, $realcnt, 1); 2836 if ($dbg_adv_apw) { 2837 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2838 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n" 2839 if $#chunks >= 1; 2840 } 2841 if ($#chunks >= 0 && $level == 0) { 2842 my $seen = 0; 2843 my $herectx = $here . "\n"; 2844 my $ln = $linenr - 1; 2845 for my $chunk (@chunks) { 2846 my ($cond, $block) = @{$chunk}; 2847 2848 # If the condition carries leading newlines, then count those as offsets. 2849 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2850 my $offset = statement_rawlines($whitespace) - 1; 2851 2852 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2853 2854 # We have looked at and allowed this specific line. 2855 $suppress_ifbraces{$ln + $offset} = 1; 2856 2857 $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2858 $ln += statement_rawlines($block) - 1; 2859 2860 substr($block, 0, length($cond), ''); 2861 2862 my $spaced_block = $block; 2863 $spaced_block =~ s/\n\+/ /g; 2864 2865 $seen++ if ($spaced_block =~ /^\s*\{/); 2866 2867 print "APW: cond<$cond> block<$block> allowed<$allowed>\n" 2868 if $dbg_adv_apw; 2869 if (statement_lines($cond) > 1) { 2870 print "APW: ALLOWED: cond<$cond>\n" 2871 if $dbg_adv_apw; 2872 $allowed = 1; 2873 } 2874 if ($block =~/\b(?:if|for|while)\b/) { 2875 print "APW: ALLOWED: block<$block>\n" 2876 if $dbg_adv_apw; 2877 $allowed = 1; 2878 } 2879 if (statement_block_size($block) > 1) { 2880 print "APW: ALLOWED: lines block<$block>\n" 2881 if $dbg_adv_apw; 2882 $allowed = 1; 2883 } 2884 } 2885 if ($seen != ($#chunks + 1) && !$allowed) { 2886 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx); 2887 } 2888 } 2889 } 2890 if (!defined $suppress_ifbraces{$linenr - 1} && 2891 $line =~ /\b(if|while|for|else)\b/ && 2892 $line !~ /\#\s*if/ && 2893 $line !~ /\#\s*else/) { 2894 my $allowed = 0; 2895 2896 # Check the pre-context. 2897 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2898 my $pre = $1; 2899 2900 if ($line !~ /else/) { 2901 print "APW: ALLOWED: pre<$pre> line<$line>\n" 2902 if $dbg_adv_apw; 2903 $allowed = 1; 2904 } 2905 } 2906 2907 my ($level, $endln, @chunks) = 2908 ctx_statement_full($linenr, $realcnt, $-[0]); 2909 2910 # Check the condition. 2911 my ($cond, $block) = @{$chunks[0]}; 2912 print "CHECKING<$linenr> cond<$cond> block<$block>\n" 2913 if $dbg_adv_checking; 2914 if (defined $cond) { 2915 substr($block, 0, length($cond), ''); 2916 } 2917 if (statement_lines($cond) > 1) { 2918 print "APW: ALLOWED: cond<$cond>\n" 2919 if $dbg_adv_apw; 2920 $allowed = 1; 2921 } 2922 if ($block =~/\b(?:if|for|while)\b/) { 2923 print "APW: ALLOWED: block<$block>\n" 2924 if $dbg_adv_apw; 2925 $allowed = 1; 2926 } 2927 if (statement_block_size($block) > 1) { 2928 print "APW: ALLOWED: lines block<$block>\n" 2929 if $dbg_adv_apw; 2930 $allowed = 1; 2931 } 2932 # Check the post-context. 2933 if (defined $chunks[1]) { 2934 my ($cond, $block) = @{$chunks[1]}; 2935 if (defined $cond) { 2936 substr($block, 0, length($cond), ''); 2937 } 2938 if ($block =~ /^\s*\{/) { 2939 print "APW: ALLOWED: chunk-1 block<$block>\n" 2940 if $dbg_adv_apw; 2941 $allowed = 1; 2942 } 2943 } 2944 print "DCS: level=$level block<$block> allowed=$allowed\n" 2945 if $dbg_adv_dcs; 2946 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) { 2947 my $herectx = $here . "\n";; 2948 my $cnt = statement_rawlines($block); 2949 2950 for (my $n = 0; $n < $cnt; $n++) { 2951 $herectx .= raw_line($linenr, $n) . "\n";; 2952 } 2953 2954 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx); 2955 } 2956 } 2957 2958# no volatiles please 2959 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 2960 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ && 2961 $line !~ /sig_atomic_t/ && 2962 !ctx_has_comment($first_line, $linenr)) { 2963 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr; 2964 ERROR($msg); 2965 } 2966 2967# warn about #if 0 2968 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2969 ERROR("if this code is redundant consider removing it\n" . 2970 $herecurr); 2971 } 2972 2973# check for needless g_free() checks 2974 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2975 my $expr = $1; 2976 if ($line =~ /\bg_free\(\Q$expr\E\);/) { 2977 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev); 2978 } 2979 } 2980 2981# warn about #ifdefs in C files 2982# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 2983# print "#ifdef in C files should be avoided\n"; 2984# print "$herecurr"; 2985# $clean = 0; 2986# } 2987 2988# warn about spacing in #ifdefs 2989 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 2990 ERROR("exactly one space required after that #$1\n" . $herecurr); 2991 } 2992# check for memory barriers without a comment. 2993 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 2994 if (!ctx_has_comment($first_line, $linenr)) { 2995 ERROR("memory barrier without comment\n" . $herecurr); 2996 } 2997 } 2998# check of hardware specific defines 2999# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases 3000# where they might be necessary. 3001 if ($line =~ m@^.\s*\#\s*if.*\b__@) { 3002 WARN("architecture specific defines should be avoided\n" . $herecurr); 3003 } 3004 3005# Check that the storage class is at the beginning of a declaration 3006 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3007 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr) 3008 } 3009 3010# check the location of the inline attribute, that it is between 3011# storage class and type. 3012 if ($line =~ /\b$Type\s+$Inline\b/ || 3013 $line =~ /\b$Inline\s+$Storage\b/) { 3014 ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 3015 } 3016 3017# check for sizeof(&) 3018 if ($line =~ /\bsizeof\s*\(\s*\&/) { 3019 ERROR("sizeof(& should be avoided\n" . $herecurr); 3020 } 3021 3022# check for new externs in .c files. 3023 if ($realfile =~ /\.c$/ && defined $stat && 3024 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3025 { 3026 my $function_name = $1; 3027 my $paren_space = $2; 3028 3029 my $s = $stat; 3030 if (defined $cond) { 3031 substr($s, 0, length($cond), ''); 3032 } 3033 if ($s =~ /^\s*;/ && 3034 $function_name ne 'uninitialized_var') 3035 { 3036 ERROR("externs should be avoided in .c files\n" . $herecurr); 3037 } 3038 3039 if ($paren_space =~ /\n/) { 3040 ERROR("arguments for function declarations should follow identifier\n" . $herecurr); 3041 } 3042 3043 } elsif ($realfile =~ /\.c$/ && defined $stat && 3044 $stat =~ /^.\s*extern\s+/) 3045 { 3046 ERROR("externs should be avoided in .c files\n" . $herecurr); 3047 } 3048 3049# check for pointless casting of g_malloc return 3050 if ($line =~ /\*\s*\)\s*g_(try|)(m|re)alloc(0?)(_n)?\b/) { 3051 if ($2 eq 'm') { 3052 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr); 3053 } else { 3054 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr); 3055 } 3056 } 3057 3058# check for gcc specific __FUNCTION__ 3059 if ($line =~ /__FUNCTION__/) { 3060 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 3061 } 3062 3063# recommend g_path_get_* over g_strdup(basename/dirname(...)) 3064 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) { 3065 WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr); 3066 } 3067 3068# enforce g_memdup2() over g_memdup() 3069 if ($line =~ /\bg_memdup\s*\(/) { 3070 ERROR("use g_memdup2() instead of unsafe g_memdup()\n" . $herecurr); 3071 } 3072 3073# recommend qemu_strto* over strto* for numeric conversions 3074 if ($line =~ /\b(strto[^kd].*?)\s*\(/) { 3075 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr); 3076 } 3077# recommend sigaction over signal for portability, when establishing a handler 3078 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) { 3079 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); 3080 } 3081# recommend qemu_bh_new_guarded instead of qemu_bh_new 3082 if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\bqemu_bh_new\s*\(/) { 3083 ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reentrancy problems\n" . $herecurr); 3084 } 3085# recommend aio_bh_new_guarded instead of aio_bh_new 3086 if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\baio_bh_new\s*\(/) { 3087 ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentrancy problems\n" . $herecurr); 3088 } 3089# check for module_init(), use category-specific init macros explicitly please 3090 if ($line =~ /^module_init\s*\(/) { 3091 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr); 3092 } 3093# check for various ops structs, ensure they are const. 3094 my $struct_ops = qr{AIOCBInfo| 3095 BdrvActionOps| 3096 BlockDevOps| 3097 BlockJobDriver| 3098 DisplayChangeListenerOps| 3099 GraphicHwOps| 3100 IDEDMAOps| 3101 KVMCapabilityInfo| 3102 MemoryRegionIOMMUOps| 3103 MemoryRegionOps| 3104 MemoryRegionPortio| 3105 QEMUFileOps| 3106 SCSIBusInfo| 3107 SCSIReqOps| 3108 Spice[A-Z][a-zA-Z0-9]*Interface| 3109 TypeInfo| 3110 USBDesc[A-Z][a-zA-Z0-9]*| 3111 VhostOps| 3112 VMStateDescription| 3113 VMStateInfo}x; 3114 if ($line !~ /\bconst\b/ && 3115 $line =~ /\b($struct_ops)\b.*=/) { 3116 ERROR("initializer for struct $1 should normally be const\n" . 3117 $herecurr); 3118 } 3119 3120# format strings checks 3121 my $string; 3122 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 3123 $string = substr($rawline, $-[1], $+[1] - $-[1]); 3124 $string =~ s/%%/__/g; 3125 # check for %L{u,d,i} in strings 3126 if ($string =~ /(?<!%)%L[udi]/) { 3127 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 3128 } 3129 # check for %# or %0# in printf-style format strings 3130 if ($string =~ /(?<!%)%0?#/) { 3131 ERROR("Don't use '#' flag of printf format " . 3132 "('%#') in format strings, use '0x' " . 3133 "prefix instead\n" . $herecurr); 3134 } 3135 } 3136 3137# QEMU specific tests 3138 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) { 3139 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr); 3140 } 3141 3142# Qemu error function tests 3143 3144 # Find newlines in error messages 3145 my $qemu_error_funcs = qr{error_setg| 3146 error_setg_errno| 3147 error_setg_win32| 3148 error_setg_file_open| 3149 error_set| 3150 error_prepend| 3151 warn_reportf_err| 3152 error_reportf_err| 3153 error_vreport| 3154 warn_vreport| 3155 info_vreport| 3156 error_report| 3157 warn_report| 3158 info_report| 3159 g_test_message}x; 3160 3161 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) { 3162 ERROR("Error messages should not contain newlines\n" . $herecurr); 3163 } 3164 3165 # Continue checking for error messages that contains newlines. This 3166 # check handles cases where string literals are spread over multiple lines. 3167 # Example: 3168 # error_report("Error msg line #1" 3169 # "Error msg line #2\n"); 3170 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"}; 3171 my $continued_str_literal = qr{\+\s*\".*\"}; 3172 3173 if ($rawline =~ /$quoted_newline_regex/) { 3174 # Backtrack to first line that does not contain only a quoted literal 3175 # and assume that it is the start of the statement. 3176 my $i = $linenr - 2; 3177 3178 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) { 3179 $i--; 3180 } 3181 3182 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) { 3183 ERROR("Error messages should not contain newlines\n" . $herecurr); 3184 } 3185 } 3186 3187# check for non-portable libc calls that have portable alternatives in QEMU 3188 if ($line =~ /\bffs\(/) { 3189 ERROR("use ctz32() instead of ffs()\n" . $herecurr); 3190 } 3191 if ($line =~ /\bffsl\(/) { 3192 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr); 3193 } 3194 if ($line =~ /\bffsll\(/) { 3195 ERROR("use ctz64() instead of ffsll()\n" . $herecurr); 3196 } 3197 if ($line =~ /\bbzero\(/) { 3198 ERROR("use memset() instead of bzero()\n" . $herecurr); 3199 } 3200 if ($line =~ /\bgetpagesize\(\)/) { 3201 ERROR("use qemu_real_host_page_size() instead of getpagesize()\n" . $herecurr); 3202 } 3203 if ($line =~ /\bsysconf\(_SC_PAGESIZE\)/) { 3204 ERROR("use qemu_real_host_page_size() instead of sysconf(_SC_PAGESIZE)\n" . $herecurr); 3205 } 3206 if ($line =~ /\b(g_)?assert\(0\)/) { 3207 ERROR("use g_assert_not_reached() instead of assert(0)\n" . $herecurr); 3208 } 3209 if ($line =~ /\b(g_)?assert\(false\)/) { 3210 ERROR("use g_assert_not_reached() instead of assert(false)\n" . 3211 $herecurr); 3212 } 3213 if ($line =~ /\bstrerrorname_np\(/) { 3214 ERROR("use strerror() instead of strerrorname_np()\n" . $herecurr); 3215 } 3216 my $non_exit_glib_asserts = qr{g_assert_cmpstr| 3217 g_assert_cmpint| 3218 g_assert_cmpuint| 3219 g_assert_cmphex| 3220 g_assert_cmpfloat| 3221 g_assert_true| 3222 g_assert_false| 3223 g_assert_nonnull| 3224 g_assert_null| 3225 g_assert_no_error| 3226 g_assert_error| 3227 g_test_assert_expected_messages| 3228 g_test_trap_assert_passed| 3229 g_test_trap_assert_stdout| 3230 g_test_trap_assert_stdout_unmatched| 3231 g_test_trap_assert_stderr| 3232 g_test_trap_assert_stderr_unmatched}x; 3233 if ($realfile !~ /^tests\// && 3234 $line =~ /\b(?:$non_exit_glib_asserts)\(/) { 3235 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr); 3236 } 3237 } 3238 3239 if ($is_patch && $chk_signoff && $signoff == 0) { 3240 ERROR("Missing Signed-off-by: line(s)\n"); 3241 } 3242 3243 # If we have no input at all, then there is nothing to report on 3244 # so just keep quiet. 3245 if ($#rawlines == -1) { 3246 return 1; 3247 } 3248 3249 # In mailback mode only produce a report in the negative, for 3250 # things that appear to be patches. 3251 if ($mailback && ($clean == 1 || !$is_patch)) { 3252 return 1; 3253 } 3254 3255 # This is not a patch, and we are are in 'no-patch' mode so 3256 # just keep quiet. 3257 if (!$chk_patch && !$is_patch) { 3258 return 1; 3259 } 3260 3261 if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 3262 ERROR("Does not appear to be a unified-diff format patch\n"); 3263 } 3264 3265 print report_dump(); 3266 if ($summary && !($clean == 1 && $quiet == 1)) { 3267 print "$filename " if ($summary_file); 3268 print "total: $cnt_error errors, $cnt_warn warnings, " . 3269 "$cnt_lines lines checked\n"; 3270 print "\n" if ($quiet == 0); 3271 } 3272 3273 if ($quiet == 0) { 3274 # If there were whitespace errors which cleanpatch can fix 3275 # then suggest that. 3276# if ($rpt_cleaners) { 3277# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 3278# print " scripts/cleanfile\n\n"; 3279# } 3280 } 3281 3282 if ($clean == 1 && $quiet == 0) { 3283 print "$vname has no obvious style problems and is ready for submission.\n" 3284 } 3285 if ($clean == 0 && $quiet == 0) { 3286 print "$vname has style problems, please review. If any of these errors\n"; 3287 print "are false positives report them to the maintainer, see\n"; 3288 print "CHECKPATCH in MAINTAINERS.\n"; 3289 } 3290 3291 return ($no_warnings ? $clean : $cnt_error == 0); 3292} 3293