From 778d0cc5f87472f2da267be8356e5aef7fb75f96 Mon Sep 17 00:00:00 2001 From: Bernhard Schommer Date: Mon, 20 Aug 2018 11:16:42 +0200 Subject: Improve support and diagnostic for type qualified arrays (#118) * Add diagnostic for type qualified arrays that occur in the wrong place Arrays with type qualifiers (e.g. int t[const 5]) are only allowed as function parameters and for them only the outermost array type derivation. Bug 23400 * Keep attributes from array for argument conversion Type qualifiers of arrays in function parameters are just syntactic sugar to allow adding them to the resulting pointer type. Hence, when a qualified array type such as `int t[const 5]` decays into a pointer type during argument conversion, the pointer type should be qualified, e.g. `int * const t`. --- cparser/Elab.ml | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'cparser/Elab.ml') diff --git a/cparser/Elab.ml b/cparser/Elab.ml index cc7648e0..da5a1d37 100644 --- a/cparser/Elab.ml +++ b/cparser/Elab.ml @@ -835,6 +835,8 @@ and elab_parameter env (PARAM (spec, id, decl, attr, loc)) = error loc "redefinition of parameter '%s'" id; (* replace array and function types by pointer types *) let ty1 = argument_conversion env1 ty in + if is_qualified_array ty1 then + error loc "type qualifier used in non-outermost array type derivation"; let (id', env2) = Env.enter_ident env1 id sto ty1 in ( (id', ty1) , env2) @@ -953,6 +955,8 @@ and elab_field_group env (Field_group (spec, fieldlist, loc)) = error loc "bit-field '%a' width not an integer constant" pp_field id; None,env end in + if is_qualified_array ty then + error loc "type qualifier used in array declarator outside of function prototype"; let anon_composite = is_anonymous_composite ty in if id = "" && not anon_composite && optbitsize = None then warning loc Missing_declarations "declaration does not declare anything"; @@ -2334,6 +2338,8 @@ let enter_decdefs local nonstatic_inline loc env sto dl = (name_of_storage_class sto) | _ -> () end; + if is_qualified_array ty then + error loc "type qualifier used in array declarator outside of function prototype"; (* Local variable declarations with default storage are treated as 'auto'. Local function declarations with default storage remain with default storage. *) -- cgit