aboutsummaryrefslogtreecommitdiffstats
path: root/cparser
diff options
context:
space:
mode:
authorBernhard Schommer <bernhardschommer@gmail.com>2021-09-21 16:30:19 +0200
committerXavier Leroy <xavier.leroy@college-de-france.fr>2021-09-28 10:25:35 +0200
commit6ede270e6f386a099bc898307168e75ebd819c7e (patch)
treef5f1e6d35f383da93346ff38e05a87b2f104f1a9 /cparser
parent2892e2caa89f927062824ca901562d085a76d619 (diff)
downloadcompcert-kvx-6ede270e6f386a099bc898307168e75ebd819c7e.tar.gz
compcert-kvx-6ede270e6f386a099bc898307168e75ebd819c7e.zip
Ignore unnamed bit fields for initialization of unions
When a union is initialized with an initializer without designator the first named member should be initialized. This commit skips members without names during the elaboration of union initializers. Note that anonymous members (unnamed members of struct or union type) should not be skipped, and are not skipped since elaboration give names to these members. Bug 31982
Diffstat (limited to 'cparser')
-rw-r--r--cparser/Cutil.ml13
-rw-r--r--cparser/Elab.ml10
2 files changed, 16 insertions, 7 deletions
diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml
index d3a830ce..bcdea107 100644
--- a/cparser/Cutil.ml
+++ b/cparser/Cutil.ml
@@ -1263,11 +1263,16 @@ let rec default_init env ty =
let ci = Env.find_struct env id in
Init_struct(id, default_init_fields ci.ci_members)
| TUnion(id, _) ->
- let ci = Env.find_union env id in
- begin match ci.ci_members with
+ let ci = Env.find_union env id in
+ let rec default_init_field = function
| [] -> raise No_default_init
- | fld :: _ -> Init_union(id, fld, default_init env fld.fld_typ)
- end
+ | fld :: fl ->
+ if fld.fld_name = "" then
+ default_init_field fl
+ else
+ Init_union(id, fld, default_init env fld.fld_typ)
+ in
+ default_init_field ci.ci_members
| _ ->
raise No_default_init
diff --git a/cparser/Elab.ml b/cparser/Elab.ml
index a78dc9d9..c2c7f943 100644
--- a/cparser/Elab.ml
+++ b/cparser/Elab.ml
@@ -1402,14 +1402,18 @@ module I = struct
| TStruct(id, _), Init_struct(id', (fld1, i1) :: flds) ->
OK(Zstruct(z, id, [], fld1, flds), i1)
| TUnion(id, _), Init_union(id', fld, i) ->
- begin match (Env.find_union env id).Env.ci_members with
+ let rec first_named = function
| [] -> NotFound
- | fld1 :: _ ->
+ | fld1 :: fl ->
+ if fld1.fld_name = "" then
+ first_named fl
+ else begin
OK(Zunion(z, id, fld1),
if fld.fld_name = fld1.fld_name
then i
else default_init env fld1.fld_typ)
- end
+ end in
+ first_named (Env.find_union env id).Env.ci_members
| (TStruct _ | TUnion _), Init_single a ->
(* This is a previous whole-struct initialization that we
are going to overwrite. Hard to support correctly