summaryrefslogtreecommitdiff
path: root/cparser
diff options
context:
space:
mode:
authorGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2013-11-06 10:39:43 +0000
committerGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2013-11-06 10:39:43 +0000
commitf9c799143067c3197dc925f7fd916206d075a25d (patch)
treea7ecd744efdd58fe38cb7ef2a2e8a77c196797b8 /cparser
parent61b43d3e1be5e8aad11cb3036fdb1872f0f363c3 (diff)
Revised treatment of _Alignas, for better compatibility with GCC and Clang, and to avoid wasting global variable space by inflating their sizeof needlessly.
git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@2362 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'cparser')
-rw-r--r--cparser/Cutil.ml27
-rw-r--r--cparser/Cutil.mli2
2 files changed, 19 insertions, 10 deletions
diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml
index 982bf78..cae831d 100644
--- a/cparser/Cutil.ml
+++ b/cparser/Cutil.ml
@@ -79,8 +79,21 @@ let rec remove_custom_attributes (names: string list) (al: attributes) =
| a :: tl ->
a :: remove_custom_attributes names tl
+(* Is an attribute type-related (true) or variable-related (false)? *)
+
+let attr_is_type_related = function
+ | Attr(("packed" | "__packed__"), _) -> true
+ | _ -> false
+
+(* Is an attribute applicable to a whole array (true) or only to
+ array elements (false)? *)
+
+let attr_array_applicable = function
+ | AConst | AVolatile | ARestrict | AAlignas _ -> false
+ | Attr _ -> true
+
(* Adding top-level attributes to a type. Doesn't need to unroll defns. *)
-(* Array types cannot carry attributes, so add them to the element type. *)
+(* For array types, standard attrs are pushed to the element type. *)
let rec add_attributes_type attr t =
match t with
@@ -88,7 +101,9 @@ let rec add_attributes_type attr t =
| TInt(ik, a) -> TInt(ik, add_attributes attr a)
| TFloat(fk, a) -> TFloat(fk, add_attributes attr a)
| TPtr(ty, a) -> TPtr(ty, add_attributes attr a)
- | TArray(ty, sz, a) -> TArray(add_attributes_type attr ty, sz, a)
+ | TArray(ty, sz, a) ->
+ let (attr_arr, attr_elt) = List.partition attr_array_applicable attr in
+ TArray(add_attributes_type attr_elt ty, sz, add_attributes attr_arr a)
| TFun(ty, params, vararg, a) -> TFun(ty, params, vararg, add_attributes attr
a)
| TNamed(s, a) -> TNamed(s, add_attributes attr a)
@@ -133,7 +148,7 @@ let rec change_attributes_type env (f: attributes -> attributes) t =
| TFloat(fk, a) -> TFloat(fk, f a)
| TPtr(ty, a) -> TPtr(ty, f a)
| TArray(ty, sz, a) ->
- TArray(change_attributes_type env f ty, sz, a)
+ TArray(change_attributes_type env f ty, sz, f a)
| TFun(ty, params, vararg, a) -> TFun(ty, params, vararg, f a)
| TNamed(s, a) ->
let t1 = unroll env t in
@@ -149,12 +164,6 @@ let remove_attributes_type env attr t =
let erase_attributes_type env t =
change_attributes_type env (fun a -> []) t
-(* Is an attribute type-related (true) or variable-related (false)? *)
-
-let attr_is_type_related = function
- | Attr(("packed" | "__packed__"), _) -> true
- | _ -> false
-
(* Extracting alignment value from a set of attributes. Return 0 if none. *)
let alignas_attribute al =
diff --git a/cparser/Cutil.mli b/cparser/Cutil.mli
index 98ab54e..35f7338 100644
--- a/cparser/Cutil.mli
+++ b/cparser/Cutil.mli
@@ -53,7 +53,7 @@ val erase_attributes_type : Env.t -> typ -> typ
val change_attributes_type : Env.t -> (attributes -> attributes) -> typ -> typ
(* Apply the given function to the top-level attributes of the given type *)
val attr_is_type_related: attribute -> bool
-(* Is an attribute type-related (true) or variable-related (false)? *)
+ (* Is an attribute type-related (true) or variable-related (false)? *)
(* Type compatibility *)
val compatible_types : ?noattrs: bool -> Env.t -> typ -> typ -> bool