[PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing
From: Bart Van Assche
Date: Tue Dec 01 2026 - 17:54:34 EST
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@xxxxxxx>
---
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.