futhark icon indicating copy to clipboard operation
futhark copied to clipboard

Simple define creates a procedure that is annotated with varargs.

Open beef331 opened this issue 3 years ago • 2 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.

beef331 avatar Aug 27 '22 04:08 beef331

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

beef331 avatar Aug 28 '22 02:08 beef331

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 avatar Aug 29 '22 08:08 PMunch

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 avatar Jan 31 '23 21:01 PMunch

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.

PMunch avatar Jan 31 '23 21:01 PMunch