Skip to content

Commit

Permalink
M_STATIC_ASSERT: provide expression compatible
Browse files Browse the repository at this point in the history
macro even in pure C11 mode
(Remove GCC extension)

Add M_OPL_AS_STR to display the oplist conditionnaly as the generated string may be too big for some compilers.

Translate some M_STATIC_FAILURE into M_STATIC_ASSERT
as M_STATIC_FAILURE is not an expression whereas M_STATIC_ASSERT is an expression.
  • Loading branch information
P-p-H-d committed Nov 4, 2023
1 parent 703ab6f commit 3a057fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8562,6 +8562,9 @@ M\_USE\_DECL / M\_USE\_DEF shall be defined without effective value.
See M\_USE\_DECL for more details.
#### M\_USE\_PRINT\_OPLIST
If defined, it displays the oplist associated to the static assert.
#### M\_MEMORY\_ALLOC
Expand Down
48 changes: 29 additions & 19 deletions m-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,13 @@ M_BEGIN_PROTECTED_CODE
/* Terminate the compilation of the current unit with an error message.
The error parameter is a C token which classifies the error
with an optional message detailling the error.
Either use C11 to get a proper message, or at least a good hint in C99.
error shall be a C name, msg a string.
It is not an expression and shall not be used in an expression.
It shall be used outside of a function, therefore it is not an expression.
In C99, it uses a bitfield to be compatible with most compilers
(so that it properly displays 'error' on the command line
Quite usefull to terminate with a proper error message rather than
a garbage of error due to incorrect code generation in the methods
expansion.
NOTE: Some implementation claims to be C11 (default mode) but fails
to deliver a working assert.h with static_assert so we test for the
explicity precense of the macro static_assert.
*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)) || defined(__cplusplus)
# define M_STATIC_FAILURE(error, msg) static_assert(false, #error ": " msg);
Expand All @@ -469,19 +468,22 @@ M_BEGIN_PROTECTED_CODE
/* Test at compile time if the given condition is true.
The error parameter is a C token which classifies the error
with an optional message detailling the error.
NOTE: Use bitfield to be compatible with most compilers
In C99, it uses a bitfield to be compatible with most compilers
(so that it properly displays 'error' on the command line
It cannot use C11 Static Assert as is not usable in expression.
NOTE: In C++, use of lambda to encapsulate static_assert in
an expression.
NOTE: For GNU compatible compilers, uses of a GNU extension.
C11 static Assert is not usable in expression,
but is usable in struct declaration. Therefore we create
a dummy struct, define the static assert within it
compute its size and cast it to void.
NOTE: Some implementation claims to be C11 (default mode) but fails
to deliver a working assert.h with static_assert so we test for the
explicity precense of the macro static_assert.
*/
#if defined(__cplusplus)
#ifdef __cplusplus
# define M_STATIC_ASSERT(cond, error, msg) \
([] { static_assert(cond, #error ": " msg); } ())
#elif defined(__GNUC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)
# define M_STATIC_ASSERT(cond, error, msg) \
M_ATTR_EXTENSION ({ static_assert(cond, #error ": " msg); })
((void) sizeof(struct { int error; static_assert(cond, #error ": " msg);}))
#else
# define M_STATIC_ASSERT(cond, error, msg) \
((void) sizeof(struct { int error : !!(cond);}))
Expand Down Expand Up @@ -2513,6 +2515,14 @@ done
#define M_AS_STR(x) M_AS_STRI(x)
#define M_AS_STRI(x) #x

/* Convert an oplist into a string. It may generate a very long string,
which generates warning (string length greater than 4095).
So the conversion is optional */
#ifdef M_USE_PRINT_OPLIST
# define M_OPL_AS_STR(x) M_AS_STR(x)
#else
# define M_OPL_AS_STR(x)
#endif

/* C11 MACROS */

Expand Down Expand Up @@ -3610,7 +3620,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])

NOTE: It starts by a comma, to ack the success of the transformation.
*/
#define M_OPLAPI_ERROR(method, oplist, ...) , M_STATIC_FAILURE(M_LIB_DISABLED_METHOD, "The method has been explictly disabled for the requested operator, yet it has been called by the container")
#define M_OPLAPI_ERROR(method, oplist, ...) , M_STATIC_ASSERT(false, M_LIB_DISABLED_METHOD, "The method has been explictly disabled for the requested operator, yet it has been called by the container")
#define M_OPLAPI_0(method, oplist, ...) , M_DELAY3(method)(__VA_ARGS__)
#define M_OPLAPI_1(method, oplist, ...) , M_DELAY3(method)(oplist, __VA_ARGS__)
#define M_OPLAPI_2(method, oplist, ...) , M_DELAY3(method)(& __VA_ARGS__)
Expand Down Expand Up @@ -3730,11 +3740,11 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
if the method is expanded.
*/
#define M_NO_DEFAULT(...) \
M_STATIC_FAILURE(M_LIB_MISSING_METHOD, \
M_STATIC_ASSERT(false, M_LIB_MISSING_METHOD, \
"The requested operator has no method registered in the given OPLIST. ")

#define M_NO_DEF_TYPE \
M_STATIC_FAILURE(M_LIB_MISSING_METHOD, \
M_STATIC_ASSERT(false, M_LIB_MISSING_METHOD, \
"The requested operator has no type/subtype/suboplist registered in the given OPLIST. ")

/* Test if the given variable is a basic C variable:
Expand Down Expand Up @@ -3800,7 +3810,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
M_LIB_TYPE_MISTMACH, \
"The given type " M_AS_STR(type) \
" and the type of the oplist does not match: " \
M_AS_STR(oplist) ); \
M_OPL_AS_STR(oplist) ); \
} \
, /* End of TYPE */)
#else
Expand All @@ -3814,7 +3824,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
M_LIB_TYPE_MISTMACH, \
"The given type " M_AS_STR(type) \
" and the type of the oplist does not match: " \
M_AS_STR(oplist) ); \
M_OPL_AS_STR(oplist) ); \
} \
, /* End of TYPE */)
#endif
Expand Down Expand Up @@ -4310,7 +4320,7 @@ m_core_parse2_enum (const char str[], const char **endptr)
M_IF_OPLIST(oplist)(M_LETI3, M_LETI2_FAILURE)(cont, oplist, __VA_ARGS__)
// Stop with a failure (invalid oplist)
#define M_LETI2_FAILURE(cont, oplist, ...) \
M_STATIC_FAILURE(M_LIB_NOT_AN_OPLIST, "(M_LET): the given argument is not a valid oplist: " M_AS_STR(oplist))
M_STATIC_ASSERT(false, M_LIB_NOT_AN_OPLIST, "(M_LET): the given argument is not a valid oplist: " M_AS_STR(oplist))
// 4. Map all variables to their own LET
#define M_LETI3(cont, oplist, ...) \
for(bool cont = true; cont ; /* unused */) \
Expand Down

0 comments on commit 3a057fc

Please sign in to comment.