workflows.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 0/2] codingstyle: avoid unused parameters for a function-like macro
@ 2024-04-03 22:42 Barry Song
  2024-04-03 22:42 ` [PATCH v6 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters Barry Song
  2024-04-03 22:42 ` [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro Barry Song
  0 siblings, 2 replies; 4+ messages in thread
From: Barry Song @ 2024-04-03 22:42 UTC (permalink / raw)
  To: akpm, linux-doc, workflows
  Cc: apw, broonie, chenhuacai, chris, corbet, dwaipayanray1, herbert,
	joe, linux-kernel, linux, lukas.bulwahn, mac.xxn, sfr,
	v-songbaohua

From: Barry Song <v-songbaohua@oppo.com>

-v6:
 * collect ack of Joe, thanks!
 * refine docs according to Jonathan, thanks!
 * add checkpatch doc according to Joe, thanks!

-v5:
 * Simplify the code for Patch 2 according to Joe's suggestions.
 * add s-o-b of Barry according to Jeff Johnson
 v5 link:
 https://lore.kernel.org/all/20240401012120.6052-1-21cnbao@gmail.com/

-v4:
 * fix Xining's email address, s/ma.xxn@outlook.com/mac.xxn@outlook.com/g
 * fix some false positives of checkpatch.pl
 * downgrade from ERROR to WARN in checkpatch.pl

 Thanks for Joe's comments!

 v4 link: https://lore.kernel.org/all/20240328022136.5789-1-21cnbao@gmail.com/

-v3:
 https://lore.kernel.org/all/20240322084937.66018-1-21cnbao@gmail.com/

A function-like macro could result in build warnings such as
"unused variable." This patchset updates the guidance to
recommend always using a static inline function instead
and also provides checkpatch support for this new rule.

Barry Song (1):
  Documentation: coding-style: ask function-like macros to evaluate
    parameters

Xining Xu (1):
  scripts: checkpatch: check unused parameters for function-like macro

 Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++
 Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++
 scripts/checkpatch.pl                  |  6 ++++++
 3 files changed, 43 insertions(+)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v6 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters
  2024-04-03 22:42 [PATCH v6 0/2] codingstyle: avoid unused parameters for a function-like macro Barry Song
@ 2024-04-03 22:42 ` Barry Song
  2024-04-03 22:42 ` [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro Barry Song
  1 sibling, 0 replies; 4+ messages in thread
From: Barry Song @ 2024-04-03 22:42 UTC (permalink / raw)
  To: akpm, linux-doc, workflows
  Cc: apw, broonie, chenhuacai, chris, corbet, dwaipayanray1, herbert,
	joe, linux-kernel, linux, lukas.bulwahn, mac.xxn, sfr,
	v-songbaohua, Max Filippov

From: Barry Song <v-songbaohua@oppo.com>

Recent commit 77292bb8ca69c80 ("crypto: scomp - remove memcpy if
sg_nents is 1 and pages are lowmem") leads to warnings on xtensa
and loongarch,
   In file included from crypto/scompress.c:12:
   include/crypto/scatterwalk.h: In function 'scatterwalk_pagedone':
   include/crypto/scatterwalk.h:76:30: warning: variable 'page' set but not used [-Wunused-but-set-variable]
      76 |                 struct page *page;
         |                              ^~~~
   crypto/scompress.c: In function 'scomp_acomp_comp_decomp':
>> crypto/scompress.c:174:38: warning: unused variable 'dst_page' [-Wunused-variable]
     174 |                         struct page *dst_page = sg_page(req->dst);
         |

The reason is that flush_dcache_page() is implemented as a noop
macro on these platforms as below,

 #define flush_dcache_page(page) do { } while (0)

The driver code, for itself, seems be quite innocent and placing
maybe_unused seems pointless,

 struct page *dst_page = sg_page(req->dst);

 for (i = 0; i < nr_pages; i++)
 	flush_dcache_page(dst_page + i);

And it should be independent of architectural implementation
differences.

Let's provide guidance on coding style for requesting parameter
evaluation or proposing the migration to a static inline
function.

Signed-off-by: Barry Song <v-songbaohua@oppo.com>
Suggested-by: Max Filippov <jcmvbkbc@gmail.com>
Reviewed-by: Mark Brown <broonie@kernel.org>
Acked-by: Joe Perches <joe@perches.com>
Cc: Chris Zankel <chris@zankel.net>
Cc: Huacai Chen <chenhuacai@loongson.cn>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Joe Perches <joe@perches.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Cc: Xining Xu <mac.xxn@outlook.com>
---
 Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 9c7cf7347394..7e768c65aa92 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -827,6 +827,29 @@ Macros with multiple statements should be enclosed in a do - while block:
 				do_this(b, c);		\
 		} while (0)
 
+Function-like macros with unused parameters should be replaced by static
+inline functions to avoid the issue of unused variables:
+
+.. code-block:: c
+
+	static inline void fun(struct foo *foo)
+	{
+	}
+
+Due to historical practices, many files still employ the "cast to (void)"
+approach to evaluate parameters. However, this method is not advisable.
+Inline functions address the issue of "expression with side effects
+evaluated more than once", circumvent unused-variable problems, and
+are generally better documented than macros for some reason.
+
+.. code-block:: c
+
+	/*
+	 * Avoid doing this whenever possible and instead opt for static
+	 * inline functions
+	 */
+	#define macrofun(foo) do { (void) (foo); } while (0)
+
 Things to avoid when using macros:
 
 1) macros that affect control flow:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro
  2024-04-03 22:42 [PATCH v6 0/2] codingstyle: avoid unused parameters for a function-like macro Barry Song
  2024-04-03 22:42 ` [PATCH v6 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters Barry Song
@ 2024-04-03 22:42 ` Barry Song
  2024-04-26  9:54   ` Barry Song
  1 sibling, 1 reply; 4+ messages in thread
From: Barry Song @ 2024-04-03 22:42 UTC (permalink / raw)
  To: akpm, linux-doc, workflows
  Cc: apw, broonie, chenhuacai, chris, corbet, dwaipayanray1, herbert,
	joe, linux-kernel, linux, lukas.bulwahn, mac.xxn, sfr,
	v-songbaohua, Max Filippov, Jeff Johnson, Charlemagne Lasse

From: Xining Xu <mac.xxn@outlook.com>

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

Signed-off-by: Xining Xu <mac.xxn@outlook.com>
Tested-by: Barry Song <v-songbaohua@oppo.com>
Signed-off-by: Barry Song <v-songbaohua@oppo.com>
Cc: Chris Zankel <chris@zankel.net>
Cc: Huacai Chen <chenhuacai@loongson.cn>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Mark Brown <broonie@kernel.org>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Joe Perches <joe@perches.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Jeff Johnson <quic_jjohnson@quicinc.com>
Cc: Charlemagne Lasse <charlemagnelasse@gmail.com>
---
 Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++
 scripts/checkpatch.pl                  |  6 ++++++
 2 files changed, 20 insertions(+)

diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst
index 127968995847..a9fac978a525 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -906,6 +906,20 @@ Macros, Attributes and Symbols
 
     See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/
 
+  **MACRO_ARG_UNUSED**
+    If function-like macros do not utilize a parameter, it might result
+    in a build warning. We advocate for utilizing static inline functions
+    to replace such macros.
+    For example, for a macro such as the one below::
+
+      #define test(a) do { } while (0)
+
+    there would be a warning like below::
+
+      WARNING: Argument 'a' is not used in function-like macro.
+
+    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
+
   **SINGLE_STATEMENT_DO_WHILE_MACRO**
     For the multi-statement macros, it is necessary to use the do-while
     loop to avoid unpredictable code paths. The do-while loop helps to
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9c4c4a61bc83..9895d7e38a9f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6040,6 +6040,12 @@ sub process {
 					CHK("MACRO_ARG_PRECEDENCE",
 					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
 				}
+
+# check if this is an unused argument
+				if ($define_stmt !~ /\b$arg\b/) {
+					WARN("MACRO_ARG_UNUSED",
+						"Argument '$arg' is not used in function-like macro\n" . "$herectx");
+				}
 			}
 
 # check for macros with flow control, but without ## concatenation
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro
  2024-04-03 22:42 ` [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro Barry Song
@ 2024-04-26  9:54   ` Barry Song
  0 siblings, 0 replies; 4+ messages in thread
From: Barry Song @ 2024-04-26  9:54 UTC (permalink / raw)
  To: akpm, linux-doc, workflows, joe
  Cc: apw, broonie, chenhuacai, chris, corbet, dwaipayanray1, herbert,
	linux-kernel, linux, lukas.bulwahn, mac.xxn, sfr, v-songbaohua,
	Max Filippov, Jeff Johnson, Charlemagne Lasse

Hi Joe,
any further comment for this ? May this patch get your reviewed / acked?

On Thu, Apr 4, 2024 at 6:43 AM Barry Song <21cnbao@gmail.com> wrote:
>
> From: Xining Xu <mac.xxn@outlook.com>
>
> 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
>
> Signed-off-by: Xining Xu <mac.xxn@outlook.com>
> Tested-by: Barry Song <v-songbaohua@oppo.com>
> Signed-off-by: Barry Song <v-songbaohua@oppo.com>
> Cc: Chris Zankel <chris@zankel.net>
> Cc: Huacai Chen <chenhuacai@loongson.cn>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Cc: Guenter Roeck <linux@roeck-us.net>
> Cc: Stephen Rothwell <sfr@canb.auug.org.au>
> Cc: Mark Brown <broonie@kernel.org>
> Cc: Andy Whitcroft <apw@canonical.com>
> Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
> Cc: Joe Perches <joe@perches.com>
> Cc: Jonathan Corbet <corbet@lwn.net>
> Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
> Cc: Max Filippov <jcmvbkbc@gmail.com>
> Cc: Jeff Johnson <quic_jjohnson@quicinc.com>
> Cc: Charlemagne Lasse <charlemagnelasse@gmail.com>
> ---
>  Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++
>  scripts/checkpatch.pl                  |  6 ++++++
>  2 files changed, 20 insertions(+)
>
> diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst
> index 127968995847..a9fac978a525 100644
> --- a/Documentation/dev-tools/checkpatch.rst
> +++ b/Documentation/dev-tools/checkpatch.rst
> @@ -906,6 +906,20 @@ Macros, Attributes and Symbols
>
>      See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/
>
> +  **MACRO_ARG_UNUSED**
> +    If function-like macros do not utilize a parameter, it might result
> +    in a build warning. We advocate for utilizing static inline functions
> +    to replace such macros.
> +    For example, for a macro such as the one below::
> +
> +      #define test(a) do { } while (0)
> +
> +    there would be a warning like below::
> +
> +      WARNING: Argument 'a' is not used in function-like macro.
> +
> +    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
> +
>    **SINGLE_STATEMENT_DO_WHILE_MACRO**
>      For the multi-statement macros, it is necessary to use the do-while
>      loop to avoid unpredictable code paths. The do-while loop helps to
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 9c4c4a61bc83..9895d7e38a9f 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -6040,6 +6040,12 @@ sub process {
>                                         CHK("MACRO_ARG_PRECEDENCE",
>                                             "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
>                                 }
> +
> +# check if this is an unused argument
> +                               if ($define_stmt !~ /\b$arg\b/) {
> +                                       WARN("MACRO_ARG_UNUSED",
> +                                               "Argument '$arg' is not used in function-like macro\n" . "$herectx");
> +                               }
>                         }
>
>  # check for macros with flow control, but without ## concatenation
> --
> 2.34.1
>

Thanks
Barry

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-04-26  9:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-03 22:42 [PATCH v6 0/2] codingstyle: avoid unused parameters for a function-like macro Barry Song
2024-04-03 22:42 ` [PATCH v6 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters Barry Song
2024-04-03 22:42 ` [PATCH v6 2/2] scripts: checkpatch: check unused parameters for function-like macro Barry Song
2024-04-26  9:54   ` Barry Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).