Skip to content

Commit e4d2bfd

Browse files
committed
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.
1 parent b1e2b33 commit e4d2bfd

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

src/systemd/sd-bus.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
extern "C" {
3333
#endif
3434

35+
#ifndef _sd_printf_attr_
36+
# if __GNUC__ >= 4
37+
# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
38+
# else
39+
# define _sd_printf_attr_(a,b)
40+
# endif
41+
#endif
42+
3543
/* TODO:
3644
* - add page donation logic
3745
* - 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
187195
#define SD_BUS_ERROR_MAKE(name, message) ((sd_bus_error) {(name), (message), 0})
188196

189197
void sd_bus_error_free(sd_bus_error *e);
190-
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
198+
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) _sd_printf_attr_(3, 0);
191199
void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message);
192200
int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e);
193201
int sd_bus_error_is_set(const sd_bus_error *e);

src/systemd/sd-daemon.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ extern "C" {
6868
*/
6969

7070
#ifndef _sd_printf_attr_
71-
#if __GNUC__ >= 4
72-
#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
73-
#else
74-
#define _sd_printf_attr_(a,b)
75-
#endif
71+
# if __GNUC__ >= 4
72+
# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
73+
# else
74+
# define _sd_printf_attr_(a,b)
75+
# endif
7676
#endif
7777

7878
/*

src/systemd/sd-journal.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,31 @@
3434
extern "C" {
3535
#endif
3636

37+
#ifndef _sd_printf_attr_
38+
# if __GNUC__ >= 4
39+
# define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
40+
# else
41+
# define _sd_printf_attr_(a,b)
42+
# endif
43+
#endif
44+
45+
#ifndef _sd_sentinel_attr_
46+
# define _sd_sentinel_attr_ __attribute__((sentinel))
47+
#endif
48+
3749
/* Journal APIs. See sd-journal(3) for more information. */
3850

3951
/* Write to daemon */
40-
int sd_journal_print(int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
41-
int sd_journal_printv(int priority, const char *format, va_list ap);
42-
int sd_journal_send(const char *format, ...) __attribute__((sentinel));
52+
int sd_journal_print(int priority, const char *format, ...) _sd_printf_attr_(2, 3);
53+
int sd_journal_printv(int priority, const char *format, va_list ap) _sd_printf_attr_(2, 0);
54+
int sd_journal_send(const char *format, ...) _sd_printf_attr_(1, 0) _sd_sentinel_attr_;
4355
int sd_journal_sendv(const struct iovec *iov, int n);
4456
int sd_journal_perror(const char *message);
4557

4658
/* Used by the macros below. Don't call this directly. */
47-
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)));
48-
int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap);
49-
int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) __attribute__((sentinel));
59+
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);
60+
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);
61+
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_;
5062
int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n);
5163
int sd_journal_perror_with_location(const char *file, const char *line, const char *func, const char *message);
5264

src/udev/udev.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#ifndef _UDEV_H_
20-
#define _UDEV_H_
19+
#pragma once
2120

2221
#include <sys/types.h>
2322
#include <sys/param.h>
2423
#include <signal.h>
2524

25+
#include "macro.h"
2626
#include "libudev.h"
2727
#include "libudev-private.h"
2828
#include "util.h"
@@ -195,7 +195,7 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool
195195
/* udev logging */
196196
void udev_main_log(struct udev *udev, int priority,
197197
const char *file, int line, const char *fn,
198-
const char *format, va_list args);
198+
const char *format, va_list args) _printf_attr_(6, 0);
199199

200200
/* udevadm commands */
201201
struct udevadm_cmd {
@@ -212,4 +212,3 @@ extern const struct udevadm_cmd udevadm_monitor;
212212
extern const struct udevadm_cmd udevadm_hwdb;
213213
extern const struct udevadm_cmd udevadm_test;
214214
extern const struct udevadm_cmd udevadm_test_builtin;
215-
#endif

0 commit comments

Comments
 (0)