From nobody Sat Mar 25 17:24:54 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PkR3y1RCjz41Tfv for ; Sat, 25 Mar 2023 17:32:46 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PkR3x4mMXz4N4D for ; Sat, 25 Mar 2023 17:32:45 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-ed1-x533.google.com with SMTP id y4so20055973edo.2 for ; Sat, 25 Mar 2023 10:32:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; t=1679765564; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=wKEkJPPYazbsf5iVeTiA5BWQwVqFs5GB8pjrV+t9v1M=; b=24ef7Houoz9FmUNBh+RPPGSs8w0GxGCskIMA/bfwQtRLBlImDn0STEPXBkUJw0NI+W e1T2mX/CNTTu1divt4K+BWYwpquQblnwxkxIF6G6Z4Rp3y2CO96Sn2Ie684la1GuGh+f ILwr+zZjqn6fPL7x8dvkNuaTuMoiUJxpkXW0vaWA+J6Sp8A8xN9QaMvOT5IDP+l1ilRQ YvFRQ8XcoNBH0/kAK7JAkj0nIiUCwtUzQcUd7jlYYl6Y3FITvqDokJ4V2P51gWr2FX71 d/WeEcDmF7o+AufVPQkEg7BgsLRmPutVDUyioHEuawiT/ETQ5rK9TTKy17zLw04rd+pU iKbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679765564; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wKEkJPPYazbsf5iVeTiA5BWQwVqFs5GB8pjrV+t9v1M=; b=JXIVf+DTce9r2bNOHmrnj8TV36ShsqmddokkgeYOd697mj0CHEGpFTOp49Bg76M5ku I6MFqmvlPyOZtBXVTuPJmPEFNMvun6HUT7v+umQfhNgzRgkvDjq2xDTj06mdk3DhzVCG dnMitnfbojoYLWsunW4tb6JPB/3Ub0itGXsE5DetvDzJMwMG77bRcHpz5VCG/7YMoPLp 2GyqyuBGCeOownYV37KozyxWq49lez9Ul+mUy6RspPsqnXpARGh7dBn9ay97b9K5bSGo JLferL9D0fWWtYpFNndV48LqilNslXdGPXuHwiz9clZN56H/TsfMhsZWo7tYwcGrvhPh wOuQ== X-Gm-Message-State: AAQBX9dg3YaP7HDWRv5fhbIc31KFyr7qtB7VtOphDH12rN+H/DAjZ84q HCwCIcRTXOyEVrMfWEyRNa5s3IS93qnF4xWjf6sljvkp/PXf88eSofc= X-Google-Smtp-Source: AKy350attlIJkvLsFhRxpIxm+S/JhnIKU3/Bw3fXJ1hUiMJy1RvigavGSnV5gL0NNrNL+vVdM0NCalhOUw//BbHpRLs= X-Received: by 2002:a50:d68c:0:b0:4fb:80cf:898b with SMTP id r12-20020a50d68c000000b004fb80cf898bmr3087599edi.7.1679765105457; Sat, 25 Mar 2023 10:25:05 -0700 (PDT) List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 References: <202303251708.32PH8BUq079177@gitrepo.freebsd.org> <5b9c31d2-4aaf-4822-b405-cae57164f314@app.fastmail.com> In-Reply-To: <5b9c31d2-4aaf-4822-b405-cae57164f314@app.fastmail.com> From: Warner Losh Date: Sat, 25 Mar 2023 18:24:54 +0100 Message-ID: Subject: Re: git: 3a3c9242739e - main - checkstyle9.pl: Perl script to check if a change is approximately style(9) To: Drew Gallatin Cc: Warner Losh , src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Content-Type: multipart/alternative; boundary="0000000000006acd1f05f7bcca27" X-Rspamd-Queue-Id: 4PkR3x4mMXz4N4D X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N --0000000000006acd1f05f7bcca27 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Yea, I did most of the work on this 18 months ago or so, but the shit in core and subsequent nastiness derailed it til now. It's gotten to 'decent or better' at things, but far from perfect. I'm sure that now the bug reports will come. Warner On Sat, Mar 25, 2023 at 6:22=E2=80=AFPM Drew Gallatin wrote: > Thanks so much. I've wanted something like this *forever* > > On Sat, Mar 25, 2023, at 1:08 PM, Warner Losh wrote: > > The branch main has been updated by imp: > > URL: > https://cgit.FreeBSD.org/src/commit/?id=3D3a3c9242739efb0c76587ffbaa54c5d= 10b2cbcb4 > > commit 3a3c9242739efb0c76587ffbaa54c5d10b2cbcb4 > Author: Warner Losh > AuthorDate: 2023-03-14 21:28:05 +0000 > Commit: Warner Losh > CommitDate: 2023-03-25 17:06:13 +0000 > > checkstyle9.pl: Perl script to check if a change is approximately > style(9) > > This code is adapted from the QEMU checkpatch.pl script. It can check > either a patch, a file or a git branch. It tries to warn about things > that I believe might be style(9) violations. It's experimental, since= I > heavily hacked on the qemu version to get it to not complain (much) > about iconic code in the tree. At the moment, it's use should be > considered expermental. It will likely miss violations, and complain > about code that's perfectly fine. It's offered as an experiment > and to make it easier for contributors to submit patches. > --- > tools/build/checkstyle9.pl | 2748 > ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 2748 insertions(+) > > diff --git a/tools/build/checkstyle9.pl b/tools/build/checkstyle9.pl > new file mode 100755 > index 000000000000..5aec3819bf7c > --- /dev/null > +++ b/tools/build/checkstyle9.pl > @@ -0,0 +1,2748 @@ > +#!/usr/bin/env perl > +# (c) 2001, Dave Jones. (the file handling bit) > +# (c) 2005, Joel Schopp (the ugly bit) > +# (c) 2007,2008, Andy Whitcroft (new conditions, test > suite) > +# (c) 2008-2010 Andy Whitcroft > +# Licensed under the terms of the GNU GPL License version 2 > + > +use strict; > +use warnings; > +use Term::ANSIColor qw(:constants); > + > +my $P =3D $0; > +$P =3D~ s@.*/@@g; > + > +our $SrcFile =3D qr{\.(?:h|c|cpp|s|S|pl|py|sh)$}; > + > +my $V =3D '0.31'; > + > +use Getopt::Long qw(:config no_auto_abbrev); > + > +my $quiet =3D 0; > +my $tree =3D 1; > +my $chk_signoff =3D 1; > +my $chk_patch =3D undef; > +my $chk_branch =3D undef; > +my $tst_only; > +my $emacs =3D 0; > +my $terse =3D 0; > +my $file =3D undef; > +my $color =3D "auto"; > +my $no_warnings =3D 0; > +my $summary =3D 1; > +my $mailback =3D 0; > +my $summary_file =3D 0; > +my $root; > +my %debug; > +my $help =3D 0; > + > +sub help { > + my ($exitcode) =3D @_; > + > + print << "EOM"; > +Usage: > + > + $P [OPTION]... [FILE]... > + $P [OPTION]... [GIT-REV-LIST] > + > +Version: $V > + > +Options: > + -q, --quiet quiet > + --patch treat FILE as patchfile > + --branch treat args as GIT revision list > + --emacs emacs compile window format > + --terse one line per report > + -f, --file treat FILE as regular source file > + --strict fail if only warnings are found > + --no-summary suppress the per-file summary > + --mailback only produce a report in case of > warnings/errors > + --summary-file include the filename in summary > + --debug KEY=3D[0|1] turn on/off debugging of KEY, where KEY i= s > one of > + 'values', 'possible', 'type', and 'attr' > (default > + is all off) > + --test-only=3DWORD report only warnings/errors containing WO= RD > + literally > + --color[=3DWHEN] Use colors 'always', 'never', or only whe= n > output > + is a terminal ('auto'). Default is 'auto'. > + -h, --help, --version display this help and exit > + > +When FILE is - read standard input. > +EOM > + > + exit($exitcode); > +} > + > +# Use at your own risk > +print "\n", MAGENTA, "WARNING:", RESET, " This code is highly > experimental ... likely isn't a great style(9) match yet\n\n"; > + > +# Perl's Getopt::Long allows options to take optional arguments after a > space. > +# Prevent --color by itself from consuming other arguments > +foreach (@ARGV) { > + if ($_ eq "--color" || $_ eq "-color") { > + $_ =3D "--color=3D$color"; > + } > +} > + > +GetOptions( > + 'q|quiet+' =3D> \$quiet, > + 'tree!' =3D> \$tree, > + 'signoff!' =3D> \$chk_signoff, > + 'patch!' =3D> \$chk_patch, > + 'branch!' =3D> \$chk_branch, > + 'emacs!' =3D> \$emacs, > + 'terse!' =3D> \$terse, > + 'f|file!' =3D> \$file, > + 'strict!' =3D> \$no_warnings, > + 'root=3Ds' =3D> \$root, > + 'summary!' =3D> \$summary, > + 'mailback!' =3D> \$mailback, > + 'summary-file!' =3D> \$summary_file, > + > + 'debug=3Ds' =3D> \%debug, > + 'test-only=3Ds' =3D> \$tst_only, > + 'color=3Ds' =3D> \$color, > + 'no-color' =3D> sub { $color =3D 'never'; }, > + 'h|help' =3D> \$help, > + 'version' =3D> \$help > +) or help(1); > + > +help(0) if ($help); > + > +my $exit =3D 0; > + > +if ($#ARGV < 0) { > + print "$P: no input files\n"; > + exit(1); > +} > + > +if (!defined $chk_branch && !defined $chk_patch && !defined $file) { > + $chk_branch =3D $ARGV[0] =3D~ /.\.\./ ? 1 : 0; > + $file =3D $ARGV[0] =3D~ /$SrcFile/ ? 1 : 0; > + $chk_patch =3D $chk_branch || $file ? 0 : 1; > +} elsif (!defined $chk_branch && !defined $chk_patch) { > + if ($file) { > + $chk_branch =3D $chk_patch =3D 0; > + } else { > + $chk_branch =3D $ARGV[0] =3D~ /.\.\./ ? 1 : 0; > + $chk_patch =3D $chk_branch ? 0 : 1; > + } > +} elsif (!defined $chk_branch && !defined $file) { > + if ($chk_patch) { > + $chk_branch =3D $file =3D 0; > + } else { > + $chk_branch =3D $ARGV[0] =3D~ /.\.\./ ? 1 : 0; > + $file =3D $chk_branch ? 0 : 1; > + } > +} elsif (!defined $chk_patch && !defined $file) { > + if ($chk_branch) { > + $chk_patch =3D $file =3D 0; > + } else { > + $file =3D $ARGV[0] =3D~ /$SrcFile/ ? 1 : 0; > + $chk_patch =3D $file ? 0 : 1; > + } > +} elsif (!defined $chk_branch) { > + $chk_branch =3D $chk_patch || $file ? 0 : 1; > +} elsif (!defined $chk_patch) { > + $chk_patch =3D $chk_branch || $file ? 0 : 1; > +} elsif (!defined $file) { > + $file =3D $chk_patch || $chk_branch ? 0 : 1; > +} > + > +if (($chk_patch && $chk_branch) || > + ($chk_patch && $file) || > + ($chk_branch && $file)) { > + die "Only one of --file, --branch, --patch is permitted\n"; > +} > +if (!$chk_patch && !$chk_branch && !$file) { > + die "One of --file, --branch, --patch is required\n"; > +} > + > +if ($color =3D~ /^always$/i) { > + $color =3D 1; > +} elsif ($color =3D~ /^never$/i) { > + $color =3D 0; > +} elsif ($color =3D~ /^auto$/i) { > + $color =3D (-t STDOUT); > +} else { > + die "Invalid color mode: $color\n"; > +} > + > +my $dbg_values =3D 0; > +my $dbg_possible =3D 0; > +my $dbg_type =3D 0; > +my $dbg_attr =3D 0; > +my $dbg_adv_dcs =3D 0; > +my $dbg_adv_checking =3D 0; > +my $dbg_adv_apw =3D 0; > +for my $key (keys %debug) { > + ## no critic > + eval "\${dbg_$key} =3D '$debug{$key}';"; > + die "$@" if ($@); > +} > + > +my $rpt_cleaners =3D 0; > + > +if ($terse) { > + $emacs =3D 1; > + $quiet++; > +} > + > +my $emitted_corrupt =3D 0; > + > +our $Ident =3D qr{ > + [A-Za-z_][A-Za-z\d_]* > + (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* > + }x; > +our $Storage =3D qr{extern|static|asmlinkage}; > +our $Sparse =3D qr{ > + __force > + }x; > + > +# Notes to $Attribute: > +our $Attribute =3D qr{ > + const| > + volatile| > + QEMU_NORETURN| > + QEMU_WARN_UNUSED_RESULT| > + QEMU_SENTINEL| > + QEMU_PACKED| > + GCC_FMT_ATTR > + }x; > +our $Modifier; > +our $Inline =3D qr{inline}; > +our $Member =3D qr{->$Ident|\.$Ident|\[[^]]*\]}; > +our $Lval =3D qr{$Ident(?:$Member)*}; > + > +our $Constant =3D qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; > +our $Assignment =3D qr{(?:\*\=3D|/=3D|%=3D|\+=3D|-=3D|<<=3D|>>=3D|&=3D|\= ^=3D|\|=3D|=3D)}; > +our $Compare =3D qr{<=3D|>=3D|=3D=3D|!=3D|<|>}; > +our $Operators =3D qr{ > + <=3D|>=3D|=3D=3D|!=3D| > + =3D>|->|<<|>>|<|>|!|~| > + &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% > + }x; > + > +our $NonptrType; > +our $Type; > +our $Declare; > + > +our $NON_ASCII_UTF8 =3D qr{ > + [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte > + | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs > + | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte > + | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates > + | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 > + | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 > + | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 > +}x; > + > +our $UTF8 =3D qr{ > + [\x09\x0A\x0D\x20-\x7E] # ASCII > + | $NON_ASCII_UTF8 > +}x; > + > +# some readers default to ISO-8859-1 when showing email source. detect > +# when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back= . > +# False positives are possible but very unlikely. > +our $UTF8_MOJIBAKE =3D qr{ > + \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf > + | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf > + | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf > 80-bf > + | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf > + | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-= bf > + | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf > 80-bf > + | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-= bf > +}x; > + > +# There are still some false positives, but this catches most > +# common cases. > +our $typeTypedefs =3D qr{(?x: > + (?![KMGTPE]iB) # IEC binary prefix (do not > match) > + [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase > + | [A-Z][A-Z\d_]*AIOCB # all uppercase > + | [A-Z][A-Z\d_]*CPU # all uppercase > + | QEMUBH # all uppercase > +)}; > + > +our @typeList =3D ( > + qr{void}, > + qr{(?:unsigned\s+)?char}, > + qr{(?:unsigned\s+)?short}, > + qr{(?:unsigned\s+)?int}, > + qr{(?:unsigned\s+)?long}, > + qr{(?:unsigned\s+)?long\s+int}, > + qr{(?:unsigned\s+)?long\s+long}, > + qr{(?:unsigned\s+)?long\s+long\s+int}, > + qr{unsigned}, > + qr{float}, > + qr{double}, > + qr{bool}, > + qr{struct\s+$Ident}, > + qr{union\s+$Ident}, > + qr{enum\s+$Ident}, > + qr{${Ident}_t}, > + qr{${Ident}_handler}, > + qr{${Ident}_handler_fn}, > + qr{target_(?:u)?long}, > + qr{hwaddr}, > +); > + > +# This can be modified by sub possible. Since it can be empty, be caref= ul > +# about regexes that always match, because they can cause infinite loops= . > +our @modifierList =3D ( > +); > + > +sub build_types { > + my $all =3D "(?x: \n" . join("|\n ", @typeList) . "\n)"; > + if (@modifierList > 0) { > + my $mods =3D "(?x: \n" . join("|\n ", @modifierList) . "\n)"; > + $Modifier =3D qr{(?:$Attribute|$Sparse|$mods)}; > + } else { > + $Modifier =3D qr{(?:$Attribute|$Sparse)}; > + } > + $NonptrType =3D qr{ > + (?:$Modifier\s+|const\s+)* > + (?: > + (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| > + (?:$typeTypedefs\b)| > + (?:${all}\b) > + ) > + (?:\s+$Modifier|\s+const)* > + }x; > + $Type =3D qr{ > + $NonptrType > + (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? > + (?:\s+$Inline|\s+$Modifier)* > + }x; > + $Declare =3D qr{(?:$Storage\s+)?$Type}; > +} > +build_types(); > + > +$chk_signoff =3D 0 if ($file); > + > +my @rawlines =3D (); > +my @lines =3D (); > +my $vname; > +if ($chk_branch) { > + my @patches; > + my %git_commits =3D (); > + my $HASH; > + open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format= =3D%H > %s", $ARGV[0]) || > + die "$P: git log --reverse --no-merges --format=3D'%H %s' $ARGV[0] fail= ed > - $!\n"; > + > + for my $line (<$HASH>) { > + $line =3D~ /^([0-9a-fA-F]{40,40}) (.*)$/; > + next if (!defined($1) || !defined($2)); > + my $sha1 =3D $1; > + my $subject =3D $2; > + push(@patches, $sha1); > + $git_commits{$sha1} =3D $subject; > + } > + > + close $HASH; > + > + die "$P: no revisions returned for revlist '$ARGV[0]'\n" > + unless @patches; > + > + my $i =3D 1; > + my $num_patches =3D @patches; > + for my $hash (@patches) { > + my $FILE; > + open($FILE, '-|', "git", "show", "--patch-with-stat", $hash) || > + die "$P: git show $hash - $!\n"; > + while (<$FILE>) { > + chomp; > + push(@rawlines, $_); > + } > + close($FILE); > + $vname =3D substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')'; > + if ($num_patches > 1 && $quiet =3D=3D 0) { > + my $prefix =3D "$i/$num_patches"; > + $prefix =3D BLUE . BOLD . $prefix . RESET if $color; > + print "$prefix Checking commit $vname\n"; > + $vname =3D "Patch $i/$num_patches"; > + } else { > + $vname =3D "Commit " . $vname; > + } > + if (!process($hash)) { > + $exit =3D 1; > + print "\n" if ($num_patches > 1 && $quiet =3D=3D 0); > + } > + @rawlines =3D (); > + @lines =3D (); > + $i++; > + } > +} else { > + for my $filename (@ARGV) { > + my $FILE; > + if ($file) { > + open($FILE, '-|', "diff -u /dev/null $filename") || > + die "$P: $filename: diff failed - $!\n"; > + } elsif ($filename eq '-') { > + open($FILE, '<&STDIN'); > + } else { > + open($FILE, '<', "$filename") || > + die "$P: $filename: open failed - $!\n"; > + } > + if ($filename eq '-') { > + $vname =3D 'Your patch'; > + } else { > + $vname =3D $filename; > + } > + print "Checking $filename...\n" if @ARGV > 1 && $quiet =3D=3D 0; > + while (<$FILE>) { > + chomp; > + push(@rawlines, $_); > + } > + close($FILE); > + if (!process($filename)) { > + $exit =3D 1; > + } > + @rawlines =3D (); > + @lines =3D (); > + } > +} > + > +exit($exit); > + > +sub top_of_kernel_tree { > + my ($root) =3D @_; > + > + my @tree_check =3D ( > + "Makefile.inc1", "README.md", "sys", > + "usr.sbin" > + ); > + > + foreach my $check (@tree_check) { > + if (! -e $root . '/' . $check) { > + return 0; > + } > + } > + return 1; > +} > + > +sub expand_tabs { > + my ($str) =3D @_; > + > + my $res =3D ''; > + my $n =3D 0; > + for my $c (split(//, $str)) { > + if ($c eq "\t") { > + $res .=3D ' '; > + $n++; > + for (; ($n % 8) !=3D 0; $n++) { > + $res .=3D ' '; > + } > + next; > + } > + $res .=3D $c; > + $n++; > + } > + > + return $res; > +} > +sub copy_spacing { > + (my $res =3D shift) =3D~ tr/\t/ /c; > + return $res; > +} > + > +sub line_stats { > + my ($line) =3D @_; > + > + # Drop the diff line leader and expand tabs > + $line =3D~ s/^.//; > + $line =3D expand_tabs($line); > + > + # Pick the indent from the front of the line. > + my ($white) =3D ($line =3D~ /^(\s*)/); > + > + return (length($line), length($white)); > +} > + > +my $sanitise_quote =3D ''; > + > +sub sanitise_line_reset { > + my ($in_comment) =3D @_; > + > + if ($in_comment) { > + $sanitise_quote =3D '*/'; > + } else { > + $sanitise_quote =3D ''; > + } > +} > +sub sanitise_line { > + my ($line) =3D @_; > + > + my $res =3D ''; > + my $l =3D ''; > + > + my $qlen =3D 0; > + my $off =3D 0; > + my $c; > + > + # Always copy over the diff marker. > + $res =3D substr($line, 0, 1); > + > + for ($off =3D 1; $off < length($line); $off++) { > + $c =3D substr($line, $off, 1); > + > + # Comments we are wacking completely including the begin > + # and end, all to $;. > + if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { > + $sanitise_quote =3D '*/'; > + > + substr($res, $off, 2, "$;$;"); > + $off++; > + next; > + } > + if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { > + $sanitise_quote =3D ''; > + substr($res, $off, 2, "$;$;"); > + $off++; > + next; > + } > + if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { > + $sanitise_quote =3D '//'; > + > + substr($res, $off, 2, $sanitise_quote); > + $off++; > + next; > + } > + > + # A \ in a string means ignore the next character. > + if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && > + $c eq "\\") { > + substr($res, $off, 2, 'XX'); > + $off++; > + next; > + } > + # Regular quotes. > + if ($c eq "'" || $c eq '"') { > + if ($sanitise_quote eq '') { > + $sanitise_quote =3D $c; > + > + substr($res, $off, 1, $c); > + next; > + } elsif ($sanitise_quote eq $c) { > + $sanitise_quote =3D ''; > + } > + } > + > + #print "c<$c> SQ<$sanitise_quote>\n"; > + if ($off !=3D 0 && $sanitise_quote eq '*/' && $c ne "\t") { > + substr($res, $off, 1, $;); > + } elsif ($off !=3D 0 && $sanitise_quote eq '//' && $c ne "\t") { > + substr($res, $off, 1, $;); > + } elsif ($off !=3D 0 && $sanitise_quote && $c ne "\t") { > + substr($res, $off, 1, 'X'); > + } else { > + substr($res, $off, 1, $c); > + } > + } > + > + if ($sanitise_quote eq '//') { > + $sanitise_quote =3D ''; > + } > + > + # The pathname on a #include may be surrounded by '<' and '>'. > + if ($res =3D~ /^.\s*\#\s*include\s+\<(.*)\>/) { > + my $clean =3D 'X' x length($1); > + $res =3D~ s@\<.*\>@<$clean>@; > + > + # The whole of a #error is a string. > + } elsif ($res =3D~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { > + my $clean =3D 'X' x length($1); > + $res =3D~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; > + } > + > + return $res; > +} > + > +sub ctx_statement_block { > + my ($linenr, $remain, $off) =3D @_; > + my $line =3D $linenr - 1; > + my $blk =3D ''; > + my $soff =3D $off; > + my $coff =3D $off - 1; > + my $coff_set =3D 0; > + > + my $loff =3D 0; > + > + my $type =3D ''; > + my $level =3D 0; > + my @stack =3D (); > + my $p; > + my $c; > + my $len =3D 0; > + > + my $remainder; > + while (1) { > + @stack =3D (['', 0]) if ($#stack =3D=3D -1); > + > + #warn "CSB: blk<$blk> remain<$remain>\n"; > + # If we are about to drop off the end, pull in more > + # context. > + if ($off >=3D $len) { > + for (; $remain > 0; $line++) { > + last if (!defined $lines[$line]); > + next if ($lines[$line] =3D~ /^-/); > + $remain--; > + $loff =3D $len; > + $blk .=3D $lines[$line] . "\n"; > + $len =3D length($blk); > + $line++; > + last; > + } > + # Bail if there is no further context. > + #warn "CSB: blk<$blk> off<$off> len<$len>\n"; > + if ($off >=3D $len) { > + last; > + } > + } > + $p =3D $c; > + $c =3D substr($blk, $off, 1); > + $remainder =3D substr($blk, $off); > + > + #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> > coff_set<$coff_set>\n"; > + > + # Handle nested #if/#else. > + if ($remainder =3D~ /^#\s*(?:ifndef|ifdef|if)\s/) { > + push(@stack, [ $type, $level ]); > + } elsif ($remainder =3D~ /^#\s*(?:else|elif)\b/) { > + ($type, $level) =3D @{$stack[$#stack - 1]}; > + } elsif ($remainder =3D~ /^#\s*endif\b/) { > + ($type, $level) =3D @{pop(@stack)}; > + } > + > + # Statement ends at the ';' or a close '}' at the > + # outermost level. > + if ($level =3D=3D 0 && $c eq ';') { > + last; > + } > + > + # An else is really a conditional as long as its not else if > + if ($level =3D=3D 0 && $coff_set =3D=3D 0 && > + (!defined($p) || $p =3D~ /(?:\s|\}|\+)/) && > + $remainder =3D~ /^(else)(?:\s|{)/ && > + $remainder !~ /^else\s+if\b/) { > + $coff =3D $off + length($1) - 1; > + $coff_set =3D 1; > + #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; > + #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; > + } > + > + if (($type eq '' || $type eq '(') && $c eq '(') { > + $level++; > + $type =3D '('; > + } > + if ($type eq '(' && $c eq ')') { > + $level--; > + $type =3D ($level !=3D 0)? '(' : ''; > + > + if ($level =3D=3D 0 && $coff < $soff) { > + $coff =3D $off; > + $coff_set =3D 1; > + #warn "CSB: mark coff<$coff>\n"; > + } > + } > + if (($type eq '' || $type eq '{') && $c eq '{') { > + $level++; > + $type =3D '{'; > + } > + if ($type eq '{' && $c eq '}') { > + $level--; > + $type =3D ($level !=3D 0)? '{' : ''; > + > + if ($level =3D=3D 0) { > + if (substr($blk, $off + 1, 1) eq ';') { > + $off++; > + } > + last; > + } > + } > + $off++; > + } > + # We are truly at the end, so shuffle to the next line. > + if ($off =3D=3D $len) { > + $loff =3D $len + 1; > + $line++; > + $remain--; > + } > + > + my $statement =3D substr($blk, $soff, $off - $soff + 1); > + my $condition =3D substr($blk, $soff, $coff - $soff + 1); > + > + #warn "STATEMENT<$statement>\n"; > + #warn "CONDITION<$condition>\n"; > + > + #print "coff<$coff> soff<$off> loff<$loff>\n"; > + > + return ($statement, $condition, > + $line, $remain + 1, $off - $loff + 1, $level); > +} > + > +sub statement_lines { > + my ($stmt) =3D @_; > + > + # Strip the diff line prefixes and rip blank lines at start and end. > + $stmt =3D~ s/(^|\n)./$1/g; > + $stmt =3D~ s/^\s*//; > + $stmt =3D~ s/\s*$//; > + > + my @stmt_lines =3D ($stmt =3D~ /\n/g); > + > + return $#stmt_lines + 2; > +} > + > +sub statement_rawlines { > + my ($stmt) =3D @_; > + > + my @stmt_lines =3D ($stmt =3D~ /\n/g); > + > + return $#stmt_lines + 2; > +} > + > +sub statement_block_size { > + my ($stmt) =3D @_; > + > + $stmt =3D~ s/(^|\n)./$1/g; > + $stmt =3D~ s/^\s*\{//; > + $stmt =3D~ s/}\s*$//; > + $stmt =3D~ s/^\s*//; > + $stmt =3D~ s/\s*$//; > + > + my @stmt_lines =3D ($stmt =3D~ /\n/g); > + my @stmt_statements =3D ($stmt =3D~ /;/g); > + > + my $stmt_lines =3D $#stmt_lines + 2; > + my $stmt_statements =3D $#stmt_statements + 1; > + > + if ($stmt_lines > $stmt_statements) { > + return $stmt_lines; > + } else { > + return $stmt_statements; > + } > +} > + > +sub ctx_statement_full { > + my ($linenr, $remain, $off) =3D @_; > + my ($statement, $condition, $level); > + > + my (@chunks); > + > + # Grab the first conditional/block pair. > + ($statement, $condition, $linenr, $remain, $off, $level) =3D > + ctx_statement_block($linenr, $remain, $off); > + #print "F: c<$condition> s<$statement> remain<$remain>\n"; > + push(@chunks, [ $condition, $statement ]); > + if (!($remain > 0 && $condition =3D~ > /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { > + return ($level, $linenr, @chunks); > + } > + > + # Pull in the following conditional/block pairs and see if they > + # could continue the statement. > + for (;;) { > + ($statement, $condition, $linenr, $remain, $off, $level) =3D > + ctx_statement_block($linenr, $remain, $off); > + #print "C: c<$condition> s<$statement> remain<$remain>\n"; > + last if (!($remain > 0 && $condition =3D~ > /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); > + #print "C: push\n"; > + push(@chunks, [ $condition, $statement ]); > + } > + > + return ($level, $linenr, @chunks); > +} > + > +sub ctx_block_get { > + my ($linenr, $remain, $outer, $open, $close, $off) =3D @_; > + my $line; > + my $start =3D $linenr - 1; > + my $blk =3D ''; > + my @o; > + my @c; > + my @res =3D (); > + > + my $level =3D 0; > + my @stack =3D ($level); > + for ($line =3D $start; $remain > 0; $line++) { > + next if ($rawlines[$line] =3D~ /^-/); > + $remain--; > + > + $blk .=3D $rawlines[$line]; > + > + # Handle nested #if/#else. > + if ($lines[$line] =3D~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { > + push(@stack, $level); > + } elsif ($lines[$line] =3D~ /^.\s*#\s*(?:else|elif)\b/) { > + $level =3D $stack[$#stack - 1]; > + } elsif ($lines[$line] =3D~ /^.\s*#\s*endif\b/) { > + $level =3D pop(@stack); > + } > + > + foreach my $c (split(//, $lines[$line])) { > + ##print "C<$c>L<$level><$open$close>O<$off>\n"; > + if ($off > 0) { > + $off--; > + next; > + } > + > + if ($c eq $close && $level > 0) { > + $level--; > + last if ($level =3D=3D 0); > + } elsif ($c eq $open) { > + $level++; > + } > + } > + > + if (!$outer || $level <=3D 1) { > + push(@res, $rawlines[$line]); > + } > + > + last if ($level =3D=3D 0); > + } > + > + return ($level, @res); > +} > +sub ctx_block_outer { > + my ($linenr, $remain) =3D @_; > + > + my ($level, @r) =3D ctx_block_get($linenr, $remain, 1, '{', '}', 0); > + return @r; > +} > +sub ctx_block { > + my ($linenr, $remain) =3D @_; > + > + my ($level, @r) =3D ctx_block_get($linenr, $remain, 0, '{', '}', 0); > + return @r; > +} > +sub ctx_statement { > + my ($linenr, $remain, $off) =3D @_; > + > + my ($level, @r) =3D ctx_block_get($linenr, $remain, 0, '(', ')', $off); > + return @r; > +} > +sub ctx_block_level { > + my ($linenr, $remain) =3D @_; > + > + return ctx_block_get($linenr, $remain, 0, '{', '}', 0); > +} > +sub ctx_statement_level { > + my ($linenr, $remain, $off) =3D @_; > + > + return ctx_block_get($linenr, $remain, 0, '(', ')', $off); > +} > + > +sub ctx_locate_comment { > + my ($first_line, $end_line) =3D @_; > + > + # Catch a comment on the end of the line itself. > + my ($current_comment) =3D ($rawlines[$end_line - 1] =3D~ m@ > .*(/\*.*\*/)\s*(?:\\\s*)?$@); > + return $current_comment if (defined $current_comment); > + > + # Look through the context and try and figure out if there is a > + # comment. > + my $in_comment =3D 0; > + $current_comment =3D ''; > + for (my $linenr =3D $first_line; $linenr < $end_line; $linenr++) { > + my $line =3D $rawlines[$linenr - 1]; > + #warn " $line\n"; > + if ($linenr =3D=3D $first_line and $line =3D~ m@^.\s*\*@) { > + $in_comment =3D 1; > + } > + if ($line =3D~ m@/\*@) { > + $in_comment =3D 1; > + } > + if (!$in_comment && $current_comment ne '') { > + $current_comment =3D ''; > + } > + $current_comment .=3D $line . "\n" if ($in_comment); > + if ($line =3D~ m@\*/@) { > + $in_comment =3D 0; > + } > + } > + > + chomp($current_comment); > + return($current_comment); > +} > +sub ctx_has_comment { > + my ($first_line, $end_line) =3D @_; > + my $cmt =3D ctx_locate_comment($first_line, $end_line); > + > + ##print "LINE: $rawlines[$end_line - 1 ]\n"; > + ##print "CMMT: $cmt\n"; > + > + return ($cmt ne ''); > +} > + > +sub raw_line { > + my ($linenr, $cnt) =3D @_; > + > + my $offset =3D $linenr - 1; > + $cnt++; > + > + my $line; > + while ($cnt) { > + $line =3D $rawlines[$offset++]; > + next if (defined($line) && $line =3D~ /^-/); > + $cnt--; > + } > + > + return $line; > +} > + > +sub cat_vet { > + my ($vet) =3D @_; > + my ($res, $coded); > + > + $res =3D ''; > + while ($vet =3D~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { > + $res .=3D $1; > + if ($2 ne '') { > + $coded =3D sprintf("^%c", unpack('C', $2) + 64); > + $res .=3D $coded; > + } > + } > + $res =3D~ s/$/\$/; > + > + return $res; > +} > + > +my $av_preprocessor =3D 0; > +my $av_pending; > +my @av_paren_type; > +my $av_pend_colon; > + > +sub annotate_reset { > + $av_preprocessor =3D 0; > + $av_pending =3D '_'; > + @av_paren_type =3D ('E'); > + $av_pend_colon =3D 'O'; > +} > + > +sub annotate_values { > + my ($stream, $type) =3D @_; > + > + my $res; > + my $var =3D '_' x length($stream); > + my $cur =3D $stream; > + > + print "$stream\n" if ($dbg_values > 1); > + > + while (length($cur)) { > + @av_paren_type =3D ('E') if ($#av_paren_type < 0); > + print " <" . join('', @av_paren_type) . > + "> <$type> <$av_pending>" if ($dbg_values > 1); > + if ($cur =3D~ /^(\s+)/o) { > + print "WS($1)\n" if ($dbg_values > 1); > + if ($1 =3D~ /\n/ && $av_preprocessor) { > + $type =3D pop(@av_paren_type); > + $av_preprocessor =3D 0; > + } > + > + } elsif ($cur =3D~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { > + print "CAST($1)\n" if ($dbg_values > 1); > + push(@av_paren_type, $type); > + $type =3D 'C'; > + > + } elsif ($cur =3D~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { > + print "DECLARE($1)\n" if ($dbg_values > 1); > + $type =3D 'T'; > + > + } elsif ($cur =3D~ /^($Modifier)\s*/) { > + print "MODIFIER($1)\n" if ($dbg_values > 1); > + $type =3D 'T'; > + > + } elsif ($cur =3D~ /^(\#\s*define\s*$Ident)(\(?)/o) { > + print "DEFINE($1,$2)\n" if ($dbg_values > 1); > *** 1791 LINES SKIPPED *** > > > --0000000000006acd1f05f7bcca27 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Yea, I did most of the work on this 18 months ago or so, b= ut the shit in core and subsequent nastiness derailed it til now.

