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, as these 1361 # files are not expected to be modified locally in QEMU. 1362 # Also don't accidentally detect own checking code. 1363 if ($file =~ m,include/standard-headers, || 1364 $file =~ m,linux-headers, || 1365 $file =~ m,checkpatch.pl,) { 1366 return; 1367 } 1368 1369 my $origexpr = $expr; 1370 1371 # Flatten sub-expressions 1372 $expr =~ s/\(|\)/ /g; 1373 $expr =~ s/OR|AND/ /g; 1374 1375 # Merge WITH exceptions to the license 1376 $expr =~ s/\s+WITH\s+/-WITH-/g; 1377 1378 # Cull more leading/trailing whitespace 1379 $expr =~ s/^\s*//g; 1380 $expr =~ s/\s*$//g; 1381 1382 my @bits = split / +/, $expr; 1383 1384 my $prefer = "GPL-2.0-or-later"; 1385 my @valid = qw( 1386 GPL-2.0-only 1387 LGPL-2.1-only 1388 LGPL-2.1-or-later 1389 BSD-2-Clause 1390 BSD-3-Clause 1391 MIT 1392 ); 1393 1394 my $nonpreferred = 0; 1395 my @unknown = (); 1396 foreach my $bit (@bits) { 1397 if ($bit eq $prefer) { 1398 next; 1399 } 1400 if (grep /^$bit$/, @valid) { 1401 $nonpreferred = 1; 1402 } else { 1403 push @unknown, $bit; 1404 } 1405 } 1406 if (@unknown) { 1407 ERROR("Saw unacceptable licenses '" . join(',', @unknown) . 1408 "', valid choices for QEMU are:\n" . join("\n", $prefer, @valid)); 1409 } 1410 1411 if ($nonpreferred) { 1412 WARN("Saw acceptable license '$origexpr' but note '$prefer' is " . 1413 "preferred for new files unless the code is derived from a " . 1414 "source file with an existing declared license that must be " . 1415 "retained. Please explain the license choice in the commit " . 1416 "message."); 1417 } 1418} 1419 1420sub process { 1421 my $filename = shift; 1422 1423 my $linenr=0; 1424 my $prevline=""; 1425 my $prevrawline=""; 1426 my $stashline=""; 1427 my $stashrawline=""; 1428 1429 my $length; 1430 my $indent; 1431 my $previndent=0; 1432 my $stashindent=0; 1433 1434 our $clean = 1; 1435 my $signoff = 0; 1436 my $is_patch = 0; 1437 1438 my $in_header_lines = $file ? 0 : 1; 1439 my $in_commit_log = 0; #Scanning lines before patch 1440 my $reported_maintainer_file = 0; 1441 my $reported_mixing_imported_file = 0; 1442 my $in_imported_file = 0; 1443 my $in_no_imported_file = 0; 1444 my $non_utf8_charset = 0; 1445 my $expect_spdx = 0; 1446 my $expect_spdx_file; 1447 1448 our @report = (); 1449 our $cnt_lines = 0; 1450 our $cnt_error = 0; 1451 our $cnt_warn = 0; 1452 our $cnt_chk = 0; 1453 1454 # Trace the real file/line as we go. 1455 my $realfile = ''; 1456 my $realline = 0; 1457 my $realcnt = 0; 1458 my $here = ''; 1459 my $in_comment = 0; 1460 my $comment_edge = 0; 1461 my $first_line = 0; 1462 my $p1_prefix = ''; 1463 1464 my $prev_values = 'E'; 1465 1466 # suppression flags 1467 my %suppress_ifbraces; 1468 my %suppress_whiletrailers; 1469 my %suppress_export; 1470 1471 my $acpi_testexpected; 1472 my $acpi_nontestexpected; 1473 1474 # Pre-scan the patch sanitizing the lines. 1475 1476 sanitise_line_reset(); 1477 my $line; 1478 foreach my $rawline (@rawlines) { 1479 $linenr++; 1480 $line = $rawline; 1481 1482 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1483 $realline=$1-1; 1484 if (defined $2) { 1485 $realcnt=$3+1; 1486 } else { 1487 $realcnt=1+1; 1488 } 1489 $in_comment = 0; 1490 1491 # Guestimate if this is a continuing comment. Run 1492 # the context looking for a comment "edge". If this 1493 # edge is a close comment then we must be in a comment 1494 # at context start. 1495 my $edge; 1496 my $cnt = $realcnt; 1497 for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 1498 next if (defined $rawlines[$ln - 1] && 1499 $rawlines[$ln - 1] =~ /^-/); 1500 $cnt--; 1501 #print "RAW<$rawlines[$ln - 1]>\n"; 1502 last if (!defined $rawlines[$ln - 1]); 1503 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1504 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1505 ($edge) = $1; 1506 last; 1507 } 1508 } 1509 if (defined $edge && $edge eq '*/') { 1510 $in_comment = 1; 1511 } 1512 1513 # Guestimate if this is a continuing comment. If this 1514 # is the start of a diff block and this line starts 1515 # ' *' then it is very likely a comment. 1516 if (!defined $edge && 1517 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1518 { 1519 $in_comment = 1; 1520 } 1521 1522 ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1523 sanitise_line_reset($in_comment); 1524 1525 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1526 # Standardise the strings and chars within the input to 1527 # simplify matching -- only bother with positive lines. 1528 $line = sanitise_line($rawline); 1529 } 1530 push(@lines, $line); 1531 1532 if ($realcnt > 1) { 1533 $realcnt-- if ($line =~ /^(?:\+| |$)/); 1534 } else { 1535 $realcnt = 0; 1536 } 1537 1538 #print "==>$rawline\n"; 1539 #print "-->$line\n"; 1540 } 1541 1542 $prefix = ''; 1543 1544 $realcnt = 0; 1545 $linenr = 0; 1546 foreach my $line (@lines) { 1547 $linenr++; 1548 1549 my $rawline = $rawlines[$linenr - 1]; 1550 1551#extract the line range in the file after the patch is applied 1552 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1553 $is_patch = 1; 1554 $first_line = $linenr + 1; 1555 $realline=$1-1; 1556 if (defined $2) { 1557 $realcnt=$3+1; 1558 } else { 1559 $realcnt=1+1; 1560 } 1561 annotate_reset(); 1562 $prev_values = 'E'; 1563 1564 %suppress_ifbraces = (); 1565 %suppress_whiletrailers = (); 1566 %suppress_export = (); 1567 next; 1568 1569# track the line number as we move through the hunk, note that 1570# new versions of GNU diff omit the leading space on completely 1571# blank context lines so we need to count that too. 1572 } elsif ($line =~ /^( |\+|$)/) { 1573 $realline++; 1574 $realcnt-- if ($realcnt != 0); 1575 1576 # Measure the line length and indent. 1577 ($length, $indent) = line_stats($rawline); 1578 1579 # Track the previous line. 1580 ($prevline, $stashline) = ($stashline, $line); 1581 ($previndent, $stashindent) = ($stashindent, $indent); 1582 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1583 1584 #warn "line<$line>\n"; 1585 1586 } elsif ($realcnt == 1) { 1587 $realcnt--; 1588 } 1589 1590 my $hunk_line = ($realcnt != 0); 1591 1592#make up the handle for any error we report on this line 1593 $prefix = "$filename:$realline: " if ($emacs && $file); 1594 $prefix = "$filename:$linenr: " if ($emacs && !$file); 1595 1596 $here = "#$linenr: " if (!$file); 1597 $here = "#$realline: " if ($file); 1598 1599 # extract the filename as it passes 1600 if ($line =~ /^diff --git.*?(\S+)$/) { 1601 $realfile = $1; 1602 $realfile =~ s@^([^/]*)/@@ if (!$file); 1603 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected); 1604 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1605 $realfile = $1; 1606 $realfile =~ s@^([^/]*)/@@ if (!$file); 1607 checkfilename($realfile, \$acpi_testexpected, \$acpi_nontestexpected); 1608 1609 $p1_prefix = $1; 1610 if (!$file && $tree && $p1_prefix ne '' && 1611 -e "$root/$p1_prefix") { 1612 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 1613 } 1614 1615 next; 1616 } 1617 1618 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 1619 1620 my $hereline = "$here\n$rawline\n"; 1621 my $herecurr = "$here\n$rawline\n"; 1622 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 1623 1624 $cnt_lines++ if ($realcnt != 0); 1625 1626# Check for incorrect file permissions 1627 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 1628 my $permhere = $here . "FILE: $realfile\n"; 1629 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) { 1630 ERROR("do not set execute permissions for source files\n" . $permhere); 1631 } 1632 } 1633 1634# Only allow Python 3 interpreter 1635 if ($realline == 1 && 1636 $line =~ /^\+#!\ *\/usr\/bin\/(?:env )?python$/) { 1637 ERROR("please use python3 interpreter\n" . $herecurr); 1638 } 1639 1640# Accept git diff extended headers as valid patches 1641 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) { 1642 $is_patch = 1; 1643 } 1644 1645 if ($line =~ /^(Author|From): .* via .*<qemu-\w+\@nongnu\.org>/) { 1646 ERROR("Author email address is mangled by the mailing list\n" . $herecurr); 1647 } 1648 1649#check the patch for a signoff: 1650 if ($line =~ /^\s*signed-off-by:/i) { 1651 # This is a signoff, if ugly, so do not double report. 1652 $signoff++; 1653 $in_commit_log = 0; 1654 1655 if (!($line =~ /^\s*Signed-off-by:/)) { 1656 ERROR("The correct form is \"Signed-off-by\"\n" . 1657 $herecurr); 1658 } 1659 if ($line =~ /^\s*signed-off-by:\S/i) { 1660 ERROR("space required after Signed-off-by:\n" . 1661 $herecurr); 1662 } 1663 } 1664 1665# Check if MAINTAINERS is being updated. If so, there's probably no need to 1666# emit the "does MAINTAINERS need updating?" message on file add/move/delete 1667 if ($line =~ /^\s*MAINTAINERS\s*\|/) { 1668 $reported_maintainer_file = 1; 1669 } 1670 1671# Check for added, moved or deleted files 1672 if (!$reported_maintainer_file && !$in_commit_log && 1673 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 1674 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 1675 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 1676 (defined($1) || defined($2)))) && 1677 !(($realfile ne '') && 1678 defined($acpi_testexpected) && 1679 ($realfile eq $acpi_testexpected))) { 1680 $reported_maintainer_file = 1; 1681 WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 1682 } 1683 1684# All new files should have a SPDX-License-Identifier tag 1685 if ($line =~ /^new file mode\s*\d+\s*$/) { 1686 if ($expect_spdx) { 1687 if ($expect_spdx_file =~ 1688 /\.(c|h|py|pl|sh|json|inc|Makefile)$/) { 1689 # source code files MUST have SPDX license declared 1690 ERROR("New file '$expect_spdx_file' requires " . 1691 "'SPDX-License-Identifier'"); 1692 } else { 1693 # Other files MAY have SPDX license if appropriate 1694 WARN("Does new file '$expect_spdx_file' need " . 1695 "'SPDX-License-Identifier'?"); 1696 } 1697 } 1698 $expect_spdx = 1; 1699 $expect_spdx_file = undef; 1700 } elsif ($expect_spdx) { 1701 $expect_spdx_file = $realfile unless 1702 defined $expect_spdx_file; 1703 1704 # SPDX tags may occurr in comments which were 1705 # stripped from '$line', so use '$rawline' 1706 if ($rawline =~ /SPDX-License-Identifier/) { 1707 $expect_spdx = 0; 1708 $expect_spdx_file = undef; 1709 } 1710 } 1711 1712# Check SPDX-License-Identifier references a permitted license 1713 if ($rawline =~ m,SPDX-License-Identifier: (.*?)(\*/)?\s*$,) { 1714 &checkspdx($realfile, $1); 1715 } 1716 1717 if ($rawline =~ m,(SPDX-[a-zA-Z0-9-_]+):,) { 1718 my $tag = $1; 1719 my @permitted = qw( 1720 SPDX-License-Identifier 1721 ); 1722 1723 unless (grep { /^$tag$/ } @permitted) { 1724 ERROR("Tag $tag not permitted in QEMU code, valid " . 1725 "choices are: " . join(", ", @permitted)); 1726 } 1727 } 1728 1729# Check for wrappage within a valid hunk of the file 1730 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1731 ERROR("patch seems to be corrupt (line wrapped?)\n" . 1732 $herecurr) if (!$emitted_corrupt++); 1733 } 1734 1735# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1736 if (($realfile =~ /^$/ || $line =~ /^\+/) && 1737 $rawline !~ m/^$UTF8*$/) { 1738 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1739 1740 my $blank = copy_spacing($rawline); 1741 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1742 my $hereptr = "$hereline$ptr\n"; 1743 1744 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 1745 } 1746 1747 if ($rawline =~ m/$UTF8_MOJIBAKE/) { 1748 ERROR("Doubly-encoded UTF-8\n" . $herecurr); 1749 } 1750# Check if it's the start of a commit log 1751# (not a header line and we haven't seen the patch filename) 1752 if ($in_header_lines && $realfile =~ /^$/ && 1753 !($rawline =~ /^\s+\S/ || 1754 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 1755 $in_header_lines = 0; 1756 $in_commit_log = 1; 1757 } 1758 1759# Check if there is UTF-8 in a commit log when a mail header has explicitly 1760# declined it, i.e defined some charset where it is missing. 1761 if ($in_header_lines && 1762 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1763 $1 !~ /utf-8/i) { 1764 $non_utf8_charset = 1; 1765 } 1766 1767 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 1768 $rawline =~ /$NON_ASCII_UTF8/) { 1769 WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr); 1770 } 1771 1772# Check for various typo / spelling mistakes 1773 if (defined($misspellings) && 1774 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 1775 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { 1776 my $typo = $1; 1777 my $blank = copy_spacing($rawline); 1778 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); 1779 my $hereptr = "$hereline$ptr\n"; 1780 my $typo_fix = $spelling_fix{lc($typo)}; 1781 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 1782 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 1783 WARN("'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr); 1784 } 1785 } 1786 1787# ignore non-hunk lines and lines being removed 1788 next if (!$hunk_line || $line =~ /^-/); 1789 1790# Check that updating imported files from Linux are not mixed with other changes 1791 if ($realfile =~ /^(linux-headers|include\/standard-headers)\//) { 1792 if (!$in_imported_file) { 1793 WARN("added, moved or deleted file(s) " . 1794 "imported from Linux, are you using " . 1795 "scripts/update-linux-headers.sh?\n" . 1796 $herecurr); 1797 } 1798 $in_imported_file = 1; 1799 } else { 1800 $in_no_imported_file = 1; 1801 } 1802 1803 if (!$reported_mixing_imported_file && 1804 $in_imported_file && $in_no_imported_file) { 1805 ERROR("headers imported from Linux should be self-" . 1806 "contained in a patch with no other changes\n" . 1807 $herecurr); 1808 $reported_mixing_imported_file = 1; 1809 } 1810 1811# ignore files that are being periodically imported from Linux 1812 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//); 1813 1814#trailing whitespace 1815 if ($line =~ /^\+.*\015/) { 1816 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1817 ERROR("DOS line endings\n" . $herevet); 1818 1819 } elsif ($realfile =~ /^docs\/.+\.txt/ || 1820 $realfile =~ /^docs\/.+\.md/) { 1821 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) { 1822 # TODO: properly check we're in a code block 1823 # (surrounding text is 4-column aligned) 1824 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1825 ERROR("code blocks in documentation should have " . 1826 "empty lines with exactly 4 columns of " . 1827 "whitespace\n" . $herevet); 1828 } 1829 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1830 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1831 ERROR("trailing whitespace\n" . $herevet); 1832 $rpt_cleaners = 1; 1833 } 1834 1835# checks for trace-events files 1836 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) { 1837 if ($rawline =~ /%[-+ 0]*#/) { 1838 ERROR("Don't use '#' flag of printf format ('%#') in " . 1839 "trace-events, use '0x' prefix instead\n" . $herecurr); 1840 } else { 1841 my $hex = 1842 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/; 1843 1844 # don't consider groups split by [.:/ ], like 2A.20:12ab 1845 my $tmpline = $rawline; 1846 $tmpline =~ s/($hex[.:\/ ])+$hex//g; 1847 1848 if ($tmpline =~ /(?<!0x)$hex/) { 1849 ERROR("Hex numbers must be prefixed with '0x'\n" . 1850 $herecurr); 1851 } 1852 } 1853 } 1854 1855# check we are in a valid source file if not then ignore this hunk 1856 next if ($realfile !~ /$SrcFile/); 1857 1858#90 column limit; exempt URLs, if no other words on line 1859 if ($line =~ /^\+/ && 1860 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1861 !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) && 1862 $length > 80) 1863 { 1864 if ($length > 90) { 1865 ERROR("line over 90 characters\n" . $herecurr); 1866 } else { 1867 WARN("line over 80 characters\n" . $herecurr); 1868 } 1869 } 1870 1871# check for spaces before a quoted newline 1872 if ($rawline =~ /^.*\".*\s\\n/) { 1873 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr); 1874 } 1875 1876# check for adding lines without a newline. 1877 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1878 ERROR("adding a line without newline at end of file\n" . $herecurr); 1879 } 1880 1881# check for RCS/CVS revision markers 1882 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) { 1883 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1884 } 1885 1886# tabs are only allowed in assembly source code, and in 1887# some scripts we imported from other projects. 1888 next if ($realfile =~ /\.(s|S)$/); 1889 next if ($realfile =~ /(checkpatch|get_maintainer)\.pl$/); 1890 next if ($realfile =~ /^target\/hexagon\/imported\/*/); 1891 1892 if ($rawline =~ /^\+.*\t/) { 1893 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1894 ERROR("code indent should never use tabs\n" . $herevet); 1895 $rpt_cleaners = 1; 1896 } 1897 1898# check we are in a valid C source file if not then ignore this hunk 1899 next if ($realfile !~ /\.((h|c)(\.inc)?|cpp)$/); 1900 1901# Block comment styles 1902 1903 # Block comments use /* on a line of its own 1904 my $commentline = $rawline; 1905 while ($commentline =~ s@^(\+.*)/\*.*\*/@$1@o) { # remove inline /*...*/ 1906 } 1907 if ($commentline =~ m@^\+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non-blank 1908 WARN("Block comments use a leading /* on a separate line\n" . $herecurr); 1909 } 1910 1911# Block comments use * on subsequent lines 1912 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 1913 $prevrawline =~ /^\+.*?\/\*/ && #starting /* 1914 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 1915 $rawline =~ /^\+/ && #line is new 1916 $rawline !~ /^\+[ \t]*\*/) { #no leading * 1917 WARN("Block comments use * on subsequent lines\n" . $hereprev); 1918 } 1919 1920# Block comments use */ on trailing lines 1921 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 1922 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 1923 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 1924 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 1925 WARN("Block comments use a trailing */ on a separate line\n" . $herecurr); 1926 } 1927 1928# Block comment * alignment 1929 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 1930 $line =~ /^\+[ \t]*$;/ && #leading comment 1931 $rawline =~ /^\+[ \t]*\*/ && #leading * 1932 (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 1933 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 1934 $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 1935 my $oldindent; 1936 $prevrawline =~ m@^\+([ \t]*/?)\*@; 1937 if (defined($1)) { 1938 $oldindent = expand_tabs($1); 1939 } else { 1940 $prevrawline =~ m@^\+(.*/?)\*@; 1941 $oldindent = expand_tabs($1); 1942 } 1943 $rawline =~ m@^\+([ \t]*)\*@; 1944 my $newindent = $1; 1945 $newindent = expand_tabs($newindent); 1946 if (length($oldindent) ne length($newindent)) { 1947 WARN("Block comments should align the * on each line\n" . $hereprev); 1948 } 1949 } 1950 1951# Check for potential 'bare' types 1952 my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 1953 $realline_next); 1954 if ($realcnt && $line =~ /.\s*\S/) { 1955 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1956 ctx_statement_block($linenr, $realcnt, 0); 1957 $stat =~ s/\n./\n /g; 1958 $cond =~ s/\n./\n /g; 1959 1960 # Find the real next line. 1961 $realline_next = $line_nr_next; 1962 if (defined $realline_next && 1963 (!defined $lines[$realline_next - 1] || 1964 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 1965 $realline_next++; 1966 } 1967 1968 my $s = $stat; 1969 $s =~ s/{.*$//s; 1970 1971 # Ignore goto labels. 1972 if ($s =~ /$Ident:\*$/s) { 1973 1974 # Ignore functions being called 1975 } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1976 1977 } elsif ($s =~ /^.\s*else\b/s) { 1978 1979 # declarations always start with types 1980 } 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) { 1981 my $type = $1; 1982 $type =~ s/\s+/ /g; 1983 possible($type, "A:" . $s); 1984 1985 # definitions in global scope can only start with types 1986 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1987 possible($1, "B:" . $s); 1988 } 1989 1990 # any (foo ... *) is a pointer cast, and foo is a type 1991 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1992 possible($1, "C:" . $s); 1993 } 1994 1995 # Check for any sort of function declaration. 1996 # int foo(something bar, other baz); 1997 # void (*store_gdt)(x86_descr_ptr *); 1998 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 1999 my ($name_len) = length($1); 2000 2001 my $ctx = $s; 2002 substr($ctx, 0, $name_len + 1, ''); 2003 $ctx =~ s/\)[^\)]*$//; 2004 2005 for my $arg (split(/\s*,\s*/, $ctx)) { 2006 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 2007 2008 possible($1, "D:" . $s); 2009 } 2010 } 2011 } 2012 2013 } 2014 2015# 2016# Checks which may be anchored in the context. 2017# 2018 2019# Check for switch () and associated case and default 2020# statements should be at the same indent. 2021 if ($line=~/\bswitch\s*\(.*\)/) { 2022 my $err = ''; 2023 my $sep = ''; 2024 my @ctx = ctx_block_outer($linenr, $realcnt); 2025 shift(@ctx); 2026 for my $ctx (@ctx) { 2027 my ($clen, $cindent) = line_stats($ctx); 2028 if ($ctx =~ /^\+\s*(case\s+|default:)/ && 2029 $indent != $cindent) { 2030 $err .= "$sep$ctx\n"; 2031 $sep = ''; 2032 } else { 2033 $sep = "[...]\n"; 2034 } 2035 } 2036 if ($err ne '') { 2037 ERROR("switch and case should be at the same indent\n$hereline$err"); 2038 } 2039 } 2040 2041# if/while/etc brace do not go on next line, unless defining a do while loop, 2042# or if that brace on the next line is for something else 2043 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2044 my $pre_ctx = "$1$2"; 2045 2046 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 2047 my $ctx_cnt = $realcnt - $#ctx - 1; 2048 my $ctx = join("\n", @ctx); 2049 2050 my $ctx_ln = $linenr; 2051 my $ctx_skip = $realcnt; 2052 2053 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2054 defined $lines[$ctx_ln - 1] && 2055 $lines[$ctx_ln - 1] =~ /^-/)) { 2056 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2057 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2058 $ctx_ln++; 2059 } 2060 2061 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 2062 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2063 2064 # The length of the "previous line" is checked against 80 because it 2065 # includes the + at the beginning of the line (if the actual line has 2066 # 79 or 80 characters, it is no longer possible to add a space and an 2067 # opening brace there) 2068 if ($#ctx == 0 && $ctx !~ /{\s*/ && 2069 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ && 2070 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) { 2071 ERROR("that open brace { should be on the previous line\n" . 2072 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 2073 } 2074 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2075 $ctx =~ /\)\s*\;\s*$/ && 2076 defined $lines[$ctx_ln - 1]) 2077 { 2078 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 2079 if ($nindent > $indent) { 2080 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" . 2081 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 2082 } 2083 } 2084 } 2085 2086# 'do ... while (0/false)' only makes sense in macros, without trailing ';' 2087 if ($line =~ /while\s*\((0|false)\);/) { 2088 ERROR("suspicious ; after while (0)\n" . $herecurr); 2089 } 2090 2091# Check superfluous trailing ';' 2092 if ($line =~ /;;$/) { 2093 ERROR("superfluous trailing semicolon\n" . $herecurr); 2094 } 2095 2096# Check relative indent for conditionals and blocks. 2097 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 2098 my ($s, $c) = ($stat, $cond); 2099 2100 substr($s, 0, length($c), ''); 2101 2102 # Make sure we remove the line prefixes as we have 2103 # none on the first line, and are going to re-add them 2104 # where necessary. 2105 $s =~ s/\n./\n/gs; 2106 2107 # Find out how long the conditional actually is. 2108 my @newlines = ($c =~ /\n/gs); 2109 my $cond_lines = 1 + $#newlines; 2110 2111 # We want to check the first line inside the block 2112 # starting at the end of the conditional, so remove: 2113 # 1) any blank line termination 2114 # 2) any opening brace { on end of the line 2115 # 3) any do (...) { 2116 my $continuation = 0; 2117 my $check = 0; 2118 $s =~ s/^.*\bdo\b//; 2119 $s =~ s/^\s*\{//; 2120 if ($s =~ s/^\s*\\//) { 2121 $continuation = 1; 2122 } 2123 if ($s =~ s/^\s*?\n//) { 2124 $check = 1; 2125 $cond_lines++; 2126 } 2127 2128 # Also ignore a loop construct at the end of a 2129 # preprocessor statement. 2130 if (($prevline =~ /^.\s*#\s*define\s/ || 2131 $prevline =~ /\\\s*$/) && $continuation == 0) { 2132 $check = 0; 2133 } 2134 2135 my $cond_ptr = -1; 2136 $continuation = 0; 2137 while ($cond_ptr != $cond_lines) { 2138 $cond_ptr = $cond_lines; 2139 2140 # If we see an #else/#elif then the code 2141 # is not linear. 2142 if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2143 $check = 0; 2144 } 2145 2146 # Ignore: 2147 # 1) blank lines, they should be at 0, 2148 # 2) preprocessor lines, and 2149 # 3) labels. 2150 if ($continuation || 2151 $s =~ /^\s*?\n/ || 2152 $s =~ /^\s*#\s*?/ || 2153 $s =~ /^\s*$Ident\s*:/) { 2154 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 2155 if ($s =~ s/^.*?\n//) { 2156 $cond_lines++; 2157 } 2158 } 2159 } 2160 2161 my (undef, $sindent) = line_stats("+" . $s); 2162 my $stat_real = raw_line($linenr, $cond_lines); 2163 2164 # Check if either of these lines are modified, else 2165 # this is not this patch's fault. 2166 if (!defined($stat_real) || 2167 $stat !~ /^\+/ && $stat_real !~ /^\+/) { 2168 $check = 0; 2169 } 2170 if (defined($stat_real) && $cond_lines > 1) { 2171 $stat_real = "[...]\n$stat_real"; 2172 } 2173 2174 #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"; 2175 2176 if ($check && (($sindent % 4) != 0 || 2177 ($sindent <= $indent && $s ne ''))) { 2178 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 2179 } 2180 } 2181 2182 # Track the 'values' across context and added lines. 2183 my $opline = $line; $opline =~ s/^./ /; 2184 my ($curr_values, $curr_vars) = 2185 annotate_values($opline . "\n", $prev_values); 2186 $curr_values = $prev_values . $curr_values; 2187 if ($dbg_values) { 2188 my $outline = $opline; $outline =~ s/\t/ /g; 2189 print "$linenr > .$outline\n"; 2190 print "$linenr > $curr_values\n"; 2191 print "$linenr > $curr_vars\n"; 2192 } 2193 $prev_values = substr($curr_values, -1); 2194 2195#ignore lines not being added 2196 if ($line=~/^[^\+]/) {next;} 2197 2198# TEST: allow direct testing of the type matcher. 2199 if ($dbg_type) { 2200 if ($line =~ /^.\s*$Declare\s*$/) { 2201 ERROR("TEST: is type\n" . $herecurr); 2202 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2203 ERROR("TEST: is not type ($1 is)\n". $herecurr); 2204 } 2205 next; 2206 } 2207# TEST: allow direct testing of the attribute matcher. 2208 if ($dbg_attr) { 2209 if ($line =~ /^.\s*$Modifier\s*$/) { 2210 ERROR("TEST: is attr\n" . $herecurr); 2211 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2212 ERROR("TEST: is not attr ($1 is)\n". $herecurr); 2213 } 2214 next; 2215 } 2216 2217# check for initialisation to aggregates open brace on the next line 2218 if ($line =~ /^.\s*\{/ && 2219 $prevline =~ /(?:^|[^=])=\s*$/) { 2220 ERROR("that open brace { should be on the previous line\n" . $hereprev); 2221 } 2222 2223# 2224# Checks which are anchored on the added line. 2225# 2226 2227# check for malformed paths in #include statements (uses RAW line) 2228 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2229 my $path = $1; 2230 if ($path =~ m{//}) { 2231 ERROR("malformed #include filename\n" . 2232 $herecurr); 2233 } 2234 } 2235 2236# no C99 // comments 2237 if ($line =~ m{//} && 2238 $rawline !~ m{// SPDX-License-Identifier: }) { 2239 ERROR("do not use C99 // comments\n" . $herecurr); 2240 } 2241 # Remove C99 comments. 2242 $line =~ s@//.*@@; 2243 $opline =~ s@//.*@@; 2244 2245# check for global initialisers. 2246 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2247 ERROR("do not initialise globals to 0 or NULL\n" . 2248 $herecurr); 2249 } 2250# check for static initialisers. 2251 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2252 ERROR("do not initialise statics to 0 or NULL\n" . 2253 $herecurr); 2254 } 2255 2256# * goes on variable not on type 2257 # (char*[ const]) 2258 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 2259 my ($from, $to) = ($1, $1); 2260 2261 # Should start with a space. 2262 $to =~ s/^(\S)/ $1/; 2263 # Should not end with a space. 2264 $to =~ s/\s+$//; 2265 # '*'s should not have spaces between. 2266 while ($to =~ s/\*\s+\*/\*\*/) { 2267 } 2268 2269 #print "from<$from> to<$to>\n"; 2270 if ($from ne $to) { 2271 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 2272 } 2273 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 2274 my ($from, $to, $ident) = ($1, $1, $2); 2275 2276 # Should start with a space. 2277 $to =~ s/^(\S)/ $1/; 2278 # Should not end with a space. 2279 $to =~ s/\s+$//; 2280 # '*'s should not have spaces between. 2281 while ($to =~ s/\*\s+\*/\*\*/) { 2282 } 2283 # Modifiers should have spaces. 2284 $to =~ s/(\b$Modifier$)/$1 /; 2285 2286 #print "from<$from> to<$to> ident<$ident>\n"; 2287 if ($from ne $to && $ident !~ /^$Modifier$/) { 2288 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 2289 } 2290 } 2291 2292# function brace can't be on same line, except for #defines of do while, 2293# or if closed on same line 2294 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and 2295 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { 2296 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 2297 } 2298 2299# open braces for enum, union and struct go on the same line. 2300 if ($line =~ /^.\s*\{/ && 2301 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2302 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 2303 } 2304 2305# missing space after union, struct or enum definition 2306 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2307 ERROR("missing space after $1 definition\n" . $herecurr); 2308 } 2309 2310# check for spacing round square brackets; allowed: 2311# 1. with a type on the left -- int [] a; 2312# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2313# 3. inside a curly brace -- = { [0...10] = 5 } 2314# 4. after a comma -- [1] = 5, [2] = 6 2315# 5. in a macro definition -- #define abc(x) [x] = y 2316 while ($line =~ /(.*?\s)\[/g) { 2317 my ($where, $prefix) = ($-[1], $1); 2318 if ($prefix !~ /$Type\s+$/ && 2319 ($where != 0 || $prefix !~ /^.\s+$/) && 2320 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ && 2321 $prefix !~ /[,{:]\s+$/) { 2322 ERROR("space prohibited before open square bracket '['\n" . $herecurr); 2323 } 2324 } 2325 2326# check for spaces between functions and their parentheses. 2327 while ($line =~ /($Ident)\s+\(/g) { 2328 my $name = $1; 2329 my $ctx_before = substr($line, 0, $-[1]); 2330 my $ctx = "$ctx_before$name"; 2331 2332 # Ignore those directives where spaces _are_ permitted. 2333 if ($name =~ /^(?: 2334 if|for|while|switch|return|case| 2335 volatile|__volatile__|coroutine_fn| 2336 __attribute__|format|__extension__| 2337 asm|__asm__)$/x) 2338 { 2339 2340 # Ignore 'catch (...)' in C++ 2341 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) { 2342 2343 # cpp #define statements have non-optional spaces, ie 2344 # if there is a space between the name and the open 2345 # parenthesis it is simply not a parameter group. 2346 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2347 2348 # cpp #elif statement condition may start with a ( 2349 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2350 2351 # If this whole things ends with a type its most 2352 # likely a typedef for a function. 2353 } elsif ($ctx =~ /$Type$/) { 2354 2355 } else { 2356 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr); 2357 } 2358 } 2359# Check operator spacing. 2360 if (!($line=~/\#\s*(include|import)/)) { 2361 my $ops = qr{ 2362 <<=|>>=|<=|>=|==|!=| 2363 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 2364 =>|->|<<|>>|<|>|=|!|~| 2365 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 2366 \?|::|: 2367 }x; 2368 my @elements = split(/($ops|;)/, $opline); 2369 my $off = 0; 2370 2371 my $blank = copy_spacing($opline); 2372 2373 for (my $n = 0; $n < $#elements; $n += 2) { 2374 $off += length($elements[$n]); 2375 2376 # Pick up the preceding and succeeding characters. 2377 my $ca = substr($opline, 0, $off); 2378 my $cc = ''; 2379 if (length($opline) >= ($off + length($elements[$n + 1]))) { 2380 $cc = substr($opline, $off + length($elements[$n + 1])); 2381 } 2382 my $cb = "$ca$;$cc"; 2383 2384 my $a = ''; 2385 $a = 'V' if ($elements[$n] ne ''); 2386 $a = 'W' if ($elements[$n] =~ /\s$/); 2387 $a = 'C' if ($elements[$n] =~ /$;$/); 2388 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 2389 $a = 'O' if ($elements[$n] eq ''); 2390 $a = 'E' if ($ca =~ /^\s*$/); 2391 2392 my $op = $elements[$n + 1]; 2393 2394 my $c = ''; 2395 if (defined $elements[$n + 2]) { 2396 $c = 'V' if ($elements[$n + 2] ne ''); 2397 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2398 $c = 'C' if ($elements[$n + 2] =~ /^$;/); 2399 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 2400 $c = 'O' if ($elements[$n + 2] eq ''); 2401 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 2402 } else { 2403 $c = 'E'; 2404 } 2405 2406 my $ctx = "${a}x${c}"; 2407 2408 my $at = "(ctx:$ctx)"; 2409 2410 my $ptr = substr($blank, 0, $off) . "^"; 2411 my $hereptr = "$hereline$ptr\n"; 2412 2413 # Pull out the value of this operator. 2414 my $op_type = substr($curr_values, $off + 1, 1); 2415 2416 # Get the full operator variant. 2417 my $opv = $op . substr($curr_vars, $off, 1); 2418 2419 # Ignore operators passed as parameters. 2420 if ($op_type ne 'V' && 2421 $ca =~ /\s$/ && $cc =~ /^\s*,/) { 2422 2423# # Ignore comments 2424# } elsif ($op =~ /^$;+$/) { 2425 2426 # ; should have either the end of line or a space or \ after it 2427 } elsif ($op eq ';') { 2428 if ($ctx !~ /.x[WEBC]/ && 2429 $cc !~ /^\\/ && $cc !~ /^;/) { 2430 ERROR("space required after that '$op' $at\n" . $hereptr); 2431 } 2432 2433 # // is a comment 2434 } elsif ($op eq '//') { 2435 2436 # Ignore : used in class declaration in C++ 2437 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ && 2438 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) { 2439 2440 # No spaces for: 2441 # -> 2442 # : when part of a bitfield 2443 } elsif ($op eq '->' || $opv eq ':B') { 2444 if ($ctx =~ /Wx.|.xW/) { 2445 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 2446 } 2447 2448 # , must have a space on the right. 2449 # not required when having a single },{ on one line 2450 } elsif ($op eq ',') { 2451 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ && 2452 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") { 2453 ERROR("space required after that '$op' $at\n" . $hereptr); 2454 } 2455 2456 # '*' as part of a type definition -- reported already. 2457 } elsif ($opv eq '*_') { 2458 #warn "'*' is part of type\n"; 2459 2460 # unary operators should have a space before and 2461 # none after. May be left adjacent to another 2462 # unary operator, or a cast 2463 } elsif ($op eq '!' || $op eq '~' || 2464 $opv eq '*U' || $opv eq '-U' || 2465 $opv eq '&U' || $opv eq '&&U') { 2466 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) { 2467 # '~' used as a name of Destructor 2468 2469 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2470 ERROR("space required before that '$op' $at\n" . $hereptr); 2471 } 2472 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2473 # A unary '*' may be const 2474 2475 } elsif ($ctx =~ /.xW/) { 2476 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2477 } 2478 2479 # unary ++ and unary -- are allowed no space on one side. 2480 } elsif ($op eq '++' or $op eq '--') { 2481 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2482 ERROR("space required one side of that '$op' $at\n" . $hereptr); 2483 } 2484 if ($ctx =~ /Wx[BE]/ || 2485 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2486 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2487 } 2488 if ($ctx =~ /ExW/) { 2489 ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2490 } 2491 2492 # A colon needs no spaces before when it is 2493 # terminating a case value or a label. 2494 } elsif ($opv eq ':C' || $opv eq ':L') { 2495 if ($ctx =~ /Wx./) { 2496 ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2497 } 2498 2499 # All the others need spaces both sides. 2500 } elsif ($ctx !~ /[EWC]x[CWE]/) { 2501 my $ok = 0; 2502 2503 if ($realfile =~ /\.cpp|\.h$/) { 2504 # Ignore template arguments <...> in C++ 2505 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) { 2506 $ok = 1; 2507 } 2508 2509 # Ignore :: in C++ 2510 if ($op eq '::') { 2511 $ok = 1; 2512 } 2513 } 2514 2515 # Ignore email addresses <foo@bar> 2516 if (($op eq '<' && 2517 $cc =~ /^\S+\@\S+>/) || 2518 ($op eq '>' && 2519 $ca =~ /<\S+\@\S+$/)) 2520 { 2521 $ok = 1; 2522 } 2523 2524 # Ignore ?: 2525 if (($opv eq ':O' && $ca =~ /\?$/) || 2526 ($op eq '?' && $cc =~ /^:/)) { 2527 $ok = 1; 2528 } 2529 2530 if ($ok == 0) { 2531 ERROR("spaces required around that '$op' $at\n" . $hereptr); 2532 } 2533 } 2534 $off += length($elements[$n + 1]); 2535 } 2536 } 2537 2538#need space before brace following if, while, etc 2539 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 2540 $line =~ /do\{/) { 2541 ERROR("space required before the open brace '{'\n" . $herecurr); 2542 } 2543 2544# closing brace should have a space following it when it has anything 2545# on the line 2546 if ($line =~ /}(?!(?:,|;|\)))\S/) { 2547 ERROR("space required after that close brace '}'\n" . $herecurr); 2548 } 2549 2550# check spacing on square brackets 2551 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2552 ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 2553 } 2554 if ($line =~ /\s\]/) { 2555 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 2556 } 2557 2558# check spacing on parentheses 2559 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 2560 $line !~ /for\s*\(\s+;/) { 2561 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 2562 } 2563 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2564 $line !~ /for\s*\(.*;\s+\)/ && 2565 $line !~ /:\s+\)/) { 2566 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 2567 } 2568 2569# Return is not a function. 2570 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2571 my $spacing = $1; 2572 my $value = $2; 2573 2574 # Flatten any parentheses 2575 $value =~ s/\(/ \(/g; 2576 $value =~ s/\)/\) /g; 2577 while ($value =~ s/\[[^\{\}]*\]/1/ || 2578 $value !~ /(?:$Ident|-?$Constant)\s* 2579 $Compare\s* 2580 (?:$Ident|-?$Constant)/x && 2581 $value =~ s/\([^\(\)]*\)/1/) { 2582 } 2583#print "value<$value>\n"; 2584 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ && 2585 $line =~ /;$/) { 2586 ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2587 2588 } elsif ($spacing !~ /\s+/) { 2589 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2590 } 2591 } 2592# Return of what appears to be an errno should normally be -'ve 2593 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 2594 my $name = $1; 2595 if ($name ne 'EOF' && $name ne 'ERROR') { 2596 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr); 2597 } 2598 } 2599 2600 if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ && 2601 $line !~ /^.typedef/) { 2602 ERROR("named $1 should be typedefed separately\n" . $herecurr); 2603 } 2604 2605# Need a space before open parenthesis after if, while etc 2606 if ($line=~/\b(if|while|for|switch)\(/) { 2607 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2608 } 2609 2610# Check for illegal assignment in if conditional -- and check for trailing 2611# statements after the conditional. 2612 if ($line =~ /do\s*(?!{)/) { 2613 my ($stat_next) = ctx_statement_block($line_nr_next, 2614 $remain_next, $off_next); 2615 $stat_next =~ s/\n./\n /g; 2616 ##print "stat<$stat> stat_next<$stat_next>\n"; 2617 2618 if ($stat_next =~ /^\s*while\b/) { 2619 # If the statement carries leading newlines, 2620 # then count those as offsets. 2621 my ($whitespace) = 2622 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2623 my $offset = 2624 statement_rawlines($whitespace) - 1; 2625 2626 $suppress_whiletrailers{$line_nr_next + 2627 $offset} = 1; 2628 } 2629 } 2630 if (!defined $suppress_whiletrailers{$linenr} && 2631 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2632 my ($s, $c) = ($stat, $cond); 2633 2634 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2635 ERROR("do not use assignment in if condition\n" . $herecurr); 2636 } 2637 2638 # Find out what is on the end of the line after the 2639 # conditional. 2640 substr($s, 0, length($c), ''); 2641 $s =~ s/\n.*//g; 2642 $s =~ s/$;//g; # Remove any comments 2643 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 2644 $c !~ /}\s*while\s*/) 2645 { 2646 # Find out how long the conditional actually is. 2647 my @newlines = ($c =~ /\n/gs); 2648 my $cond_lines = 1 + $#newlines; 2649 my $stat_real = ''; 2650 2651 $stat_real = raw_line($linenr, $cond_lines) 2652 . "\n" if ($cond_lines); 2653 if (defined($stat_real) && $cond_lines > 1) { 2654 $stat_real = "[...]\n$stat_real"; 2655 } 2656 2657 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 2658 } 2659 } 2660 2661# Check for bitwise tests written as boolean 2662 if ($line =~ / 2663 (?: 2664 (?:\[|\(|\&\&|\|\|) 2665 \s*0[xX][0-9]+\s* 2666 (?:\&\&|\|\|) 2667 | 2668 (?:\&\&|\|\|) 2669 \s*0[xX][0-9]+\s* 2670 (?:\&\&|\|\||\)|\]) 2671 )/x) 2672 { 2673 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 2674 } 2675 2676# if and else should not have general statements after it 2677 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 2678 my $s = $1; 2679 $s =~ s/$;//g; # Remove any comments 2680 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2681 ERROR("trailing statements should be on next line\n" . $herecurr); 2682 } 2683 } 2684# if should not continue a brace 2685 if ($line =~ /}\s*if\b/) { 2686 ERROR("trailing statements should be on next line\n" . 2687 $herecurr); 2688 } 2689# case and default should not have general statements after them 2690 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2691 $line !~ /\G(?: 2692 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2693 \s*return\s+ 2694 )/xg) 2695 { 2696 ERROR("trailing statements should be on next line\n" . $herecurr); 2697 } 2698 2699 # Check for }<nl>else {, these must be at the same 2700 # indent level to be relevant to each other. 2701 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 2702 $previndent == $indent) { 2703 ERROR("else should follow close brace '}'\n" . $hereprev); 2704 } 2705 2706 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2707 $previndent == $indent) { 2708 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2709 2710 # Find out what is on the end of the line after the 2711 # conditional. 2712 substr($s, 0, length($c), ''); 2713 $s =~ s/\n.*//g; 2714 2715 if ($s =~ /^\s*;/) { 2716 ERROR("while should follow close brace '}'\n" . $hereprev); 2717 } 2718 } 2719 2720#studly caps, commented out until figure out how to distinguish between use of existing and adding new 2721# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 2722# print "No studly caps, use _\n"; 2723# print "$herecurr"; 2724# $clean = 0; 2725# } 2726 2727#no spaces allowed after \ in define 2728 if ($line=~/\#\s*define.*\\\s$/) { 2729 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr); 2730 } 2731 2732# multi-statement macros should be enclosed in a do while loop, grab the 2733# first statement and ensure its the whole macro if its not enclosed 2734# in a known good container 2735 if ($realfile !~ m@/vmlinux.lds.h$@ && 2736 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2737 my $ln = $linenr; 2738 my $cnt = $realcnt; 2739 my ($off, $dstat, $dcond, $rest); 2740 my $ctx = ''; 2741 2742 my $args = defined($1); 2743 2744 # Find the end of the macro and limit our statement 2745 # search to that. 2746 while ($cnt > 0 && defined $lines[$ln - 1] && 2747 $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2748 { 2749 $ctx .= $rawlines[$ln - 1] . "\n"; 2750 $cnt-- if ($lines[$ln - 1] !~ /^-/); 2751 $ln++; 2752 } 2753 $ctx .= $rawlines[$ln - 1]; 2754 2755 ($dstat, $dcond, $ln, $cnt, $off) = 2756 ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2757 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2758 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2759 2760 # Extract the remainder of the define (if any) and 2761 # rip off surrounding spaces, and trailing \'s. 2762 $rest = ''; 2763 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2764 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2765 if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2766 $rest .= substr($lines[$ln - 1], $off) . "\n"; 2767 $cnt--; 2768 } 2769 $ln++; 2770 $off = 0; 2771 } 2772 $rest =~ s/\\\n.//g; 2773 $rest =~ s/^\s*//s; 2774 $rest =~ s/\s*$//s; 2775 2776 # Clean up the original statement. 2777 if ($args) { 2778 substr($dstat, 0, length($dcond), ''); 2779 } else { 2780 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2781 } 2782 $dstat =~ s/$;//g; 2783 $dstat =~ s/\\\n.//g; 2784 $dstat =~ s/^\s*//s; 2785 $dstat =~ s/\s*$//s; 2786 2787 # Flatten any parentheses and braces 2788 while ($dstat =~ s/\([^\(\)]*\)/1/ || 2789 $dstat =~ s/\{[^\{\}]*\}/1/ || 2790 $dstat =~ s/\[[^\{\}]*\]/1/) 2791 { 2792 } 2793 2794 my $exceptions = qr{ 2795 $Declare| 2796 module_param_named| 2797 MODULE_PARAM_DESC| 2798 DECLARE_PER_CPU| 2799 DEFINE_PER_CPU| 2800 __typeof__\(| 2801 union| 2802 struct| 2803 \.$Ident\s*=\s*| 2804 ^\"|\"$ 2805 }x; 2806 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2807 if ($rest ne '' && $rest ne ',') { 2808 if ($rest !~ /while\s*\(/ && 2809 $dstat !~ /$exceptions/) 2810 { 2811 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2812 } 2813 2814 } elsif ($ctx !~ /;/) { 2815 if ($dstat ne '' && 2816 $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2817 $dstat !~ /$exceptions/ && 2818 $dstat !~ /^\.$Ident\s*=/ && 2819 $dstat =~ /$Operators/) 2820 { 2821 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2822 } 2823 } 2824 } 2825 2826# check for missing bracing around if etc 2827 if ($line =~ /(^.*)\b(?:if|while|for)\b/ && 2828 $line !~ /\#\s*if/) { 2829 my $allowed = 0; 2830 2831 # Check the pre-context. 2832 if ($line =~ /(\}.*?)$/) { 2833 my $pre = $1; 2834 2835 if ($line !~ /else/) { 2836 print "APW: ALLOWED: pre<$pre> line<$line>\n" 2837 if $dbg_adv_apw; 2838 $allowed = 1; 2839 } 2840 } 2841 my ($level, $endln, @chunks) = 2842 ctx_statement_full($linenr, $realcnt, 1); 2843 if ($dbg_adv_apw) { 2844 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2845 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n" 2846 if $#chunks >= 1; 2847 } 2848 if ($#chunks >= 0 && $level == 0) { 2849 my $seen = 0; 2850 my $herectx = $here . "\n"; 2851 my $ln = $linenr - 1; 2852 for my $chunk (@chunks) { 2853 my ($cond, $block) = @{$chunk}; 2854 2855 # If the condition carries leading newlines, then count those as offsets. 2856 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2857 my $offset = statement_rawlines($whitespace) - 1; 2858 2859 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2860 2861 # We have looked at and allowed this specific line. 2862 $suppress_ifbraces{$ln + $offset} = 1; 2863 2864 $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2865 $ln += statement_rawlines($block) - 1; 2866 2867 substr($block, 0, length($cond), ''); 2868 2869 my $spaced_block = $block; 2870 $spaced_block =~ s/\n\+/ /g; 2871 2872 $seen++ if ($spaced_block =~ /^\s*\{/); 2873 2874 print "APW: cond<$cond> block<$block> allowed<$allowed>\n" 2875 if $dbg_adv_apw; 2876 if (statement_lines($cond) > 1) { 2877 print "APW: ALLOWED: cond<$cond>\n" 2878 if $dbg_adv_apw; 2879 $allowed = 1; 2880 } 2881 if ($block =~/\b(?:if|for|while)\b/) { 2882 print "APW: ALLOWED: block<$block>\n" 2883 if $dbg_adv_apw; 2884 $allowed = 1; 2885 } 2886 if (statement_block_size($block) > 1) { 2887 print "APW: ALLOWED: lines block<$block>\n" 2888 if $dbg_adv_apw; 2889 $allowed = 1; 2890 } 2891 } 2892 if ($seen != ($#chunks + 1) && !$allowed) { 2893 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx); 2894 } 2895 } 2896 } 2897 if (!defined $suppress_ifbraces{$linenr - 1} && 2898 $line =~ /\b(if|while|for|else)\b/ && 2899 $line !~ /\#\s*if/ && 2900 $line !~ /\#\s*else/) { 2901 my $allowed = 0; 2902 2903 # Check the pre-context. 2904 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2905 my $pre = $1; 2906 2907 if ($line !~ /else/) { 2908 print "APW: ALLOWED: pre<$pre> line<$line>\n" 2909 if $dbg_adv_apw; 2910 $allowed = 1; 2911 } 2912 } 2913 2914 my ($level, $endln, @chunks) = 2915 ctx_statement_full($linenr, $realcnt, $-[0]); 2916 2917 # Check the condition. 2918 my ($cond, $block) = @{$chunks[0]}; 2919 print "CHECKING<$linenr> cond<$cond> block<$block>\n" 2920 if $dbg_adv_checking; 2921 if (defined $cond) { 2922 substr($block, 0, length($cond), ''); 2923 } 2924 if (statement_lines($cond) > 1) { 2925 print "APW: ALLOWED: cond<$cond>\n" 2926 if $dbg_adv_apw; 2927 $allowed = 1; 2928 } 2929 if ($block =~/\b(?:if|for|while)\b/) { 2930 print "APW: ALLOWED: block<$block>\n" 2931 if $dbg_adv_apw; 2932 $allowed = 1; 2933 } 2934 if (statement_block_size($block) > 1) { 2935 print "APW: ALLOWED: lines block<$block>\n" 2936 if $dbg_adv_apw; 2937 $allowed = 1; 2938 } 2939 # Check the post-context. 2940 if (defined $chunks[1]) { 2941 my ($cond, $block) = @{$chunks[1]}; 2942 if (defined $cond) { 2943 substr($block, 0, length($cond), ''); 2944 } 2945 if ($block =~ /^\s*\{/) { 2946 print "APW: ALLOWED: chunk-1 block<$block>\n" 2947 if $dbg_adv_apw; 2948 $allowed = 1; 2949 } 2950 } 2951 print "DCS: level=$level block<$block> allowed=$allowed\n" 2952 if $dbg_adv_dcs; 2953 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) { 2954 my $herectx = $here . "\n";; 2955 my $cnt = statement_rawlines($block); 2956 2957 for (my $n = 0; $n < $cnt; $n++) { 2958 $herectx .= raw_line($linenr, $n) . "\n";; 2959 } 2960 2961 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx); 2962 } 2963 } 2964 2965# no volatiles please 2966 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 2967 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ && 2968 $line !~ /sig_atomic_t/ && 2969 !ctx_has_comment($first_line, $linenr)) { 2970 my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr; 2971 ERROR($msg); 2972 } 2973 2974# warn about #if 0 2975 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2976 ERROR("if this code is redundant consider removing it\n" . 2977 $herecurr); 2978 } 2979 2980# check for needless g_free() checks 2981 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2982 my $expr = $1; 2983 if ($line =~ /\bg_free\(\Q$expr\E\);/) { 2984 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev); 2985 } 2986 } 2987 2988# warn about #ifdefs in C files 2989# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 2990# print "#ifdef in C files should be avoided\n"; 2991# print "$herecurr"; 2992# $clean = 0; 2993# } 2994 2995# warn about spacing in #ifdefs 2996 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 2997 ERROR("exactly one space required after that #$1\n" . $herecurr); 2998 } 2999# check for memory barriers without a comment. 3000 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 3001 if (!ctx_has_comment($first_line, $linenr)) { 3002 ERROR("memory barrier without comment\n" . $herecurr); 3003 } 3004 } 3005# check of hardware specific defines 3006# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases 3007# where they might be necessary. 3008 if ($line =~ m@^.\s*\#\s*if.*\b__@) { 3009 WARN("architecture specific defines should be avoided\n" . $herecurr); 3010 } 3011 3012# Check that the storage class is at the beginning of a declaration 3013 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3014 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr) 3015 } 3016 3017# check the location of the inline attribute, that it is between 3018# storage class and type. 3019 if ($line =~ /\b$Type\s+$Inline\b/ || 3020 $line =~ /\b$Inline\s+$Storage\b/) { 3021 ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 3022 } 3023 3024# check for sizeof(&) 3025 if ($line =~ /\bsizeof\s*\(\s*\&/) { 3026 ERROR("sizeof(& should be avoided\n" . $herecurr); 3027 } 3028 3029# check for new externs in .c files. 3030 if ($realfile =~ /\.c$/ && defined $stat && 3031 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3032 { 3033 my $function_name = $1; 3034 my $paren_space = $2; 3035 3036 my $s = $stat; 3037 if (defined $cond) { 3038 substr($s, 0, length($cond), ''); 3039 } 3040 if ($s =~ /^\s*;/ && 3041 $function_name ne 'uninitialized_var') 3042 { 3043 ERROR("externs should be avoided in .c files\n" . $herecurr); 3044 } 3045 3046 if ($paren_space =~ /\n/) { 3047 ERROR("arguments for function declarations should follow identifier\n" . $herecurr); 3048 } 3049 3050 } elsif ($realfile =~ /\.c$/ && defined $stat && 3051 $stat =~ /^.\s*extern\s+/) 3052 { 3053 ERROR("externs should be avoided in .c files\n" . $herecurr); 3054 } 3055 3056# check for pointless casting of g_malloc return 3057 if ($line =~ /\*\s*\)\s*g_(try|)(m|re)alloc(0?)(_n)?\b/) { 3058 if ($2 eq 'm') { 3059 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr); 3060 } else { 3061 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr); 3062 } 3063 } 3064 3065# check for gcc specific __FUNCTION__ 3066 if ($line =~ /__FUNCTION__/) { 3067 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 3068 } 3069 3070# recommend g_path_get_* over g_strdup(basename/dirname(...)) 3071 if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) { 3072 WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr); 3073 } 3074 3075# enforce g_memdup2() over g_memdup() 3076 if ($line =~ /\bg_memdup\s*\(/) { 3077 ERROR("use g_memdup2() instead of unsafe g_memdup()\n" . $herecurr); 3078 } 3079 3080# recommend qemu_strto* over strto* for numeric conversions 3081 if ($line =~ /\b(strto[^kd].*?)\s*\(/) { 3082 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr); 3083 } 3084# recommend sigaction over signal for portability, when establishing a handler 3085 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) { 3086 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); 3087 } 3088# recommend qemu_bh_new_guarded instead of qemu_bh_new 3089 if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\bqemu_bh_new\s*\(/) { 3090 ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reentrancy problems\n" . $herecurr); 3091 } 3092# recommend aio_bh_new_guarded instead of aio_bh_new 3093 if ($realfile =~ /.*\/hw\/.*/ && $line =~ /\baio_bh_new\s*\(/) { 3094 ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentrancy problems\n" . $herecurr); 3095 } 3096# check for module_init(), use category-specific init macros explicitly please 3097 if ($line =~ /^module_init\s*\(/) { 3098 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr); 3099 } 3100# check for various ops structs, ensure they are const. 3101 my $struct_ops = qr{AIOCBInfo| 3102 BdrvActionOps| 3103 BlockDevOps| 3104 BlockJobDriver| 3105 DisplayChangeListenerOps| 3106 GraphicHwOps| 3107 IDEDMAOps| 3108 KVMCapabilityInfo| 3109 MemoryRegionIOMMUOps| 3110 MemoryRegionOps| 3111 MemoryRegionPortio| 3112 QEMUFileOps| 3113 SCSIBusInfo| 3114 SCSIReqOps| 3115 Spice[A-Z][a-zA-Z0-9]*Interface| 3116 TypeInfo| 3117 USBDesc[A-Z][a-zA-Z0-9]*| 3118 VhostOps| 3119 VMStateDescription| 3120 VMStateInfo}x; 3121 if ($line !~ /\bconst\b/ && 3122 $line =~ /\b($struct_ops)\b.*=/) { 3123 ERROR("initializer for struct $1 should normally be const\n" . 3124 $herecurr); 3125 } 3126 3127# format strings checks 3128 my $string; 3129 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 3130 $string = substr($rawline, $-[1], $+[1] - $-[1]); 3131 $string =~ s/%%/__/g; 3132 # check for %L{u,d,i} in strings 3133 if ($string =~ /(?<!%)%L[udi]/) { 3134 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 3135 } 3136 # check for %# or %0# in printf-style format strings 3137 if ($string =~ /(?<!%)%0?#/) { 3138 ERROR("Don't use '#' flag of printf format " . 3139 "('%#') in format strings, use '0x' " . 3140 "prefix instead\n" . $herecurr); 3141 } 3142 } 3143 3144# QEMU specific tests 3145 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) { 3146 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr); 3147 } 3148 3149# Qemu error function tests 3150 3151 # Find newlines in error messages 3152 my $qemu_error_funcs = qr{error_setg| 3153 error_setg_errno| 3154 error_setg_win32| 3155 error_setg_file_open| 3156 error_set| 3157 error_prepend| 3158 warn_reportf_err| 3159 error_reportf_err| 3160 error_vreport| 3161 warn_vreport| 3162 info_vreport| 3163 error_report| 3164 warn_report| 3165 info_report| 3166 g_test_message}x; 3167 3168 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) { 3169 ERROR("Error messages should not contain newlines\n" . $herecurr); 3170 } 3171 3172 # Continue checking for error messages that contains newlines. This 3173 # check handles cases where string literals are spread over multiple lines. 3174 # Example: 3175 # error_report("Error msg line #1" 3176 # "Error msg line #2\n"); 3177 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"}; 3178 my $continued_str_literal = qr{\+\s*\".*\"}; 3179 3180 if ($rawline =~ /$quoted_newline_regex/) { 3181 # Backtrack to first line that does not contain only a quoted literal 3182 # and assume that it is the start of the statement. 3183 my $i = $linenr - 2; 3184 3185 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) { 3186 $i--; 3187 } 3188 3189 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) { 3190 ERROR("Error messages should not contain newlines\n" . $herecurr); 3191 } 3192 } 3193 3194# check for non-portable libc calls that have portable alternatives in QEMU 3195 if ($line =~ /\bffs\(/) { 3196 ERROR("use ctz32() instead of ffs()\n" . $herecurr); 3197 } 3198 if ($line =~ /\bffsl\(/) { 3199 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr); 3200 } 3201 if ($line =~ /\bffsll\(/) { 3202 ERROR("use ctz64() instead of ffsll()\n" . $herecurr); 3203 } 3204 if ($line =~ /\bbzero\(/) { 3205 ERROR("use memset() instead of bzero()\n" . $herecurr); 3206 } 3207 if ($line =~ /\bgetpagesize\(\)/) { 3208 ERROR("use qemu_real_host_page_size() instead of getpagesize()\n" . $herecurr); 3209 } 3210 if ($line =~ /\bsysconf\(_SC_PAGESIZE\)/) { 3211 ERROR("use qemu_real_host_page_size() instead of sysconf(_SC_PAGESIZE)\n" . $herecurr); 3212 } 3213 if ($line =~ /\b(g_)?assert\(0\)/) { 3214 ERROR("use g_assert_not_reached() instead of assert(0)\n" . $herecurr); 3215 } 3216 if ($line =~ /\b(g_)?assert\(false\)/) { 3217 ERROR("use g_assert_not_reached() instead of assert(false)\n" . 3218 $herecurr); 3219 } 3220 if ($line =~ /\bstrerrorname_np\(/) { 3221 ERROR("use strerror() instead of strerrorname_np()\n" . $herecurr); 3222 } 3223 my $non_exit_glib_asserts = qr{g_assert_cmpstr| 3224 g_assert_cmpint| 3225 g_assert_cmpuint| 3226 g_assert_cmphex| 3227 g_assert_cmpfloat| 3228 g_assert_true| 3229 g_assert_false| 3230 g_assert_nonnull| 3231 g_assert_null| 3232 g_assert_no_error| 3233 g_assert_error| 3234 g_test_assert_expected_messages| 3235 g_test_trap_assert_passed| 3236 g_test_trap_assert_stdout| 3237 g_test_trap_assert_stdout_unmatched| 3238 g_test_trap_assert_stderr| 3239 g_test_trap_assert_stderr_unmatched}x; 3240 if ($realfile !~ /^tests\// && 3241 $line =~ /\b(?:$non_exit_glib_asserts)\(/) { 3242 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr); 3243 } 3244 } 3245 3246 if ($is_patch && $chk_signoff && $signoff == 0) { 3247 ERROR("Missing Signed-off-by: line(s)\n"); 3248 } 3249 3250 # If we have no input at all, then there is nothing to report on 3251 # so just keep quiet. 3252 if ($#rawlines == -1) { 3253 return 1; 3254 } 3255 3256 # In mailback mode only produce a report in the negative, for 3257 # things that appear to be patches. 3258 if ($mailback && ($clean == 1 || !$is_patch)) { 3259 return 1; 3260 } 3261 3262 # This is not a patch, and we are are in 'no-patch' mode so 3263 # just keep quiet. 3264 if (!$chk_patch && !$is_patch) { 3265 return 1; 3266 } 3267 3268 if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 3269 ERROR("Does not appear to be a unified-diff format patch\n"); 3270 } 3271 3272 print report_dump(); 3273 if ($summary && !($clean == 1 && $quiet == 1)) { 3274 print "$filename " if ($summary_file); 3275 print "total: $cnt_error errors, $cnt_warn warnings, " . 3276 "$cnt_lines lines checked\n"; 3277 print "\n" if ($quiet == 0); 3278 } 3279 3280 if ($quiet == 0) { 3281 # If there were whitespace errors which cleanpatch can fix 3282 # then suggest that. 3283# if ($rpt_cleaners) { 3284# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 3285# print " scripts/cleanfile\n\n"; 3286# } 3287 } 3288 3289 if ($clean == 1 && $quiet == 0) { 3290 print "$vname has no obvious style problems and is ready for submission.\n" 3291 } 3292 if ($clean == 0 && $quiet == 0) { 3293 print "$vname has style problems, please review. If any of these errors\n"; 3294 print "are false positives report them to the maintainer, see\n"; 3295 print "CHECKPATCH in MAINTAINERS.\n"; 3296 } 3297 3298 return ($no_warnings ? $clean : $cnt_error == 0); 3299} 3300