dstep icon indicating copy to clipboard operation
dstep copied to clipboard

const attribute is removed from parameter of a function pointer

Open joakim-noah opened this issue 10 years ago • 9 comments

I ran into this when translating the following:

typedef void (*AStorageManager_obbCallbackFunc)(const char* filename, const int32_t state, void* data);

DStep turned it into this, removing the const for the second parameter:

alias void function (const(char)*, int, void*) AStorageManager_obbCallbackFunc;

I haven't checked to see if dmd will actually complain about this, but it strikes me as a bug.

joakim-noah avatar Aug 23 '14 08:08 joakim-noah

DStep turned it into this, removing the const for the second parameter

This looks a bit odd. But according to libclang, which DStep uses under the hood, state is not const.

I haven't checked to see if dmd will actually complain about this

Why would DMD complain about this?

jacob-carlborg avatar Aug 23 '14 10:08 jacob-carlborg

Hmm, if I remove the typedef, i.e. during it into a variable declaration, libclang will see it as const.

jacob-carlborg avatar Aug 23 '14 10:08 jacob-carlborg

I've asked about this on the Clang mailing list.

jacob-carlborg avatar Aug 23 '14 11:08 jacob-carlborg

Why would DMD complain about this?

Because the callback signature doesn't match the C library? I've had that happen in other situations, not sure if it would here. If you mean that it's the linker that normally catches stuff like this, sure.

Hmm, if I remove the typedef, i.e. during it into a variable declaration, libclang will see it as const.

Yeah, if you turn it into an actual function declaration, it sees the parameter as const, just not when it's a function pointer.

joakim-noah avatar Aug 23 '14 11:08 joakim-noah

Because the callback signature doesn't match the C library? I've had that happen in other situations, not sure if it would here. If you mean that it's the linker that normally catches stuff like this, sure.

Right, when you use the function pointer. Yes, then DMD will complain.

Yeah, if you turn it into an actual function declaration, it sees the parameter as const, just not when it's a function pointer.

It's not the function pointer that is the problem, it's the typedef.

jacob-carlborg avatar Aug 23 '14 11:08 jacob-carlborg

I got a reply [1] from the mailing list and this is apparently how C behaves. I'm not sure that I can do anything about it.

[1] http://clang-developers.42468.n3.nabble.com/const-stripped-in-typedefs-for-parameters-of-primitive-type-td4041337.html

jacob-carlborg avatar Aug 24 '14 09:08 jacob-carlborg

Perhaps D should allow the callbacks anyway since const doesn't really matter for value types.

jacob-carlborg avatar Aug 25 '14 06:08 jacob-carlborg

I have no real inclination on what to do here, I wasn't even sure it was a bug. I just reported it as I was double-checking the translation and it seemed like it might be.

joakim-noah avatar Aug 27 '14 10:08 joakim-noah

It behaves as it should according to C. But that will cause problems on the D side. I really, really don't want to parse C code without the help of Clang.

jacob-carlborg avatar Aug 27 '14 15:08 jacob-carlborg