From 2b01e38ca2448364b6470e471ebfedf5793638af Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Fri, 12 Jun 2020 10:27:12 +0200 Subject: [PATCH] Various minor changes - Removed trailing ';' from convenience macros. - Convenience macros did not really need the ## __VA_ARGS__ extension. Simply grouping all parameters into '...', while decreasing readability, solves the portability issue. - Added C99 check to dynstr.c. --- dynstr.c | 4 ++++ include/dynstr.h | 21 +++++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/dynstr.c b/dynstr.c index f816155..847b75f 100644 --- a/dynstr.c +++ b/dynstr.c @@ -21,6 +21,10 @@ #include #include +#if __STDC_VERSION__ < 199901L +#error C99 support is mandatory for dynstr +#endif /* __STDC_VERSION < 199901L */ + void dynstr_init(struct dynstr *const d) { memset(d, 0, sizeof *d); diff --git a/include/dynstr.h b/include/dynstr.h index 66d33c8..ee3b22b 100644 --- a/include/dynstr.h +++ b/include/dynstr.h @@ -17,42 +17,35 @@ #ifndef DYNSTR_H #define DYNSTR_H -#include - #if __STDC_VERSION__ < 199901L #error C99 support is mandatory for dynstr #endif /* __STDC_VERSION < 199901L */ -#ifdef __GNUC__ +#include +#include /* Since dynstr_append() might fail (as it is based on dynamic memory - * allocation), these macros can be used to avoid boilerplate code. - * However, C99 variadic macros might expand with a trailing comma - * when no parameters are given after "format", a GNU extension is - * needed to avoid this problem. */ -#include + * allocation), these macros can be used to avoid boilerplate code. */ /** * Convenience macro that calls dynstr_append and returns NULL if failed. */ -#define dynstr_append_or_ret_null(d, format, ...) if (dynstr_append(d, format, ## __VA_ARGS__)) return NULL; +#define dynstr_append_or_ret_null(...) if (dynstr_append(__VA_ARGS__) != DYNSTR_OK) return NULL /** * Convenience macro that calls dynstr_append and returns false if failed. */ -#define dynstr_append_or_ret_false(d, format, ...) if (dynstr_append(d, format, ## __VA_ARGS__)) return false; +#define dynstr_append_or_ret_false(...) if (dynstr_append(__VA_ARGS__) != DYNSTR_OK) return false /** * Convenience macro that calls dynstr_append and returns its error code if failed. */ -#define dynstr_append_or_ret(d, format, ...) {const enum dynstr_err err = dynstr_append(d, format, ## __VA_ARGS__); if (err) return err;} +#define dynstr_append_or_ret(...) {const enum dynstr_err err = dynstr_append(__VA_ARGS__); if (err != DYNSTR_OK) return err;} /** * Convenience macro that calls dynstr_append and returns zero if failed. */ -#define dynstr_append_or_ret_zero(d, format, ...) if (dynstr_append(d, format, ## __VA_ARGS__)) return 0; - -#endif /* __GNUC__ */ +#define dynstr_append_or_ret_zero( ...) if (dynstr_append( __VA_ARGS__) != DYNSTR_OK) return 0 /** * Dynamic string type used for this library.