codingstyle: avoid unused parameters for a function-like macro
Subsystems (3)
v5 cover codingstyle: avoid unused parameters for a function-like macro
From: Barry Song <[email protected]>
-v5:
* Simplify the code for Patch 2 according to Joe's suggestions.
* add s-o-b of Barry according to Jeff Johnson
-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!
v4 link: https://lore.kernel.org/all/[email protected]/
-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 | 6 ++++++ 2 files changed, 22 insertions(+)
Signature · 1 line
2.34.1
PATCH v5 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
Jonathan Corbet
maintainerRe: [PATCH v5 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters
So I'm not sure what your desired path for getting this upstream is. I can take it, but I'm generally quite leery of taking coding-style changes without some serious acks on them - nobody elected me as the arbiter of proper coding style.
A nit below
Quoted 58 lines · Barry Song
From: Barry Song <[email protected]>
Recent commit
77292bb8ca69c80("crypto: scomp - remove memcpy ifsg_nentsis 1 and pages are lowmem") leads to warnings on xtensa
and loongarch,
In file included fromcrypto/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_unusedseems 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 <[email protected]>Suggested-by Max Filippov <[email protected]>Reviewed-by Mark Brown <[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 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 Xining Xu <[email protected]>--- Documentation/process/coding-style.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)Documentation/process/coding-style.rst +0
-0
Documentation/process/coding-style.rst Macros with multiple statements should be enclosed in a do - while block: · line 833
Quoted 7 lines
Documentation/process/coding-style.rst +4
-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
I would just use the "::" notation here; the ..code-block:: just adds noise IMO.
Documentation/process/coding-style.rst Macros with multiple statements should be enclosed in a do - while block: · line 845
Quoted 11 lines
Documentation/process/coding-style.rst +11
-0+ 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) +
1) If you're putting in examples of something *not* to do, it's probably better to also put in something like:
/* don't do this */
people don't always read closely.
2) Can we say *why* it's not recommended?
Thanks,
jon
Re: [PATCH v5 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters
Quoted 5 lines · Jonathan Corbet
So I'm not sure what your desired path for getting this upstream is. I can take it, but I'm generally quite leery of taking coding-style changes without some serious acks on them - nobody elected me as the arbiter of proper coding style.
Hi Jonathan,
Thanks!
Andrew previously integrated it into mm-nomm and tagged it as [TO-BE-UPDATED] before removing it a few days back. Here's the link:
https://lore.kernel.org/mm-commits/[email protected]/
So if feasible, I'd prefer to stick with Andrew's channel.
Quoted 66 lines
A nit below
Quoted 61 lines · Barry Song
From: Barry Song <[email protected]>
Recent commit
77292bb8ca69c80("crypto: scomp - remove memcpy ifsg_nentsis 1 and pages are lowmem") leads to warnings on xtensa
and loongarch,
In file included fromcrypto/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_unusedseems 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 <[email protected]>Suggested-by Max Filippov <[email protected]>Reviewed-by Mark Brown <[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 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 Xining Xu <[email protected]>--- Documentation/process/coding-style.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)Documentation/process/coding-style.rst +0
-0@@ -827,6 +827,22 @@ Macros with multiple statements should be enclosed in a do - while block: do_this(b, c); \ } while (0)
Quoted 7 lines
Documentation/process/coding-style.rst Macros with multiple statements should be enclosed in a do - while block: · line 833
Quoted 4 lines
Documentation/process/coding-style.rst +4
-0+Function-like macros with unused parameters should be replaced by static +inline functions to avoid the issue of unused variables: + +.. code-block:: cI would just use the "::" notation here; the ..code-block:: just adds noise IMO.
I am not quite sure we want this. as reading the whole coding-style.rst,
.. code-block:: c is everywhere :-) Should I do something different or
just follow the tradition?
Quoted 16 lines
Documentation/process/coding-style.rst Macros with multiple statements should be enclosed in a do - while block: · line 845
Quoted 12 lines
Documentation/process/coding-style.rst +11
-0+ 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) +1) If you're putting in examples of something *not* to do, it's probably better to also put in something like:
Quoted 3 lines
/* don't do this */people don't always read closely.
ok.
Quoted 3 lines
2) Can we say *why* it's not recommended?
Andrew makes a valid point.
https://lore.kernel.org/all/[email protected]/
"I think so. My overall view is that we should write things in C. Only use macros if the thing we're trying to do simply cannot be done in a C function.
- inline functions don't have the "expression with side effects evaluated more than once" problem.
- inline functions avoid the unused-variable issue which started this thread
- inline functions look better
- for some reason, people are more inclined to document inline functions than macros."
Andrew's point seems too lengthy for inclusion in the coding-style.rst document? I'll attempt to condense it.
Quoted 3 lines
Thanks,
jon
Thanks
Barry
Re: [PATCH v5 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters
Quoted 6 lines · Barry Song
Quoted 5 lines · Jonathan Corbet
So I'm not sure what your desired path for getting this upstream is. I can take it, but I'm generally quite leery of taking coding-style changes without some serious acks on them - nobody elected me as the arbiter of proper coding style.
I believe it is generally appropriate for macros that take arguments to use static inlines where feasible, so:
And yes, mm is the usual path for upstreaming at least this sort of checkpatch change.
PATCH v5 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 | 6 ++++++ 1 file changed, 6 insertions(+)
scripts/checkpatch.pl +6 -0
@@ -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
Signature · 1 line
2.34.1
Joe Perches
maintainerRe: [PATCH v5 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 9 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,
Quoted 8 lines
#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)
This is no longer true.
Please update the ERROR->WARN and message as below
Ideally, this would have an update toDocumentation/dev-tools/checkpatch.rst
to describe the new --verbose message type
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
Quoted 10 lines
scripts/checkpatch.pl +6
-0@@ -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"); + }
Re: [PATCH v5 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 20 lines · Barry Song
Quoted 9 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.
For a macro such as the one below,
Quoted 8 lines
#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 7 lines
This is no longer true.
Please update theERROR->WARNand message as belowIdeally, this would have an update to
Documentation/dev-tools/checkpatch.rstto describe the new --verbose message type
Hi Joe,
Thank you for the comments, here's the code:
+# 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");
+}
and here's the document for it which is inserted into the "Macros, Attributes and Symbols" section of checkpatch.rst starting from line 909:
+
+ **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 as below::
+
+ #define test(a) do { } while (0)
+
+ there would be a warning as below::
+
+ WARNING: Parameter 'a' is not used in function-like macro, please use
+ static inline instead.
Please let me know if the document needs further re-wording to make it helpful enough to the readers.
Thanks,
Xining
Joe Perches
maintainerRe: [PATCH v5 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 53 lines · Mac Xu
Quoted 19 lines · Barry Song
Quoted 9 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.
For a macro such as the one below,
Quoted 8 lines
#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 7 lines
This is no longer true.
Please update theERROR->WARNand message as belowIdeally, this would have an update to
Documentation/dev-tools/checkpatch.rstto describe the new --verbose message type
Hi Joe,
Thank you for the comments, here's the code:
scripts/checkpatch.pl +5
-0+# 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"); +}and here's the document for it which is inserted into the "Macros, Attributes and Symbols" section of
checkpatch.rststarting from line 909:(quoted diff) +13
-0+ + **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 as below:: + + #define test(a) do { } while (0) + + there would be a warning as below:: + + WARNING: Parameter 'a' is not used in function-like macro, please use + static inline instead.
Quoted 2 lines
Please let me know if the document needs further re-wording to make it helpful enough to the readers.
Hi again Xining.
Thanks.
That looks good but it doesn't match the script output which doesn't use ", please use static inline instead." (and I believe the script should not output that too)
Another good thing would be to add a line like:
See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
For example, from: checkpatch.rst
**ALLOC_SIZEOF_STRUCT**
The allocation style is bad. In general for family of allocation functions using sizeof() to get memory size, constructs like::
p = alloc(sizeof(struct foo), ...)
should be::
p = alloc(sizeof(*p), ...)
See: https://www.kernel.org/doc/html/latest/process/coding-style.html#allocating-memory
Re: [PATCH v5 2/2] scripts: checkpatch: check unused parameters for function-like macro
Quoted 78 lines · Mac Xu
Quoted 53 lines
Quoted 19 lines · Barry Song
Quoted 9 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.
For a macro such as the one below,
Quoted 8 lines
#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 7 lines
This is no longer true.
Please update theERROR->WARNand message as belowIdeally, this would have an update to
Documentation/dev-tools/checkpatch.rstto describe the new --verbose message type
Hi Joe,
Thank you for the comments, here's the code:
scripts/checkpatch.pl +5
-0+# 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"); +}and here's the document for it which is inserted into the "Macros, Attributes and Symbols" section of
checkpatch.rststarting from line 909:(quoted diff) +13
-0+ + **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 as below:: + + #define test(a) do { } while (0) + + there would be a warning as below:: + + WARNING: Parameter 'a' is not used in function-like macro, please use + static inline instead.Quoted 2 lines
Please let me know if the document needs further re-wording to make it helpful enough to the readers.
Hi again Xining.
Thanks.
That looks good but it doesn't match the script output which doesn't use ", please use static inline instead." (and I believe the script should not output that too)
Another good thing would be to add a line like:
See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
For example, from:
checkpatch.rst**ALLOC_SIZEOF_STRUCT**
The allocation style is bad. In general for family of allocation functions usingsizeof()to get memory size, constructs like::
Quoted 9 lines
p =
alloc(sizeof(struct foo), ...)should be::
p =
alloc(sizeof(*p), ...)See: https://www.kernel.org/doc/html/latest/process/coding-style.html#allocating-memory
Hi again Joe,
Thanks again for the detailed comments.
Here's the code (which keeps unchanged):
+# 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");
+}
And here's the updated document, which drops the ", please use static inline instead" from the warning message, and adds a link at the end of this document block:
+
+ **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: Parameter 'a' is not used in function-like macro.
+
+ See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
Regards,
Xining.