Discussion:
[BUG] LuaJIT cdef parser: C99 array parameter size hints aren't supported
Tomash Brechko
2014-09-18 14:18:44 UTC
Permalink
Hello!

According to documentation LuaJIT parser complies to C99 standard, however
the following code fails:

ffi = require("ffi")
ffi.cdef[[ void f(int size, char a[size]); ]]

or

ffi = require("ffi")
ffi.cdef[[ void g(char b[static 5]); ]]

Both are optimizer hints meaning that 'a' has exactly 'size' elements, and
'b' is at least 5 elements long (IIRC). Since those are hints LuaJIT may
parse and ignore them, so the fix shouldn't be complex.

Tested with 2.0 and 2.1. Sorry if this has been discussed before.
--
Tomash Brechko
Mike Pall
2014-09-18 15:14:00 UTC
Permalink
Post by Tomash Brechko
According to documentation LuaJIT parser complies to C99 standard, however
ffi = require("ffi")
ffi.cdef[[ void f(int size, char a[size]); ]]
or
ffi = require("ffi")
ffi.cdef[[ void g(char b[static 5]); ]]
Both are optimizer hints meaning that 'a' has exactly 'size' elements, and
'b' is at least 5 elements long (IIRC). Since those are hints LuaJIT may
parse and ignore them, so the fix shouldn't be complex.
Ignoring them wouldn't be difficult. But this leads to the problem
where the name of an enum constant is misspelled and ignored,
which changes the size of the type. E.g. 'int a[MAX_WITDH]' would
then be interpreted as 'int a[]'. That's an accident just waiting
to happen ...

The parser is not context-aware at this level. It doesn't know
it's dealing with an array declaration inside a function
declaration, so it can't check the preceding parameter names.

In other words: it's not so easy to do it right. Given that these
C99isms are rarely found in the wild and easy to fix while
cut'n'pasting, I'd rather not add a halfhearted solution.

--Mike
Tomash Brechko
2014-09-18 15:26:31 UTC
Permalink
Post by Mike Pall
Ignoring them wouldn't be difficult. But this leads to the problem
where the name of an enum constant is misspelled and ignored,
which changes the size of the type. E.g. 'int a[MAX_WITDH]' would
then be interpreted as 'int a[]'. That's an accident just waiting
to happen ...
I didn't mean blindly ignore everything between brackets. Misspelling is
not an argument IMHO because many things might go wrong when something is
misspelled. Isaid "parse and ignore", but should said "parse, validate,
and then ignore". For function call 'int a[MAX_WITDH]' is the same as 'int
a[]' - essentially a non-NULL pointer to an int. But context-free parser
is a different story. Even if the construct is rare the lack of support
limits what header files you may feed to cdef automatically (i.e. without
human intervention). But I get the point, thanks for reply.
--
Tomash Brechko
Loading...