From aa5a1105e838fbf2f9f918c2a544be6fa0c35e22 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Wed, 31 Jul 2024 09:22:25 -0700 Subject: [PATCH] aro_translate_c: do not translate atomic types --- lib/compiler/aro_translate_c.zig | 24 ++++++++++++++++++++++++ test/cases/translate_c/atomic types.c | 8 ++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/cases/translate_c/atomic types.c diff --git a/lib/compiler/aro_translate_c.zig b/lib/compiler/aro_translate_c.zig index cec10a301ab9..9de4ec52d474 100644 --- a/lib/compiler/aro_translate_c.zig +++ b/lib/compiler/aro_translate_c.zig @@ -78,6 +78,17 @@ fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: ZigNode) !void { } } +fn fail( + c: *Context, + err: anytype, + source_loc: TokenIndex, + comptime format: []const u8, + args: anytype, +) (@TypeOf(err) || error{OutOfMemory}) { + try warn(c, &c.global_scope.base, source_loc, format, args); + return err; +} + fn failDecl(c: *Context, loc: TokenIndex, name: []const u8, comptime format: []const u8, args: anytype) Error!void { // location // pub const name = @compileError(msg); @@ -687,8 +698,21 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const Type.Enum, field_ } } +fn getTypeStr(c: *Context, ty: Type) ![]const u8 { + var buf: std.ArrayListUnmanaged(u8) = .{}; + defer buf.deinit(c.gpa); + const w = buf.writer(c.gpa); + try ty.print(c.mapper, c.comp.langopts, w); + return c.arena.dupe(u8, buf.items); +} + fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualHandling, source_loc: TokenIndex) TypeError!ZigNode { const ty = raw_ty.canonicalize(qual_handling); + if (ty.qual.atomic) { + const type_name = try getTypeStr(c, ty); + return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name}); + } + switch (ty.specifier) { .void => return ZigTag.type.create(c.arena, "anyopaque"), .bool => return ZigTag.type.create(c.arena, "bool"), diff --git a/test/cases/translate_c/atomic types.c b/test/cases/translate_c/atomic types.c new file mode 100644 index 000000000000..ad1af598c424 --- /dev/null +++ b/test/cases/translate_c/atomic types.c @@ -0,0 +1,8 @@ +typedef _Atomic(int) AtomicInt; + +// translate-c +// target=x86_64-linux +// c_frontend=aro +// +// tmp.c:1:22: warning: unsupported type: '_Atomic(int)' +// pub const AtomicInt = @compileError("unable to resolve typedef child type");