checker/cgen: add checks and fix cgen for [typedef] for C structs (#8169)
							parent
							
								
									460f32baf2
								
							
						
					
					
						commit
						334b66b311
					
				|  | @ -4,7 +4,6 @@ module big | |||
| #flag -I @VROOT/thirdparty/bignum | ||||
| #flag @VROOT/thirdparty/bignum/bn.o | ||||
| #include "bn.h" | ||||
| [typedef] | ||||
| struct C.bn { | ||||
| mut: | ||||
| 	array [32]u32 | ||||
|  |  | |||
|  | @ -387,6 +387,11 @@ pub fn (mut c Checker) struct_decl(mut decl ast.StructDecl) { | |||
| 				c.error('`$embed_sym.name` is not a struct', embed.pos) | ||||
| 			} | ||||
| 		} | ||||
| 		for attr in decl.attrs { | ||||
| 			if attr.name == 'typedef' && decl.language != .c { | ||||
| 				c.error('`typedef` attribute can only be used with C structs', decl.pos) | ||||
| 			} | ||||
| 		} | ||||
| 		for i, field in decl.fields { | ||||
| 			if decl.language == .v { | ||||
| 				c.check_valid_snake_case(field.name, 'field name', field.pos) | ||||
|  |  | |||
|  | @ -0,0 +1,6 @@ | |||
| vlib/v/checker/tests/typedef_attr_v_struct_err.vv:2:1: error: `typedef` attribute can only be used with C structs | ||||
|     1 | [typedef] | ||||
|     2 | struct Point{} | ||||
|       | ~~~~~~~~~~~~ | ||||
|     3 |  | ||||
|     4 | fn main() { | ||||
|  | @ -0,0 +1,7 @@ | |||
| [typedef] | ||||
| struct Point{} | ||||
| 
 | ||||
| fn main() { | ||||
|     p := Point{} | ||||
|     println(p) | ||||
| } | ||||
|  | @ -660,7 +660,15 @@ typedef struct { | |||
| 			.alias { | ||||
| 				parent := unsafe { &g.table.types[typ.parent_idx] } | ||||
| 				is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.` | ||||
| 				parent_styp := if is_c_parent { 'struct ' + parent.cname[3..] } else { parent.cname } | ||||
| 				mut is_typedef := false | ||||
| 				if parent.info is table.Struct { | ||||
| 					is_typedef = parent.info.is_typedef | ||||
| 				} | ||||
| 				parent_styp := if is_c_parent { | ||||
| 					if !is_typedef { 'struct ' + parent.cname[3..] } else { parent.cname[3..] } | ||||
| 				} else { | ||||
| 					parent.cname | ||||
| 				} | ||||
| 				g.type_definitions.writeln('typedef $parent_styp $typ.cname;') | ||||
| 			} | ||||
| 			.array { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue