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/Checks.ml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'cparser/Checks.ml') diff --git a/cparser/Checks.ml b/cparser/Checks.ml index 62d85c1b..a30cde7d 100644 --- a/cparser/Checks.ml +++ b/cparser/Checks.ml @@ -18,19 +18,12 @@ open Diagnostics open Cutil open Env -let attribute_string = function - | AConst -> "const" - | AVolatile -> "volatile" - | ARestrict -> "restrict" - | AAlignas n -> "_Alignas" - | Attr(name, _) -> name - let unknown_attrs loc attrs = let unknown attr = let attr_class = class_of_attribute attr in if attr_class = Attr_unknown then warning loc Unknown_attribute - "unknown attribute '%s' ignored" (attribute_string attr) in + "unknown attribute '%s' ignored" (name_of_attribute attr) in List.iter unknown attrs let unknown_attrs_typ env loc ty = -- cgit