authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 20:23:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 20:23:36-04:00
logbf21747a426887ed2ac866c9a9d317f64b22da79
tree46cb3462bfbd66db3ebf6878a706c7b22d1d2ae6
parent116914ab3e72491c863ca8a062bd8fa184c01e04

translate-c: fix typedef duplicate definition of variable

closes #998

2 files changed, 27 insertions(+), 2 deletions(-)

src/translate_c.cpp+6-2
...@@ -3667,6 +3667,7 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_...@@ -3667,6 +3667,7 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_
3667 if (existing_entry) {3667 if (existing_entry) {
3668 return existing_entry->value;3668 return existing_entry->value;
3669 }3669 }
3670
3670 QualType child_qt = typedef_decl->getUnderlyingType();3671 QualType child_qt = typedef_decl->getUnderlyingType();
3671 Buf *type_name = buf_create_from_str(decl_name(typedef_decl));3672 Buf *type_name = buf_create_from_str(decl_name(typedef_decl));
36723673
...@@ -3700,16 +3701,19 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_...@@ -3700,16 +3701,19 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_
3700 // use the name of this typedef3701 // use the name of this typedef
3701 // TODO3702 // TODO
37023703
3704 // trans_qual_type here might cause us to look at this typedef again so we put the item in the map first
3705 AstNode *symbol_node = trans_create_node_symbol(c, type_name);
3706 c->decl_table.put(typedef_decl->getCanonicalDecl(), symbol_node);
3707
3703 AstNode *type_node = trans_qual_type(c, child_qt, typedef_decl->getLocation());3708 AstNode *type_node = trans_qual_type(c, child_qt, typedef_decl->getLocation());
3704 if (type_node == nullptr) {3709 if (type_node == nullptr) {
3705 emit_warning(c, typedef_decl->getLocation(), "typedef %s - unresolved child type", buf_ptr(type_name));3710 emit_warning(c, typedef_decl->getLocation(), "typedef %s - unresolved child type", buf_ptr(type_name));
3706 c->decl_table.put(typedef_decl, nullptr);3711 c->decl_table.put(typedef_decl, nullptr);
3712 // TODO add global var with type_name equal to @compileError("unable to resolve C type")
3707 return nullptr;3713 return nullptr;
3708 }3714 }
3709 add_global_var(c, type_name, type_node);3715 add_global_var(c, type_name, type_node);
37103716
3711 AstNode *symbol_node = trans_create_node_symbol(c, type_name);
3712 c->decl_table.put(typedef_decl->getCanonicalDecl(), symbol_node);
3713 return symbol_node;3717 return symbol_node;
3714}3718}
37153719
test/translate_c.zig+21
...@@ -1,6 +1,27 @@...@@ -1,6 +1,27 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: &tests.TranslateCContext) void {3pub fn addCases(cases: &tests.TranslateCContext) void {
4 cases.add("double define struct",
5 \\typedef struct Bar Bar;
6 \\typedef struct Foo Foo;
7 \\
8 \\struct Foo {
9 \\ Foo *a;
10 \\};
11 \\
12 \\struct Bar {
13 \\ Foo *a;
14 \\};
15 ,
16 \\pub const struct_Foo = extern struct {
17 \\ a: ?&Foo,
18 \\};
19 \\pub const Foo = struct_Foo;
20 \\pub const struct_Bar = extern struct {
21 \\ a: ?&Foo,
22 \\};
23 );
24
4 cases.addAllowWarnings("simple data types",25 cases.addAllowWarnings("simple data types",
5 \\#include <stdint.h>26 \\#include <stdint.h>
6 \\int foo(char a, unsigned char b, signed char c);27 \\int foo(char a, unsigned char b, signed char c);