From e4d2bfdd73648f4b2bce123b6425d3e63bb99f33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 25 Apr 2013 21:31:05 -0400 Subject: [PATCH] Add printf attributes in exported headers gcc (and other compilers) sometimes generate spurious warnings, and thus users of public headers must be able to disable warnings. Printf format attributes can be disabled by setting #define _sd_printf_attr_ before including the header file. Also, add similar logic for sentinel attribute: #define _sd_sentinel_attr_ before including the header file disables the attribute. --- src/systemd/sd-bus.h | 10 +++++++++- src/systemd/sd-daemon.h | 10 +++++----- src/systemd/sd-journal.h | 24 ++++++++++++++++++------ src/udev/udev.h | 7 +++---- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index 36fab9ffa..55648e0b3 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -32,6 +32,14 @@ extern "C" { #endif +#ifndef _sd_printf_attr_ +# if __GNUC__ >= 4 +# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) +# else +# define _sd_printf_attr_(a,b) +# endif +#endif + /* TODO: * - add page donation logic * - api for appending/reading fixed arrays @@ -187,7 +195,7 @@ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machi #define SD_BUS_ERROR_MAKE(name, message) ((sd_bus_error) {(name), (message), 0}) void sd_bus_error_free(sd_bus_error *e); -int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...); +int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) _sd_printf_attr_(3, 0); void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message); int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e); int sd_bus_error_is_set(const sd_bus_error *e); diff --git a/src/systemd/sd-daemon.h b/src/systemd/sd-daemon.h index fb7456d50..daa3f4c85 100644 --- a/src/systemd/sd-daemon.h +++ b/src/systemd/sd-daemon.h @@ -68,11 +68,11 @@ extern "C" { */ #ifndef _sd_printf_attr_ -#if __GNUC__ >= 4 -#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) -#else -#define _sd_printf_attr_(a,b) -#endif +# if __GNUC__ >= 4 +# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) +# else +# define _sd_printf_attr_(a,b) +# endif #endif /* diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h index 51653bafe..5375d4996 100644 --- a/src/systemd/sd-journal.h +++ b/src/systemd/sd-journal.h @@ -34,19 +34,31 @@ extern "C" { #endif +#ifndef _sd_printf_attr_ +# if __GNUC__ >= 4 +# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) +# else +# define _sd_printf_attr_(a,b) +# endif +#endif + +#ifndef _sd_sentinel_attr_ +# define _sd_sentinel_attr_ __attribute__((sentinel)) +#endif + /* Journal APIs. See sd-journal(3) for more information. */ /* Write to daemon */ -int sd_journal_print(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3))); -int sd_journal_printv(int priority, const char *format, va_list ap); -int sd_journal_send(const char *format, ...) __attribute__((sentinel)); +int sd_journal_print(int priority, const char *format, ...) _sd_printf_attr_(2, 3); +int sd_journal_printv(int priority, const char *format, va_list ap) _sd_printf_attr_(2, 0); +int sd_journal_send(const char *format, ...) _sd_printf_attr_(1, 0) _sd_sentinel_attr_; int sd_journal_sendv(const struct iovec *iov, int n); int sd_journal_perror(const char *message); /* Used by the macros below. Don't call this directly. */ -int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) __attribute__ ((format (printf, 5, 6))); -int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap); -int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) __attribute__((sentinel)); +int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_attr_(5, 6); +int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) _sd_printf_attr_(5, 0); +int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) _sd_printf_attr_(4, 0) _sd_sentinel_attr_; int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n); int sd_journal_perror_with_location(const char *file, const char *line, const char *func, const char *message); diff --git a/src/udev/udev.h b/src/udev/udev.h index 906dfbaa0..caec5f0a5 100644 --- a/src/udev/udev.h +++ b/src/udev/udev.h @@ -16,13 +16,13 @@ * along with this program. If not, see . */ -#ifndef _UDEV_H_ -#define _UDEV_H_ +#pragma once #include #include #include +#include "macro.h" #include "libudev.h" #include "libudev-private.h" #include "util.h" @@ -195,7 +195,7 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool /* udev logging */ void udev_main_log(struct udev *udev, int priority, const char *file, int line, const char *fn, - const char *format, va_list args); + const char *format, va_list args) _printf_attr_(6, 0); /* udevadm commands */ struct udevadm_cmd { @@ -212,4 +212,3 @@ extern const struct udevadm_cmd udevadm_monitor; extern const struct udevadm_cmd udevadm_hwdb; extern const struct udevadm_cmd udevadm_test; extern const struct udevadm_cmd udevadm_test_builtin; -#endif -- 2.30.2