aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorH. Peter Anvin (Intel) <hpa@zytor.com>2018-12-14 14:21:16 -0800
committerH. Peter Anvin (Intel) <hpa@zytor.com>2018-12-14 15:42:03 -0800
commitb7f24e7715d8fdca52fda7fb9df1b70d8d4bbc6b (patch)
tree15669a93ec7bc21743b0928d04b7861180082cb6 /include
parentc3c6cea83804e7ba36b31d15ba25954ea3a5bdfd (diff)
downloadnasm-b7f24e7715d8fdca52fda7fb9df1b70d8d4bbc6b.tar.gz
nasm-b7f24e7715d8fdca52fda7fb9df1b70d8d4bbc6b.tar.xz
nasm-b7f24e7715d8fdca52fda7fb9df1b70d8d4bbc6b.zip
nasm_assert(): try to run at compile time if possible
Try to make nasm_assert() do a static assert if the argument can be evaluated at compile time by any particular compiler. We also provide nasm_try_static_assert() which will assert a compile-time expression if and only if we can determine we have a constant at compile time *and* we know that the compiler has a way to handle it. Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Diffstat (limited to 'include')
-rw-r--r--include/compiler.h21
-rw-r--r--include/nasmlib.h62
2 files changed, 66 insertions, 17 deletions
diff --git a/include/compiler.h b/include/compiler.h
index 7beeb8fe..f0258a8b 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -359,6 +359,27 @@ size_t strnlen(const char *s, size_t maxlen);
#endif
/*
+ * If we can guarantee that a particular expression is constant, use it,
+ * otherwise use a different version.
+ */
+#if defined(__GNUC__) && (__GNUC__ >= 3)
+# define not_pedantic_start \
+ _Pragma("GCC diagnostic push") \
+ _Pragma("GCC diagnostic ignored \"-Wpedantic\"")
+# define not_pedantic_end \
+ _Pragma("GCC diagnostic pop")
+#else
+# define not_pedantic_start
+# define not_pedantic_end
+#endif
+
+#ifdef HAVE___BUILTIN_CHOOSE_EXPR
+# define if_constant(x,y) __builtin_choose_expr(is_constant(x),(x),(y))
+#else
+# define if_constant(x,y) (y)
+#endif
+
+/*
* The autoconf documentation states:
*
* `va_copy'
diff --git a/include/nasmlib.h b/include/nasmlib.h
index 66fbebcf..14f4dc7f 100644
--- a/include/nasmlib.h
+++ b/include/nasmlib.h
@@ -114,33 +114,61 @@ void nasm_read(void *, size_t, FILE *);
void nasm_write(const void *, size_t, FILE *);
/*
- * NASM assert failure
- */
-fatal_func nasm_assert_failed(const char *, int, const char *);
-#define nasm_assert(x) \
- do { \
- if (unlikely(!(x))) \
- nasm_assert_failed(__FILE__,__LINE__,#x); \
- } while (0)
-
-/*
* NASM failure at build time if the argument is false
*/
#ifdef static_assert
-# define nasm_static_assert(x) static_assert(x, #x)
+# define nasm_static_assert(x) static_assert((x), #x)
#elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
-# define nasm_static_assert(x) \
- if (!(x)) { \
- extern void __attribute__((error("assertion " #x " failed"))) \
- _nasm_static_fail(void); \
- _nasm_static_fail(); \
- }
+# define nasm_static_assert(x) \
+ do { \
+ if (!(x)) { \
+ extern void __attribute__((error("assertion " #x " failed"))) \
+ _nasm_static_fail(void); \
+ _nasm_static_fail(); \
+ } \
+ } while (0)
#else
/* See http://www.drdobbs.com/compile-time-assertions/184401873 */
# define nasm_static_assert(x) \
do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
#endif
+/*
+ * conditional static assert, if we know it is possible to determine
+ * the assert value at compile time. Since if_constant triggers
+ * pedantic warnings on gcc, turn them off explicitly around this code.
+ */
+#ifdef static_assert
+# define nasm_try_static_assert(x) \
+ do { \
+ not_pedantic_start \
+ static_assert(if_constant(x, true), #x); \
+ not_pedantic_end \
+ } while (0)
+#elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
+# define nasm_try_static_assert(x) \
+ do { \
+ if (!if_constant(x, true)) { \
+ extern void __attribute__((error("assertion " #x " failed"))) \
+ _nasm_static_fail(void); \
+ _nasm_static_fail(); \
+ } \
+ } while (0)
+#else
+# define nasm_try_static_assert(x) ((void)0)
+#endif
+
+/*
+ * NASM assert failure
+ */
+fatal_func nasm_assert_failed(const char *, int, const char *);
+#define nasm_assert(x) \
+ do { \
+ nasm_try_static_assert(x); \
+ if (unlikely(!(x))) \
+ nasm_assert_failed(__FILE__,__LINE__,#x); \
+ } while (0)
+
/* Utility function to generate a string for an invalid enum */
const char *invalid_enum_str(int);