mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 05/10] checkpatch: avoid dynamic regex recompilation in hot checks
Date: Tue,  1 Dec 2026 14:53:49 -0800	[thread overview]
Message-ID: <5896ccc8853fef7656779f477b98b58fda1009d9.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

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;

  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 ` Bart Van Assche [this message]
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 ` [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=5896ccc8853fef7656779f477b98b58fda1009d9.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®