From c849b894edbf5bb170721e836bbef514fb74d074 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 8 Aug 2020 10:39:26 +0900 Subject: Reintroduce `mrb_static_assert`; #5051 Note that the home brew version of `mrb_static_assert` only works within the function body. This reverts commit 8f99689. --- include/mruby.h | 12 +++++++++--- src/gc.c | 3 +++ src/string.c | 3 +++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/mruby.h b/include/mruby.h index 5bbfbb1ad..5aced9ca7 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -62,11 +62,17 @@ #define mrb_assert_int_fit(t1,n,t2,max) ((void)0) #endif -#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L -#define mrb_static_assert(exp, str) _Static_assert(exp, str) +#if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L) || \ + (defined __cplusplus && __cplusplus >= 201103L) +# include +# define mrb_static_assert(exp, str) static_assert(exp, str) #else -#define mrb_static_assert(exp, str) +/* C version of static_assert() */ +# define mrb_static_assert(cond, str) \ + do { int assertion_failed[(cond) ? 1 : -1];\ + (void) assertion_failed; } while(0) #endif +#define mrb_static_assert1(exp) mrb_static_assert(exp, #exp) #include "mrbconf.h" diff --git a/src/gc.c b/src/gc.c index 64e0b3eae..be812c4d3 100644 --- a/src/gc.c +++ b/src/gc.c @@ -1619,6 +1619,9 @@ mrb_init_gc(mrb_state *mrb) { struct RClass *gc; + mrb_static_assert(sizeof(RVALUE) <= sizeof(void*) * 6, + "RVALUE size must be within 6 words"); + gc = mrb_define_module(mrb, "GC"); mrb_define_class_method(mrb, gc, "start", gc_start, MRB_ARGS_NONE()); diff --git a/src/string.c b/src/string.c index e8f81c0ae..78c41c5f3 100644 --- a/src/string.c +++ b/src/string.c @@ -2926,6 +2926,9 @@ mrb_init_string(mrb_state *mrb) { struct RClass *s; + mrb_static_assert(RSTRING_EMBED_LEN_MAX < (1 << MRB_STR_EMBED_LEN_BIT), + "pointer size too big for embedded string"); + mrb->string_class = s = mrb_define_class(mrb, "String", mrb->object_class); /* 15.2.10 */ MRB_SET_INSTANCE_TT(s, MRB_TT_STRING); -- cgit v1.2.3