Re: [PATCH v2 1/7] sim: Add ATTRIBUTE_PRINTF

Andrew Burgess via Gdb-patches
2022-10-11T14:25:17Z

Tsukasa OI <research_trasio@irq.a4lg.com> writes:

> Clang generates a warning if the format string of a printf-like function is
> not a literal ("-Wformat-nonliteral"). On the default configuration, it
> causes a build failure (unless "--disable-werror" is specified).
>
> To avoid warnings on the printf-like wrapper, it requires proper
> __attribute__((format)) and we have ATTRIBUTE_PRINTF macro for this reason.
>
> This commit adds ATTRIBUTE_PRINTF to a printf-like function.
> ---
> sim/common/sim-utils.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c
> index 259b14cce29..f079bb3f330 100644
> --- a/sim/common/sim-utils.c
> +++ b/sim/common/sim-utils.c
> @@ -146,7 +146,7 @@ sim_cpu_msg_prefix (sim_cpu *cpu)
>
> /* Cover fn to sim_io_eprintf. */
>
> -void
> +void ATTRIBUTE_PRINTF (2, 3)
> sim_io_eprintf_cpu (sim_cpu *cpu, const char *fmt, ...)
> {
> SIM_DESC sd = CPU_STATE (cpu);

I've pushed this patch, but I moved the ATTRIBUTE_PRINTF to the function
declaration.

Thanks,
Andrew

---

commit 7aaf9c03d143802302c49ed70d05766c9cf3cb5b
Author: Tsukasa OI <research_trasio@irq.a4lg.com>
Date: Thu Oct 6 06:36:27 2022 +0000

sim/common: Add ATTRIBUTE_PRINTF

Clang generates a warning if the format string of a printf-like function is
not a literal ("-Wformat-nonliteral"). On the default configuration, it
causes a build failure (unless "--disable-werror" is specified).

To avoid warnings on the printf-like wrapper, it requires proper
__attribute__((format)) and we have ATTRIBUTE_PRINTF macro for this reason.

This commit adds ATTRIBUTE_PRINTF to a printf-like function.

diff --git a/sim/common/sim-cpu.h b/sim/common/sim-cpu.h
index d8b2b9c494a..9e09a55bf81 100644
--- a/sim/common/sim-cpu.h
+++ b/sim/common/sim-cpu.h
@@ -137,7 +137,8 @@ extern sim_cpu *sim_cpu_lookup (SIM_DESC, const char *);
/* Return prefix to use in cpu specific messages. */
extern const char *sim_cpu_msg_prefix (sim_cpu *);
/* Cover fn to sim_io_eprintf. */
-extern void sim_io_eprintf_cpu (sim_cpu *, const char *, ...);
+extern void sim_io_eprintf_cpu (sim_cpu *, const char *, ...)
+ ATTRIBUTE_PRINTF (2, 3);

/* Get/set a pc value. */
#define CPU_PC_GET(cpu) ((* CPU_PC_FETCH (cpu)) (cpu))