translate-c: fix enums that require c_uint type

This commit is contained in:
xackus 2021-03-30 18:28:50 +02:00 committed by Veikka Tuominen
parent 986a71234b
commit b5c117d051
3 changed files with 186 additions and 152 deletions

View file

@ -1117,9 +1117,7 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) E
// default to the usual integer type used for all the enums. // default to the usual integer type used for all the enums.
// default to c_int since msvc and gcc default to different types // default to c_int since msvc and gcc default to different types
const init_arg_expr = if (int_type.ptr != null and const init_arg_expr = if (int_type.ptr != null)
!isCBuiltinType(int_type, .UInt) and
!isCBuiltinType(int_type, .Int))
transQualType(c, scope, int_type, enum_loc) catch |err| switch (err) { transQualType(c, scope, int_type, enum_loc) catch |err| switch (err) {
error.UnsupportedType => { error.UnsupportedType => {
return failDecl(c, enum_loc, name, "unable to translate enum tag type", .{}); return failDecl(c, enum_loc, name, "unable to translate enum tag type", .{});
@ -2285,8 +2283,8 @@ fn transCCast(
// 1. If src_type is an enum, determine the underlying signed int type // 1. If src_type is an enum, determine the underlying signed int type
// 2. Extend or truncate without changing signed-ness. // 2. Extend or truncate without changing signed-ness.
// 3. Bit-cast to correct signed-ness // 3. Bit-cast to correct signed-ness
const src_type_is_signed = cIsSignedInteger(src_type) or cIsEnum(src_type);
const src_int_type = if (cIsInteger(src_type)) src_type else cIntTypeForEnum(src_type); const src_int_type = if (cIsInteger(src_type)) src_type else cIntTypeForEnum(src_type);
const src_type_is_signed = cIsSignedInteger(src_int_type);
var src_int_expr = if (cIsInteger(src_type)) expr else try Tag.enum_to_int.create(c.arena, expr); var src_int_expr = if (cIsInteger(src_type)) expr else try Tag.enum_to_int.create(c.arena, expr);
if (isBoolRes(src_int_expr)) { if (isBoolRes(src_int_expr)) {

View file

@ -1540,4 +1540,14 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0; \\ return 0;
\\} \\}
, ""); , "");
cases.add("enum with value that fits in c_uint but not c_int, issue #8003",
\\#include <stdlib.h>
\\enum my_enum {
\\ FORCE_UINT = 0xffffffff
\\};
\\int main(void) {
\\ if(FORCE_UINT != 0xffffffff) abort();
\\}
, "");
} }

View file

@ -3,6 +3,8 @@ const std = @import("std");
const CrossTarget = std.zig.CrossTarget; const CrossTarget = std.zig.CrossTarget;
pub fn addCases(cases: *tests.TranslateCContext) void { pub fn addCases(cases: *tests.TranslateCContext) void {
const default_enum_type = if (std.Target.current.abi == .msvc) "c_int" else "c_uint";
cases.add("field access is grouped if necessary", cases.add("field access is grouped if necessary",
\\unsigned long foo(unsigned long x) { \\unsigned long foo(unsigned long x) {
\\ return ((union{unsigned long _x}){x})._x; \\ return ((union{unsigned long _x}){x})._x;
@ -28,7 +30,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ int a, b; \\ int a, b;
\\} Bar; \\} Bar;
, &[_][]const u8{ , &[_][]const u8{
\\pub const Foo = extern enum(c_int) { \\pub const Foo = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ _, \\ _,
@ -118,7 +122,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\} \\}
, &[_][]const u8{ , &[_][]const u8{
\\pub export fn foo() void { \\pub export fn foo() void {
\\ const enum_Foo = extern enum(c_int) { \\ const enum_Foo = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ C, \\ C,
@ -129,7 +135,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ const C = @enumToInt(enum_Foo.C); \\ const C = @enumToInt(enum_Foo.C);
\\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B); \\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B);
\\ { \\ {
\\ const enum_Foo = extern enum(c_int) { \\ const enum_Foo = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ C, \\ C,
@ -1702,7 +1710,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ p, \\ p,
\\}; \\};
, &[_][]const u8{ , &[_][]const u8{
\\pub const d = extern enum(c_int) { \\pub const d = extern enum(
++ default_enum_type ++
\\) {
\\ a, \\ a,
\\ b, \\ b,
\\ c, \\ c,
@ -1711,7 +1721,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const a = @enumToInt(d.a); \\pub const a = @enumToInt(d.a);
\\pub const b = @enumToInt(d.b); \\pub const b = @enumToInt(d.b);
\\pub const c = @enumToInt(d.c); \\pub const c = @enumToInt(d.c);
\\const enum_unnamed_1 = extern enum(c_int) { \\const enum_unnamed_1 = extern enum(
++ default_enum_type ++
\\) {
\\ e = 0, \\ e = 0,
\\ f = 4, \\ f = 4,
\\ g = 5, \\ g = 5,
@ -1721,7 +1733,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const f = @enumToInt(enum_unnamed_1.f); \\pub const f = @enumToInt(enum_unnamed_1.f);
\\pub const g = @enumToInt(enum_unnamed_1.g); \\pub const g = @enumToInt(enum_unnamed_1.g);
\\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e); \\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e);
\\const enum_unnamed_2 = extern enum(c_int) { \\const enum_unnamed_2 = extern enum(
++ default_enum_type ++
\\) {
\\ i, \\ i,
\\ j, \\ j,
\\ k, \\ k,
@ -1734,7 +1748,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ l: enum_unnamed_2, \\ l: enum_unnamed_2,
\\ m: d, \\ m: d,
\\}; \\};
\\pub const enum_i = extern enum(c_int) { \\pub const enum_i = extern enum(
++ default_enum_type ++
\\) {
\\ n, \\ n,
\\ o, \\ o,
\\ p, \\ p,
@ -2234,7 +2250,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ Two, \\ Two,
\\}; \\};
, &[_][]const u8{ , &[_][]const u8{
\\const enum_unnamed_1 = extern enum(c_int) { \\const enum_unnamed_1 = extern enum(
++ default_enum_type ++
\\) {
\\ One, \\ One,
\\ Two, \\ Two,
\\ _, \\ _,
@ -2338,7 +2356,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p); \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
\\} \\}
, &[_][]const u8{ , &[_][]const u8{
\\pub const enum_Foo = extern enum(c_int) { \\pub const enum_Foo = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ C, \\ C,
@ -2387,7 +2407,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ y: c_int, \\ y: c_int,
\\}; \\};
, ,
\\pub const enum_Bar = extern enum(c_int) { \\pub const enum_Bar = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ _, \\ _,
@ -2664,7 +2686,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ return 4; \\ return 4;
\\} \\}
, &[_][]const u8{ , &[_][]const u8{
\\pub const enum_SomeEnum = extern enum(c_int) { \\pub const enum_SomeEnum = extern enum(
++ default_enum_type ++
\\) {
\\ A, \\ A,
\\ B, \\ B,
\\ C, \\ C,
@ -3130,7 +3154,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ Foo1, \\ Foo1,
\\}; \\};
, &[_][]const u8{ , &[_][]const u8{
\\pub const enum_Foo = extern enum(c_int) { \\pub const enum_Foo = extern enum(
++ default_enum_type ++
\\) {
\\ A = 2, \\ A = 2,
\\ B = 5, \\ B = 5,
\\ @"1" = 6, \\ @"1" = 6,