diff options
author | Iain Sandoe <iain@sandoe.co.uk> | 2020-10-24 15:51:46 +0100 |
---|---|---|
committer | Iain Sandoe <iain@sandoe.co.uk> | 2020-11-07 11:45:46 +0000 |
commit | 0c30bf43eb2325caa4cb32a697ac1127c15205d7 (patch) | |
tree | b48fce515527857a67855662c4fde50b90cb6328 /gcc/c-family | |
parent | df784801daf0185a1e22ebf03d48363530717882 (diff) | |
download | gcc-0c30bf43eb2325caa4cb32a697ac1127c15205d7.zip gcc-0c30bf43eb2325caa4cb32a697ac1127c15205d7.tar.gz gcc-0c30bf43eb2325caa4cb32a697ac1127c15205d7.tar.bz2 |
Objective-C : Implement NSObject attribute.
This attribute allows pointers to be marked as pointers to
an NSObject-compatible object. This allows for additional
checking of assignment etc. when refering to pointers to
opaque types.
gcc/c-family/ChangeLog:
* c-attribs.c (handle_nsobject_attribute): New.
* c.opt: Add WNSObject-attribute.
gcc/objc/ChangeLog:
* objc-act.c (objc_compare_types): Handle NSObject type
attributes.
(objc_type_valid_for_messaging): Likewise.
gcc/testsuite/ChangeLog:
* obj-c++.dg/attributes/nsobject-01.mm: New test.
* objc.dg/attributes/nsobject-01.m: New test.
Diffstat (limited to 'gcc/c-family')
-rw-r--r-- | gcc/c-family/c-attribs.c | 39 | ||||
-rw-r--r-- | gcc/c-family/c.opt | 4 |
2 files changed, 43 insertions, 0 deletions
diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index 65cba50..f168082 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -157,6 +157,7 @@ static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *); static tree handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *); static tree handle_copy_attribute (tree *, tree, tree, int, bool *); +static tree handle_nsobject_attribute (tree *, tree, tree, int, bool *); /* Helper to define attribute exclusions. */ #define ATTR_EXCL(name, function, type, variable) \ @@ -509,6 +510,9 @@ const struct attribute_spec c_common_attribute_table[] = handle_noinit_attribute, attr_noinit_exclusions }, { "access", 1, 3, false, true, true, false, handle_access_attribute, NULL }, + /* Attributes used by Objective-C. */ + { "NSObject", 0, 0, true, false, false, false, + handle_nsobject_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; @@ -5124,6 +5128,41 @@ handle_patchable_function_entry_attribute (tree *, tree name, tree args, return NULL_TREE; } +/* Handle a "NSObject" attributes; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_nsobject_attribute (tree *node, tree name, tree args, + int /*flags*/, bool *no_add_attrs) +{ + *no_add_attrs = true; + + /* This attribute only applies to typedefs (or field decls for properties), + we drop it otherwise - but warn about this if enabled. */ + if (TREE_CODE (*node) != TYPE_DECL && TREE_CODE (*node) != FIELD_DECL) + { + warning (OPT_WNSObject_attribute, "%qE attribute may be put on a" + " typedef only; attribute is ignored", name); + return NULL_TREE; + } + + /* The original implementation only allowed pointers to records, however + recent implementations also allow void *. */ + tree type = TREE_TYPE (*node); + if (!type || !POINTER_TYPE_P (type) + || (TREE_CODE (TREE_TYPE (type)) != RECORD_TYPE + && !VOID_TYPE_P (TREE_TYPE (type)))) + { + error ("%qE attribute is for pointer types only", name); + return NULL_TREE; + } + + tree t = tree_cons (name, args, TYPE_ATTRIBUTES (type)); + TREE_TYPE (*node) = build_type_attribute_variant (type, t); + + return NULL_TREE; +} + /* Attempt to partially validate a single attribute ATTR as if it were to be applied to an entity OPER. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index ebd07cc..fe16357 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -256,6 +256,10 @@ U C ObjC C++ ObjC++ Joined Separate MissingArgError(macro name missing after %qs) -U<macro> Undefine <macro>. +WNSObject-attribute +C ObjC C++ ObjC++ LTO Var(warn_nsobject_attribute) Warning Init(1) +Warn if the NSObject attribute is applied to a non-typedef. + Wabi C ObjC C++ ObjC++ LTO Var(warn_abi) Warning Warn about things that will change when compiling with an ABI-compliant compiler. |