authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-20 13:32:07+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-20 13:32:07+02:00
log4074e79748ad9ecc39a4127cd1c28c115efff56a
tree4cbf2a7ba6e12926aa9be0e16a2ff8819ee82bcc
parent669c2054a83cac594e5bbca4a7ca677e3b3a1a0d
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: use global scope for typedef/record/enum type translation if needed

If the type is a reference to a global declaration that has not yet been translated we need to use the global scope for translation so that other functions can also reference it.

3 files changed, 37 insertions(+), 5 deletions(-)

src/clang.zig-2
......@@ -697,8 +697,6 @@ pub const ReturnStmt = opaque {
697697 extern fn ZigClangReturnStmt_getRetValue(*const ReturnStmt) ?*const Expr;
698698};
699699
700pub const SkipFunctionBodiesScope = opaque {};
701
702700pub const SourceManager = opaque {
703701 pub const getSpellingLoc = ZigClangSourceManager_getSpellingLoc;
704702 extern fn ZigClangSourceManager_getSpellingLoc(*const SourceManager, Loc: SourceLocation) SourceLocation;
src/translate_c.zig+18-3
......@@ -3741,7 +3741,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
37413741 const typedef_ty = @ptrCast(*const clang.TypedefType, ty);
37423742
37433743 const typedef_decl = typedef_ty.getDecl();
3744 try transTypeDef(c, scope, typedef_decl);
3744 var trans_scope = scope;
3745 if (@ptrCast(*const clang.Decl, typedef_decl).castToNamedDecl()) |named_decl| {
3746 const decl_name = try c.str(named_decl.getName_bytes_begin());
3747 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3748 }
3749 try transTypeDef(c, trans_scope, typedef_decl);
37453750 const name = c.decl_table.get(@ptrToInt(typedef_decl.getCanonicalDecl())).?;
37463751 return Tag.identifier.create(c.arena, name);
37473752 },
......@@ -3749,7 +3754,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
37493754 const record_ty = @ptrCast(*const clang.RecordType, ty);
37503755
37513756 const record_decl = record_ty.getDecl();
3752 try transRecordDecl(c, scope, record_decl);
3757 var trans_scope = scope;
3758 if (@ptrCast(*const clang.Decl, record_decl).castToNamedDecl()) |named_decl| {
3759 const decl_name = try c.str(named_decl.getName_bytes_begin());
3760 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3761 }
3762 try transRecordDecl(c, trans_scope, record_decl);
37533763 const name = c.decl_table.get(@ptrToInt(record_decl.getCanonicalDecl())).?;
37543764 return Tag.identifier.create(c.arena, name);
37553765 },
......@@ -3757,7 +3767,12 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
37573767 const enum_ty = @ptrCast(*const clang.EnumType, ty);
37583768
37593769 const enum_decl = enum_ty.getDecl();
3760 try transEnumDecl(c, scope, enum_decl);
3770 var trans_scope = scope;
3771 if (@ptrCast(*const clang.Decl, enum_decl).castToNamedDecl()) |named_decl| {
3772 const decl_name = try c.str(named_decl.getName_bytes_begin());
3773 if (c.global_names.get(decl_name)) |_| trans_scope = &c.global_scope.base;
3774 }
3775 try transEnumDecl(c, trans_scope, enum_decl);
37613776 const name = c.decl_table.get(@ptrToInt(enum_decl.getCanonicalDecl())).?;
37623777 return Tag.identifier.create(c.arena, name);
37633778 },
test/run_translated_c.zig+19
......@@ -3,6 +3,25 @@ const tests = @import("tests.zig");
33const nl = std.cstr.line_sep;
44
55pub fn addCases(cases: *tests.RunTranslatedCContext) void {
6 cases.add("use global scope for record/enum/typedef type transalation if needed",
7 \\void bar(void);
8 \\void baz(void);
9 \\struct foo { int x; };
10 \\void bar() {
11 \\ struct foo tmp;
12 \\}
13 \\
14 \\void baz() {
15 \\ struct foo tmp;
16 \\}
17 \\
18 \\int main(void) {
19 \\ bar();
20 \\ baz();
21 \\ return 0;
22 \\}
23 , "");
24
625 cases.add("failed macros are only declared once",
726 \\#define FOO =
827 \\#define FOO =