codingstyle: avoid unused parameters for a function-like macro
Subsystems (4)
v7 cover codingstyle: avoid unused parameters for a function-like macro
From: Barry Song <[email protected]>
-v7:
* collect ack of Joe for checkpatch.pl, thanks!
* fixed an indentation per Joe
-v6:
* collect ack of Joe, thanks!
* refine docs according to Jonathan, thanks!
* add checkpatch doc according to Joe, thanks!
v6 link:
https://lore.kernel.org/lkml/[email protected]/
-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/[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!
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/dev-tools/checkpatch.rst | 14 ++++++++++++++ Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++ scripts/checkpatch.pl | 6 ++++++ 3 files changed, 43 insertions(+)
Signature · 1 line
2.34.1
PATCH v7 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 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
Documentation/process/coding-style.rst +23 -0
@@ -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:
Signature · 1 line
2.34.1
PATCH v7 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.
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
--- Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++ scripts/checkpatch.pl | 6 ++++++ 2 files changed, 20 insertions(+)
Documentation/dev-tools/checkpatch.rst +14 -0
@@ -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
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