From d60b593c8b1d19a4adfdadaeeaa93aa10b9dba53 Mon Sep 17 00:00:00 2001 From: Bernhard Schommer Date: Tue, 24 Jan 2017 10:29:30 +0100 Subject: New version to support designators. The c standard allows member designators for offsetof. The current implementation works by recursively combining the offset of each of the member designators. For array access the size of the subtypes is multiplied by the index and for members the offset of the member is calculated. Bug 20765 --- cparser/Cutil.ml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'cparser/Cutil.ml') diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml index 30f7294b..f55c1703 100644 --- a/cparser/Cutil.ml +++ b/cparser/Cutil.ml @@ -534,26 +534,23 @@ let sizeof_struct env members = in sizeof_rec 0 members (* Compute the offset of a struct member *) -let offsetof env ty fields = +let offsetof env ty field = let rec sub acc name = function | [] -> List.rev acc | m::rem -> if m.fld_name = name then List.rev acc else sub (m::acc) name rem in - let offset (ofs,ty) field = - match unroll env ty with + match unroll env ty with | TStruct (id,_) -> let str = Env.find_struct env id in let pre = sub [] field.fld_name str.ci_members in begin match sizeof_struct env pre ,alignof env field.fld_typ with | Some s, Some a -> - (ofs + align s a),field.fld_typ + align s a | _ -> assert false end - | _ -> ofs,field.fld_typ - in - let fields = List.rev fields in - fst (List.fold_left offset (0,ty) fields) + | TUnion _ -> 0 + | _ -> assert false (* Simplified version to compute offsets on structs without bitfields *) let struct_layout env members = -- cgit