#216 [cffi] Fix CFFI conversion of constant expressions


CFFI converts constant expressions in #define'd constants and in enumeration items into equivalent lisp expressions. This patch (hopefully) fixes this conversion.
This patch implements a simple naive parser for constant expressions (replacing even more naive approach with a more correct one), memoization of previously met constant values (so that code like
enum { A = 123, B = A + 10 };
would translate into correct lisp expression). Still broken things are: not supporting ternary operator, somewhat wrong operators precedence order and associativity, but fixing those would require a full parser.
Also, one small fix goes into SWIG's parser: it would incorrectly parse character constant expressions in enumeration items: in
enum { A = '1' };
for '1' it would get node with enumvalue="1", type="int" instead of enumvalue="'1'", type="int", which makes correct parsing of such expressions impossible. Not fixed is parsing of expressions like
enum { A = '1' + 1 };
which would parse into enumvalue="1+1", type="int" instead of enumvalue="'1'+1", type="int" (again, which makes correct generation of lisp-side code impossible).


  • Dmitry Kalyanov

    Dmitry Kalyanov - 2008-09-07

    Logged In: YES
    Originator: YES

    Attaching a better variant of patch.
    1) Using custom macro define-constant instead of cl:defconstant (see http://www.sbcl.org/manual/Defining-Constants.html\)
    2) Better handling of character constants and arithemitcs with them
    3) Better handling of escape sequences in characters and strings;
    4) Safe way of specifying string literals with non-ascii characters.
    5) Change to c parser to retain the single quotes in character constants. This probably fixes some problems not only with cffi, but with some other modules.
    File Added: swig-full.patch

  • Dmitry Kalyanov

    Dmitry Kalyanov - 2008-09-07

    Version 2 of the patch

  • Olly Betts

    Olly Betts - 2008-09-17

    I don't have the time to check right now, but the fix for character constants sounds like this bug:


    I applied a fix for that but had to revert it. I forget the exact details, but it was discussed on swig-dev at the time.

    Anyway, this patch could well fix that bug. Or it could have the same flaw my fix did (I've checked and it isn't the same change I made).


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks