From eec6d60b5fa43fa8cf011747d6b98322dcdaaae8 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Wed, 13 Feb 2019 16:04:27 +0100 Subject: Reject object-related and struct-related attributes on typedefs This commit adds a check to reject type definitions such as ``` typedef __attribute((section "foo")) int fooint; ``` GCC and Clang also reject this as an error. Without the check, the behavior is somewhat surprising: ``` fooint x; // placed in section "foo" fooint * x; // placed in default section, attribute "foo" is ignored ``` Note that the following must be accepted: ``` typedef struct { ... } __attribute((packed)) t; ``` The "packed" attribute is correctly attached to the struct type and should not be checked. This is achieved by using `attribute_of_type_no_expand` to get the attributes of the typedef-ed type, excluding the attributes carried by a struct/union or another typedef. --- cparser/Elab.ml | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'cparser/Elab.ml') diff --git a/cparser/Elab.ml b/cparser/Elab.ml index 46bc63a9..d7124663 100644 --- a/cparser/Elab.ml +++ b/cparser/Elab.ml @@ -2366,6 +2366,13 @@ let enter_typedefs loc env sto dl = error loc "initializer in typedef"; if has_std_alignas env ty then error loc "alignment specified for typedef '%s'" s; + List.iter + (fun a -> match class_of_attribute a with + | Attr_object | Attr_struct -> + error loc "attribute '%s' not allowed in 'typedef'" + (name_of_attribute a) + | _ -> ()) + (attributes_of_type_no_expand ty); match previous_def Env.lookup_typedef env s with | Some (s',ty') when Env.in_current_scope env s' -> if equal_types env ty ty' then begin -- cgit