PMunch/futhark

Simple define creates a procedure that is annotated with varargs.

beef331 opened this issue · 4 comments

#define own
own int* wasm_engine_new();
import futhark

importc:
  sysPath "/usr/lib/clang/13.0.1/include"
  path "./"
  "test.h"
discard wasmEngineNew()

Creates a wasmEngineNew annotated with varags and on usage of this procedure it errors in the C compiler.

Looking into it isFunctionTypeVariadic seems to return 1 on the above procedure, causing this issue. Perhaps it's a bug with libclang?

Hmm, yeah this seems to be a bug with Clang. Not entirely sure if there's anything we can do to fix it from the Nim side of things. Although it would be possible to run Futhark in two steps and insert a small replacement in the json step between those.

PMunch commented

After having raised this with the Clang developers it seems like this is actually a correct interpretation of the C code. Specifically K&R style function definitions, or pre-ANSI C function definitions. You can read more about the nuance here: https://jameshfisher.com/2016/11/27/c-k-and-r/. I guess it's actually a bug in what Nim does with the varargs pragma given an empty argument list though. But I think it would be pretty safe to default to not adding the varargs pragma when the type is FunctionNoProto and instead put the old behaviour behind a switch for those trying to wrap pre-ANSI C libraries.

PMunch commented

Interestingly enough it seems like it was actually a bug that the macro had to be there for this behaviour to trigger. Testing this with clang 14.0.6 and above it seems like only the line int* wasm_engine_new(); is enough to cause it to be regarded as varargs. While int* wasm_engine_new(void); is correctly identified as a non-varargs definition.