<= /div>
It's gotten to 'decent or better' at things, but far = from perfect. I'm sure that now the bug reports will come.
Warner

On Sat, Mar 25, 2023 at 6:22=E2=80=AFPM Drew Galla= tin <gallatin@freebsd.org>= ; wrote:
Thanks so much.=C2=A0 I'= ;ve wanted something like this *forever*

On Sa= t, Mar 25, 2023, at 1:08 PM, Warner Losh wrote:
The branch main has been upda= ted by imp:


commit 3a3c9242739ef= b0c76587ffbaa54c5d10b2cbcb4
Author:=C2=A0=C2=A0=C2=A0=C2=A0 W= arner Losh <imp@Fre= eBSD.org>
AuthorDate: 2023-03-14 21:28:05 +0000
Commit:=C2=A0=C2=A0=C2=A0=C2=A0 Warner Losh <imp@FreeBSD.org>
Com= mitDate: 2023-03-25 17:06:13 +0000

=C2=A0=C2= =A0=C2=A0 checkstyle9.p= l: Perl script to check if a change is approximately style(9)
=
=C2=A0=C2=A0=C2=A0=C2=A0
=C2=A0=C2=A0=C2=A0 This code is= adapted from the QEMU c= heckpatch.pl script. It can check
=C2=A0=C2=A0=C2=A0 eith= er a patch, a file or a git branch. It tries to warn about things
=
=C2=A0=C2=A0=C2=A0 that I believe might be style(9) violations. It'= ;s experimental, since I
=C2=A0=C2=A0=C2=A0 heavily hacked on= the qemu version to get it to not complain (much)
=C2=A0=C2= =A0=C2=A0 about iconic code in the tree. At the moment, it's use should= be
=C2=A0=C2=A0=C2=A0 considered expermental. It will likely= miss violations, and complain
=C2=A0=C2=A0=C2=A0 about code = that's perfectly fine.=C2=A0 It's offered as an experiment
=C2=A0=C2=A0=C2=A0 and to make it easier for contributors to submit p= atches.
---
tools/build/checkstyle9.pl | 2748 ++++++++++++++++++++= ++++++++++++++++++++++++
1 file changed, 2748 insertions(+)

