[PATCH v3] printf: convert test_hashed into macro

Tamir Duberstein posted 1 patch 2 weeks, 2 days ago
lib/tests/printf_kunit.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
[PATCH v3] printf: convert test_hashed into macro
Posted by Tamir Duberstein 2 weeks, 2 days ago
This allows the compiler to check the arguments against the __printf()
attribute on __test(). This produces better diagnostics when incorrect
inputs are passed.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202512061600.89CKQ3ag-lkp@intel.com/
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
Changes in v3:
- Added parentheses to function references.
- Changed escaped newline indentation to tabs.
- Link to v2: https://patch.msgid.link/20260116-printf-kunit-printf-attr-v2-1-3dd7fefc3207@kernel.org

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/20251206-printf-kunit-printf-attr-v1-1-1682808b51d0@gmail.com
---
 lib/tests/printf_kunit.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index 7617e5b8b02c..f6f21b445ece 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -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: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7
change-id: 20251206-printf-kunit-printf-attr-19369fc57bf0

Best regards,
--  
Tamir Duberstein <tamird@kernel.org>
Re: [PATCH v3] printf: convert test_hashed into macro
Posted by Petr Mladek 1 week, 3 days ago
On Wed 2026-01-21 11:10:08, Tamir Duberstein wrote:
> This allows the compiler to check the arguments against the __printf()
> attribute on __test(). This produces better diagnostics when incorrect
> inputs are passed.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202512061600.89CKQ3ag-lkp@intel.com/
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>

JFYI, the patch has been comitted into printk/linux.git,
branch for-6.20.

Best Regards,
Petr
Re: [PATCH v3] printf: convert test_hashed into macro
Posted by Tamir Duberstein 1 week, 3 days ago
On Wed, Jan 28, 2026 at 4:33 AM Petr Mladek <pmladek@suse.com> wrote:
>
> On Wed 2026-01-21 11:10:08, Tamir Duberstein wrote:
> > This allows the compiler to check the arguments against the __printf()
> > attribute on __test(). This produces better diagnostics when incorrect
> > inputs are passed.
> >
> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes: https://lore.kernel.org/oe-kbuild-all/202512061600.89CKQ3ag-lkp@intel.com/
> > Signed-off-by: Tamir Duberstein <tamird@kernel.org>
>
> JFYI, the patch has been comitted into printk/linux.git,
> branch for-6.20.
>
> Best Regards,
> Petr

Thanks all!
Tamir
Re: [PATCH v3] printf: convert test_hashed into macro
Posted by Petr Mladek 1 week, 3 days ago
On Wed 2026-01-21 11:10:08, Tamir Duberstein wrote:
> This allows the compiler to check the arguments against the __printf()
> attribute on __test(). This produces better diagnostics when incorrect
> inputs are passed.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202512061600.89CKQ3ag-lkp@intel.com/
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>

Looks good to me:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr

PS: I am going to push it right now. It took me some time to get back
    to this mail. I am a bit overloaded at the moment.
Re: [PATCH v3] printf: convert test_hashed into macro
Posted by Andy Shevchenko 2 weeks, 2 days ago
On Wed, Jan 21, 2026 at 11:10:08AM -0500, Tamir Duberstein wrote:
> This allows the compiler to check the arguments against the __printf()
> attribute on __test(). This produces better diagnostics when incorrect
> inputs are passed.

Seems there is a consensus about this version, FWIW,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v3] printf: convert test_hashed into macro
Posted by Tamir Duberstein 1 week, 3 days ago
On Wed, Jan 21, 2026 at 11:39 AM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Jan 21, 2026 at 11:10:08AM -0500, Tamir Duberstein wrote:
> > This allows the compiler to check the arguments against the __printf()
> > attribute on __test(). This produces better diagnostics when incorrect
> > inputs are passed.
>
> Seems there is a consensus about this version, FWIW,
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Thanks Andy. Anything further required before this can be taken through printk?

Cheers,
Tamir