diff --git a/Macros.h b/Macros.h index 14249f3d..5e8891a4 100644 --- a/Macros.h +++ b/Macros.h @@ -37,8 +37,6 @@ #define ATTR_NONNULL __attribute__((nonnull)) #define ATTR_NORETURN __attribute__((noreturn)) #define ATTR_UNUSED __attribute__((unused)) -#define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a))) -#define ATTR_ALLOC_SIZE2(a, b) __attribute__((alloc_size (a, b))) #define ATTR_MALLOC __attribute__((malloc)) #else /* __GNUC__ */ @@ -47,12 +45,22 @@ #define ATTR_NONNULL #define ATTR_NORETURN #define ATTR_UNUSED -#define ATTR_ALLOC_SIZE1(a) -#define ATTR_ALLOC_SIZE2(a, b) #define ATTR_MALLOC #endif /* __GNUC__ */ +#ifdef HAVE_ATTR_ALLOC_SIZE + +#define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a))) +#define ATTR_ALLOC_SIZE2(a, b) __attribute__((alloc_size (a, b))) + +#else + +#define ATTR_ALLOC_SIZE1(a) +#define ATTR_ALLOC_SIZE2(a, b) + +#endif /* HAVE_ATTR_ALLOC_SIZE */ + // ignore casts discarding const specifier, e.g. // const char [] -> char * / void * // const char *[2]' -> char *const * diff --git a/configure.ac b/configure.ac index 8431242f..e9900e75 100644 --- a/configure.ac +++ b/configure.ac @@ -173,6 +173,20 @@ AC_TYPE_UINT16_T AC_TYPE_UINT32_T AC_TYPE_UINT64_T +AC_MSG_CHECKING(for alloc_size) +old_CFLAGS="$CFLAGS" +CFLAGS="$CFLAGS -Wno-error -Werror=attributes" +AC_COMPILE_IFELSE([ + AC_LANG_SOURCE( + [ + __attribute__((alloc_size(1))) char* my_alloc(int size) { return 0; } + ],[] + )], + AC_DEFINE([HAVE_ATTR_ALLOC_SIZE], 1, [The alloc_size attribute is supported.]) + AC_MSG_RESULT(yes), + AC_MSG_RESULT(no)) +CFLAGS="$old_CFLAGS" + # ----------------------------------------------------------------------