0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-11-08 23:27:15 -05:00

compiler: add and use unreachable() macro

C23 defines unreachable() as a macro in <stddef.h>. For earlier
versions of gcc, __builtin_unreachable() is possible.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel)
2025-10-13 17:53:42 -07:00
parent 9ba21c1e50
commit 44ec97993a
4 changed files with 36 additions and 20 deletions

View File

@@ -15,15 +15,19 @@ unsigned int opt_verbose_info; /* Informational messages? */
/* Common function body */
#define nasm_do_error(_sev,_flags) \
do { \
const errflags nde_severity = (_sev); \
const errflags nde_flags = nde_severity | (_flags); \
va_list ap; \
va_start(ap, fmt); \
if ((_sev) >= ERR_CRITICAL) \
nasm_verror_critical((_sev)|(_flags), fmt, ap); \
else \
nasm_verror((_sev)|(_flags), fmt, ap); \
if (nde_severity >= ERR_CRITICAL) { \
nasm_verror_critical(nde_flags, fmt, ap); \
unreachable(); \
} else { \
nasm_verror(nde_flags, fmt, ap); \
if (nde_severity >= ERR_FATAL) \
unreachable(); \
} \
va_end(ap); \
if ((_sev) >= ERR_FATAL) \
abort(); \
} while (0)
/*

View File

@@ -1874,11 +1874,9 @@ static const char no_file_name[] = "nasm"; /* What to print if no file name */
*/
static_fatal_func die_hard(errflags true_type, errflags severity)
{
if (true_type < ERR_PANIC || !abort_on_panic) {
fflush(NULL);
if (true_type == ERR_PANIC && abort_on_panic)
abort();
if (ofile) {
fclose(ofile);
if (!keep_all)
@@ -1891,6 +1889,10 @@ static_fatal_func die_hard(errflags true_type, errflags severity)
/* Terminate immediately */
exit(true_type - ERR_FATAL + 1);
}
while (1)
abort();
}
/*
@@ -1950,6 +1952,7 @@ fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args
fputc('\n', error_file);
die_hard(true_type, severity);
unreachable();
}
/**

View File

@@ -224,6 +224,7 @@ AC_CHECK_FUNCS(sysconf)
AC_CHECK_FUNCS([access _access faccessat])
PA_HAVE_FUNC(__builtin_expect,(1,1))
PA_HAVE_FUNC(__builtin_unreachable,())
PA_FUNC_SNPRINTF
PA_FUNC_VSNPRINTF

View File

@@ -469,4 +469,12 @@ static inline unsigned int watcom_switch_hack(uint64_t x)
# define default case BOGUS_CASE: default
#endif
#ifndef unreachable /* C23 defines as a macro in <stddef.h> */
# ifdef HAVE___BUILTIN_UNREACHABLE
# define unreachable() __builtin_unreachable()
# else
# define unreachable() do { abort(); } while(1)
# endif
#endif
#endif /* NASM_COMPILER_H */