dstep icon indicating copy to clipboard operation
dstep copied to clipboard

Zero argument function pointers not working

Open helikopterodaktyl opened this issue 5 years ago • 6 comments

It looks like DStep can't handle function pointers with no arguments. Tested on DStep 1.0 on Windows with latest LLVM. For an input header file that consists of:

typedef void (*fooCallback)();
typedef void (*barCallback)(int f);

The resulting D file is:

extern (C):

alias fooCallback = <unimplemented>*;
alias barCallback = void function (int f);

I'd expect the output to have alias fooCallback = void function();

instead.

helikopterodaktyl avatar May 02 '19 12:05 helikopterodaktyl

There is no reliable way to translate fooCallback to D. In D, a function declaration with zero parameters is a function that doesn't take any arguments. In C, that is a function that have not specified the number of arguments. To specify a function declaration that doesn't take any arguments in C, void is used. The following D code:

alias fooCallback = void function();

Would correspond to the following C code:

typedef void (*fooCallback)(void);

jacob-carlborg avatar May 02 '19 17:05 jacob-carlborg

I see. I actually never heard of that feature of C, it sounds like something that would occur in very old versions of C and something that you wouldn't see in a modern code. I rarely see code doing explicitly (void).

Perhaps it should be assumed that the author meant function that doesn't take any arguments, and the original behavior should be hidden behind a switch, something like --ignore-noarg-functions ?

helikopterodaktyl avatar May 02 '19 18:05 helikopterodaktyl

I see. I actually never heard of that feature of C, it sounds like something that would occur in very old versions of C and something that you wouldn't see in a modern code

Which feature? Explicit void or zero argument function declarations?

I rarely see code doing explicitly (void)

Really? I would say it's the opposite. Are you reading C++ code? In C++ void foo() and void foo(void) are the same, not in C.

Perhaps it should be assumed that the author meant function that doesn't take any arguments, and the original behavior should be hidden behind a switch, something like --ignore-noarg-functions ?

Actually, DStep is already doing that for function declarations. But apparently it doesn't work for function pointers.

See this as well: https://stackoverflow.com/questions/5929711/c-function-with-no-parameters-behavior.

jacob-carlborg avatar May 02 '19 18:05 jacob-carlborg

Actually, DStep is already doing that for function declarations. But apparently it doesn't work for function pointers.

Do you know if it's valid? E.g. if it works if I have C vararg function f, declare it as 0 arguments function at the D side and call it with zero arguments at D side. Will such a call be valid?

The same for function pointer.

ciechowoj avatar May 05 '19 18:05 ciechowoj

Will such a call be valid?

I have no idea. But DStep has a flag, --zero-param-is-vararg, to treat it as vararg.

jacob-carlborg avatar May 05 '19 18:05 jacob-carlborg

Do you know if it's valid?

I'm guessing it depends on how many arguments the actual function definition expects. This example compiles and runs with Clang without any warnings or errors:

#include <stdio.h>

void func();

int main() {
    func("foo");
    func(3);
    func(3.5, 4);
    return 0;
}

void func() {
    printf("in func()\n");
}

If you compile the above with -Weverything you do get warnings.

jacob-carlborg avatar May 05 '19 18:05 jacob-carlborg