[PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks
From: Bart Van Assche
Date: Tue Dec 01 2026 - 17:54:28 EST
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@xxxxxxx>
---
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;