codingstyle: avoid unused parameters for a function-like macro
Subsystems (3)
v4 cover codingstyle: avoid unused parameters for a function-like macro
From: Barry Song <[email protected]>
-v4:
* fix Xining's email address, s/[email protected]/[email protected]/g
* fix some false positives of checkpatch.pl
* downgrade from ERROR to WARN in checkpatch.pl
Thanks for Joe's comments!
-v3:
https://lore.kernel.org/all/[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 | 30 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
Signature · 1 line
2.34.1
PATCH v4 1/2 Documentation: coding-style: ask function-like macros to evaluate parameters
From: Barry Song <[email protected]>
Recent commit 77292bb8ca69c80 ("crypto: scomp - remove memcpy ifsg_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.
--- Documentation/process/coding-style.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
Documentation/process/coding-style.rst +16 -0
@@ -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
PATCH v4 2/2 scripts: checkpatch: check unused parameters for function-like macro
In reply to Barry SongFrom: 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
--- scripts/checkpatch.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
scripts/checkpatch.pl +30 -0
@@ -6109,6 +6109,36 @@ sub process { WARN("TRAILING_SEMICOLON", "macros should not use a trailing semicolon\n" . "$herectx"); } + + # match "\s*" rather than "\s+" after the balanced parens, as macro definition with arguments + # is not required to have whitespace after arguments + if ($dstat =~ /^\+\s*#\s*define\s+$Ident$balanced_parens\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 ($param =~ /\.\.\.$/) { + # if the param name ends with "...", skip the check + next; + } + if ($body !~ /\b$param\b/) { + WARN("UNUSED_PARAM_IN_MACRO", + "Parameter '$param' is not used in function-like macro\n" . "$herectx"); + } + } + } + } } # check for redundant bracing round if etc
Signature · 1 line
2.34.1
Joe Perches
maintainerRe: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 6 lines · 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.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 6115
Quoted 8 lines
scripts/checkpatch.pl +4
-0@@ -6109,6 +6109,36 @@ sub process { WARN("TRAILING_SEMICOLON", "macros should not use a trailing semicolon\n" . "$herectx"); } + + # match "\s*" rather than "\s+" after the balanced parens, as macro definition with arguments + # is not required to have whitespace after arguments + if ($dstat =~ /^\+\s*#\s*define\s+$Ident$balanced_parens\s*(\S+.*)(\/[\/*].*)?/) {
I think '(\/[\/*].*)?' doesn't do what you expect perhaps '(\/[\/\*].*)?'
though I don't know why this should be capture group
scripts/checkpatch.pl sub process {
Quoted 4 lines
scripts/checkpatch.pl +2
-0+ my $params = $1 || ""; + my $body = $2 || "";
Should never get the || "" as the 2nd capture group is not optional
scripts/checkpatch.pl sub process { · line 6128
Quoted 11 lines
scripts/checkpatch.pl +11
-0+ + # 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 "") {
probably unnecessary
scripts/checkpatch.pl sub process { · line 6129
Quoted 1 line
scripts/checkpatch.pl +1
-0+ my @paramList = split /,\s*/, $params;
please use split() with parentheses
scripts/checkpatch.pl sub process { · line 6130
Quoted 1 line
scripts/checkpatch.pl +1
-0+ foreach my $param(@paramList) {
maybe
foreach my $param (split(/,/, $params) {
$param = trim($param);
next if ($param =~ /\.\.\.$/);scripts/checkpatch.pl sub process { · line 6139
Quoted 9 lines
scripts/checkpatch.pl +9
-0+ if ($param =~ /\.\.\.$/) { + # if the param name ends with "...", skip the check + next; + } + if ($body !~ /\b$param\b/) { + WARN("UNUSED_PARAM_IN_MACRO", + "Parameter '$param' is not used in function-like macro\n" . "$herectx"); + } + }
It seems this logic is a bit redundant to existing code and might be better added in the block that starts
(line 6026)
# check if any macro arguments are reused (ignore '...' and 'type')
as that already does each param in a #define and
ignores ... and type
Re: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 11 lines · Barry Song
Quoted 6 lines
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.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0[]
Quoted 12 lines
scripts/checkpatch.pl sub process { · line 6115
Quoted 8 lines
scripts/checkpatch.pl +4
-0@@ -6109,6 +6109,36 @@ sub process { WARN("TRAILING_SEMICOLON", "macros should not use a trailing semicolon\n" . "$herectx"); } + + # match "\s*" rather than "\s+" after the balanced parens, as macro definition with arguments + # is not required to have whitespace after arguments + if ($dstat =~ /^\+\s*#\s*define\s+$Ident$balanced_parens\s*(\S+.*)(\/[\/*].*)?/) {I think '(\/[\/*].*)?' doesn't do what you expect perhaps '(\/[\/\*].*)?'
though I don't know why this should be capture group
I'd wanted to capture the comment to handle a case where a unused param happens to appears in a comment
Quoted 41 lines
scripts/checkpatch.pl sub process {
Quoted 5 lines
scripts/checkpatch.pl +2
-0+ my $params = $1 || ""; + my $body = $2 || "";Should never get the || "" as the 2nd capture group is not optional
scripts/checkpatch.pl sub process { · line 6128
Quoted 11 lines
scripts/checkpatch.pl +11
-0+ + # 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 "") {probably unnecessary
scripts/checkpatch.pl sub process { · line 6129
Quoted 1 line
scripts/checkpatch.pl +1
-0+ my @paramList = split /,\s*/, $params;please use
split()with parenthesesscripts/checkpatch.pl sub process { · line 6130
Quoted 1 line
scripts/checkpatch.pl +1
-0+ foreach my $param(@paramList) {maybe
foreach my $param (split(/,/, $params) { $param = trim($param); next if ($param =~ /\.\.\.$/);scripts/checkpatch.pl sub process { · line 6139
Quoted 9 lines
scripts/checkpatch.pl +9
-0+ if ($param =~ /\.\.\.$/) { + # if the param name ends with "...", skip the check + next; + } + if ($body !~ /\b$param\b/) { + WARN("UNUSED_PARAM_IN_MACRO", + "Parameter '$param' is not used in function-like macro\n" . "$herectx"); + } + }
Quoted 8 lines
It seems this logic is a bit redundant to existing code and might be better added in the block that starts
(line 6026)
# check if any macro arguments are reused (ignore '...' and 'type')as that already does each param in a #define and
ignores ... and type
Hi Joe,
Thank you for your comments with insights, as you said, code block of line 6026 is a better place to place this new logic, as it already handles the logic I'd wanted like extracting, splitting and trimming the arguments, excluding the trailing comments etc.
By placing the logic in the new place, code duplicates are drastically reduced.
Here's my new code (inserted from line 6044):
+# check if this is an unused argument
+ if ($define_stmt !~ /\b$arg\b/) {
+ WARN("UNUSED_ARG_IN_MACRO",
+ "Argument '$arg' is not used in function-like macro\n" . "$herectx");
+ }
+}
Please note that I use the wording of "arg/argument" instead of "param/parameter" for consistency, please let me know if if this is the correct wording to use here.
Thanks,
Mac.
Re: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
In reply to Joe PerchesQuoted 10 lines · Barry Song
Quoted 6 lines
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.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0[]
Quoted 12 lines
scripts/checkpatch.pl sub process { · line 6115
Quoted 8 lines
scripts/checkpatch.pl +4
-0@@ -6109,6 +6109,36 @@ sub process { WARN("TRAILING_SEMICOLON", "macros should not use a trailing semicolon\n" . "$herectx"); } + + # match "\s*" rather than "\s+" after the balanced parens, as macro definition with arguments + # is not required to have whitespace after arguments + if ($dstat =~ /^\+\s*#\s*define\s+$Ident$balanced_parens\s*(\S+.*)(\/[\/*].*)?/) {I think '(\/[\/*].*)?' doesn't do what you expect perhaps '(\/[\/\*].*)?'
though I don't know why this should be capture group
I'd wanted to capture the comment to handle a case where a unused param happens to appears in a comment
Quoted 41 lines
scripts/checkpatch.pl sub process {
Quoted 5 lines
scripts/checkpatch.pl +2
-0+ my $params = $1 || ""; + my $body = $2 || "";Should never get the || "" as the 2nd capture group is not optional
scripts/checkpatch.pl sub process { · line 6128
Quoted 11 lines
scripts/checkpatch.pl +11
-0+ + # 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 "") {probably unnecessary
scripts/checkpatch.pl sub process { · line 6129
Quoted 1 line
scripts/checkpatch.pl +1
-0+ my @paramList = split /,\s*/, $params;please use
split()with parenthesesscripts/checkpatch.pl sub process { · line 6130
Quoted 1 line
scripts/checkpatch.pl +1
-0+ foreach my $param(@paramList) {maybe
foreach my $param (split(/,/, $params) { $param = trim($param); next if ($param =~ /\.\.\.$/);scripts/checkpatch.pl sub process { · line 6139
Quoted 9 lines
scripts/checkpatch.pl +9
-0+ if ($param =~ /\.\.\.$/) { + # if the param name ends with "...", skip the check + next; + } + if ($body !~ /\b$param\b/) { + WARN("UNUSED_PARAM_IN_MACRO", + "Parameter '$param' is not used in function-like macro\n" . "$herectx"); + } + }
Quoted 8 lines
It seems this logic is a bit redundant to existing code and might be better added in the block that starts
(line 6026)
# check if any macro arguments are reused (ignore '...' and 'type')as that already does each param in a #define and
ignores ... and type
Hi Joe,
Thank you for your comments with insights, as you said, code block of line 6026 is a better place to place this new logic, as it already handles the logic I'd wanted like extracting, splitting and trimming the arguments, excluding the trailing comments etc.
By placing the logic in the new place, code duplicates are reduced.
Here's my new code (inserted from line 6044):
+# check if this is an unused argument
+ if ($define_stmt !~ /\b$arg\b/) {
+ WARN("UNUSED_ARG_IN_MACRO",
+ "Argument '$arg' is not used in function-like macro\n" . "$herectx");
+ }
+}
Please note that I use the wording of "arg/argument" instead of "param/parameter" for consistency, please let me know if if this is the correct wording to use here.
Thanks,
Mac.
Joe Perches
maintainerRe: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 10 lines · Mac Xu
Quoted 9 lines · Barry Song
Quoted 6 lines
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.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
Quoted 18 lines
Quoted 8 lines
It seems this logic is a bit redundant to existing code and might be better added in the block that starts
(line 6026)
# check if any macro arguments are reused (ignore '...' and 'type')as that already does each param in a #define and
ignores ... and typeHi Joe,
Thank you for your comments with insights, as you said, code block of line 6026 is a better place to place this new logic, as it already handles the logic I'd wanted like extracting, splitting and trimming the arguments, excluding the trailing comments etc.
By placing the logic in the new place, code duplicates are reduced.
Here's my new code (inserted from line 6044):
Review
Quoted 3 lines
(quoted diff) +3
-0+# check if this is an unused argument + if ($define_stmt !~ /\b$arg\b/) { + WARN("UNUSED_ARG_IN_MACRO",
Perhaps
WARN("MACRO_ARG_UNUSED",
...to better match the others above it in the block:
CHK("MACRO_ARG_REUSE",and
CHK("MACRO_ARG_PRECEDENCE",
Other than that trivial bit, seems ok.
Re: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 38 lines · Mac Xu
Quoted 10 lines
Quoted 9 lines · Barry Song
Quoted 6 lines
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.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0[]
Quoted 18 lines
Quoted 8 lines
It seems this logic is a bit redundant to existing code and might be better added in the block that starts
(line 6026)
# check if any macro arguments are reused (ignore '...' and 'type')as that already does each param in a #define and
ignores ... and typeHi Joe,
Thank you for your comments with insights, as you said, code block of line 6026 is a better place to place this new logic, as it already handles the logic I'd wanted like extracting, splitting and trimming the arguments, excluding the trailing comments etc.
By placing the logic in the new place, code duplicates are reduced.
Here's my new code (inserted from line 6044):
Review
Quoted 3 lines
(quoted diff) +3
-0+# check if this is an unused argument + if ($define_stmt !~ /\b$arg\b/) { + WARN("UNUSED_ARG_IN_MACRO",Perhaps
WARN("MACRO_ARG_UNUSED", ...
Quoted 7 lines
to better match the others above it in the block:
CHK("MACRO_ARG_REUSE",and
CHK("MACRO_ARG_PRECEDENCE",Other than that trivial bit, seems ok.
Sure, updated, thank you!
+# 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");
+}
Regards,
Xining
Re: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
In reply to Barry SongQuoted 13 lines · 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.
Quoted 8 lines
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
Signed-off-by Xining Xu <[email protected]>
if you are re-posting somebody else's work you need to add your own Signed-off-by
Quoted 13 lines
Tested-by Barry Song <[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]>Cc Andy Whitcroft <[email protected]>Cc Dwaipayan Ray <[email protected]>Cc Joe Perches <[email protected]>Cc Jonathan Corbet <[email protected]>Cc Lukas Bulwahn <[email protected]>Cc Max Filippov <[email protected]>
Re: [PATCH v4 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 20 lines · Jeff Johnson
Quoted 18 lines · 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)
Quoted 5 lines
Quoted 3 lines
total: 1 errors, 0 warnings, 8 lines checked
Signed-off-by Xining Xu <[email protected]>if you are re-posting somebody else's work you need to add your own Signed-off-by
Ok. Jeff, I will do it in the new version and obviously Joe still has some remaining
comments to be addressed by Xining.
Quoted 15 lines
Quoted 14 lines
Tested-by Barry Song <[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]>Cc Andy Whitcroft <[email protected]>Cc Dwaipayan Ray <[email protected]>Cc Joe Perches <[email protected]>Cc Jonathan Corbet <[email protected]>Cc Lukas Bulwahn <[email protected]>Cc Max Filippov <[email protected]>
Quoted 5 lines · Barry Song
From: Barry Song <[email protected]>
-v4:
* fix Xining's email address, s/[email protected]/[email protected]/g
Hi Andrew,
Apologies for the oversight. Could you please apply these two patches to replace the ones in the mm-nonmm-unstable branch? We need to correct Xining's email address regardless.
Quoted 20 lines
* fix some false positives of checkpatch.pl
* downgrade from ERROR to WARN in checkpatch.plThanks for Joe's comments!
-v3:
https://lore.kernel.org/all/[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
parametersXining Xu (1):
scripts: checkpatch: check unused parameters for function-like macro
Quoted 7 lines
Documentation/process/coding-style.rst | 16 ++++++++++++++ scripts/checkpatch.pl | 30 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)--
2.34.1
Thanks
Barry