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 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks
Date: Tue, 1 Dec 2026 14:53:50 -0800 [thread overview]
Message-ID: <4a83e98a65f3656d1b82467932e5c0c6f3a5aa8d.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>
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);
}
next prev 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 ` Bart Van Assche [this message]
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 ` [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing Bart Van Assche
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=4a83e98a65f3656d1b82467932e5c0c6f3a5aa8d.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®