From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=199.89.1.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796165657; b=ZvGY6WIqFz9AmSYzbBvY/Ekqoa2Ztod83R65SjjdTL7AUrNXjoVBa4MxCflzma+UNLyu SQ134u1sq4IVgvN0FRvNFTSilwnMuu749B++OE3gLjgUiUb3mu2dVh1ui6xxlHoMsIqj/ 4IDKaJrEqMcemEZNqoktiFeUu+7nW8FYW7eqLuuZ1XYqmj3kVdZh3B+UKoeR+LDCYsKio bnaw7xhaIYPAIAeGHH+iZVwEBPY4XMahoqiOvGu1etQ9Uo22M1/UAPhyblj63Hy/lzBmK RlthDm4IHM2hf/Qtv8APG6VEBwZMhISelToS0Dq9X5sJBZ7hUXrtlZQF9ahLRBEwQMw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796165657; h=DMARC-Filter:Received:DKIM-Signature:X-Virus-Scanned:Received: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding; bh=eOu/54omIE5sQnazrKL7yrgETswxIT2JEsL97Lupb/A=; b=BRSte+qBSmdG06T0evnlST1yEHxJKV5CiWlKjCM10P0QjbJgvJAJZIZP+NtVFTKr4pne OKyEbx1flekdu/BZvbP6Ry8pNSK6nQCy5sKBY4GTLzzYgeBbp5Ohxz7Qm9yHb6e00pogl Jf2iSvWXQgXD8YtL991vtlsDagOb1UYDKNnMyJsSKuFuSRZ0fe8bNEOd7qIHQ1X8pbHxm zAzcRmLTeAD3kWg6cm7NTYRwXeVkHGH3PqIfZdJ0HlJeOnZES4Hu6z0GVXzJWab6RLH3A XBi9t65kYkUCjw+736XLsFaIMN4X1R85xSXCP6fdutkl4gUxToFwoZkDcb9BQdyMtUQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b="h/Xitezr"; arc=none smtp.remote-ip=199.89.1.14 Received: from 011.lax.mailroute.net (011.lax.mailroute.net [199.89.1.14]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CB89606A329 for ; Tue, 1 Dec 2026 22:54:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4CB89606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=h/Xitezr Received: from localhost (localhost [127.0.0.1]) by 011.lax.mailroute.net (Postfix) with ESMTP id 4jfGRF0YLsz1XMYJH; Tue, 1 Dec 2026 22:54:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=mr01; t=1796165649; x=1798757650; bh=eOu/5 4omIE5sQnazrKL7yrgETswxIT2JEsL97Lupb/A=; b=h/Xitezra9L778MQaE2Vt cFwhDpguOeSsW8NnFu4Mt/n3bOJ9WLEZRonoza9jJaeIMd5D2QRKq/rrzhVJ6+si bmmKCPet7OQbDVXVMYs2vztBii8mPXG6qVxMLx9bg9/bU1MGEo7KUHqzqaXV1EPA kwhHMlDMl96EO5WH/Qo9jwv5dA7gqHDTbwHdEa9PwCPxCbpuzDnYXMIsDfrTCOBa 6xCIkcIJjqYgdnK7FzMpPguCBmOYVU64abMGwOdL/xJZyAcTc/6SRFN5WLhQKSHM XGMQqvkXbrYkIJMIvZ2rvtBHnNvzhgpSi3ZBvPdmB8e/xspz9dsXC2JS6X8oE9Dp A== X-Virus-Scanned: by MailRoute Received: from 011.lax.mailroute.net ([127.0.0.1]) by localhost (011.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id 1g72theNEXrN; Tue, 1 Dec 2026 22:54:09 +0000 (UTC) Received: from bvanassche.c.googlers.com.com (57.237.82.34.bc.googleusercontent.com [34.82.237.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 011.lax.mailroute.net (Postfix) with ESMTPSA id 4jfGR106WKz1XMYJZ; Tue, 1 Dec 2026 22:54:04 +0000 (UTC) From: Bart Van Assche To: Joe Perches , Andy Whitcroft Cc: Andrew Morton , linux-kernel@vger.kernel.org, Bart Van Assche Subject: [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch Date: Tue, 1 Dec 2026 14:53:53 -0800 Message-ID: <24a2cbf5675d590976eafc428c1ea66b379bc25d.1796165270.git.bvanassche@acm.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Replace per-token $cur =3D 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 wher= e 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 --- 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 '$con= ststructsfile': $!\n"; } =20 +my $has_other_typedefs =3D 0; +my $typedefs_need_substr =3D 0; if (defined($typedefsfile)) { my $typeOtherTypedefs; read_words(\$typeOtherTypedefs, $typedefsfile) or warn "No additional types will be considered - file '$typedefsfi= le': $!\n"; - $typeTypedefs .=3D '|' . $typeOtherTypedefs if (defined $typeOtherTyped= efs); + if (defined $typeOtherTypedefs) { + $typeTypedefs .=3D '|' . $typeOtherTypedefs; + $has_other_typedefs =3D 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 =3D + ($typeOtherTypedefs !~ /^[A-Za-z0-9_]+(?:\|[A-Za-z0-9_]+)*\z/); + } } =20 +my $mods_ident_only =3D 1; +my $types_ident_only =3D 1; +my $types_need_substr =3D 0; sub build_types { + $mods_ident_only =3D !grep { !/^[A-Za-z_]/ } @modifierListFile; + $types_ident_only =3D $mods_ident_only && !$has_other_typedefs && + !grep { !/^[A-Za-z_]/ } @typeListFile; + $types_need_substr =3D $typedefs_need_substr || + grep { !/^[A-Za-z0-9_]+\z/ } (@modifierListFile, @typeListFile); my $mods =3D "(?x: \n" . join("|\n ", (@modifierList, @modifierListFi= le)) . "\n)"; my $all =3D "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\= n)"; my $Misordered =3D "(?x: \n" . join("|\n ", @typeListMisordered) . "\= n)"; @@ -2337,36 +2355,55 @@ sub annotate_values { my ($stream, $type) =3D @_; =20 my $res; - my $var =3D '_' x length($stream); - my $cur =3D $stream; + my $slen =3D length($stream); + my $var =3D '_' x $slen; + my $pos =3D 0; =20 print "$stream\n" if ($dbg_values > 1); =20 - while (length($cur)) { + while ($pos < $slen) { @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) { + my $ch =3D substr($stream, $pos, 1); + my $is_ident_start =3D ($ch =3D~ /[A-Za-z_]/); + pos($stream) =3D $pos; + my $tok_len; + if ($stream =3D~ /\G(\s+)/o) { + $tok_len =3D length($1); print "WS($1)\n" if ($dbg_values > 1); if ($1 =3D~ /\n/ && $av_preprocessor) { $type =3D pop(@av_paren_type); $av_preprocessor =3D 0; } =20 - } elsif ($cur =3D~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { + } elsif ($ch eq '(' && $av_pending eq '_' && + ($types_need_substr ? + substr($stream, $pos) =3D~ /^(\(\s*$Type\s*)\)/ : + $stream =3D~ /\G(\(\s*$Type\s*)\)/)) { + $tok_len =3D length($1); print "CAST($1)\n" if ($dbg_values > 1); push(@av_paren_type, $type); $type =3D 'c'; =20 - } elsif ($cur =3D~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { + } elsif (($is_ident_start || !$types_ident_only) && + ($types_need_substr ? + substr($stream, $pos) =3D~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/ : + $stream =3D~ /\G($Type)\s*(?:$Ident|,|\)|\(|\s*$)/)) { + $tok_len =3D length($1); print "DECLARE($1)\n" if ($dbg_values > 1); $type =3D 'T'; =20 - } elsif ($cur =3D~ /^($Modifier)\s*/) { + } elsif (($is_ident_start || !$mods_ident_only) && + ($types_need_substr ? + substr($stream, $pos) =3D~ /^($Modifier)\s*/ : + $stream =3D~ /\G($Modifier)\s*/)) { + $tok_len =3D length($1); print "MODIFIER($1)\n" if ($dbg_values > 1); $type =3D 'T'; =20 - } elsif ($cur =3D~ /^(\#\s*define\s*$Ident)(\(?)/o) { + } elsif ($ch eq '#' && $stream =3D~ /\G(\#\s*define\s*$Ident)(\(?)/o) = { + $tok_len =3D length($1); print "DEFINE($1,$2)\n" if ($dbg_values > 1); $av_preprocessor =3D 1; push(@av_paren_type, $type); @@ -2375,12 +2412,14 @@ sub annotate_values { } $type =3D 'E'; =20 - } elsif ($cur =3D~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { + } elsif ($ch eq '#' && $stream =3D~ /\G(\#\s*(?:undef\s*$Ident|include= \b))/o) { + $tok_len =3D length($1); print "UNDEF($1)\n" if ($dbg_values > 1); $av_preprocessor =3D 1; push(@av_paren_type, $type); =20 - } elsif ($cur =3D~ /^(\#\s*(?:ifdef|ifndef|if))/o) { + } elsif ($ch eq '#' && $stream =3D~ /\G(\#\s*(?:ifdef|ifndef|if))/o) { + $tok_len =3D length($1); print "PRE_START($1)\n" if ($dbg_values > 1); $av_preprocessor =3D 1; =20 @@ -2388,7 +2427,8 @@ sub annotate_values { push(@av_paren_type, $type); $type =3D 'E'; =20 - } elsif ($cur =3D~ /^(\#\s*(?:else|elif))/o) { + } elsif ($ch eq '#' && $stream =3D~ /\G(\#\s*(?:else|elif))/o) { + $tok_len =3D length($1); print "PRE_RESTART($1)\n" if ($dbg_values > 1); $av_preprocessor =3D 1; =20 @@ -2396,7 +2436,8 @@ sub annotate_values { =20 $type =3D 'E'; =20 - } elsif ($cur =3D~ /^(\#\s*(?:endif))/o) { + } elsif ($ch eq '#' && $stream =3D~ /\G(\#\s*(?:endif))/o) { + $tok_len =3D length($1); print "PRE_END($1)\n" if ($dbg_values > 1); =20 $av_preprocessor =3D 1; @@ -2407,57 +2448,67 @@ sub annotate_values { push(@av_paren_type, $type); $type =3D 'E'; =20 - } elsif ($cur =3D~ /^(\\\n)/o) { + } elsif ($ch eq "\\" && $stream =3D~ /\G(\\\n)/o) { + $tok_len =3D length($1); print "PRECONT($1)\n" if ($dbg_values > 1); =20 - } elsif ($cur =3D~ /^(__attribute__)\s*\(?/o) { + } elsif ($is_ident_start && $stream =3D~ /\G(__attribute__)\s*\(?/o) { + $tok_len =3D length($1); print "ATTR($1)\n" if ($dbg_values > 1); $av_pending =3D $type; $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(sizeof)\s*(\()?/o) { + } elsif ($is_ident_start && $stream =3D~ /\G(sizeof)\s*(\()?/o) { + $tok_len =3D length($1); print "SIZEOF($1)\n" if ($dbg_values > 1); if (defined $2) { $av_pending =3D 'V'; } $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(if|while|for)\b/o) { + } elsif ($is_ident_start && $stream =3D~ /\G(if|while|for)\b/o) { + $tok_len =3D length($1); print "COND($1)\n" if ($dbg_values > 1); $av_pending =3D 'E'; $type =3D 'N'; =20 - } elsif ($cur =3D~/^(case)/o) { + } elsif ($is_ident_start && $stream =3D~ /\G(case)/o) { + $tok_len =3D length($1); print "CASE($1)\n" if ($dbg_values > 1); $av_pend_colon =3D 'C'; $type =3D 'N'; =20 - } elsif ($cur =3D~/^(return|else|goto|typeof|__typeof__)\b/o) { + } elsif ($is_ident_start && $stream =3D~ /\G(return|else|goto|typeof|_= _typeof__)\b/o) { + $tok_len =3D length($1); print "KEYWORD($1)\n" if ($dbg_values > 1); $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(\()/o) { - print "PAREN('$1')\n" if ($dbg_values > 1); + } elsif ($ch eq '(') { + $tok_len =3D 1; + print "PAREN('(')\n" if ($dbg_values > 1); push(@av_paren_type, $av_pending); $av_pending =3D '_'; $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(\))/o) { + } elsif ($ch eq ')') { + $tok_len =3D 1; my $new_type =3D pop(@av_paren_type); if ($new_type ne '_') { $type =3D $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); } =20 - } elsif ($cur =3D~ /^($Ident)\s*\(/o) { + } elsif ($is_ident_start && $stream =3D~ /\G($Ident)\s*\(/o) { + $tok_len =3D length($1); print "FUNC($1)\n" if ($dbg_values > 1); $type =3D 'V'; $av_pending =3D 'V'; =20 - } elsif ($cur =3D~ /^($Ident\s*):(?:\s*\d+\s*(,|=3D|;))?/) { + } elsif ($is_ident_start && $stream =3D~ /\G($Ident\s*):(?:\s*\d+\s*(,= |=3D|;))?/) { + $tok_len =3D length($1); if (defined $2 && $type eq 'C' || $type eq 'T') { $av_pend_colon =3D '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 =3D 'V'; =20 - } elsif ($cur =3D~ /^($Ident|$Constant)/o) { + } elsif (($is_ident_start || $ch =3D~ /[0-9.]/) && $stream =3D~ /\G($I= dent|$Constant)/o) { + $tok_len =3D length($1); print "IDENT($1)\n" if ($dbg_values > 1); $type =3D 'V'; =20 - } elsif ($cur =3D~ /^($Assignment)/o) { + } elsif ($stream =3D~ /\G($Assignment)/o) { + $tok_len =3D length($1); print "ASSIGN($1)\n" if ($dbg_values > 1); $type =3D 'N'; =20 - } elsif ($cur =3D~/^(;|{|})/) { - print "END($1)\n" if ($dbg_values > 1); + } elsif ($ch eq ';' || $ch eq '{' || $ch eq '}') { + $tok_len =3D 1; + print "END($ch)\n" if ($dbg_values > 1); $type =3D 'E'; $av_pend_colon =3D 'O'; =20 - } elsif ($cur =3D~/^(,)/) { - print "COMMA($1)\n" if ($dbg_values > 1); + } elsif ($ch eq ',') { + $tok_len =3D 1; + print "COMMA(,)\n" if ($dbg_values > 1); $type =3D 'C'; =20 - } elsif ($cur =3D~ /^(\?)/o) { - print "QUESTION($1)\n" if ($dbg_values > 1); + } elsif ($ch eq '?') { + $tok_len =3D 1; + print "QUESTION(?)\n" if ($dbg_values > 1); $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(:)/o) { - print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); + } elsif ($ch eq ':') { + $tok_len =3D 1; + print "COLON(:,$av_pend_colon)\n" if ($dbg_values > 1); =20 - 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 =3D 'E'; } else { @@ -2498,11 +2555,13 @@ sub annotate_values { } $av_pend_colon =3D 'O'; =20 - } elsif ($cur =3D~ /^(\[)/o) { - print "CLOSE($1)\n" if ($dbg_values > 1); + } elsif ($ch eq '[') { + $tok_len =3D 1; + print "CLOSE([)\n" if ($dbg_values > 1); $type =3D 'N'; =20 - } elsif ($cur =3D~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { + } elsif ($stream =3D~ /\G(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { + $tok_len =3D length($1); my $variant; =20 print "OPV($1)\n" if ($dbg_values > 1); @@ -2512,21 +2571,23 @@ sub annotate_values { $variant =3D 'U'; } =20 - substr($var, length($res), 1, $variant); + substr($var, $pos, 1, $variant); $type =3D 'N'; =20 - } elsif ($cur =3D~ /^($Operators)/o) { + } elsif ($stream =3D~ /\G($Operators)/o) { + $tok_len =3D length($1); print "OP($1)\n" if ($dbg_values > 1); if ($1 ne '++' && $1 ne '--') { $type =3D 'N'; } =20 - } elsif ($cur =3D~ /(^.)/o) { + } elsif ($stream =3D~ /\G(.)/o) { + $tok_len =3D length($1); print "C($1)\n" if ($dbg_values > 1); } - if (defined $1) { - $cur =3D substr($cur, length($1)); - $res .=3D $type x length($1); + if (defined $tok_len) { + $pos +=3D $tok_len; + $res .=3D $type x $tok_len; } } =20