mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jim Cromie <jim.cromie@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: akpm@linuxfoundation.org, Jim Cromie <jim.cromie@gmail.com>,
	Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>
Subject: [PATCH 2/3] checkpatch: recognize __chkp_no_side_effects(_var) hint macro
Date: Sat, 25 Oct 2025 15:15:17 -0600	[thread overview]
Message-ID: <20251025211519.1616439-3-jim.cromie@gmail.com> (raw)
In-Reply-To: <20251025211519.1616439-1-jim.cromie@gmail.com>

Teach checkpatch --strict to allow an author to suppress specific
var-reuse complaints:

  CHECK: Macro argument reuse '_var' - possible side-effects?

Consider:

   #define module_param_named(name, value, type, perm)  \
  +     __chkp_no_side_effects(name, value)		\
        param_check_##type(name, &(value));		\
        module_param_cb(name, &param_ops_##type, &value, perm); \
        __MODULE_PARM_TYPE(name, #type)

Without the +line, this kind of multi-statement macro-defn would cause
checkpatch to carp about name and value being reused.

Note that it wouldn't complain about type, even though type is
expanded 3 times, because checkpatch knew to strip # and ## constructs
on an arg before counting its uses.

module_param_named is actually a named $exception, amongst a ~dozen
others, so it wouldn't draw the complaint (I said kind of above), but
this approach doesn't require further checkpatch tweaks, is specific
to the _vars named, and is author initiated, not a carte-blanche
exception on the macro-name.

IIRC, trybot kicks of a CI pipeline that calls checkpatch --strict,
this hint could help reduce CI-noise going forward.

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 scripts/checkpatch.pl | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2b0275dcc5a4..bc807c20fcde 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6070,11 +6070,24 @@ sub process {
 			        next if ($arg =~ /\.\.\./);
 			        next if ($arg =~ /^type$/i);
 				my $tmp_stmt = $define_stmt;
-				$tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
-				$tmp_stmt =~ s/\#+\s*$arg\b//g;
-				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
+
+				$tmp_stmt =~ s/\#+\s*$arg\b/drx_print("strip '#|## arg catenations")/ge;
+				$tmp_stmt =~ s/\b$arg\s*\#\#/drx_print("strip 'arg ##' catenations");/ge;
+
+				$tmp_stmt =~ s{
+					\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|
+					   typeof|__typeof__|__builtin\w+|typecheck
+					   \s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b }
+				{
+					drx_print("-builtins-");
+				}xge;
+
+				my $no_side_effect_vars = "";
+				if ($tmp_stmt =~ s/__chkp_no_side_effects\((.+)\)//) {
+					$no_side_effect_vars = $1;
+				}
 				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
-				if ($use_cnt > 1) {
+				if ($use_cnt > 1 and $no_side_effect_vars !~ m/\b$arg\b/) {
 					CHK("MACRO_ARG_REUSE",
 					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
 				    }
-- 
2.51.0


  parent reply	other threads:[~2025-10-25 21:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-25 21:15 [PATCH 0/3] checkpatch --strict | ci-pipeline improvements Jim Cromie
2025-10-25 21:15 ` [PATCH 1/3] checkpatch: add --drx option and drx_print() helper Jim Cromie
2025-10-25 23:24   ` Joe Perches
2025-10-25 21:15 ` Jim Cromie [this message]
2025-10-26 16:48   ` [PATCH 2/3] checkpatch: recognize __chkp_no_side_effects(_var) hint macro Joe Perches
2025-10-25 21:15 ` [PATCH 3/3] compiler.h: add __chkp_no_side_effects() empty hint/assertion macro Jim Cromie
2025-10-26  0:09   ` Linus Torvalds

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=20251025211519.1616439-3-jim.cromie@gmail.com \
    --to=jim.cromie@gmail.com \
    --cc=akpm@linuxfoundation.org \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /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®