Improve constants handling

Handle char constants defined using #define or %constant correctly.

Also handle static member const variables, which didn't work at all
before, and quote values of such variables of char type.
This commit is contained in:
Vadim Zeitlin 2019-08-04 17:47:54 +02:00
commit 8f1b9dbbc5

View file

@ -1522,7 +1522,32 @@ public:
virtual int constantWrapper(Node *n) {
String *name = Getattr(n, "sym:name");
String *value = Getattr(n, "value");
// If it's a #define or a %constant, use raw value and hope that it will work in C as well as in C++. This is not ideal, but using "value" is even worse, as
// it doesn't even work for simple char constants such as "#define MY_X 'x'", that would end up unquoted in the generated code.
String *value = Getattr(n, "rawval");
if (!value) {
// Check if it's not a static member variable because its "value" is a reference to a C++ variable and won't translate to C correctly.
//
// Arguably, those should be handled in overridden memberconstantHandler() and not here.
value = Getattr(n, "staticmembervariableHandler:value");
if (value && Equal(Getattr(n, "valuetype"), "char")) {
// We need to quote this value.
const unsigned char c = *Char(value);
Clear(value);
if (isalnum(c)) {
Printf(value, "'%c'", c);
} else {
Printf(value, "'\\x%x%x'", c / 0x10, c % 0x10);
}
}
}
if (!value) {
// Fall back on whatever SWIG parsed the value as for all the rest.
value = Getattr(n, "value");
}
Printv(f_wrappers_decl, "#define ", name, " ", value, "\n", NIL);
return SWIG_OK;
}