* [PATCH 01/10] checkpatch: pre-filter obsolete MAINTAINERS checks and gate on OBSOLETE
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
@ 2026-12-01 22:53 ` 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
` (9 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Pre-filter candidate paths in is_maintained_obsolete() by scanning
MAINTAINERS once via load_obsolete_candidates() for sections whose S: line
matches obsolete, and gate the call site in process() on
show_type("OBSOLETE").
Avoid spawning perl scripts/get_maintainer.pl (~210 ms per unique file)
for paths that cannot match any obsolete F: or N: entry and when OBSOLETE
warnings are suppressed via --ignore or --types. Fall back to invoking
scripts/get_maintainer.pl whenever MAINTAINERS is not a regular file, a
.get_maintainer.conf file is present, an obsolete section contains K: or
bracket/brace globs, or an N: regex fails to compile.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 144 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 142 insertions(+), 2 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b8b64f720191..543fd002ee29 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -11,7 +11,8 @@ use strict;
use warnings;
use POSIX;
use File::Basename;
-use Cwd 'abs_path';
+use Cwd qw(abs_path fastgetcwd);
+use File::Spec::Functions qw(canonpath);
use Term::ANSIColor qw(:constants);
use Encode qw(decode encode);
@@ -1170,6 +1171,121 @@ sub seed_camelcase_file {
}
our %maintained_status = ();
+my $obsolete_candidates_loaded = 0;
+my $obsolete_candidates_fallback = 0;
+my @obsolete_candidates = ();
+
+sub flush_obsolete_section {
+ my ($header, $is_obsolete, $has_r, $r_obsolete, $has_k, $fn_ref) = @_;
+
+ return 1 if (!$is_obsolete && !($has_r && ($r_obsolete || $header =~ /obsolete/i)));
+ return 0 if ($has_k);
+
+ foreach my $entry (@$fn_ref) {
+ my ($tag, $pat) = @$entry;
+ return 0 if ($pat eq '');
+ if ($tag eq 'N') {
+ return 0 if ($pat =~ /[^\x00-\x7f]/);
+ local $@;
+ my $nre = eval { no warnings 'regexp'; qr{$pat}x };
+ return 0 if ($@);
+ push(@obsolete_candidates, $nre);
+ } else {
+ return 0 if ($pat =~ /[\[\{\(\)\|\+\\\^\$\x00]/ ||
+ $pat =~ /[^\x00-\x7f]/);
+ my $re = quotemeta($pat);
+ $re =~ s/\\\*/.*/g;
+ $re =~ s/\\\?/./g;
+ push(@obsolete_candidates, qr{^$re});
+ }
+ }
+ return 1;
+}
+
+sub load_obsolete_candidates {
+ return if ($obsolete_candidates_loaded);
+ $obsolete_candidates_loaded = 1;
+
+ my $cwd = abs_path('.');
+ my $root_abs = abs_path($root);
+ my $home = defined($ENV{HOME}) ? $ENV{HOME} : "";
+ my $has_conf = grep { -e "$_/.get_maintainer.conf" }
+ split(/:/, ".:$home:.scripts");
+ if (!defined($cwd) || !defined($root_abs) || $cwd ne $root_abs ||
+ $root =~ /obsolete/i ||
+ !(-f "MAINTAINERS") ||
+ $has_conf) {
+ $obsolete_candidates_fallback = 1;
+ return;
+ }
+
+ if (-f ".mailmap") {
+ open(my $mm, '<', ".mailmap") or do {
+ $obsolete_candidates_fallback = 1;
+ return;
+ };
+ while (my $line = <$mm>) {
+ $line =~ s/#.*$//;
+ if ($line =~ /obsolete/i) {
+ $obsolete_candidates_fallback = 1;
+ close($mm);
+ return;
+ }
+ }
+ close($mm);
+ }
+
+ # Decode as UTF-8 like get_maintainer.pl does such that \s matches
+ # the same (Unicode) whitespace characters.
+ open(my $maint, '<:encoding(UTF-8)', "MAINTAINERS") or do {
+ $obsolete_candidates_fallback = 1;
+ return;
+ };
+
+ my $header = '';
+ my $is_obsolete = 0;
+ my $has_r = 0;
+ my $r_obsolete = 0;
+ my $has_k = 0;
+ my @fn = ();
+
+ no warnings 'utf8';
+ while (my $line = <$maint>) {
+ chomp $line;
+ if ($line =~ /^([A-Z]):\s*(.*)/) {
+ my ($tag, $val) = ($1, $2);
+ if ($tag eq 'S') {
+ $is_obsolete = 1 if ($val =~ /obsolete/i);
+ } elsif ($tag eq 'R') {
+ $has_r = 1;
+ $r_obsolete = 1 if ($val =~ /obsolete/i);
+ } elsif ($tag eq 'K') {
+ $has_k = 1;
+ } elsif ($tag eq 'F' || $tag eq 'N') {
+ push(@fn, [$tag, $val]);
+ }
+ } elsif ($line !~ /^\s*$/ && $line !~ /^\s*\#/) {
+ if (!flush_obsolete_section($header, $is_obsolete, $has_r,
+ $r_obsolete, $has_k, \@fn)) {
+ $obsolete_candidates_fallback = 1;
+ close($maint);
+ return;
+ }
+ $header = $line;
+ $is_obsolete = 0;
+ $has_r = 0;
+ $r_obsolete = 0;
+ $has_k = 0;
+ @fn = ();
+ }
+ }
+ close($maint);
+
+ if (!flush_obsolete_section($header, $is_obsolete, $has_r,
+ $r_obsolete, $has_k, \@fn)) {
+ $obsolete_candidates_fallback = 1;
+ }
+}
sub is_maintained_obsolete {
my ($filename) = @_;
@@ -1177,6 +1293,30 @@ sub is_maintained_obsolete {
return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
if (!exists($maintained_status{$filename})) {
+ load_obsolete_candidates();
+ if (!$obsolete_candidates_fallback &&
+ $filename =~ m{^[A-Za-z0-9_/.+,:\@=%][A-Za-z0-9_/.+,:\-\@=%]*\z} &&
+ $filename !~ /obsolete/i) {
+ my $norm = canonpath($filename);
+ if ($norm !~ m@(?:^|/)\.\.(?:/|$)@ &&
+ $norm !~ /\.yaml$/i) {
+ $norm .= '/' if (-d $norm && $norm !~ m@/$@);
+ my $cur_path = fastgetcwd() . '/';
+ $norm =~ s/^\Q$cur_path\E//;
+ $norm =~ s@^\./@@;
+ my $matched = 0;
+ foreach my $re (@obsolete_candidates) {
+ if ($norm =~ $re) {
+ $matched = 1;
+ last;
+ }
+ }
+ if (!$matched) {
+ $maintained_status{$filename} = "";
+ return 0;
+ }
+ }
+ }
$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
}
@@ -2945,7 +3085,7 @@ sub process {
}
if ($found_file) {
- if (is_maintained_obsolete($realfile)) {
+ if (show_type("OBSOLETE") && is_maintained_obsolete($realfile)) {
WARN("OBSOLETE",
"$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
}
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 02/10] checkpatch: cache SPDX license checks and gate on SPDX_LICENSE_TAG
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 ` 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
` (8 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Memoize is_SPDX_License_valid() results in %spdx_license_cache keyed by
license string, and gate the call site in process() on
show_type("SPDX_LICENSE_TAG").
Avoid launching a new Python interpreter for scripts/spdxcheck.py (~140 ms
per call) when checking repeated license expressions across multiple files
or when SPDX_LICENSE_TAG warnings are ignored.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 543fd002ee29..3602e79fc091 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1323,15 +1323,19 @@ sub is_maintained_obsolete {
return $maintained_status{$filename} =~ /obsolete/i;
}
+my %spdx_license_cache = ();
+
sub is_SPDX_License_valid {
my ($license) = @_;
return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
- my $root_path = abs_path($root);
- my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
- return 0 if ($status ne "");
- return 1;
+ if (!exists($spdx_license_cache{$license})) {
+ my $root_path = abs_path($root);
+ my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
+ $spdx_license_cache{$license} = ($status ne "") ? 0 : 1;
+ }
+ return $spdx_license_cache{$license};
}
my $camelcase_seeded = 0;
@@ -3996,7 +4000,8 @@ sub process {
"Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
my $spdx_license = $1;
- if (!is_SPDX_License_valid($spdx_license)) {
+ if (show_type("SPDX_LICENSE_TAG") &&
+ !is_SPDX_License_valid($spdx_license)) {
WARN("SPDX_LICENSE_TAG",
"'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
}
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 03/10] checkpatch: cache git and PATH lookups and skip git_is_single_file()
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 ` Bart Van Assche
2026-12-01 22:53 ` [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds Bart Van Assche
` (7 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Memoize git_commit_info() outcomes in %git_commit_info_cache while
preserving ok, bad, and passthrough return semantics, skip calling
git_is_single_file() when $git or $file is set or when reading from stdin
('-'), and memoize PATH lookups in which() via %which_cache.
Eliminate redundant git log -1 subprocesses for repeated commit hashes
(including Fixes: and commit references checked by two separate rules on
the same line), stop running git ls-files on commit hashes under -g or
when -f is already specified, and avoid repeating stat() scans across PATH.
Skipping git_is_single_file() is not strictly behavior-preserving. That
function runs an unquoted "git ls-files -- $filename" and matches its
output against an unescaped regular expression. Hence, with this change,
"checkpatch.pl -f 'a[b.c'" for a git-tracked file named 'a[b.c' no longer
dies with "Unmatched [ in regex", and shell syntax errors are no longer
reported for -f arguments that contain shell metacharacters. Behavior
without -f, -g and '-' is unchanged.
As a side effect of the caching, Perl warnings triggered inside which()
(e.g. with PATH unset) or git_commit_info() are reported once per unique
argument instead of once per call.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 30 ++++++++++++++++++++++++++----
1 file changed, 26 insertions(+), 4 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3602e79fc091..4ca3ce0f4502 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1408,16 +1408,31 @@ sub git_is_single_file {
return $count eq 1 && $output =~ m{^${filename}$};
}
+my %git_commit_info_cache = ();
+
sub git_commit_info {
my ($commit, $id, $desc) = @_;
return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
+ if (defined($commit) && exists($git_commit_info_cache{$commit})) {
+ my $cached = $git_commit_info_cache{$commit};
+ if ($cached->[0] eq 'ok') {
+ return ($cached->[1], $cached->[2]);
+ } elsif ($cached->[0] eq 'bad') {
+ return (undef, $desc);
+ }
+ return ($id, $desc);
+ }
+
my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
$output =~ s/^\s*//gm;
my @lines = split("\n", $output);
- return ($id, $desc) if ($#lines < 0);
+ if ($#lines < 0) {
+ $git_commit_info_cache{$commit} = ['passthrough'] if defined($commit);
+ return ($id, $desc);
+ }
if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
# Maybe one day convert this block of bash into something that returns
@@ -1429,12 +1444,15 @@ sub git_commit_info {
# git log --format='%H %s' -1 $line |
# echo "commit $(cut -c 1-12,41-)"
# done
+ $git_commit_info_cache{$commit} = ['passthrough'] if defined($commit);
} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
$lines[0] =~ /^fatal: bad object $commit/) {
$id = undef;
+ $git_commit_info_cache{$commit} = ['bad'] if defined($commit);
} else {
$id = substr($lines[0], 0, 12);
$desc = substr($lines[0], 41);
+ $git_commit_info_cache{$commit} = ['ok', $id, $desc] if defined($commit);
}
return ($id, $desc);
@@ -1483,7 +1501,7 @@ my $vname;
$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
for my $filename (@ARGV) {
my $FILE;
- my $is_git_file = git_is_single_file($filename);
+ my $is_git_file = (!$git && !$file && $filename ne '-') ? git_is_single_file($filename) : 0;
my $oldfile = $file;
$file = 1 if ($is_git_file);
if ($git) {
@@ -1678,16 +1696,20 @@ sub same_email_addresses {
$comment1 eq $comment2;
}
+my %which_cache;
+
sub which {
my ($bin) = @_;
+ return $which_cache{$bin} if exists($which_cache{$bin});
+
foreach my $path (split(/:/, $ENV{PATH})) {
if (-e "$path/$bin") {
- return "$path/$bin";
+ return $which_cache{$bin} = "$path/$bin";
}
}
- return "";
+ return $which_cache{$bin} = "";
}
sub which_conf {
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (2 preceding siblings ...)
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 ` Bart Van Assche
2026-12-01 22:53 ` [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks Bart Van Assche
` (6 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Call build_types() at the end of the per-file loop only when
@modifierListFile or @typeListFile is non-empty, and call build_types() in
possible() only when a new modifier or type is appended.
Avoid rebuilding the composite $Type, $NonptrType, and $Declare regular
expressions and invalidating Perl's cached patterns that interpolate them
when the per-file type and modifier lists have not changed.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 4ca3ce0f4502..ffafef4cd4f1 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1545,9 +1545,11 @@ for my $filename (@ARGV) {
@fixed_inserted = ();
@fixed_deleted = ();
$fixlinenr = -1;
- @modifierListFile = ();
- @typeListFile = ();
- build_types();
+ if (@modifierListFile || @typeListFile) {
+ @modifierListFile = ();
+ @typeListFile = ();
+ build_types();
+ }
$file = $oldfile if ($is_git_file);
}
@@ -2508,6 +2510,7 @@ sub possible {
# Check for modifiers.
$possible =~ s/\s*$Storage\s*//g;
$possible =~ s/\s*$Sparse\s*//g;
+ my $types_changed = 0;
if ($possible =~ /^\s*$/) {
} elsif ($possible =~ /\s/) {
@@ -2516,14 +2519,16 @@ sub possible {
if ($modifier !~ $notPermitted) {
warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
push(@modifierListFile, $modifier);
+ $types_changed = 1;
}
}
} else {
warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
push(@typeListFile, $possible);
+ $types_changed = 1;
}
- build_types();
+ build_types() if ($types_changed);
} else {
warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
}
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (3 preceding siblings ...)
2026-12-01 22:53 ` [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds Bart Van Assche
@ 2026-12-01 22:53 ` Bart Van Assche
2026-12-01 22:53 ` [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks Bart Van Assche
` (5 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Replace dynamic regex patterns that force Perl to recompile large regular
expressions on every match:
- SELF_ASSIGNMENT: guard on index($line, '=') >= 0, capture the right-hand
side in the initial $Declare match, and check it with a static pattern
plus string equality instead of interpolating $var alongside $Declare.
Keep using the original two-regex check if --typedefsfile is used or if
the line contains __aligned because $Declare may then match across an
'=' sign and the original second regex may select a different split of
the line than the first match.
- EXPORT_SYMBOL: test \n.}\s*$ first and guard the remaining branches on
index($stat, $name) >= 0 and simpler matches before falling back to
patterns interpolating $name with $Type or $Attribute.
- PREFER_FALLTHROUGH: hoist @fallthroughs to file scope as a single
precompiled $fallthrough_re guarded by /fall/i instead of rebuilding the
string list per line and recompiling seven patterns per comment line.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 66 +++++++++++++++++++++++--------------------
1 file changed, 36 insertions(+), 30 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index ffafef4cd4f1..fbde43ed1767 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1123,6 +1123,20 @@ our $declaration_macros = qr{(?x:
__dma_from_device_group_(?:begin|end)\s*\(
)};
+our @fallthroughs = (
+ 'fallthrough',
+ '@fallthrough@',
+ 'lint -fallthrough[ \t]*',
+ 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
+ '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
+ 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
+ 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
+);
+our $fallthrough_re = do {
+ my $joined = join('|', @fallthroughs);
+ qr{$joined};
+};
+
our %allow_repeated_words = (
add => '',
added => '',
@@ -4739,9 +4753,14 @@ sub process {
# check for self assignments used to avoid compiler warnings
# e.g.: int foo = foo, *bar = NULL;
# struct foo bar = *(&(bar));
- if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
- my $var = $1;
- if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
+ if (index($line, '=') >= 0 &&
+ $line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=(.*)/) {
+ my ($var, $rhs) = ($1, $2);
+ if ((!defined($typedefsfile) &&
+ $rhs =~ /^\s*(?:([A-Za-z_][A-Za-z\d_]*)|\*\s*\(?\s*&\s*\(?\s*([A-Za-z_][A-Za-z\d_]*)\s*\)?\s*\)?)\s*[;,]/ &&
+ (defined($1) ? $1 : $2) eq $var) ||
+ ((defined($typedefsfile) || index($line, '__aligned') >= 0) &&
+ $line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/)) {
WARN("SELF_ASSIGNMENT",
"Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
}
@@ -4874,14 +4893,13 @@ sub process {
#print "FOO C name<$name>\n";
$suppress_export{$realline_next} = 1;
- } elsif ($stat !~ /(?:
- \n.}\s*$|
- ^.DEFINE_$Ident\(\Q$name\E\)|
- ^.DECLARE_$Ident\(\Q$name\E\)|
- ^.LIST_HEAD\(\Q$name\E\)|
- ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
- \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
- )/x) {
+ } elsif ($stat !~ /\n.}\s*$/ &&
+ (index($stat, $name) < 0 ||
+ ($stat !~ /^.(?:(?:DEFINE|DECLARE)_$Ident|LIST_HEAD)\(\Q$name\E\)/ &&
+ $stat !~ /\b\Q$name\E\s*(?:;|=|\[|\()/ &&
+ !($stat =~ /\(\s*\*\s*\Q$name\E\s*\)\s*\(/ &&
+ $stat =~ /^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
+ $stat !~ /\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()/))) {
#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
$suppress_export{$realline_next} = 2;
} else {
@@ -7631,25 +7649,13 @@ sub process {
}
# check for /* fallthrough */ like comment, prefer fallthrough;
- my @fallthroughs = (
- 'fallthrough',
- '@fallthrough@',
- 'lint -fallthrough[ \t]*',
- 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
- '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
- 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
- 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
- );
- if ($raw_comment ne '') {
- foreach my $ft (@fallthroughs) {
- if ($raw_comment =~ /$ft/) {
- my $msg_level = \&WARN;
- $msg_level = \&CHK if ($file);
- &{$msg_level}("PREFER_FALLTHROUGH",
- "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
- last;
- }
- }
+ if ($raw_comment ne '' &&
+ $raw_comment =~ /fall/i &&
+ $raw_comment =~ $fallthrough_re) {
+ my $msg_level = \&WARN;
+ $msg_level = \&CHK if ($file);
+ &{$msg_level}("PREFER_FALLTHROUGH",
+ "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
}
# check for switch/default statements without a break;
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (4 preceding siblings ...)
2026-12-01 22:53 ` [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks Bart Van Assche
@ 2026-12-01 22:53 ` Bart Van Assche
2026-12-01 22:53 ` [PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks Bart Van Assche
` (4 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Add cheap literal substring, keyword, or $check guards ahead of expensive
$Declare, $Type, $NonptrType, $Lval, and $balanced_parens regular
expressions in process():
- MULTILINE_DEREFERENCE: require /(?:\.|->)\s*$/ on $prevline first.
- UNSPECIFIED_INT: require index($line, 'signed') >= 0.
- FUNCTION_WITHOUT_ARGS: require /\(\s*\)/.
- POINTER_LOCATION: require '*' or $Modifier\b (and '(' for casts).
- OPEN_BRACE: require '{' and '(' in $sline.
- STORAGE_CLASS and INLINE_LOCATION: require \b$Storage\b / \b$Inline\b.
- WEAK_DECLARATION: check index($line, 'weak') >= 0 and the weak patterns
before matching (?:$Declare|$DeclareMisordered).
- PREFER_KERNEL_TYPES: require $check and \b$typeC99Typedefs. Omit the
trailing \b since the guard only has to be a necessary condition for the
\b($typeC99Typedefs)\b match on the declared type.
- BOOL_COMPARISON: require $check, '=', and \b(?:true|false)\b.
- UNINITIALIZED_PTR_WITH_FREE: require index($line, '__free') >= 0.
Skip backtracking across complex declaration patterns on lines that cannot
match or when --strict checks are disabled.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 43 ++++++++++++++++++++++++++++++-------------
1 file changed, 30 insertions(+), 13 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index fbde43ed1767..64c965c84d7d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4767,7 +4767,8 @@ sub process {
}
# check for dereferences that span multiple lines
- if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
+ if ($prevline =~ /(?:\.|->)\s*$/ &&
+ $prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
$line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
my $ref = $1;
@@ -4779,7 +4780,8 @@ sub process {
}
# check for declarations of signed or unsigned without int
- while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
+ while (index($line, 'signed') >= 0 &&
+ $line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
my $type = $1;
my $var = $2;
$var = "" if (!defined $var);
@@ -5032,7 +5034,8 @@ sub process {
}
# check for function declarations without arguments like "int foo()"
- if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
+ if ($line =~ /\(\s*\)/ &&
+ $line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
if (ERROR("FUNCTION_WITHOUT_ARGS",
"Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
$fix) {
@@ -5053,7 +5056,9 @@ sub process {
# * goes on variable not on type
# (char*[ const])
- while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
+ my $has_ptr_or_mod = (index($line, '*') >= 0 || $line =~ /$Modifier\b/);
+ while ($has_ptr_or_mod && index($line, '(') >= 0 &&
+ $line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
#print "AA<$1>\n";
my ($ident, $from, $to) = ($1, $2, $2);
@@ -5078,7 +5083,8 @@ sub process {
}
}
}
- while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
+ while ($has_ptr_or_mod &&
+ $line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
#print "BB<$1>\n";
my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
@@ -5187,6 +5193,8 @@ sub process {
# function brace can't be on same line, except for #defines of do while,
# or if closed on same line
if ($perl_version_ok &&
+ index($sline, '{') >= 0 &&
+ index($sline, '(') >= 0 &&
$sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
$sline !~ /\#\s*define\b.*do\s*\{/ &&
$sline !~ /}/) {
@@ -7019,7 +7027,8 @@ sub process {
}
# check that the storage class is not after a type
- if ($line =~ /\b($Type)\s+($Storage)\b/) {
+ if ($line =~ /\b$Storage\b/ &&
+ $line =~ /\b($Type)\s+($Storage)\b/) {
WARN("STORAGE_CLASS",
"storage class '$2' should be located before type '$1'\n" . $herecurr);
}
@@ -7034,8 +7043,9 @@ sub process {
# check the location of the inline attribute, that it is between
# storage class and type.
- if ($line =~ /\b$Type\s+$Inline\b/ ||
- $line =~ /\b$Inline\s+$Storage\b/) {
+ if ($line =~ /\b$Inline\b/ &&
+ ($line =~ /\b$Type\s+$Inline\b/ ||
+ $line =~ /\b$Inline\s+$Storage\b/)) {
ERROR("INLINE_LOCATION",
"inline keyword should sit between storage class and type\n" . $herecurr);
}
@@ -7118,15 +7128,18 @@ sub process {
# Check for __attribute__ weak, or __weak declarations (may have link issues)
if ($perl_version_ok &&
- $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
+ index($line, 'weak') >= 0 &&
($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
- $line =~ /\b__weak\b/)) {
+ $line =~ /\b__weak\b/) &&
+ $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/) {
ERROR("WEAK_DECLARATION",
"Using weak declarations can have unintended link defects\n" . $herecurr);
}
# check for c99 types like uint8_t used outside of uapi/ and tools/
- if ($realfile !~ m@\binclude/uapi/@ &&
+ if ($check &&
+ $line =~ /\b$typeC99Typedefs/ &&
+ $realfile !~ m@\binclude/uapi/@ &&
!is_userspace($realfile) &&
$line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
my $type = $1;
@@ -7691,7 +7704,10 @@ sub process {
}
# check for comparisons against true and false
- if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
+ if ($check &&
+ index($line, '=') >= 0 &&
+ $line =~ /\b(?:true|false)\b/i &&
+ $line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
my $lead = $1;
my $arg = $2;
my $test = $3;
@@ -8033,7 +8049,8 @@ sub process {
}
# check for uninitialized pointers with __free attribute
- while ($line =~ /\*\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*[,;]/g) {
+ while (index($line, '__free') >= 0 &&
+ $line =~ /\*\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*[,;]/g) {
ERROR("UNINITIALIZED_PTR_WITH_FREE",
"pointer '$1' with __free attribute should be initialized\n" . $herecurr);
}
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (5 preceding siblings ...)
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 ` Bart Van Assche
2026-12-01 22:53 ` [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing Bart Van Assche
` (3 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Split %spelling_fix entries at startup into %spelling_fix_word for single
tokens matching /^[\w\-'`]+$/ and lazily compiled multi-word regexes
($misspellings_multi_re and $misspellings_multi_has_re) for the remaining
phrases instead of joining all entries into one alternation regex.
Gate the TYPO_SPELLING check on (!$file || $check) and
show_type("TYPO_SPELLING"), skip decode("utf8", $rawline) for pure-ASCII
lines unless a multi-word entry contains a raw 0xDF byte (which only
case-folds to "ss" in UTF-8 flagged strings), and preserve left-to-right
report ordering and consumed trailing-delimiter semantics via
$min_start = $end + 2.
Avoid running a ~1,800 branch alternation regex (or tens of thousands of
branches with --codespell) on every added and commit-log line, and skip
spelling scans under -f when CHECK messages are disabled.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 93 ++++++++++++++++++++++++++++++++++++++++---
1 file changed, 87 insertions(+), 6 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 64c965c84d7d..3eb2ac2c7a9b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -965,9 +965,15 @@ our $allowed_asm_includes = qr{(?x:
)};
# memory.h: ARM has a custom one
-# Load common spelling mistakes and build regular expression list.
+# Load common spelling mistakes and build lookup table / multi-word regex.
my $misspellings;
my %spelling_fix;
+my %spelling_fix_word;
+my $misspellings_multi_joined;
+my $misspellings_multi_has_df = 0;
+my $misspellings_multi_re;
+my $misspellings_multi_has_re;
+my $misspellings_full_re;
if (open(my $spelling, '<', $spelling_file)) {
while (<$spelling>) {
@@ -1012,7 +1018,23 @@ if ($codespell) {
}
}
-$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
+if (keys %spelling_fix) {
+ $misspellings = 1;
+ my @multi = ();
+ foreach my $suspect (sort keys %spelling_fix) {
+ if ($suspect =~ /^[\w\-'`]+$/) {
+ $spelling_fix_word{lc($suspect)} = 1;
+ } else {
+ push(@multi, $suspect);
+ }
+ }
+ if (@multi) {
+ $misspellings_multi_joined = join("|", @multi);
+ # A raw (non-UTF-8) 0xDF byte case-folds to "ss" only when
+ # matched against a string with the UTF8 flag set.
+ $misspellings_multi_has_df = ($misspellings_multi_joined =~ /\xdf/);
+ }
+}
sub read_words {
my ($wordsRef, $file) = @_;
@@ -3745,12 +3767,71 @@ sub process {
# Check for various typo / spelling mistakes
if (defined($misspellings) &&
+ (!$file || $check) &&
+ show_type("TYPO_SPELLING") &&
($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
- my $rawline_utf8 = decode("utf8", $rawline);
- while ($rawline_utf8 =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
- my $typo = $1;
+ my $is_non_ascii = ($rawline =~ /[^\x00-\x7f]/);
+ my $rawline_utf8 = ($is_non_ascii || $misspellings_multi_has_df) ?
+ decode("utf8", $rawline) : $rawline;
+ my @typo_matches = ();
+ my $use_full = ($is_non_ascii &&
+ $rawline_utf8 =~ /[\x{00df}\x{017f}\x{1e9e}\x{fb00}-\x{fb06}]/);
+ if (!$use_full && defined($misspellings_multi_joined) &&
+ !defined($misspellings_multi_re)) {
+ $misspellings_multi_re = qr{(?:^|[^\w\-'`])($misspellings_multi_joined)(?:[^\w\-'`]|$)}i;
+ }
+ if (!$use_full && defined($misspellings_multi_re) &&
+ $rawline_utf8 =~ $misspellings_multi_re) {
+ if (!defined($misspellings_multi_has_re)) {
+ $misspellings_multi_has_re = qr{(?=(?:^|[^\w\-'`])($misspellings_multi_joined)(?:[^\w\-'`]|$))}i;
+ }
+ my @cands = ();
+ while ($rawline_utf8 =~ /([\w\-'`]+)/g) {
+ if (exists($spelling_fix_word{lc($1)})) {
+ push(@cands, [$-[1], $+[1], $1]);
+ }
+ }
+ while ($rawline_utf8 =~ /$misspellings_multi_has_re/g) {
+ push(@cands, [$-[1], $+[1], $1]);
+ }
+ @cands = sort { $a->[0] <=> $b->[0] } @cands;
+ for (my $i = 1; $i < @cands; $i++) {
+ if ($cands[$i]->[0] == $cands[$i - 1]->[0]) {
+ $use_full = 1;
+ last;
+ }
+ }
+ if (!$use_full) {
+ my $min_start = 0;
+ foreach my $cand (@cands) {
+ next if ($cand->[0] < $min_start);
+ push(@typo_matches, $cand);
+ $min_start = $cand->[1] + 2;
+ }
+ }
+ } elsif (!$use_full) {
+ my $min_start = 0;
+ while ($rawline_utf8 =~ /([\w\-'`]+)/g) {
+ next if ($-[1] < $min_start);
+ if (exists($spelling_fix_word{lc($1)})) {
+ push(@typo_matches, [$-[1], $+[1], $1]);
+ $min_start = $+[1] + 2;
+ }
+ }
+ }
+ if ($use_full) {
+ if (!defined($misspellings_full_re)) {
+ my $all = join("|", sort keys %spelling_fix);
+ $misspellings_full_re = qr{(?:^|[^\w\-'`])($all)(?:[^\w\-'`]|$)}i;
+ }
+ while ($rawline_utf8 =~ /$misspellings_full_re/g) {
+ push(@typo_matches, [$-[1], $+[1], $1]);
+ }
+ }
+ foreach my $m (@typo_matches) {
+ my ($typo_start, $typo) = ($m->[0], $m->[2]);
my $blank = copy_spacing($rawline_utf8);
- my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
+ my $ptr = substr($blank, 0, $typo_start) . "^" x length($typo);
my $hereptr = "$hereline$ptr\n";
my $typo_fix = $spelling_fix{lc($typo)};
$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (6 preceding siblings ...)
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
2026-12-01 22:53 ` [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch Bart Van Assche
` (2 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
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.
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (7 preceding siblings ...)
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
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
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
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;
}
}
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (8 preceding siblings ...)
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 ` Bart Van Assche
2026-12-01 23:26 ` [PATCH 00/10] Make checkpatch faster Joe Perches
10 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 22:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel, Bart Van Assche
Speed up three per-line character-scanning helpers:
- sanitise_line(): return $line immediately when length($line) <= 1 or
when $sanitise_quote is empty and substr($line, 1) contains none of
'/', "'", '"', or '#'.
- expand_tabs(): return $str immediately when index($str, "\t") < 0 and
copy contiguous non-tab slices using index() and substr() instead of
splitting into a per-character array.
- get_raw_comment(): return '' immediately when index($line, "$;") < 0 and
copy contiguous $;+ spans instead of iterating per character.
Avoid per-character Perl loops on the common lines that lack quotes,
comments, preprocessor directives, or tabs.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
scripts/checkpatch.pl | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b3854bc3ad6d..31723f178629 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1789,20 +1789,17 @@ sub which_conf {
sub expand_tabs {
my ($str) = @_;
+ return $str if (index($str, "\t") < 0);
+
my $res = '';
- my $n = 0;
- for my $c (split(//, $str)) {
- if ($c eq "\t") {
- $res .= ' ';
- $n++;
- for (; ($n % $tabsize) != 0; $n++) {
- $res .= ' ';
- }
- next;
- }
- $res .= $c;
- $n++;
+ my $pos = 0;
+ my $idx;
+ while (($idx = index($str, "\t", $pos)) >= 0) {
+ $res .= substr($str, $pos, $idx - $pos);
+ $res .= ' ' x ($tabsize - (length($res) % $tabsize));
+ $pos = $idx + 1;
}
+ $res .= substr($str, $pos);
return $res;
}
@@ -1838,6 +1835,9 @@ sub sanitise_line_reset {
sub sanitise_line {
my ($line) = @_;
+ return $line if (length($line) <= 1 ||
+ ($sanitise_quote eq '' && substr($line, 1) !~ m{[/'"#]}));
+
my $res = '';
my $l = '';
@@ -2928,12 +2928,12 @@ sub pos_last_openparen {
sub get_raw_comment {
my ($line, $rawline) = @_;
- my $comment = '';
- for my $i (0 .. (length($line) - 1)) {
- if (substr($line, $i, 1) eq "$;") {
- $comment .= substr($rawline, $i, 1);
- }
+ return '' if (index($line, "$;") < 0);
+
+ my $comment = '';
+ while ($line =~ /$;+/g) {
+ $comment .= substr($rawline, $-[0], $+[0] - $-[0]);
}
return $comment;
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH 00/10] Make checkpatch faster
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
` (9 preceding siblings ...)
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 ` Joe Perches
2026-12-01 23:53 ` Bart Van Assche
10 siblings, 1 reply; 13+ messages in thread
From: Joe Perches @ 2026-12-01 23:26 UTC (permalink / raw)
To: Bart Van Assche, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel
On Tue, 2026-12-01 at 14:53 -0800, Bart Van Assche wrote:
> Hi ,
>
> LLMs tend to run checkpatch often. This patch series makes checkpatch about
> twice as fast and prevents that it slows down LLM-based workflows
> significantly. All then patches have been generated by an LLM.
All these?
> 50 recent commits, tree mode: 11.1 s -> 2.4 s
> checkpatch -f kernel/sched/core.c: 8.1 s -> 4.1 s
> checkpatch -f fs/btrfs/inode.c: 8.7 s -> 4.2 s
> checkpatch -f ufs/core/ufshcd.c: 8.7 s -> 4.3 s
Nice.
Give me a bit to read all the patches but all the
caching seems good and useful.
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH 00/10] Make checkpatch faster
2026-12-01 23:26 ` [PATCH 00/10] Make checkpatch faster Joe Perches
@ 2026-12-01 23:53 ` Bart Van Assche
0 siblings, 0 replies; 13+ messages in thread
From: Bart Van Assche @ 2026-12-01 23:53 UTC (permalink / raw)
To: Joe Perches, Andy Whitcroft; +Cc: Andrew Morton, linux-kernel
On 12/1/26 3:26 PM, Joe Perches wrote:
> On Tue, 2026-12-01 at 14:53 -0800, Bart Van Assche wrote:
>> LLMs tend to run checkpatch often. This patch series makes checkpatch about
>> twice as fast and prevents that it slows down LLM-based workflows
>> significantly. All then patches have been generated by an LLM.
>
> All these?
There is a spelling error in what I wrote: I meant "all ten" instead of
"all then".
>> 50 recent commits, tree mode: 11.1 s -> 2.4 s
>> checkpatch -f kernel/sched/core.c: 8.1 s -> 4.1 s
>> checkpatch -f fs/btrfs/inode.c: 8.7 s -> 4.2 s
>> checkpatch -f ufs/core/ufshcd.c: 8.7 s -> 4.3 s
>
> Nice.
>
> Give me a bit to read all the patches but all the
> caching seems good and useful.
Thanks!
Bart.
^ permalink raw reply [flat|nested] 13+ messages in thread