[PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks
From: Bart Van Assche
Date: Tue Dec 01 2026 - 17:54:33 EST
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@xxxxxxx>
---
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]/);