diff --git a/tools/build/checkstyle9.pl b/tools/build/checkstyle9.pl
ne= w file mode 100755
index 000000000000..5aec3819bf7c
=
--- /dev/null
+++ b/tools/build/checkstyle9.pl
@@ -0,0 +1,274= 8 @@
+#!/usr/bin/env perl
+# (c) 2001, Dave Jon= es. (the file handling bit)
+# (c) 2005, Joel Schopp <jschopp@austin.ibm.= com> (the ugly bit)
+# (c) 2007,2008, Andy Whitcroft &= lt;apw@uk.ibm.com&g= t; (new conditions, test suite)
+# (c) 2008-2010 Andy Whitcro= ft <apw@canonical= .com>
+# Licensed under the terms of the GNU GPL Licen= se version 2
+
+use strict;
+use = warnings;
+use Term::ANSIColor qw(:constants);
= +
+my $P =3D $0;
+$P =3D~ s@.*/@@g;
+
+our $SrcFile=C2=A0=C2=A0=C2=A0 =3D qr{\.(?:h|c|cpp|s|S|= pl|py|sh)$};
+
+my $V =3D '0.31';
+
+use Getopt::Long qw(:config no_auto_abbrev);
<= /div>
+
+my $quiet =3D 0;
+my $tree =3D 1;<= br>
+my $chk_signoff =3D 1;
+my $chk_patch =3D unde= f;
+my $chk_branch =3D undef;
+my $tst_only;
+my $emacs =3D 0;
+my $terse =3D 0;
+my $file =3D undef;
+my $color =3D "auto";
+my $no_warnings =3D 0;
+my $summary =3D 1;
=
+my $mailback =3D 0;
+my $summary_file =3D 0;
<= div>+my $root;
+my %debug;
+my $help =3D 0;
=
+
+sub help {
+ my ($exitcode) =3D @= _;
+
+ print << "EOM";
+Usage:
+
+=C2=A0=C2=A0=C2=A0 $P [OPTION]= ... [FILE]...
+=C2=A0=C2=A0=C2=A0 $P [OPTION]... [GIT-REV-LIS= T]
+
+Version: $V
+
+Options:
+=C2=A0 -q, --quiet=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 quiet
+=C2=A0 --patch=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 treat FILE as = patchfile
+=C2=A0 --branch=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= treat args as GIT revision list
+=C2=A0 --emacs=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 emacs compile window format
+=C2= =A0 --terse=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 one line per report
=
+=C2=A0 -f, --file=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 treat FILE as regular s= ource file
+=C2=A0 --strict=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= fail if only warnings are found
+=C2=A0 --no-summary=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 suppress the per-file summary
+=C2=A0 --mailback=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 only produce a report in case of warnings/errors
+=C2=A0 --summary-file=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 include the filename in summary
+=C2=A0= --debug KEY=3D[0|1]=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = turn on/off debugging of KEY, where KEY is one of
+=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 'values', 'possible', 'type', and 'at= tr' (default
+=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 is all off)
+=C2=A0 --test-only=3DWORD=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 report only warnings/errors containing WORD
+= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 literally
+=C2=A0 --color[=3DWHEN]=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Use colo= rs 'always', 'never', or only when output
+= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 is a terminal ('auto'). Default is 'auto'= ;.
+=C2=A0 -h, --help, --version=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 display this help and exit
+
+When FILE is = - read standard input.
+EOM
+
+ e= xit($exitcode);
+}
+
+# Use at yo= ur own risk
+print "\n", MAGENTA, "WARNING:&qu= ot;, RESET, " This code is highly experimental ... likely isn't a = great style(9) match yet\n\n";
+
+# Perl&#= 39;s Getopt::Long allows options to take optional arguments after a space.<= br>
+# Prevent --color by itself from consuming other arguments
+foreach (@ARGV) {
+ if ($_ eq "--color&quo= t; || $_ eq "-color") {
+ $_ =3D "--color=3D$= color";
+ }
+}
+
+GetOptions(
+ 'q|quiet+' =3D> \$quiet,
+ 'tree!' =3D> \$tree,
+ 'signoff!'= ; =3D> \$chk_signoff,
+ 'patch!' =3D> \$chk_pat= ch,
+ 'branch!' =3D> \$chk_branch,
+= 'emacs!' =3D> \$emacs,
+ 'terse!' =3D>= \$terse,
+ 'f|file!' =3D> \$file,
+= 'strict!' =3D> \$no_warnings,
+ 'root=3Ds'= ; =3D> \$root,
+ 'summary!' =3D> \$summary,
=
+ 'mailback!' =3D> \$mailback,
+ 's= ummary-file!' =3D> \$summary_file,
+
+ &= #39;debug=3Ds' =3D> \%debug,
+ 'test-only=3Ds'= =3D> \$tst_only,
+ 'color=3Ds'=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 =3D> \$color,
+ 'no-color'=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D> sub { $color =3D 'never'; },
+ 'h|help' =3D> \$help,
+ 'version= ' =3D> \$help
+) or help(1);
+
=
+help(0) if ($help);
+
+my $exit =3D 0;
+
+if ($#ARGV < 0) {
+ print &qu= ot;$P: no input files\n";
+ exit(1);
+}
+
+if (!defined $chk_branch && !defined $= chk_patch && !defined $file) {
+ $chk_branch =3D $ARG= V[0] =3D~ /.\.\./ ? 1 : 0;
+ $file =3D $ARGV[0] =3D~ /$SrcFil= e/ ? 1 : 0;
+ $chk_patch =3D $chk_branch || $file ? 0 : 1;
+} elsif (!defined $chk_branch && !defined $chk_patch) = {
+ if ($file) {
+ $chk_branch =3D $chk_patch = =3D 0;
+ } else {
+ $chk_branch =3D $ARGV[0] = =3D~ /.\.\./ ? 1 : 0;
+ $chk_patch =3D $chk_branch ? 0 : 1;<= br>
+ }
+} elsif (!defined $chk_branch && != defined $file) {
+ if ($chk_patch) {
+ $chk_br= anch =3D $file =3D 0;
+ } else {
+ $chk_branch= =3D $ARGV[0] =3D~ /.\.\./ ? 1 : 0;
+ $file =3D $chk_branch = ? 0 : 1;
+ }
+} elsif (!defined $chk_patch &= ;& !defined $file) {
+ if ($chk_branch) {
+= $chk_patch =3D $file =3D 0;
+ } else {
+ $fi= le =3D $ARGV[0] =3D~ /$SrcFile/ ? 1 : 0;
+ $chk_patch =3D $f= ile ? 0 : 1;
+ }
+} elsif (!defined $chk_branch= ) {
+ $chk_branch =3D $chk_patch || $file ? 0 : 1;
<= div>+} elsif (!defined $chk_patch) {
+ $chk_patch =3D $chk_br= anch || $file ? 0 : 1;
+} elsif (!defined $file) {
<= div>+ $file =3D $chk_patch || $chk_branch ? 0 : 1;
+}
+
+if (($chk_patch && $chk_branch) ||
+=C2=A0=C2=A0=C2=A0 ($chk_patch && $file) ||
+= =C2=A0=C2=A0=C2=A0 ($chk_branch && $file)) {
+ die &q= uot;Only one of --file, --branch, --patch is permitted\n";
+}
+if (!$chk_patch && !$chk_branch && !$f= ile) {
+ die "One of --file, --branch, --patch is requir= ed\n";
+}
+
+if ($color =3D~= /^always$/i) {
+ $color =3D 1;
+} elsif ($colo= r =3D~ /^never$/i) {
+ $color =3D 0;
+} elsif (= $color =3D~ /^auto$/i) {
+ $color =3D (-t STDOUT);
<= div>+} else {
+ die "Invalid color mode: $color\n";=
+}
+
+my $dbg_values =3D 0;
<= /div>
+my $dbg_possible =3D 0;
+my $dbg_type =3D 0;
+my $dbg_attr =3D 0;
+my $dbg_adv_dcs =3D 0;
+my $dbg_adv_checking =3D 0;
+my $dbg_adv_apw =3D 0;
+for my $key (keys %debug) {
+ ## no critic
<= /div>
+ eval "\${dbg_$key} =3D '$debug{$key}';";
<= /div>
+ die "$@" if ($@);
+}
+
+my $rpt_cleaners =3D 0;
+
+if ($te= rse) {
+ $emacs =3D 1;
+ $quiet++;
+}
+
+my $emitted_corrupt =3D 0;
+
+our $Ident =3D qr{
+ [A-Za-z_][A-Za-z\d_= ]*
+ (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
+ = }x;
+our $Storage =3D qr{extern|static|asmlinkage};
=
+our $Sparse =3D qr{
+ __force
+ }x;
+
+# Notes to $Attribute:
+our $Att= ribute =3D qr{
+ const|
+ volatile|
+ QEMU_NORETURN|
+ QEMU_WARN_UNUSED_RESULT|
+ QEMU_SENTINEL|
+ QEMU_PACKED|
+ = GCC_FMT_ATTR
+ =C2=A0 }x;
+our $Modifier;
=
+our $Inline =3D qr{inline};
+our $Member =3D qr{-= >$Ident|\.$Ident|\[[^]]*\]};
+our $Lval =3D qr{$Ident(?:$M= ember)*};
+
+our $Constant =3D qr{(?:[0-9]+|0x[= 0-9a-fA-F]+)[UL]*};
+our $Assignment =3D qr{(?:\*\=3D|/=3D|%= =3D|\+=3D|-=3D|<<=3D|>>=3D|&=3D|\^=3D|\|=3D|=3D)};
+our $Compare=C2=A0=C2=A0=C2=A0 =3D qr{<=3D|>=3D|=3D=3D|!=3D|&l= t;|>};
+our $Operators =3D qr{
+ <=3D|&= gt;=3D|=3D=3D|!=3D|
+ =3D>|->|<<|>>|<|= >|!|~|
+ &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\= /|%
+ =C2=A0 }x;
+
+our $NonptrT= ype;
+our $Type;
+our $Declare;
+=
+our $NON_ASCII_UTF8 =3D qr{
+ [\xC2-\xDF][\x8= 0-\xBF]=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 # non-overlong 2-byte
+ |=C2=A0 \xE0[\xA0-= \xBF][\x80-\xBF]=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # excluding over= longs
+ | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}=C2=A0 # straight = 3-byte
+ |=C2=A0 \xED[\x80-\x9F][\x80-\xBF]=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 # excluding surrogates
+ |=C2=A0 \xF= 0[\x90-\xBF][\x80-\xBF]{2}=C2=A0=C2=A0=C2=A0=C2=A0 # planes 1-3
+ | [\xF1-\xF3][\x80-\xBF]{3}=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 # planes 4-15
+ |=C2=A0 \xF4[\x80-\x8F][\x80-\xB= F]{2}=C2=A0=C2=A0=C2=A0=C2=A0 # plane 16
+}x;
+=
+our $UTF8 =3D qr{
+ [\x09\x0A\x0D\x20-\x7E]= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 # ASCII
+ | $NON_ASCII_UTF8
+}x;
<= div>+
+# some readers default to ISO-8859-1 when showing emai= l source. detect
+# when UTF-8 is incorrectly interpreted as = ISO-8859-1 and reencoded back.
+# False positives are possibl= e but very unlikely.
+our $UTF8_MOJIBAKE =3D qr{
+ \xC3[\x82-\x9F] \xC2[\x80-\xBF]=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= # c2-df 80-bf
+ | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF]= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # e0 a0-bf 80-bf
=
+ | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2}=C2=A0 # e1-ec/ee/e= f 80-bf 80-bf
+ | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF]=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # ed 80-9f 80-bf
+ | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-= bf
+ | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3}=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # f1-f3 80-bf 80-bf 80-bf
=
+ | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf = 80-bf
+}x;
+
+# There are still s= ome false positives, but this catches most
+# common cases.
+our $typeTypedefs =3D qr{(?x:
+=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 (?![KMGTPE]iB)=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 # IEC binary prefix (do not match)
+=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]*=C2=A0= =C2=A0=C2=A0=C2=A0 # camelcase
+=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 | [A-Z][A-Z\d_]*AIOCB=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # all uppercase
<= div>+=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | [A-Z][A-Z\d_]*CPU=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 # all uppercase
+=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 | QEMUBH=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # all uppercase
+)};
=
+
+our @typeList =3D (
+ qr{void},
+ qr{(?:unsigned\s+)?char},
+ qr{(?:unsigned\s+)= ?short},
+ qr{(?:unsigned\s+)?int},
+ qr{(?:uns= igned\s+)?long},
+ qr{(?:unsigned\s+)?long\s+int},
<= div>+ qr{(?:unsigned\s+)?long\s+long},
+ qr{(?:unsigned\s+)?l= ong\s+long\s+int},
+ qr{unsigned},
+ qr{float},=
+ qr{double},
+ qr{bool},
+ qr{s= truct\s+$Ident},
+ qr{union\s+$Ident},
+ qr{enu= m\s+$Ident},
+ qr{${Ident}_t},
+ qr{${Ident}_ha= ndler},
+ qr{${Ident}_handler_fn},
+ qr{target_= (?:u)?long},
+ qr{hwaddr},
+);
+<= br>
+# This can be modified by sub possible.=C2=A0 Since it can b= e empty, be careful
+# about regexes that always match, becau= se they can cause infinite loops.
+our @modifierList =3D (
+);
+
+sub build_types {
<= div>+ my $all =3D "(?x:=C2=A0 \n" . join("|\n=C2=A0 ", = @typeList) . "\n)";
+ if (@modifierList > 0) {
+ my $mods =3D "(?x:=C2=A0 \n" . join("|\n=C2= =A0 ", @modifierList) . "\n)";
+ $Modifier = =3D qr{(?:$Attribute|$Sparse|$mods)};
+ } else {
+ $Modifier =3D qr{(?:$Attribute|$Sparse)};
+ }
<= div>+ $NonptrType =3D qr{
+ (?:$Modifier\s+|const\s+)*
<= /div>
+ (?:
+ (?:typeof|__typeof__)\s*\(\s*\**\s*$Id= ent\s*\)|
+ (?:$typeTypedefs\b)|
+ (?:${a= ll}\b)
+ )
+ (?:\s+$Modifier|\s+const)*
=
+ =C2=A0 }x;
+ $Type =3D qr{
+ $N= onptrType
+ (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
+ (?:\s+$Inline|\s+$Modifier)*
+ =C2=A0 }x;
+ $Declare =3D qr{(?:$Storage\s+)?$Type};
+}
<= /div>
+build_types();
+
+$chk_signoff =3D 0= if ($file);
+
+my @rawlines =3D ();
<= div>+my @lines =3D ();
+my $vname;
+if ($chk_br= anch) {
+ my @patches;
+ my %git_commits =3D ()= ;
+ my $HASH;
+ open($HASH, "-|", &qu= ot;git", "log", "--reverse", "--no-merges&quo= t;, "--format=3D%H %s", $ARGV[0]) ||
+ die "$= P: git log --reverse --no-merges --format=3D'%H %s' $ARGV[0] failed= - $!\n";
+
+ for my $line (<$HASH>)= {
+ $line =3D~ /^([0-9a-fA-F]{40,40}) (.*)$/;
+ next if (!defined($1) || !defined($2));
+ my $sha1 =3D $= 1;
+ my $subject =3D $2;
+ push(@patches, $sh= a1);
+ $git_commits{$sha1} =3D $subject;
+ }
+
+ close $HASH;
+
+= die "$P: no revisions returned for revlist '$ARGV[0]'\n"=
+ =C2=A0=C2=A0=C2=A0 unless @patches;
+
+ my $i =3D 1;
+ my $num_patches =3D @patches;
+ for my $hash (@patches) {
+ my $FILE;
+ open($FILE, '-|', "git", "show", "--p= atch-with-stat", $hash) ||
+ die "$P: git show $h= ash - $!\n";
+ while (<$FILE>) {
+ = chomp;
+ push(@rawlines, $_);
+ }
+ close($FILE);
+ $vname =3D substr($hash, 0, 12) . &= #39; (' . $git_commits{$hash} . ')';
+ if ($num_= patches > 1 && $quiet =3D=3D 0) {
+ my $prefix = =3D "$i/$num_patches";
+ $prefix =3D BLUE . BOLD = . $prefix . RESET if $color;
+ print "$prefix Checking= commit $vname\n";
+ $vname =3D "Patch $i/$num_pa= tches";
+ } else {
+ $vname =3D "C= ommit " . $vname;
+ }
+ if (!process($ha= sh)) {
+ $exit =3D 1;
+ print "\n"= ; if ($num_patches > 1 && $quiet =3D=3D 0);
+ }
+ @rawlines =3D ();
+ @lines =3D ();
=
+ $i++;
+ }
+} else {
+ for= my $filename (@ARGV) {
+ my $FILE;
+ if ($fi= le) {
+ open($FILE, '-|', "diff -u /dev/null $= filename") ||
+ die "$P: $filename: diff failed = - $!\n";
+ } elsif ($filename eq '-') {
+ open($FILE, '<&STDIN');
+ } else {=
+ open($FILE, '<', "$filename") ||
+ die "$P: $filename: open failed - $!\n";
+ }
+ if ($filename eq '-') {
= + $vname =3D 'Your patch';
+ } else {
+ $vname =3D $filename;
+ }
+ print "= ;Checking $filename...\n" if @ARGV > 1 && $quiet =3D=3D 0;<= br>
+ while (<$FILE>) {
+ chomp;
=
+ push(@rawlines, $_);
+ }
+ close($FI= LE);
+ if (!process($filename)) {
+ $exit = =3D 1;
+ }
+ @rawlines =3D ();
= + @lines =3D ();
+ }
+}
+
+exit($exit);
+
+sub top_of_kernel_tree= {
+ my ($root) =3D @_;
+
+ my @t= ree_check =3D (
+ =C2=A0=C2=A0=C2=A0 "Makefile.inc1"= ;, "README.md", "sys",
+ =C2=A0=C2=A0=C2= =A0 "usr.sbin"
+ );
+
+= foreach my $check (@tree_check) {
+ if (! -e $root . '/= ' . $check) {
+ return 0;
+ }
<= div>+ }
+ return 1;
+}
+
+sub expand_tabs {
+ my ($str) =3D @_;
+<= br>
+ my $res =3D '';
+ my $n =3D 0;
+ for my $c (split(//, $str)) {
+ if ($c eq "\t= ") {
+ $res .=3D ' ';
+ $n++;<= br>
+ for (; ($n % 8) !=3D 0; $n++) {
+ $res .= =3D ' ';
+ }
+ next;
= + }
+ $res .=3D $c;
+ $n++;
+ = }
+
+ return $res;
+}
+sub copy_spacing {
+ (my $res =3D shift) =3D~ tr/\t/ /c;<= br>
+ return $res;
+}
+
+sub line_stats {
+ my ($line) =3D @_;
+
+ # Drop the diff line leader and expand tabs
+ $lin= e =3D~ s/^.//;
+ $line =3D expand_tabs($line);
= +
+ # Pick the indent from the front of the line.
+ my ($white) =3D ($line =3D~ /^(\s*)/);
+
+= return (length($line), length($white));
+}
+
+my $sanitise_quote =3D '';
+
<= div>+sub sanitise_line_reset {
+ my ($in_comment) =3D @_;
=
+
+ if ($in_comment) {
+ $sanitise_= quote =3D '*/';
+ } else {
+ $sanitise= _quote =3D '';
+ }
+}
+su= b sanitise_line {
+ my ($line) =3D @_;
+
+ my $res =3D '';
+ my $l =3D '';
=
+
+ my $qlen =3D 0;
+ my $off =3D 0;=
+ my $c;
+
+ # Always copy over = the diff marker.
+ $res =3D substr($line, 0, 1);
+
+ for ($off =3D 1; $off < length($line); $off++) {
=
+ $c =3D substr($line, $off, 1);
+
= + # Comments we are wacking completely including the begin
+= # and end, all to $;.
+ if ($sanitise_quote eq '' = && substr($line, $off, 2) eq '/*') {
+ $san= itise_quote =3D '*/';
+
+ substr($res= , $off, 2, "$;$;");
+ $off++;
+ n= ext;
+ }
+ if ($sanitise_quote eq '*/'= ; && substr($line, $off, 2) eq '*/') {
+ $s= anitise_quote =3D '';
+ substr($res, $off, 2, "= ;$;$;");
+ $off++;
+ next;
+ }
+ if ($sanitise_quote eq '' && subst= r($line, $off, 2) eq '//') {
+ $sanitise_quote =3D = '//';
+
+ substr($res, $off, 2, $sani= tise_quote);
+ $off++;
+ next;
+ }
+
+ # A \ in a string means ignore the = next character.
+ if (($sanitise_quote eq "'" = || $sanitise_quote eq '"') &&
+ =C2=A0= =C2=A0=C2=A0 $c eq "\\") {
+ substr($res, $off, 2= , 'XX');
+ $off++;
+ next;
+ }
+ # Regular quotes.
+ if ($c eq &q= uot;'" || $c eq '"') {
+ if ($sanitis= e_quote eq '') {
+ $sanitise_quote =3D $c;
+
+ substr($res, $off, 1, $c);
+ n= ext;
+ } elsif ($sanitise_quote eq $c) {
+ = $sanitise_quote =3D '';
+ }
+ }
=
+
+ #print "c<$c> SQ<$sanitise_quot= e>\n";
+ if ($off !=3D 0 && $sanitise_quote = eq '*/' && $c ne "\t") {
+ substr= ($res, $off, 1, $;);
+ } elsif ($off !=3D 0 && $sani= tise_quote eq '//' && $c ne "\t") {
+ substr($res, $off, 1, $;);
+ } elsif ($off !=3D 0 &= & $sanitise_quote && $c ne "\t") {
+ = substr($res, $off, 1, 'X');
+ } else {
+ substr($res, $off, 1, $c);
+ }
+ }
+
+ if ($sanitise_quote eq '//') {
<= div>+ $sanitise_quote =3D '';
+ }
+
+ # The pathname on a #include may be surrounded by '<&#= 39; and '>'.
+ if ($res =3D~ /^.\s*\#\s*include\s+= \<(.*)\>/) {
+ my $clean =3D 'X' x length($1);=
+ $res =3D~ s@\<.*\>@<$clean>@;
+=
+ # The whole of a #error is a string.
+ } els= if ($res =3D~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
+ my= $clean =3D 'X' x length($1);
+ $res =3D~ s@(\#\s*(?= :error|warning)\s+).*@$1$clean@;
+ }
+
+ return $res;
+}
+
+sub ct= x_statement_block {
+ my ($linenr, $remain, $off) =3D @_;
=
+ my $line =3D $linenr - 1;
+ my $blk =3D '= 9;;
+ my $soff =3D $off;
+ my $coff =3D $off - = 1;
+ my $coff_set =3D 0;
+
+ my $= loff =3D 0;
+
+ my $type =3D '';
+ my $level =3D 0;
+ my @stack =3D ();
= + my $p;
+ my $c;
+ my $len =3D 0;
+
+ my $remainder;
+ while (1) {
+ @stack =3D (['', 0]) if ($#stack =3D=3D -1);
+
+ #warn "CSB: blk<$blk> remain<$remain>\n&qu= ot;;
+ # If we are about to drop off the end, pull in more
+ # context.
+ if ($off >=3D $len) {
+ for (; $remain > 0; $line++) {
+ last if (!= defined $lines[$line]);
+ next if ($lines[$line] =3D~ /^-/= );
+ $remain--;
+ $loff =3D $len;
+ $blk .=3D $lines[$line] . "\n";
+ $l= en =3D length($blk);
+ $line++;
+ last;
+ }
+ # Bail if there is no further context.=
+ #warn "CSB: blk<$blk> off<$off> len<= $len>\n";
+ if ($off >=3D $len) {
= + last;
+ }
+ }
+ $p =3D $= c;
+ $c =3D substr($blk, $off, 1);
+ $remaind= er =3D substr($blk, $off);
+
+ #warn "CSB= : c<$c> type<$type> level<$level> remainder<$remainder= > coff_set<$coff_set>\n";
+
+ # = Handle nested #if/#else.
+ if ($remainder =3D~ /^#\s*(?:ifnd= ef|ifdef|if)\s/) {
+ push(@stack, [ $type, $level ]);
+ } elsif ($remainder =3D~ /^#\s*(?:else|elif)\b/) {
+ ($type, $level) =3D @{$stack[$#stack - 1]};
+ } elsif = ($remainder =3D~ /^#\s*endif\b/) {
+ ($type, $level) =3D @{= pop(@stack)};
+ }
+
+ # Stateme= nt ends at the ';' or a close '}' at the
+ #= outermost level.
+ if ($level =3D=3D 0 && $c eq = 9;;') {
+ last;
+ }
+
<= /div>
+ # An else is really a conditional as long as its not else if
+ if ($level =3D=3D 0 && $coff_set =3D=3D 0 &&= ;
+ (!defined($p) || $p =3D~ /(?:\s|\}|\+)/) &&
+ $remainder =3D~ /^(else)(?:\s|{)/ &&
+ $remainder !~ /^else\s+if\b/) {
+ $coff =3D $off + le= ngth($1) - 1;
+ $coff_set =3D 1;
+ #warn &q= uot;CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
<= /div>
+ #warn "[" . substr($blk, $soff, $coff - $soff + 1) = . "]\n";
+ }
+
+ if (= ($type eq '' || $type eq '(') && $c eq '(')= {
+ $level++;
+ $type =3D '(';
=
+ }
+ if ($type eq '(' && $c eq = ')') {
+ $level--;
+ $type =3D ($le= vel !=3D 0)? '(' : '';
+
+ if= ($level =3D=3D 0 && $coff < $soff) {
+ $coff = =3D $off;
+ $coff_set =3D 1;
+ #warn &quo= t;CSB: mark coff<$coff>\n";
+ }
+ = }
+ if (($type eq '' || $type eq '{') &= & $c eq '{') {
+ $level++;
+ $t= ype =3D '{';
+ }
+ if ($type eq '= {' && $c eq '}') {
+ $level--;
+ $type =3D ($level !=3D 0)? '{' : '';
+
+ if ($level =3D=3D 0) {
+ if (substr(= $blk, $off + 1, 1) eq ';') {
+ $off++;
<= div>+ }
+ last;
+ }
+ }<= br>
+ $off++;
+ }
+ # We are truly a= t the end, so shuffle to the next line.
+ if ($off =3D=3D $le= n) {
+ $loff =3D $len + 1;
+ $line++;
+ $remain--;
+ }
+
+ my $= statement =3D substr($blk, $soff, $off - $soff + 1);
+ my $co= ndition =3D substr($blk, $soff, $coff - $soff + 1);
+
+ #warn "STATEMENT<$statement>\n";
+ #= warn "CONDITION<$condition>\n";
+
+ #print "coff<$coff> soff<$off> loff<$loff>\n&qu= ot;;
+
+ return ($statement, $condition,
+ $line, $remain + 1, $off - $loff + 1, $level);
+}=
+
+sub statement_lines {
+ my ($= stmt) =3D @_;
+
+ # Strip the diff line prefixe= s and rip blank lines at start and end.
+ $stmt =3D~ s/(^|\n)= ./$1/g;
+ $stmt =3D~ s/^\s*//;
+ $stmt =3D~ s/\= s*$//;
+
+ my @stmt_lines =3D ($stmt =3D~ /\n/g= );
+
+ return $#stmt_lines + 2;
+= }
+
+sub statement_rawlines {
+ m= y ($stmt) =3D @_;
+
+ my @stmt_lines =3D ($stmt= =3D~ /\n/g);
+
+ return $#stmt_lines + 2;
<= /div>
+}
+
+sub statement_block_size {
<= /div>
+ my ($stmt) =3D @_;
+
+ $stmt =3D~ s= /(^|\n)./$1/g;
+ $stmt =3D~ s/^\s*\{//;
+ $stmt= =3D~ s/}\s*$//;
+ $stmt =3D~ s/^\s*//;
+ $stmt= =3D~ s/\s*$//;
+
+ my @stmt_lines =3D ($stmt = =3D~ /\n/g);
+ my @stmt_statements =3D ($stmt =3D~ /;/g);
=
+
+ my $stmt_lines =3D $#stmt_lines + 2;
=
+ my $stmt_statements =3D $#stmt_statements + 1;
+
+ if ($stmt_lines > $stmt_statements) {
+ return= $stmt_lines;
+ } else {
+ return $stmt_statem= ents;
+ }
+}
+
+sub= ctx_statement_full {
+ my ($linenr, $remain, $off) =3D @_;
+ my ($statement, $condition, $level);
+
+ my (@chunks);
+
+ # Grab the first con= ditional/block pair.
+ ($statement, $condition, $linenr, $rem= ain, $off, $level) =3D
+ ctx_statement_block($linenr, $rem= ain, $off);
+ #print "F: c<$condition> s<$state= ment> remain<$remain>\n";
+ push(@chunks, [ $co= ndition, $statement ]);
+ if (!($remain > 0 && $co= ndition =3D~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
+ ret= urn ($level, $linenr, @chunks);
+ }
+
=
+ # Pull in the following conditional/block pairs and see if they
<= /div>
+ # could continue the statement.
+ for (;;) {
<= /div>
+ ($statement, $condition, $linenr, $remain, $off, $level) =3D
+ ctx_statement_block($linenr, $remain, $off);
+ #print "C: c<$condition> s<$statement> remain<$rem= ain>\n";
+ last if (!($remain > 0 && $con= dition =3D~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
+ #print &= quot;C: push\n";
+ push(@chunks, [ $condition, $stateme= nt ]);
+ }
+
+ return ($level, $l= inenr, @chunks);
+}
+
+sub ctx_bl= ock_get {
+ my ($linenr, $remain, $outer, $open, $close, $off= ) =3D @_;
+ my $line;
+ my $start =3D $linenr -= 1;
+ my $blk =3D '';
+ my @o;
+ my @c;
+ my @res =3D ();
+
+ my $level =3D 0;
+ my @stack =3D ($level);
= + for ($line =3D $start; $remain > 0; $line++) {
+ next i= f ($rawlines[$line] =3D~ /^-/);
+ $remain--;
+=
+ $blk .=3D $rawlines[$line];
+
+ # Handle nested #if/#else.
+ if ($lines[$line] =3D~ /^.\= s*#\s*(?:ifndef|ifdef|if)\s/) {
+ push(@stack, $level);
=
+ } elsif ($lines[$line] =3D~ /^.\s*#\s*(?:else|elif)\b/) {
=
+ $level =3D $stack[$#stack - 1];
+ } elsif ($l= ines[$line] =3D~ /^.\s*#\s*endif\b/) {
+ $level =3D pop(@st= ack);
+ }
+
+ foreach my $c (sp= lit(//, $lines[$line])) {
+ ##print "C<$c>L<$= level><$open$close>O<$off>\n";
+ if ($o= ff > 0) {
+ $off--;
+ next;
<= div>+ }
+
+ if ($c eq $close && $le= vel > 0) {
+ $level--;
+ last if ($lev= el =3D=3D 0);
+ } elsif ($c eq $open) {
+ = $level++;
+ }
+ }
+
<= div>+ if (!$outer || $level <=3D 1) {
+ push(@res, $raw= lines[$line]);
+ }
+
+ last if = ($level =3D=3D 0);
+ }
+
+ return= ($level, @res);
+}
+sub ctx_block_outer {
<= /div>
+ my ($linenr, $remain) =3D @_;
+
+ m= y ($level, @r) =3D ctx_block_get($linenr, $remain, 1, '{', '}&#= 39;, 0);
+ return @r;
+}
+sub ctx= _block {
+ my ($linenr, $remain) =3D @_;
+
<= /div>
+ my ($level, @r) =3D ctx_block_get($linenr, $remain, 0, '{&#= 39;, '}', 0);
+ return @r;
+}
=
+sub ctx_statement {
+ my ($linenr, $remain, $off) =3D @= _;
+
+ my ($level, @r) =3D ctx_block_get($linen= r, $remain, 0, '(', ')', $off);
+ return @r;<= br>
+}
+sub ctx_block_level {
+ my ($= linenr, $remain) =3D @_;
+
+ return ctx_block_g= et($linenr, $remain, 0, '{', '}', 0);
+}
<= /div>
+sub ctx_statement_level {
+ my ($linenr, $remain, = $off) =3D @_;
+
+ return ctx_block_get($linenr,= $remain, 0, '(', ')', $off);
+}
+
+sub ctx_locate_comment {
+ my ($first_line= , $end_line) =3D @_;
+
+ # Catch a comment on t= he end of the line itself.
+ my ($current_comment) =3D ($rawl= ines[$end_line - 1] =3D~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
+ re= turn $current_comment if (defined $current_comment);
+
+ # Look through the context and try and figure out if there is a
+ # comment.
+ my $in_comment =3D 0;
+ $current_comment =3D '';
+ for (my $linenr =3D $= first_line; $linenr < $end_line; $linenr++) {
+ my $line = =3D $rawlines[$linenr - 1];
+ #warn "=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $line\n";
+ = if ($linenr =3D=3D $first_line and $line =3D~ m@^.\s*\*@) {
+= $in_comment =3D 1;
+ }
+ if ($line =3D~ m@= /\*@) {
+ $in_comment =3D 1;
+ }
+ if (!$in_comment && $current_comment ne '') {
+ $current_comment =3D '';
+ }
+ $current_comment .=3D $line . "\n" if ($in_comment);
+ if ($line =3D~ m@\*/@) {
+ $in_comment =3D 0;
=
+ }
+ }
+
+ chomp($cu= rrent_comment);
+ return($current_comment);
+}<= br>
+sub ctx_has_comment {
+ my ($first_line, $end_= line) =3D @_;
+ my $cmt =3D ctx_locate_comment($first_line, $= end_line);
+
+ ##print "LINE: $rawlines[$e= nd_line - 1 ]\n";
+ ##print "CMMT: $cmt\n";
+
+ return ($cmt ne '');
+}=
+
+sub raw_line {
+ my ($linenr,= $cnt) =3D @_;
+
+ my $offset =3D $linenr - 1;<= br>
+ $cnt++;
+
+ my $line;
=
+ while ($cnt) {
+ $line =3D $rawlines[$offset++];
<= /div>
+ next if (defined($line) && $line =3D~ /^-/);
=
+ $cnt--;
+ }
+
+ return $l= ine;
+}
+
+sub cat_vet {
+ my ($vet) =3D @_;
+ my ($res, $coded);
= +
+ $res =3D '';
+ while ($vet =3D~ /([= ^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
+ $res .=3D $1;
<= div>+ if ($2 ne '') {
+ $coded =3D sprintf("^= %c", unpack('C', $2) + 64);
+ $res .=3D $coded= ;
+ }
+ }
+ $res =3D~ s/$/\$/;
+
+ return $res;
+}
= +
+my $av_preprocessor =3D 0;
+my $av_pending;<= br>
+my @av_paren_type;
+my $av_pend_colon;
+
+sub annotate_reset {
+ $av_preprocess= or =3D 0;
+ $av_pending =3D '_';
+ @av_= paren_type =3D ('E');
+ $av_pend_colon =3D 'O'= ;;
+}
+
+sub annotate_values {
+ my ($stream, $type) =3D @_;
+
+ m= y $res;
+ my $var =3D '_' x length($stream);
+ my $cur =3D $stream;
+
+ print "$s= tream\n" if ($dbg_values > 1);
+
+ whil= e (length($cur)) {
+ @av_paren_type =3D ('E') if ($#= av_paren_type < 0);
+ print " <" . join('= ;', @av_paren_type) .
+ "> <$type> <$= av_pending>" if ($dbg_values > 1);
+ if ($cur =3D= ~ /^(\s+)/o) {
+ print "WS($1)\n" if ($dbg_values= > 1);
+ if ($1 =3D~ /\n/ && $av_preprocessor) {=
+ $type =3D pop(@av_paren_type);
+ $av_p= reprocessor =3D 0;
+ }
+
+ } e= lsif ($cur =3D~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_')= {
+ print "CAST($1)\n" if ($dbg_values > 1);<= br>
+ push(@av_paren_type, $type);
+ $type =3D = 'C';
+
+ } elsif ($cur =3D~ /^($Type)\= s*(?:$Ident|,|\)|\(|\s*$)/) {
+ print "DECLARE($1)\n&q= uot; if ($dbg_values > 1);
+ $type =3D 'T';
<= /div>
+
+ } elsif ($cur =3D~ /^($Modifier)\s*/) {
+ print "MODIFIER($1)\n" if ($dbg_values > 1);
+ $type =3D 'T';
+
+ } elsi= f ($cur =3D~ /^(\#\s*define\s*$Ident)(\(?)/o) {
+ print &qu= ot;DEFINE($1,$2)\n" if ($dbg_values > 1);
*** 1791 LI= NES SKIPPED ***


<= /div>
--0000000000006acd1f05f7bcca27--