printf: convert test_hashed into macro
PATCH v2 1/1 printf: convert test_hashed into macro
This allows the compiler to check the arguments against the __printf attribute on __test. This produces better diagnostics when incorrect inputs are passed.
---
Changes in v2:
- Convert test_hashed to macro rather than try to annotate it in a way
that is compatible with both clang and gcc.
- Link to v1: https://patch.msgid.link/[email protected]
--- lib/tests/printf_kunit.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
lib/tests/printf_kunit.c +11 -9
@@ -266,15 +266,17 @@ hash_pointer(struct kunit *kunittest)KUNIT_EXPECT_MEMNEQ (kunittest, buf,PTR_STR ,PTR_WIDTH ); } -static void -test_hashed (struct kunit *kunittest,const char *fmt,const void *p) -{ -char buf[PLAIN_BUF_SIZE ]; - -plain_hash_to_buffer (kunittest, p, buf,PLAIN_BUF_SIZE ); - -test (buf, fmt, p); -} +/* +* This is a macro so that the compiler can compare its arguments to the +* __printf attribute on __test. This cannot be a function with a __printf +* attribute because GCC requires __printf functions to be variadic. +*/ +#define test_hashed (kunittest, fmt, p) \ + do { \ + char buf[PLAIN_BUF_SIZE]; \ + plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ + test(buf, fmt, p); \ + } while (0)/* * NULL pointers aren't hashed.
---
base-commit: 983d014aafb14ee5e4915465bf8948e8f3a723b5
change-id: 20251206-printf-kunit-printf-attr-19369fc57bf0
Best regards,
Signature · 1 line
Tamir Duberstein <[email protected]>
Andy Shevchenko
reviewerQuoted 1 line · Tamir Duberstein
This allows the compiler to check the arguments against the
__printf
__printf() since it takes parameters, OTOH it's an attribute at the end, so I have no strong opinion on how to spell it.
Quoted 1 line
attribute on
__test. This produces better diagnostics when incorrect
__test()
*This is reference to a function.
Quoted 1 line
inputs are passed.
...
Quoted 4 lines
+/*
+ * This is a macro so that the compiler can compare its arguments to the
+ *__printfattribute on__test. This cannot be a function with a__printf
+ * attribute because GCC requires__printffunctions to be variadic.
As per commit message remarks.
lib/tests/printf_kunit.c hash_pointer(struct kunit *kunittest) · line 279
Quoted 7 lines
+ */
lib/tests/printf_kunit.c +6
-0+#define test_hashed (kunittest, fmt, p) \ + do { \ + char buf[PLAIN_BUF_SIZE]; \ + plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ + test(buf, fmt, p); \ + } while (0)
Make sure you used tabs to indent the \:s.
...
The downside of a macro is a killing of compile-time type checks.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 32 lines · Andy Shevchenko
Quoted 1 line · Tamir Duberstein
This allows the compiler to check the arguments against the
__printf
__printf()since it takes parameters, OTOH it's an attribute at the end, so I have no strong opinion on how to spell it.Quoted 1 line
attribute on
__test. This produces better diagnostics when incorrect
__test()*This is reference to a function.
Quoted 1 line
inputs are passed.
...
Quoted 4 lines
+/*
+ * This is a macro so that the compiler can compare its arguments to the
+ *__printfattribute on__test. This cannot be a function with a__printf
+ * attribute because GCC requires__printffunctions to be variadic.As per commit message remarks.
lib/tests/printf_kunit.c hash_pointer(struct kunit *kunittest) · line 279
Quoted 7 lines
+ */
lib/tests/printf_kunit.c +6
-0+#define test_hashed (kunittest, fmt, p) \ + do { \ + char buf[PLAIN_BUF_SIZE]; \ + plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ + test(buf, fmt, p); \ + } while (0)
Quoted 5 lines
Make sure you used tabs to indent the \:s.
...
The downside of a macro is a killing of compile-time type checks.
This macro still bottoms out in __test() which is a function, so I believe it preserves (and enhances, per the commit message) compile-time checks.
I'll send v3 with the changes you requested above.
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Petr Mladek
maintainerQuoted 38 lines · Tamir Duberstein
Quoted 36 lines · Andy Shevchenko
Quoted 1 line · Tamir Duberstein
This allows the compiler to check the arguments against the
__printf
__printf()since it takes parameters, OTOH it's an attribute at the end, so I have no strong opinion on how to spell it.Quoted 1 line
attribute on
__test. This produces better diagnostics when incorrect
__test()*This is reference to a function.
Quoted 1 line
inputs are passed.
...
Quoted 4 lines
+/*
+ * This is a macro so that the compiler can compare its arguments to the
+ *__printfattribute on__test. This cannot be a function with a__printf
+ * attribute because GCC requires__printffunctions to be variadic.As per commit message remarks.
lib/tests/printf_kunit.c hash_pointer(struct kunit *kunittest) · line 279
Quoted 7 lines
+ */
lib/tests/printf_kunit.c +6
-0+#define test_hashed (kunittest, fmt, p) \ + do { \ + char buf[PLAIN_BUF_SIZE]; \ + plain_hash_to_buffer(kunittest, p, buf, PLAIN_BUF_SIZE); \ + test(buf, fmt, p); \ + } while (0)Make sure you used tabs to indent the \:s.
...
Quoted 7 lines
Quoted 1 line
The downside of a macro is a killing of compile-time type checks.
This macro still bottoms out in
__test()which is a function, so I believe it preserves (and enhances, per the commit message) compile-time checks.I'll send v3 with the changes you requested above.
JFYI, I am fine with this approach.
Best Regards,
Petr