If we have new features introduced by C11, use them

Instead of using hacks or compiler-specific features, if we have
standard features as defined in ISO C11, use them.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
This commit is contained in:
H. Peter Anvin 2016-12-20 02:29:58 -08:00
parent fd610f27d6
commit abd28c9ab9
5 changed files with 44 additions and 5 deletions

24
aclocal.m4 vendored
View File

@ -73,3 +73,27 @@ void *foo(void)
[Define to 1 if your compiler supports __attribute__(($1)) on functions])],
[AC_MSG_RESULT([no])])
])
dnl --------------------------------------------------------------------------
dnl PA_FUNC_ATTRIBUTE_ERROR
dnl
dnl See if this compiler supports __attribute__((error("foo")))
dnl The generic version of this doesn't work as it makes the compiler
dnl throw an error by design.
dnl --------------------------------------------------------------------------
AC_DEFUN(PA_FUNC_ATTRIBUTE_ERROR,
[AC_MSG_CHECKING([if $CC supports the error function attribute])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <stdarg.h>
extern void __attribute__((error("message"))) barf(void);
void foo(void)
{
if (0)
barf();
}
])],
[AC_MSG_RESULT([yes])
AC_DEFINE(m4_toupper([HAVE_FUNC_ATTRIBUTE_ERROR]), 1,
[Define to 1 if your compiler supports __attribute__((error)) on functions])],
[AC_MSG_RESULT([no])])
])

View File

@ -492,7 +492,7 @@ restart_parse:
if (i == TOKEN_EOS)
goto fail;
nasm_build_assert(P_none != 0);
nasm_build_assert(P_none == 0);
memset(result->prefixes, P_none, sizeof(result->prefixes));
result->times = 1L;

View File

@ -32,8 +32,6 @@ AH_TEMPLATE(WORDS_LITTLEENDIAN,
[Define to 1 if your processor stores words with the least significant
byte first (like Intel and VAX, unlike Motorola and SPARC).])
PA_ADD_CFLAGS([-std=c99])
dnl Force gcc and gcc-compatible compilers treat signed integers
dnl as 2's complement
PA_ADD_CFLAGS([-fwrapv])
@ -73,6 +71,7 @@ AC_HEADER_STDC
AC_CHECK_HEADERS(inttypes.h)
AC_CHECK_HEADERS(strings.h)
AC_HEADER_STDBOOL
AC_CHECK_HEADERS(stdnoreturn.h)
AC_CHECK_HEADERS(io.h)
AC_CHECK_HEADERS(fcntl.h)
AC_CHECK_HEADERS(unistd.h)
@ -145,6 +144,7 @@ PA_FUNC_ATTRIBUTE(alloc_size, (1))
PA_FUNC_ATTRIBUTE(format, [(printf,1,2)], int, [const char *, ...], ["%d",1])
PA_FUNC_ATTRIBUTE(const)
PA_FUNC_ATTRIBUTE(pure)
PA_FUNC_ATTRIBUTE_ERROR
dnl
dnl support cchace

View File

@ -77,6 +77,7 @@
# include "nasmint.h"
#endif
#include <assert.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
@ -208,7 +209,10 @@ char *strsep(char **, const char *);
/*
* How to tell the compiler that a function doesn't return
*/
#ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
#ifdef HAVE_STDNORETURN_H
# include <stdnoreturn.h>
# define no_return noreturn void
#elif defined(HAVE_FUNC_ATTRIBUTE_NORETURN)
# define no_return void __attribute__((noreturn))
#else
# define no_return void

View File

@ -175,7 +175,18 @@ no_return nasm_assert_failed(const char *, int, const char *);
/*
* NASM failure at build time if x != 0
*/
#define nasm_build_assert(x) (void)(sizeof(char[1-2*!!(x)]))
#ifdef static_assert
# define nasm_build_assert(x) static_assert(x, "assertion " #x " failed")
#elif defined(HAVE_FUNC_ATTRIBUTE_ERROR)
# define nasm_build_assert(x) \
if (!(x)) { \
extern void __attribute__((error("assertion " #x " failed"))) \
fail(void); \
fail(); \
}
#else
# define nasm_build_assert(x) (void)(sizeof(char[1-2*!(x)]))
#endif
/*
* ANSI doesn't guarantee the presence of `stricmp' or