mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
translate-c: fix enums that require c_uint type
This commit is contained in:
parent
986a71234b
commit
b5c117d051
@ -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 c_int since msvc and gcc default to different types
|
||||
const init_arg_expr = if (int_type.ptr != null and
|
||||
!isCBuiltinType(int_type, .UInt) and
|
||||
!isCBuiltinType(int_type, .Int))
|
||||
const init_arg_expr = if (int_type.ptr != null)
|
||||
transQualType(c, scope, int_type, enum_loc) catch |err| switch (err) {
|
||||
error.UnsupportedType => {
|
||||
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
|
||||
// 2. Extend or truncate without changing 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_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);
|
||||
|
||||
if (isBoolRes(src_int_expr)) {
|
||||
|
||||
@ -1540,4 +1540,14 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
|
||||
\\ 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();
|
||||
\\}
|
||||
, "");
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ const std = @import("std");
|
||||
const CrossTarget = std.zig.CrossTarget;
|
||||
|
||||
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",
|
||||
\\unsigned long foo(unsigned long x) {
|
||||
\\ return ((union{unsigned long _x}){x})._x;
|
||||
@ -28,7 +30,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ int a, b;
|
||||
\\} Bar;
|
||||
, &[_][]const u8{
|
||||
\\pub const Foo = extern enum(c_int) {
|
||||
\\pub const Foo = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\ _,
|
||||
@ -118,7 +122,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
\\pub export fn foo() void {
|
||||
\\ const enum_Foo = extern enum(c_int) {
|
||||
\\ const enum_Foo = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\ C,
|
||||
@ -129,7 +135,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ const C = @enumToInt(enum_Foo.C);
|
||||
\\ 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,
|
||||
\\ B,
|
||||
\\ C,
|
||||
@ -1702,7 +1710,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ p,
|
||||
\\};
|
||||
, &[_][]const u8{
|
||||
\\pub const d = extern enum(c_int) {
|
||||
\\pub const d = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ a,
|
||||
\\ b,
|
||||
\\ c,
|
||||
@ -1711,7 +1721,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\pub const a = @enumToInt(d.a);
|
||||
\\pub const b = @enumToInt(d.b);
|
||||
\\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,
|
||||
\\ f = 4,
|
||||
\\ g = 5,
|
||||
@ -1721,7 +1733,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\pub const f = @enumToInt(enum_unnamed_1.f);
|
||||
\\pub const g = @enumToInt(enum_unnamed_1.g);
|
||||
\\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,
|
||||
\\ j,
|
||||
\\ k,
|
||||
@ -1734,7 +1748,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ l: enum_unnamed_2,
|
||||
\\ m: d,
|
||||
\\};
|
||||
\\pub const enum_i = extern enum(c_int) {
|
||||
\\pub const enum_i = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ n,
|
||||
\\ o,
|
||||
\\ p,
|
||||
@ -2234,7 +2250,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ Two,
|
||||
\\};
|
||||
, &[_][]const u8{
|
||||
\\const enum_unnamed_1 = extern enum(c_int) {
|
||||
\\const enum_unnamed_1 = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ One,
|
||||
\\ Two,
|
||||
\\ _,
|
||||
@ -2338,7 +2356,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
\\pub const enum_Foo = extern enum(c_int) {
|
||||
\\pub const enum_Foo = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\ C,
|
||||
@ -2387,7 +2407,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ y: c_int,
|
||||
\\};
|
||||
,
|
||||
\\pub const enum_Bar = extern enum(c_int) {
|
||||
\\pub const enum_Bar = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\ _,
|
||||
@ -2664,7 +2686,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ return 4;
|
||||
\\}
|
||||
, &[_][]const u8{
|
||||
\\pub const enum_SomeEnum = extern enum(c_int) {
|
||||
\\pub const enum_SomeEnum = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\ C,
|
||||
@ -3130,7 +3154,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ Foo1,
|
||||
\\};
|
||||
, &[_][]const u8{
|
||||
\\pub const enum_Foo = extern enum(c_int) {
|
||||
\\pub const enum_Foo = extern enum(
|
||||
++ default_enum_type ++
|
||||
\\) {
|
||||
\\ A = 2,
|
||||
\\ B = 5,
|
||||
\\ @"1" = 6,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user