From: Bart Van Assche <bvanassche@acm.org>
To: Joe Perches <joe@perches.com>, Andy Whitcroft <apw@canonical.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org,
Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch
Date: Tue, 1 Dec 2026 14:53:53 -0800 [thread overview]
Message-ID: <24a2cbf5675d590976eafc428c1ea66b379bc25d.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>
Replace per-token $cur = substr($cur, length($1)) slicing in
annotate_values() with in-place pos($stream) scanning using \G-anchored
patterns, and dispatch token checks on the leading character $ch.
Avoid repeated tail copying on every token and stop evaluating $Type,
$Modifier, and preprocessor regexes on punctuation or numeric tokens where
they cannot match.
Keep matching $Type and $Modifier against substr($stream, $pos) when
--typedefsfile or the per-file type and modifier lists contain anything
other than plain identifiers, since anchors, lookarounds and zero-length
alternatives followed by the implicit \b in $BasicType and $NonptrType
behave differently at a \G offset than at the start of a substring.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 153 +++++++++++++++++++++++++++++-------------
1 file changed, 107 insertions(+), 46 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 400fae964e60..b3854bc3ad6d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1069,14 +1069,32 @@ if (show_type("CONST_STRUCT")) {
or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
}
+my $has_other_typedefs = 0;
+my $typedefs_need_substr = 0;
if (defined($typedefsfile)) {
my $typeOtherTypedefs;
read_words(\$typeOtherTypedefs, $typedefsfile)
or warn "No additional types will be considered - file '$typedefsfile': $!\n";
- $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
+ if (defined $typeOtherTypedefs) {
+ $typeTypedefs .= '|' . $typeOtherTypedefs;
+ $has_other_typedefs = 1;
+ # Anything other than plain identifiers may contain anchors,
+ # lookarounds or zero-length alternatives whose behavior
+ # differs between \G at an offset and ^ on a substring.
+ $typedefs_need_substr =
+ ($typeOtherTypedefs !~ /^[A-Za-z0-9_]+(?:\|[A-Za-z0-9_]+)*\z/);
+ }
}
+my $mods_ident_only = 1;
+my $types_ident_only = 1;
+my $types_need_substr = 0;
sub build_types {
+ $mods_ident_only = !grep { !/^[A-Za-z_]/ } @modifierListFile;
+ $types_ident_only = $mods_ident_only && !$has_other_typedefs &&
+ !grep { !/^[A-Za-z_]/ } @typeListFile;
+ $types_need_substr = $typedefs_need_substr ||
+ grep { !/^[A-Za-z0-9_]+\z/ } (@modifierListFile, @typeListFile);
my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
@@ -2337,36 +2355,55 @@ sub annotate_values {
my ($stream, $type) = @_;
my $res;
- my $var = '_' x length($stream);
- my $cur = $stream;
+ my $slen = length($stream);
+ my $var = '_' x $slen;
+ my $pos = 0;
print "$stream\n" if ($dbg_values > 1);
- while (length($cur)) {
+ while ($pos < $slen) {
@av_paren_type = ('E') if ($#av_paren_type < 0);
print " <" . join('', @av_paren_type) .
"> <$type> <$av_pending>" if ($dbg_values > 1);
- if ($cur =~ /^(\s+)/o) {
+ my $ch = substr($stream, $pos, 1);
+ my $is_ident_start = ($ch =~ /[A-Za-z_]/);
+ pos($stream) = $pos;
+ my $tok_len;
+ if ($stream =~ /\G(\s+)/o) {
+ $tok_len = length($1);
print "WS($1)\n" if ($dbg_values > 1);
if ($1 =~ /\n/ && $av_preprocessor) {
$type = pop(@av_paren_type);
$av_preprocessor = 0;
}
- } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
+ } elsif ($ch eq '(' && $av_pending eq '_' &&
+ ($types_need_substr ?
+ substr($stream, $pos) =~ /^(\(\s*$Type\s*)\)/ :
+ $stream =~ /\G(\(\s*$Type\s*)\)/)) {
+ $tok_len = length($1);
print "CAST($1)\n" if ($dbg_values > 1);
push(@av_paren_type, $type);
$type = 'c';
- } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
+ } elsif (($is_ident_start || !$types_ident_only) &&
+ ($types_need_substr ?
+ substr($stream, $pos) =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/ :
+ $stream =~ /\G($Type)\s*(?:$Ident|,|\)|\(|\s*$)/)) {
+ $tok_len = length($1);
print "DECLARE($1)\n" if ($dbg_values > 1);
$type = 'T';
- } elsif ($cur =~ /^($Modifier)\s*/) {
+ } elsif (($is_ident_start || !$mods_ident_only) &&
+ ($types_need_substr ?
+ substr($stream, $pos) =~ /^($Modifier)\s*/ :
+ $stream =~ /\G($Modifier)\s*/)) {
+ $tok_len = length($1);
print "MODIFIER($1)\n" if ($dbg_values > 1);
$type = 'T';
- } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
+ } elsif ($ch eq '#' && $stream =~ /\G(\#\s*define\s*$Ident)(\(?)/o) {
+ $tok_len = length($1);
print "DEFINE($1,$2)\n" if ($dbg_values > 1);
$av_preprocessor = 1;
push(@av_paren_type, $type);
@@ -2375,12 +2412,14 @@ sub annotate_values {
}
$type = 'E';
- } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
+ } elsif ($ch eq '#' && $stream =~ /\G(\#\s*(?:undef\s*$Ident|include\b))/o) {
+ $tok_len = length($1);
print "UNDEF($1)\n" if ($dbg_values > 1);
$av_preprocessor = 1;
push(@av_paren_type, $type);
- } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
+ } elsif ($ch eq '#' && $stream =~ /\G(\#\s*(?:ifdef|ifndef|if))/o) {
+ $tok_len = length($1);
print "PRE_START($1)\n" if ($dbg_values > 1);
$av_preprocessor = 1;
@@ -2388,7 +2427,8 @@ sub annotate_values {
push(@av_paren_type, $type);
$type = 'E';
- } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
+ } elsif ($ch eq '#' && $stream =~ /\G(\#\s*(?:else|elif))/o) {
+ $tok_len = length($1);
print "PRE_RESTART($1)\n" if ($dbg_values > 1);
$av_preprocessor = 1;
@@ -2396,7 +2436,8 @@ sub annotate_values {
$type = 'E';
- } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
+ } elsif ($ch eq '#' && $stream =~ /\G(\#\s*(?:endif))/o) {
+ $tok_len = length($1);
print "PRE_END($1)\n" if ($dbg_values > 1);
$av_preprocessor = 1;
@@ -2407,57 +2448,67 @@ sub annotate_values {
push(@av_paren_type, $type);
$type = 'E';
- } elsif ($cur =~ /^(\\\n)/o) {
+ } elsif ($ch eq "\\" && $stream =~ /\G(\\\n)/o) {
+ $tok_len = length($1);
print "PRECONT($1)\n" if ($dbg_values > 1);
- } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
+ } elsif ($is_ident_start && $stream =~ /\G(__attribute__)\s*\(?/o) {
+ $tok_len = length($1);
print "ATTR($1)\n" if ($dbg_values > 1);
$av_pending = $type;
$type = 'N';
- } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
+ } elsif ($is_ident_start && $stream =~ /\G(sizeof)\s*(\()?/o) {
+ $tok_len = length($1);
print "SIZEOF($1)\n" if ($dbg_values > 1);
if (defined $2) {
$av_pending = 'V';
}
$type = 'N';
- } elsif ($cur =~ /^(if|while|for)\b/o) {
+ } elsif ($is_ident_start && $stream =~ /\G(if|while|for)\b/o) {
+ $tok_len = length($1);
print "COND($1)\n" if ($dbg_values > 1);
$av_pending = 'E';
$type = 'N';
- } elsif ($cur =~/^(case)/o) {
+ } elsif ($is_ident_start && $stream =~ /\G(case)/o) {
+ $tok_len = length($1);
print "CASE($1)\n" if ($dbg_values > 1);
$av_pend_colon = 'C';
$type = 'N';
- } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
+ } elsif ($is_ident_start && $stream =~ /\G(return|else|goto|typeof|__typeof__)\b/o) {
+ $tok_len = length($1);
print "KEYWORD($1)\n" if ($dbg_values > 1);
$type = 'N';
- } elsif ($cur =~ /^(\()/o) {
- print "PAREN('$1')\n" if ($dbg_values > 1);
+ } elsif ($ch eq '(') {
+ $tok_len = 1;
+ print "PAREN('(')\n" if ($dbg_values > 1);
push(@av_paren_type, $av_pending);
$av_pending = '_';
$type = 'N';
- } elsif ($cur =~ /^(\))/o) {
+ } elsif ($ch eq ')') {
+ $tok_len = 1;
my $new_type = pop(@av_paren_type);
if ($new_type ne '_') {
$type = $new_type;
- print "PAREN('$1') -> $type\n"
+ print "PAREN(')') -> $type\n"
if ($dbg_values > 1);
} else {
- print "PAREN('$1')\n" if ($dbg_values > 1);
+ print "PAREN(')')\n" if ($dbg_values > 1);
}
- } elsif ($cur =~ /^($Ident)\s*\(/o) {
+ } elsif ($is_ident_start && $stream =~ /\G($Ident)\s*\(/o) {
+ $tok_len = length($1);
print "FUNC($1)\n" if ($dbg_values > 1);
$type = 'V';
$av_pending = 'V';
- } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
+ } elsif ($is_ident_start && $stream =~ /\G($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
+ $tok_len = length($1);
if (defined $2 && $type eq 'C' || $type eq 'T') {
$av_pend_colon = 'B';
} elsif ($type eq 'E') {
@@ -2466,31 +2517,37 @@ sub annotate_values {
print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
$type = 'V';
- } elsif ($cur =~ /^($Ident|$Constant)/o) {
+ } elsif (($is_ident_start || $ch =~ /[0-9.]/) && $stream =~ /\G($Ident|$Constant)/o) {
+ $tok_len = length($1);
print "IDENT($1)\n" if ($dbg_values > 1);
$type = 'V';
- } elsif ($cur =~ /^($Assignment)/o) {
+ } elsif ($stream =~ /\G($Assignment)/o) {
+ $tok_len = length($1);
print "ASSIGN($1)\n" if ($dbg_values > 1);
$type = 'N';
- } elsif ($cur =~/^(;|{|})/) {
- print "END($1)\n" if ($dbg_values > 1);
+ } elsif ($ch eq ';' || $ch eq '{' || $ch eq '}') {
+ $tok_len = 1;
+ print "END($ch)\n" if ($dbg_values > 1);
$type = 'E';
$av_pend_colon = 'O';
- } elsif ($cur =~/^(,)/) {
- print "COMMA($1)\n" if ($dbg_values > 1);
+ } elsif ($ch eq ',') {
+ $tok_len = 1;
+ print "COMMA(,)\n" if ($dbg_values > 1);
$type = 'C';
- } elsif ($cur =~ /^(\?)/o) {
- print "QUESTION($1)\n" if ($dbg_values > 1);
+ } elsif ($ch eq '?') {
+ $tok_len = 1;
+ print "QUESTION(?)\n" if ($dbg_values > 1);
$type = 'N';
- } elsif ($cur =~ /^(:)/o) {
- print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
+ } elsif ($ch eq ':') {
+ $tok_len = 1;
+ print "COLON(:,$av_pend_colon)\n" if ($dbg_values > 1);
- substr($var, length($res), 1, $av_pend_colon);
+ substr($var, $pos, 1, $av_pend_colon);
if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
$type = 'E';
} else {
@@ -2498,11 +2555,13 @@ sub annotate_values {
}
$av_pend_colon = 'O';
- } elsif ($cur =~ /^(\[)/o) {
- print "CLOSE($1)\n" if ($dbg_values > 1);
+ } elsif ($ch eq '[') {
+ $tok_len = 1;
+ print "CLOSE([)\n" if ($dbg_values > 1);
$type = 'N';
- } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
+ } elsif ($stream =~ /\G(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
+ $tok_len = length($1);
my $variant;
print "OPV($1)\n" if ($dbg_values > 1);
@@ -2512,21 +2571,23 @@ sub annotate_values {
$variant = 'U';
}
- substr($var, length($res), 1, $variant);
+ substr($var, $pos, 1, $variant);
$type = 'N';
- } elsif ($cur =~ /^($Operators)/o) {
+ } elsif ($stream =~ /\G($Operators)/o) {
+ $tok_len = length($1);
print "OP($1)\n" if ($dbg_values > 1);
if ($1 ne '++' && $1 ne '--') {
$type = 'N';
}
- } elsif ($cur =~ /(^.)/o) {
+ } elsif ($stream =~ /\G(.)/o) {
+ $tok_len = length($1);
print "C($1)\n" if ($dbg_values > 1);
}
- if (defined $1) {
- $cur = substr($cur, length($1));
- $res .= $type x length($1);
+ if (defined $tok_len) {
+ $pos += $tok_len;
+ $res .= $type x $tok_len;
}
}
next prev parent reply other threads:[~2026-12-01 22:54 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
2026-12-01 22:53 ` [PATCH 01/10] checkpatch: pre-filter obsolete MAINTAINERS checks and gate on OBSOLETE Bart Van Assche
2026-12-01 22:53 ` [PATCH 02/10] checkpatch: cache SPDX license checks and gate on SPDX_LICENSE_TAG Bart Van Assche
2026-12-01 22:53 ` [PATCH 03/10] checkpatch: cache git and PATH lookups and skip git_is_single_file() Bart Van Assche
2026-12-01 22:53 ` [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds Bart Van Assche
2026-12-01 22:53 ` [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing Bart Van Assche
2026-12-01 22:53 ` Bart Van Assche [this message]
2026-12-01 22:53 ` [PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers Bart Van Assche
2026-12-01 23:26 ` [PATCH 00/10] Make checkpatch faster Joe Perches
2026-12-01 23:53 ` Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=24a2cbf5675d590976eafc428c1ea66b379bc25d.1796165270.git.bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=akpm@linux-foundation.org \
--cc=apw@canonical.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®