Hack around parser bug with char enum element values

Enum element somehow lose the single quotes around them, compensate for it in
C module code (other modules use module-specific constvalue feature to work
around it, but it seems better to do it in SWIG itself rather than leaving the
user code to deal with it).

This finally makes the "enums" unit test pass.
This commit is contained in:
Vadim Zeitlin 2016-04-16 00:25:50 +02:00
commit 3d21bb2c96
3 changed files with 21 additions and 3 deletions

View file

@ -29,7 +29,6 @@ CPP_TEST_CASES := \
# The following tests are currently broken and need to be fixed.
FAILING_C_TESTS := \
arrays \
enums \
funcptr \
function_typedef \
lextype \

View file

@ -4,6 +4,7 @@
int main() {
assert(GlobalInstance == globalinstance1);
assert(Char == 'a');
bar2(1);
bar3(1);
bar1(1);

View file

@ -1752,13 +1752,31 @@ ready:
if (!GetFlag(n, "firstenumitem"))
Printv(f_wrappers_types, ",\n", NIL);
Printv(f_wrappers_types, cindent, Swig_name_mangle(Getattr(n, "value")), NIL);
String* const enumitemname = Getattr(n, "value");
Printv(f_wrappers_types, cindent, Swig_name_mangle(enumitemname), NIL);
// We only use "enumvalue", which comes from the input, and not "enumvalueex" synthesized by SWIG itself because C should use the correct value for the enum
// items without an explicit one anyhow (and "enumvalueex" can't be always used as is in C code for enum elements inside a class or even a namespace).
String *value = Getattr(n, "enumvalue");
if (value) {
Printv(f_wrappers_types, " = ", value, NIL);
String* const cvalue = Copy(value);
// Due to what seems to be a bug in SWIG parser, char values for enum elements lose their quotes, i.e.
//
// enum { x = 'a', y = '\x62' };
//
// in input results in value being just "a" or "\x62". Try to repair this brokenness.
if (*Char(value) == '\\') {
Push(cvalue, "'");
Append(cvalue, "'");
} else if (Len(value) == 1 && !Swig_symbol_clookup(enumitemname, NULL)) {
Push(cvalue, "'");
Append(cvalue, "'");
}
Printv(f_wrappers_types, " = ", cvalue, NIL);
Delete(cvalue);
}
Swig_restore(n);