Skip to content

Commit

Permalink
Add configure check for nonnull attribute
Browse files Browse the repository at this point in the history
The main reason I do this is to document the minimum compiler version
(GCC 3.3) for the attribute. But it may work with other compilers, too.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Oct 10, 2023
1 parent 83041f3 commit f541f70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,31 @@ in the source distribution for its full text.
#ifdef __GNUC__ // defined by GCC and Clang

#define ATTR_FORMAT(type, index, check) __attribute__((format (type, index, check)))
#define ATTR_NONNULL __attribute__((nonnull))
#define ATTR_NORETURN __attribute__((noreturn))
#define ATTR_UNUSED __attribute__((unused))
#define ATTR_MALLOC __attribute__((malloc))

#else /* __GNUC__ */

#define ATTR_FORMAT(type, index, check)
#define ATTR_NONNULL
#define ATTR_NORETURN
#define ATTR_UNUSED
#define ATTR_MALLOC

#endif /* __GNUC__ */

#ifdef HAVE_ATTR_NONNULL

#define ATTR_NONNULL __attribute__((nonnull))
#define ATTR_NONNULL_N(...) __attribute__((nonnull(__VA_ARGS__)))

#else

#define ATTR_NONNULL
#define ATTR_NONNULL_N(...)

#endif /* HAVE_ATTR_NONNULL */

#ifdef HAVE_ATTR_ALLOC_SIZE

#define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a)))
Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ AC_COMPILE_IFELSE([
AC_MSG_RESULT(no))
CFLAGS="$old_CFLAGS"

AC_MSG_CHECKING(for nonnull)
old_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wno-error -Werror=attributes"
AC_COMPILE_IFELSE([
AC_LANG_SOURCE(
[[
/* Attribute supported in GCC 3.3 or later */
__attribute__((nonnull)) int my_strcmp(const char* a, const char* b);
__attribute__((nonnull(1))) long my_strtol(const char* str, char** endptr, int base);
]]
)],
AC_DEFINE([HAVE_ATTR_NONNULL], 1, [The nonnull attribute is supported.])
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
CFLAGS="$old_CFLAGS"

AC_MSG_CHECKING(for NaN support)
dnl Note: AC_RUN_IFELSE does not try compiling the program at all when
dnl $cross_compiling is 'yes'.
Expand Down

0 comments on commit f541f70

Please sign in to comment.