Skip to content

Enums aren't cast from ints in macros with translate-c #5656

@shtanton

Description

@shtanton
Contributor

macro.h:

#define noargs someargs(Flag1)

typedef enum {
    Flag0,
    Flag1
} Flags;

int someargs(Flags flags);

macro.c:

typedef enum {
    Flag0,
    Flag1
} Flags;

int someargs(Flags flags) {
    if (flags == Flag1) {
        return 1;
    } else {
        return 0;
    }
}

test.zig:

const c = @cImport({
    @cInclude("macro.h");
});

pub fn main() !void {
    _ = c.noargs;
}

Then compile with zig build-exe test.zig --c-source macro.c -lc -I. and the error

zig-cache/o/INfBWWWMN1znJ7cpk1aHFpWrWqAEwqd-Uefh0pPlK7Sau8WYxYrtuxOF09n_V9xa/cimport.zig:225:35: error: expected type '.cimport:2:11.enum_unnamed_1', found 'c_int'

is produced.

As far as I can tell this is only a problem on the right hand side of c macros, in other places @intToEnum is inserted to make it work. I don't know why expressions in macros are handled differently but it's probably causing other bugs too.

Activity

Vexu

Vexu commented on Jun 27, 2020

@Vexu
Member

Macros are translated separately from the rest of the code and have no type info available which is why the cast is not being inserted. Fixing this would require finding the types of the function and the argument (possibly through multiple macros) and casting based on that.

added
translate-cC to Zig source translation feature (@cImport)
on Jun 27, 2020
added this to the 0.8.0 milestone on Jun 27, 2020
modified the milestones: 0.8.0, 0.10.0 on May 19, 2021
linked a pull request that will close this issuetranslate-c: Remove usage of `extern enum` #9164on Jun 22, 2021
modified the milestones: 0.10.0, 0.9.0 on Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    translate-cC to Zig source translation feature (@cImport)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @andrewrk@Vexu@shtanton

      Issue actions

        Enums aren't cast from ints in macros with translate-c · Issue #5656 · ziglang/zig