cython

changeset 1383:92baafe0edf3 0.10.2

Fix gcc crash on recursive typedef struct
Won't break existing code, only makes bad code compile now.
author Robert Bradshaw <robertwb@math.washington.edu>
date Tue Nov 25 14:04:41 2008 -0800 (3 years ago)
parents c8d4fc990029
children 8074acd65e8a
files Cython/Compiler/Nodes.py tests/compile/ctypedefstruct.pyx
line diff
1.1 --- a/Cython/Compiler/Nodes.py Sun Nov 23 18:18:00 2008 +0100 1.2 +++ b/Cython/Compiler/Nodes.py Tue Nov 25 14:04:41 2008 -0800 1.3 @@ -784,11 +784,30 @@ 1.4 self.entry = env.declare_struct_or_union( 1.5 self.name, self.kind, scope, self.typedef_flag, self.pos, 1.6 self.cname, visibility = self.visibility) 1.7 + need_typedef_indirection = False 1.8 if self.attributes is not None: 1.9 if self.in_pxd and not env.in_cinclude: 1.10 self.entry.defined_in_pxd = 1 1.11 for attr in self.attributes: 1.12 attr.analyse_declarations(env, scope) 1.13 + for attr in scope.var_entries: 1.14 + type = attr.type 1.15 + while type.is_array: 1.16 + type = type.base_type 1.17 + if type == self.entry.type: 1.18 + error(attr.pos, "Struct cannot contain itself as a member.") 1.19 + if self.typedef_flag: 1.20 + while type.is_ptr: 1.21 + type = type.base_type 1.22 + if type == self.entry.type: 1.23 + need_typedef_indirection = True 1.24 + if need_typedef_indirection and self.visibility != 'extern': 1.25 + # C can't handle typedef structs that refer to themselves. 1.26 + struct_entry = self.entry 1.27 + cname = env.new_const_cname() 1.28 + self.entry = env.declare_typedef(self.name, struct_entry.type, self.pos, cname = self.cname, visibility='ignore') 1.29 + struct_entry.type.typedef_flag = False 1.30 + struct_entry.cname = struct_entry.type.cname = env.new_const_cname() 1.31 1.32 def analyse_expressions(self, env): 1.33 pass
2.1 --- a/tests/compile/ctypedefstruct.pyx Sun Nov 23 18:18:00 2008 +0100 2.2 +++ b/tests/compile/ctypedefstruct.pyx Tue Nov 25 14:04:41 2008 -0800 2.3 @@ -7,3 +7,6 @@ 2.4 order1.spam = 7 2.5 order1.eggs = 2 2.6 2.7 +ctypedef struct linked: 2.8 + int a 2.9 + linked *next