Skip to content

Commit 6813216

Browse files
Barry Songakpm00
authored andcommitted
Documentation: coding-style: ask function-like macros to evaluate parameters
Patch series "codingstyle: avoid unused parameters for a function-like macro", v7. 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. This patch (of 2): Recent commit 77292bb ("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. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Barry Song <[email protected]> Suggested-by: Max Filippov <[email protected]> Reviewed-by: Mark Brown <[email protected]> Acked-by: Joe Perches <[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]> Cc: Charlemagne Lasse <[email protected]> Cc: Jeff Johnson <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 33580d6 commit 6813216

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Documentation/process/coding-style.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,29 @@ Macros with multiple statements should be enclosed in a do - while block:
827827
do_this(b, c); \
828828
} while (0)
829829
830+
Function-like macros with unused parameters should be replaced by static
831+
inline functions to avoid the issue of unused variables:
832+
833+
.. code-block:: c
834+
835+
static inline void fun(struct foo *foo)
836+
{
837+
}
838+
839+
Due to historical practices, many files still employ the "cast to (void)"
840+
approach to evaluate parameters. However, this method is not advisable.
841+
Inline functions address the issue of "expression with side effects
842+
evaluated more than once", circumvent unused-variable problems, and
843+
are generally better documented than macros for some reason.
844+
845+
.. code-block:: c
846+
847+
/*
848+
* Avoid doing this whenever possible and instead opt for static
849+
* inline functions
850+
*/
851+
#define macrofun(foo) do { (void) (foo); } while (0)
852+
830853
Things to avoid when using macros:
831854

832855
1) macros that affect control flow:

0 commit comments

Comments
 (0)