mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 08/10] checkpatch: scan ctx_statement_block() in place without slicing
Date: Tue,  1 Dec 2026 14:53:52 -0800	[thread overview]
Message-ID: <f9fbc70a048bb5bb2caa40164e84e0dca900b6dd.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

Remove the per-character $remainder = 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 <bvanassche@acm.org>
---
 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 = 0;
 
-	my $remainder;
 	while (1) {
 		@stack = (['', 0]) if ($#stack == -1);
 
@@ -1958,79 +1957,93 @@ sub ctx_statement_block {
 			if ($off >= $len) {
 				last;
 			}
-			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
-				$level++;
-				$type = '#';
+			if ($level == 0) {
+				pos($blk) = $off;
+				if ($blk =~ /\G.\s*#\s*define/) {
+					$level++;
+					$type = '#';
+				}
 			}
 		}
-		$p = $c;
-		$c = substr($blk, $off, 1);
-		$remainder = substr($blk, $off);
-
-		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
-
-		# Handle nested #if/#else.
-		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
-			push(@stack, [ $type, $level ]);
-		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
-			($type, $level) = @{$stack[$#stack - 1]};
-		} elsif ($remainder =~ /^#\s*endif\b/) {
-			($type, $level) = @{pop(@stack)};
-		}
 
-		# Statement ends at the ';' or a close '}' at the
-		# outermost level.
-		if ($level == 0 && $c eq ';') {
-			last;
-		}
-
-		# An else is really a conditional as long as its not else if
-		if ($level == 0 && $coff_set == 0 &&
-				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
-				$remainder =~ /^(else)(?:\s|{)/ &&
-				$remainder !~ /^else\s+if\b/) {
-			$coff = $off + length($1) - 1;
-			$coff_set = 1;
-			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
-			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
+		pos($blk) = $off;
+		if ($blk =~ /[#;(){}\ne]/g) {
+			$off = $-[0];
+		} else {
+			$off = $len;
+			next;
 		}
+		$c = substr($blk, $off, 1);
 
-		if (($type eq '' || $type eq '(') && $c eq '(') {
-			$level++;
-			$type = '(';
-		}
-		if ($type eq '(' && $c eq ')') {
-			$level--;
-			$type = ($level != 0)? '(' : '';
+		# Handle nested #if/#else.
+		if ($c eq '#') {
+			pos($blk) = $off;
+			if ($blk =~ /\G#\s*(?:ifndef|ifdef|if)\s/) {
+				push(@stack, [ $type, $level ]);
+			} elsif ($blk =~ /\G#\s*(?:else|elif)\b/) {
+				($type, $level) = @{$stack[$#stack - 1]};
+			} elsif ($blk =~ /\G#\s*endif\b/) {
+				($type, $level) = @{pop(@stack)};
+			}
+		} elsif ($c eq ';') {
+			# Statement ends at the ';' or a close '}' at the
+			# outermost level.
+			last if ($level == 0);
+		} elsif ($c eq 'e') {
+			# An else is really a conditional as long as its not else if
+			if ($level == 0 && $coff_set == 0) {
+				$p = ($off > $soff) ? substr($blk, $off - 1, 1) : undef;
+				pos($blk) = $off;
+				if ((!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
+				    $blk =~ /\G(else)(?:(?!\s+if\b)\s|\{)/) {
+					$coff = $off + length($1) - 1;
+					$coff_set = 1;
+				}
+			}
+		} elsif ($c eq '(') {
+			if ($type eq '' || $type eq '(') {
+				$level++;
+				$type = '(';
+			}
+		} elsif ($c eq ')') {
+			if ($type eq '(') {
+				$level--;
+				$type = ($level != 0)? '(' : '';
 
-			if ($level == 0 && $coff < $soff) {
-				$coff = $off;
-				$coff_set = 1;
-				#warn "CSB: mark coff<$coff>\n";
+				if ($level == 0 && $coff < $soff) {
+					$coff = $off;
+					$coff_set = 1;
+				}
 			}
-		}
-		if (($type eq '' || $type eq '{') && $c eq '{') {
-			$level++;
-			$type = '{';
-		}
-		if ($type eq '{' && $c eq '}') {
-			$level--;
-			$type = ($level != 0)? '{' : '';
+		} elsif ($c eq '{') {
+			if ($type eq '' || $type eq '{') {
+				$level++;
+				$type = '{';
+			}
+		} elsif ($c eq '}') {
+			if ($type eq '{') {
+				$level--;
+				$type = ($level != 0)? '{' : '';
 
-			if ($level == 0) {
-				if (substr($blk, $off + 1, 1) eq ';') {
+				if ($level == 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 = ($off > $soff) ? substr($blk, $off - 1, 1) : undef;
+				if (!defined($p) || $p ne "\\") {
+					$level--;
+					$type = '';
 					$off++;
+					last;
 				}
-				last;
 			}
 		}
-		# Preprocessor commands end at the newline unless escaped.
-		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
-			$level--;
-			$type = '';
-			$off++;
-			last;
-		}
 		$off++;
 	}
 	# We are truly at the end, so shuffle to the next line.

  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 ` Bart Van Assche [this message]
2026-12-01 22:53 ` [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch Bart Van Assche
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=f9fbc70a048bb5bb2caa40164e84e0dca900b6dd.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®