Linux wireless drivers development
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Julian Sun <sunjunchao2870@gmail.com>
Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-fsdevel@vger.kernel.org, jack@suse.cz, brauner@kernel.org,
	 viro@zeniv.linux.org.uk, masahiroy@kernel.org,
	akpm@linux-foundation.org,  n.schier@avm.de, ojeda@kernel.org,
	djwong@kernel.org, kvalo@kernel.org
Subject: Re: [PATCH] scripts: add macro_checker script to check unused parameters in macros
Date: Wed, 24 Jul 2024 20:40:23 -0700	[thread overview]
Message-ID: <778f1ccf1945f79c317dd0a4d2a90d3855770713.camel@perches.com> (raw)
In-Reply-To: <CAHB1NaijJ16haCsH3uHy_zVZFXJ7_-qFOk8mFx7QSeqD+X6Z3g@mail.gmail.com>

On Wed, 2024-07-24 at 22:09 -0400, Julian Sun wrote:
> Joe Perches <joe@perches.com> 于2024年7月24日周三 09:30写道:
> > 
> > On Tue, 2024-07-23 at 05:11 -0400, Julian Sun wrote:
> > > Hi,
> > > 
> > > Recently, I saw a patch[1] on the ext4 mailing list regarding
> > > the correction of a macro definition error. Jan mentioned
> > > that "The bug in the macro is a really nasty trap...".
> > > Because existing compilers are unable to detect
> > > unused parameters in macro definitions. This inspired me
> > > to write a script to check for unused parameters in
> > > macro definitions and to run it.
> > > 
> > 
> > checkpatch has a similar test:
> > 
> > https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com
> > 
> > $ git log --format=email -1 b1be5844c1a0124a49a30a20a189d0a53aa10578
> > From b1be5844c1a0124a49a30a20a189d0a53aa10578 Mon Sep 17 00:00:00 2001
> > From: Xining Xu <mac.xxn@outlook.com>
> > Date: Tue, 7 May 2024 15:27:57 +1200
> > Subject: [PATCH] scripts: checkpatch: check unused parameters for
> >  function-like macro
> > 
> > If function-like macros do not utilize a parameter, it might result in a
> > build warning.  In our coding style guidelines, we advocate for utilizing
> > static inline functions to replace such macros.  This patch verifies
> > compliance with the new rule.
> > 
> > For a macro such as the one below,
> > 
> >  #define test(a) do { } while (0)
> > 
> > The test result is as follows.
> > 
> >  WARNING: Argument 'a' is not used in function-like macro
> >  #21: FILE: mm/init-mm.c:20:
> >  +#define test(a) do { } while (0)
> > 
> >  total: 0 errors, 1 warnings, 8 lines checked
> > 
> > 
> > > Link: https://lkml.kernel.org/r/20240507032757.146386-3-21cnbao@gmail.com
> Yeah, I noticted the test. The difference between checkpatch and
> macro_checker is that checkpatch only checks the patch files, instead
> of the entire source files, which results in the inability to check
> all macros in source files.

Another possibility:

$ git ls-files -- '*.[ch]' | \
  xargs ./scripts/checkpatch -f --terse --no-summary --types=MACRO_ARG_UNUSED

Though I agree the addition of a test for "do {} while (0)" and
no content would be also be useful for unused macro args tests.
---
 scripts/checkpatch.pl | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 39032224d504f..285d29b3e9010 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6060,7 +6060,9 @@ sub process {
 				}
 
 # check if this is an unused argument
-				if ($define_stmt !~ /\b$arg\b/) {
+				if ($define_stmt !~ /\b$arg\b/ &&
+				    $define_stmt !~ /^$/ &&
+				    $define_stmt !~ /^do\s*\{\s*\}\s*while\s*\(\s*0\s*\)$/) {
 					WARN("MACRO_ARG_UNUSED",
 					     "Argument '$arg' is not used in function-like macro\n" . "$herectx");
 				}


      reply	other threads:[~2024-07-25  3:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-23  9:11 [PATCH] scripts: add macro_checker script to check unused parameters in macros Julian Sun
2024-07-23 22:09 ` Andrew Morton
2024-07-24  3:03   ` Julian Sun
2024-07-24  5:03     ` Andrew Morton
2024-07-24  9:06   ` Johannes Berg
2024-07-25  2:30   ` Julian Sun
2024-07-25  2:43     ` Andrew Morton
2024-07-25  3:05       ` Julian Sun
2024-07-25  4:48         ` Nicolas Schier
2024-07-25  8:00           ` Julian Sun
2024-07-24 10:00 ` Jan Kara
2024-07-25  2:12   ` Julian Sun
2024-07-24 13:30 ` Joe Perches
2024-07-25  2:09   ` Julian Sun
2024-07-25  3:40     ` Joe Perches [this message]

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=778f1ccf1945f79c317dd0a4d2a90d3855770713.camel@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=djwong@kernel.org \
    --cc=jack@suse.cz \
    --cc=kvalo@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=n.schier@avm.de \
    --cc=ojeda@kernel.org \
    --cc=sunjunchao2870@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    /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