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_
36673667 if (existing_entry) {
36683668 return existing_entry->value;
36693669 }
3670
36703671 QualType child_qt = typedef_decl->getUnderlyingType();
36713672 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_
37003701 // use the name of this typedef
37013702 // 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
37033708 AstNode *type_node = trans_qual_type(c, child_qt, typedef_decl->getLocation());
37043709 if (type_node == nullptr) {
37053710 emit_warning(c, typedef_decl->getLocation(), "typedef %s - unresolved child type", buf_ptr(type_name));
37063711 c->decl_table.put(typedef_decl, nullptr);
3712 // TODO add global var with type_name equal to @compileError("unable to resolve C type")
37073713 return nullptr;
37083714 }
37093715 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);
37133717 return symbol_node;
37143718}
37153719
test/translate_c.zig+21
......@@ -1,6 +1,27 @@
11const tests = @import("tests.zig");
22
33pub 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
425 cases.addAllowWarnings("simple data types",
526 \\#include <stdint.h>
627 \\int foo(char a, unsigned char b, signed char c);