Skip to contentSkip to conversation

codingstyle: avoid unused parameters for a function-like macro

PATCH Superseded Barry Song 2 participants 4 messages Mar 22, 2024

Subsystems (3)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

v3 cover codingstyle: avoid unused parameters for a function-like macro

From: Barry Song <[email protected]>

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/process/coding-style.rst | 16 ++++++++++++++++
 scripts/checkpatch.pl                  | 24 ++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/2 Documentation: coding-style: ask function-like macros to evaluate parameters

From: Barry Song <[email protected]>

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':

Quoted 1 line
Quoted 1 line

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.

Cc Andrew Morton <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Mark Brown <[email protected]>
---
 Documentation/process/coding-style.rst | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Documentation/process/coding-style.rst +16 -0
index 9c7cf7347394..791d333a57fd 100644
@@ -827,6 +827,22 @@ 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)
+	{
+	}
+
+For historical reasons, many files still use the cast to (void) to evaluate
+parameters, but this method is not recommended:
+
+.. code-block:: c
+
+	#define macrofun(foo) do { (void) (foo); } while (0)
+
 Things to avoid when using macros:
 
 1) macros that affect control flow:
Signature · 1 line
2.34.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters

Quoted 8 lines · Barry Song

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);

Quoted 6 lines

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.

author
#3 lore raw

PATCH v3 2/2 scripts: checkpatch: Check unused parameters for function-like macro

In reply to Barry Song

From: Xining Xu <[email protected]>

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.

ERROR: Parameter 'a' is not used in function-like macro, please use static inline instead
#21: FILE: mm/init-mm.c:20:
+#define test(a) do { } while (0)

total: 1 errors, 0 warnings, 8 lines checked

Cc Andrew Morton <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Stephen Rothwell <[email protected]>
Cc Mark Brown <[email protected]>
---
 scripts/checkpatch.pl | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
scripts/checkpatch.pl +24 -0
index 9c4c4a61bc83..6f778f3403b5 100755
@@ -6109,6 +6109,30 @@ sub process {
 				WARN("TRAILING_SEMICOLON",
 				     "macros should not use a trailing semicolon\n" . "$herectx");
 			}
+
+			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*(\((?:[^\(\)]++|(?-1))*\))\s+(\S+.*)(\/\/.*)?/) {
+				my $params = $1 || "";
+				my $body = $2 || "";
+
+			    # get the individual params
+				$params =~ tr/()//d;
+				# remove leading and trailing whitespace
+				$params =~ s/^\s+|\s+$//g;
+
+				$ctx =~ s/\n*$//;
+				my $cnt = statement_rawlines($ctx);
+				my $herectx = get_stat_here($linenr, $cnt, $here);
+
+				if ($params ne "") {
+					my @paramList = split /,\s*/, $params;
+					foreach my $param(@paramList) {
+						if ($body !~ /\b$param\b/) {
+							ERROR("UNUSED_PARAM_IN_MACRO",
+							     "Parameter '$param' is not used in function-like macro, please use static inline instead\n" . "$herectx");
+						}
+					}
+				}
+			}
 		}
 
 # check for redundant bracing round if etc
Signature · 1 line
2.34.1