Improve assertions.

This commit is contained in:
Mike Pall
2020-06-13 00:52:54 +02:00
parent 8b55054473
commit 8ae5170cdc
71 changed files with 1363 additions and 927 deletions

View File

@@ -28,6 +28,12 @@
** If in doubt, please check the input against your favorite C compiler.
*/
#ifdef LUA_USE_ASSERT
#define lj_assertCP(c, ...) (lj_assertG_(G(cp->L), (c), __VA_ARGS__))
#else
#define lj_assertCP(c, ...) ((void)cp)
#endif
/* -- Miscellaneous ------------------------------------------------------- */
/* Match string against a C literal. */
@@ -61,7 +67,7 @@ LJ_NORET static void cp_err(CPState *cp, ErrMsg em);
static const char *cp_tok2str(CPState *cp, CPToken tok)
{
lua_assert(tok < CTOK_FIRSTDECL);
lj_assertCP(tok < CTOK_FIRSTDECL, "bad CPToken %d", tok);
if (tok > CTOK_OFS)
return ctoknames[tok-CTOK_OFS-1];
else if (!lj_char_iscntrl(tok))
@@ -392,7 +398,7 @@ static void cp_init(CPState *cp)
cp->curpack = 0;
cp->packstack[0] = 255;
lj_buf_init(cp->L, &cp->sb);
lua_assert(cp->p != NULL);
lj_assertCP(cp->p != NULL, "uninitialized cp->p");
cp_get(cp); /* Read-ahead first char. */
cp->tok = 0;
cp->tmask = CPNS_DEFAULT;
@@ -853,12 +859,13 @@ static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl)
/* The cid is already part of info for copies of pointers/functions. */
idx = ct->next;
if (ctype_istypedef(info)) {
lua_assert(id == 0);
lj_assertCP(id == 0, "typedef not at toplevel");
id = ctype_cid(info);
/* Always refetch info/size, since struct/enum may have been completed. */
cinfo = ctype_get(cp->cts, id)->info;
csize = ctype_get(cp->cts, id)->size;
lua_assert(ctype_isstruct(cinfo) || ctype_isenum(cinfo));
lj_assertCP(ctype_isstruct(cinfo) || ctype_isenum(cinfo),
"typedef of bad type");
} else if (ctype_isfunc(info)) { /* Intern function. */
CType *fct;
CTypeID fid;
@@ -891,7 +898,7 @@ static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl)
/* Inherit csize/cinfo from original type. */
} else {
if (ctype_isnum(info)) { /* Handle mode/vector-size attributes. */
lua_assert(id == 0);
lj_assertCP(id == 0, "number not at toplevel");
if (!(info & CTF_BOOL)) {
CTSize msize = ctype_msizeP(decl->attr);
CTSize vsize = ctype_vsizeP(decl->attr);
@@ -946,7 +953,7 @@ static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl)
info = (info & ~CTF_ALIGN) | (cinfo & CTF_ALIGN);
info |= (cinfo & CTF_QUAL); /* Inherit qual. */
} else {
lua_assert(ctype_isvoid(info));
lj_assertCP(ctype_isvoid(info), "bad ctype %08x", info);
}
csize = size;
cinfo = info+id;
@@ -1585,7 +1592,7 @@ end_decl:
cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC);
sz = sizeof(int);
}
lua_assert(sz != 0);
lj_assertCP(sz != 0, "basic ctype with zero size");
info += CTALIGN(lj_fls(sz)); /* Use natural alignment. */
info += (decl->attr & CTF_QUAL); /* Merge qualifiers. */
cp_push(decl, info, sz);
@@ -1845,7 +1852,7 @@ static void cp_decl_multi(CPState *cp)
/* Treat both static and extern function declarations as extern. */
ct = ctype_get(cp->cts, ctypeid);
/* We always get new anonymous functions (typedefs are copied). */
lua_assert(gcref(ct->name) == NULL);
lj_assertCP(gcref(ct->name) == NULL, "unexpected named function");
id = ctypeid; /* Just name it. */
} else if ((scl & CDF_STATIC)) { /* Accept static constants. */
id = cp_decl_constinit(cp, &ct, ctypeid);
@@ -1902,7 +1909,7 @@ static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud)
cp_decl_single(cp);
if (cp->param && cp->param != cp->L->top)
cp_err(cp, LJ_ERR_FFI_NUMPARAM);
lua_assert(cp->depth == 0);
lj_assertCP(cp->depth == 0, "unbalanced cparser declaration depth");
return NULL;
}