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=1796165653; b=Te6cNwZmWRX0vLC8HXAy1lUjrv2kgdUekF5n/yjiA7u3faG7jS2J5DAbd0KjLR6vdHJM Bqb69wzcumI4Jlv76MWCHik2E1Tw+trWcU+0c24j9dIvXw+ppPPVZNhmr1ZBsZvjfxe/7 Vrvb8vbwUO7UUoBGp+coL9wkQBP/dTc2nvI1hLVnqzWTpcOCOsffTitG6+Vn8BrmZqRKH Hv7S6/4/mp/djdw7epJogCpM7E0t3873P6g1DwNKBvMbnqS2xYri/oD4SnLoQ9WXYU+JC z93zLLgTNEfA1CcZcZ6/Tt6yNw2P8eNco6VBuVYUqPe0TuqRcIdEctxbevXhmFXJRjw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796165653; 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=hQL239Ok+w+QuUM4YUvVAd2Ml4LpXurECNHGooM97SI=; b=WsRZ8URBT7OKLgHJkqeEgCH8dJc27Te6KBp/nlv7VXksDLLtDfmKqPHKvYKvboU6bR81 UDpqsHS3YeJH25Ia1H3Ar8Bv1z6GwqESH1ICh7u85q51sIoAGPN+7QTQQ2cc0O5nFiZh3 C9hOnIqTzrRxYwqMzm+/+Y9krJaeakN1Bi1dKeN2JSeAxUPxhV+SBSDjGLCzctubsTbfS 5JGMYdG4z3+ORX/SePypbysmoxYii8H9P9FSvBae+TgOWDdORGf6og+A2TqV9zPm4hVmj xX5gtwNDsJheFu3sAKfCdqSswD+Vp2nw7Gj2hiVgQTxnAtGlHdABYPEmIjB4WawMMyA== 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=bPoOZKi8; 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 F2749606A33D for ; Tue, 1 Dec 2026 22:54:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F2749606A33D 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=bPoOZKi8 Received: from localhost (localhost [127.0.0.1]) by 011.lax.mailroute.net (Postfix) with ESMTP id 4jfGR82jlKz1XMYJS; Tue, 1 Dec 2026 22:54:12 +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=1796165647; x=1798757648; bh=hQL23 9Ok+w+QuUM4YUvVAd2Ml4LpXurECNHGooM97SI=; b=bPoOZKi8W+O3MdMOURH/w RX5uh2GHVJwRn7T0YLI8bGMmgqDxx/H7Xde/Que4wSm1ZsX4h09C5rwPEsRkVCdT rHph0JEP9o3hynh+07sUzR1N2SZ0WbtSCTOXaCor3SwOqUmdRclvSGfPM4aCzZ5d bZqqiZgVdEVwOoEsbd3TxTO6awcjB/iumXQzRmuHH1Yu14eg9jSvUIiJQYd34v7m vwcEHC2gw84xmSyyHkGD4e6FhyxeeWwtha5AZcbrMRufrIaWirAimwR1JtNEhlY5 XKart6ej0zUCIqm5iPeVr6CTrf2jQ2KiiSan3SSSSYWVHKiehalaNrev6UMx3jFX 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 4evBm9jjnA2W; Tue, 1 Dec 2026 22:54:07 +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 4jfGR059Rjz1XMYJd; 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 08/10] checkpatch: scan ctx_statement_block() in place without slicing Date: Tue, 1 Dec 2026 14:53:52 -0800 Message-ID: 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 Remove the per-character $remainder =3D substr($blk, $off) allocation in ctx_statement_block() and advance directly across /[#;(){}\ne]/g using pos($blk) and \G-anchored matches. Compute $p only when handling 'e' or newlines. Eliminate quadratic tail-string copying and skip irrelevant characters in C instead of stepping one byte at a time in Perl. Signed-off-by: Bart Van Assche --- scripts/checkpatch.pl | 137 +++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 62 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 3eb2ac2c7a9b..400fae964e60 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -1935,7 +1935,6 @@ sub ctx_statement_block { my $c; my $len =3D 0; =20 - my $remainder; while (1) { @stack =3D (['', 0]) if ($#stack =3D=3D -1); =20 @@ -1958,79 +1957,93 @@ sub ctx_statement_block { if ($off >=3D $len) { last; } - if ($level =3D=3D 0 && substr($blk, $off) =3D~ /^.\s*#\s*define/) { - $level++; - $type =3D '#'; + if ($level =3D=3D 0) { + pos($blk) =3D $off; + if ($blk =3D~ /\G.\s*#\s*define/) { + $level++; + $type =3D '#'; + } } } - $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)}; - } =20 - # 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"; + pos($blk) =3D $off; + if ($blk =3D~ /[#;(){}\ne]/g) { + $off =3D $-[0]; + } else { + $off =3D $len; + next; } + $c =3D substr($blk, $off, 1); =20 - if (($type eq '' || $type eq '(') && $c eq '(') { - $level++; - $type =3D '('; - } - if ($type eq '(' && $c eq ')') { - $level--; - $type =3D ($level !=3D 0)? '(' : ''; + # Handle nested #if/#else. + if ($c eq '#') { + pos($blk) =3D $off; + if ($blk =3D~ /\G#\s*(?:ifndef|ifdef|if)\s/) { + push(@stack, [ $type, $level ]); + } elsif ($blk =3D~ /\G#\s*(?:else|elif)\b/) { + ($type, $level) =3D @{$stack[$#stack - 1]}; + } elsif ($blk =3D~ /\G#\s*endif\b/) { + ($type, $level) =3D @{pop(@stack)}; + } + } elsif ($c eq ';') { + # Statement ends at the ';' or a close '}' at the + # outermost level. + last if ($level =3D=3D 0); + } elsif ($c eq 'e') { + # An else is really a conditional as long as its not else if + if ($level =3D=3D 0 && $coff_set =3D=3D 0) { + $p =3D ($off > $soff) ? substr($blk, $off - 1, 1) : undef; + pos($blk) =3D $off; + if ((!defined($p) || $p =3D~ /(?:\s|\}|\+)/) && + $blk =3D~ /\G(else)(?:(?!\s+if\b)\s|\{)/) { + $coff =3D $off + length($1) - 1; + $coff_set =3D 1; + } + } + } elsif ($c eq '(') { + if ($type eq '' || $type eq '(') { + $level++; + $type =3D '('; + } + } elsif ($c eq ')') { + if ($type eq '(') { + $level--; + $type =3D ($level !=3D 0)? '(' : ''; =20 - if ($level =3D=3D 0 && $coff < $soff) { - $coff =3D $off; - $coff_set =3D 1; - #warn "CSB: mark coff<$coff>\n"; + if ($level =3D=3D 0 && $coff < $soff) { + $coff =3D $off; + $coff_set =3D 1; + } } - } - if (($type eq '' || $type eq '{') && $c eq '{') { - $level++; - $type =3D '{'; - } - if ($type eq '{' && $c eq '}') { - $level--; - $type =3D ($level !=3D 0)? '{' : ''; + } elsif ($c eq '{') { + if ($type eq '' || $type eq '{') { + $level++; + $type =3D '{'; + } + } elsif ($c eq '}') { + if ($type eq '{') { + $level--; + $type =3D ($level !=3D 0)? '{' : ''; =20 - if ($level =3D=3D 0) { - if (substr($blk, $off + 1, 1) eq ';') { + if ($level =3D=3D 0) { + if (substr($blk, $off + 1, 1) eq ';') { + $off++; + } + last; + } + } + } elsif ($c eq "\n") { + # Preprocessor commands end at the newline unless escaped. + if ($type eq '#') { + $p =3D ($off > $soff) ? substr($blk, $off - 1, 1) : undef; + if (!defined($p) || $p ne "\\") { + $level--; + $type =3D ''; $off++; + last; } - last; } } - # Preprocessor commands end at the newline unless escaped. - if ($type eq '#' && $c eq "\n" && $p ne "\\") { - $level--; - $type =3D ''; - $off++; - last; - } $off++; } # We are truly at the end, so shuffle to the next line.