[RFC 3/6] compiler.h: add __attribute__((format_arg)) shorthand

From: Rasmus Villemoes
Date: Wed Nov 08 2017 - 17:31:08 EST


The __format_arg attribute tells gcc that it can use a specific
argument to the annotated function as the format string for the
purpose of type-checking a surrounding __printf function call, e.g.

sprintf(buf, fmtcheck(what->ever, "%d %lx", 0), i, m)

makes gcc check that i and m are indeed an int and a long; with

sprintf(buf, what->ever, i, m)

the compiler cannot do any type checking.

Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
include/linux/compiler-gcc.h | 1 +
include/linux/compiler.h | 4 ++++
2 files changed, 5 insertions(+)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index bb78e5bdff26..69a233f9fa26 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -128,6 +128,7 @@
#define __maybe_unused __attribute__((unused))
#define __always_unused __attribute__((unused))
#define __mode(x) __attribute__((mode(x)))
+#define __format_arg(n) __attribute__((format_arg(n)))

/* gcc version specific checks */

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 478914ad280b..2c8793f72fb0 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -631,4 +631,8 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
#define __format_template(x)
#endif

+#ifndef __format_arg
+#define __format_arg(n)
+#endif
+
#endif /* __LINUX_COMPILER_H */
--
2.11.0