[M3devel] assert in Modula-3 C code?
Jay K
jay.krell at cornell.edu
Tue Feb 8 15:06:34 CET 2011
It was recently suspect that asserts aren't enabled in the C code in m3core.h
I don't think so. I think there might just be a missing check of a return value in m3core/thread/*.m3
However, if we want to dispell this fear now and forever, we could do e.g.:
m3core.h:
M3_DLL_LOCAL
void
__stdcall
M3C__AssertFailed (const char* file, unsigned line, const char* expression);
#define M3_ASSERT(expr) ((expr) ? ((void)0) ? M3C__AssertFailed ( \
__FILE__, __LINE__, #expression))
hand.c:
M3_DLL_LOCAL
void
__stdcall
M3C__AssertFailed (const char* file, unsigned line, const char* expression)
{
fprintf (stderr, "%s(%u): assertion failed: %s\n", file, line, expression);
abort ();
}
This might slightly reduce quality on some platforms, e.g.:
Assertion failed: (0), function main, file 1.c, line 2.
Abort trap
jbook2:libm3 jay$ cat 1.c
#include <assert.h>
int main() { assert(0); return 0; }
that is -- see how it printed the file name? Anyway, we could get that back too, like
#if __GNUC__ > whatever
#define M3_FUNCTION something
#elif _MSC_VER > something
#define M3_FUNCTION __FUNCTION__
#else
#define M3_FUNCTION 0
#endif
?
But I think probably using standard assert is adequate or preferable.
We could #undef NDEBUG in m3core.h, in case someone added to the C compiler on the config files.
or #ifdef NDEBUG, #error.
- Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20110208/2dadf4ae/attachment-0001.html>
More information about the M3devel
mailing list