diff options
author | Iain Sandoe <iain@sandoe.co.uk> | 2020-11-08 09:04:07 +0000 |
---|---|---|
committer | Iain Sandoe <iain@sandoe.co.uk> | 2021-09-01 15:09:38 +0100 |
commit | 8433baadec88e5f31fa141b6d78094e91256079d (patch) | |
tree | bbe8d1332389ecdee6c6849b903c0aa5d12c978d /gcc/cp | |
parent | 21b4d0ef543d68187d258415b51d0d6676af89fd (diff) | |
download | gcc-8433baadec88e5f31fa141b6d78094e91256079d.zip gcc-8433baadec88e5f31fa141b6d78094e91256079d.tar.gz gcc-8433baadec88e5f31fa141b6d78094e91256079d.tar.bz2 |
C-family: Add attribute 'unavailable'.
If an interface is marked 'deprecated' then, presumably, at some point it
will be withdrawn and no longer available. The 'unavailable' attribute
makes it possible to mark up interfaces to indicate this status. It is used
quite extensively in some codebases where a single set of headers can be used
to permit code generation for multiple system versions.
From a configuration perspective, it also allows a compile test to determine
that an interface is missing - rather than requiring a link test.
The implementation follows the pattern of attribute deprecated, but produces
an error (where deprecation produces a warning).
This attribute has been implemented in clang for some years.
Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
gcc/c-family/ChangeLog:
* c-attribs.c (handle_unavailable_attribute): New.
gcc/c/ChangeLog:
* c-decl.c (enum deprecated_states): Add unavailable state.
(merge_decls): Copy unavailability.
(quals_from_declspecs): Handle unavailable case.
(start_decl): Amend the logic handling suppression of nested
deprecation states to include unavailability.
(smallest_type_quals_location): Amend comment.
(grokdeclarator): Handle the unavailable deprecation state.
(declspecs_add_type): Set TREE_UNAVAILABLE from the decl specs.
* c-tree.h (struct c_declspecs): Add unavailable_p.
* c-typeck.c (build_component_ref): Handle unavailability.
(build_external_ref): Likewise.
gcc/cp/ChangeLog:
* call.c (build_over_call): Handle unavailable state in addition to
deprecation.
* class.c (type_build_ctor_call): Likewise.
(type_build_dtor_call): Likewise.
* cp-tree.h: Rename cp_warn_deprecated_use to
cp_handle_deprecated_or_unavailable.
* decl.c (duplicate_decls): Merge unavailability.
(grokdeclarator): Handle unavailability in addition to deprecation.
(type_is_unavailable): New.
(grokparms): Handle unavailability in addition to deprecation.
* decl.h (enum deprecated_states): Add
UNAVAILABLE_DEPRECATED_SUPPRESS.
* decl2.c (cplus_decl_attributes): Propagate unavailability to
templates.
(cp_warn_deprecated_use): Rename to ...
(cp_handle_deprecated_or_unavailable): ... this and amend to handle
the unavailable case. It remains a warning in the case of deprecation
but becomes an error in the case of unavailability.
(cp_warn_deprecated_use_scopes): Handle unavailability.
(mark_used): Likewise.
* parser.c (cp_parser_template_name): Likewise.
(cp_parser_template_argument): Likewise.
(cp_parser_parameter_declaration_list): Likewise.
* typeck.c (build_class_member_access_expr): Likewise.
(finish_class_member_access_expr): Likewise.
* typeck2.c (build_functional_cast_1): Likewise.
gcc/ChangeLog:
* doc/extend.texi: Document unavailable attribute.
* print-tree.c (print_node): Handle unavailable attribute.
* tree-core.h (struct tree_base): Add a bit to carry unavailability.
* tree.c (error_unavailable_use): New.
* tree.h (TREE_UNAVAILABLE): New.
(error_unavailable_use): New.
gcc/objc/ChangeLog:
* objc-act.c (objc_add_property_declaration): Register unavailable
attribute.
(maybe_make_artificial_property_decl): Set available.
(objc_maybe_build_component_ref): Generalise to the method prototype
to count availability.
(objc_build_class_component_ref): Likewise.
(build_private_template): Likewise.
(objc_decl_method_attributes): Handle unavailable attribute.
(lookup_method_in_hash_lists): Amend comments.
(objc_finish_message_expr): Handle unavailability in addition to
deprecation.
(start_class): Likewise.
(finish_class): Likewise.
(lookup_protocol): Likewise.
(objc_declare_protocol): Likewise.
(start_protocol): Register unavailable attribute.
(really_start_method): Likewise.
(objc_gimplify_property_ref): Emit error on encountering an
unavailable entity (and a warning for a deprecated one).
gcc/testsuite/ChangeLog:
* g++.dg/ext/attr-unavailable-1.C: New test.
* g++.dg/ext/attr-unavailable-2.C: New test.
* g++.dg/ext/attr-unavailable-3.C: New test.
* g++.dg/ext/attr-unavailable-4.C: New test.
* g++.dg/ext/attr-unavailable-5.C: New test.
* g++.dg/ext/attr-unavailable-6.C: New test.
* g++.dg/ext/attr-unavailable-7.C: New test.
* g++.dg/ext/attr-unavailable-8.C: New test.
* g++.dg/ext/attr-unavailable-9.C: New test.
* gcc.dg/attr-unavailable-1.c: New test.
* gcc.dg/attr-unavailable-2.c: New test.
* gcc.dg/attr-unavailable-3.c: New test.
* gcc.dg/attr-unavailable-4.c: New test.
* gcc.dg/attr-unavailable-5.c: New test.
* gcc.dg/attr-unavailable-6.c: New test.
* obj-c++.dg/attributes/method-unavailable-1.mm: New test.
* obj-c++.dg/attributes/method-unavailable-2.mm: New test.
* obj-c++.dg/attributes/method-unavailable-3.mm: New test.
* obj-c++.dg/property/at-property-unavailable-1.mm: New test.
* obj-c++.dg/property/at-property-unavailable-2.mm: New test.
* obj-c++.dg/property/dotsyntax-unavailable-1.mm: New test.
* objc.dg/attributes/method-unavailable-1.m: New test.
* objc.dg/attributes/method-unavailable-2.m: New test.
* objc.dg/attributes/method-unavailable-3.m: New test.
* objc.dg/property/at-property-unavailable-1.m: New test.
* objc.dg/property/at-property-unavailable-2.m: New test.
* objc.dg/property/dotsyntax-unavailable-1.m: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/call.c | 4 | ||||
-rw-r--r-- | gcc/cp/class.c | 2 | ||||
-rw-r--r-- | gcc/cp/cp-tree.h | 2 | ||||
-rw-r--r-- | gcc/cp/decl.c | 66 | ||||
-rw-r--r-- | gcc/cp/decl.h | 3 | ||||
-rw-r--r-- | gcc/cp/decl2.c | 58 | ||||
-rw-r--r-- | gcc/cp/parser.c | 32 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 9 | ||||
-rw-r--r-- | gcc/cp/typeck2.c | 2 |
9 files changed, 146 insertions, 32 deletions
diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 80e6121..7bbf134 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -9506,7 +9506,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) already_used = true; } else - cp_warn_deprecated_use (fn, complain); + cp_handle_deprecated_or_unavailable (fn, complain); if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn) && !make_base_init_ok (arg)) @@ -9580,7 +9580,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) suppress_warning (val, OPT_Wunused); } - cp_warn_deprecated_use (fn, complain); + cp_handle_deprecated_or_unavailable (fn, complain); return val; } diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 7138e30..17876c7 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -5719,6 +5719,7 @@ type_build_ctor_call (tree t) tree fn = *iter; if (!DECL_ARTIFICIAL (fn) || TREE_DEPRECATED (fn) + || TREE_UNAVAILABLE (fn) || DECL_DELETED_FN (fn)) return true; } @@ -5747,6 +5748,7 @@ type_build_dtor_call (tree t) tree fn = *iter; if (!DECL_ARTIFICIAL (fn) || TREE_DEPRECATED (fn) + || TREE_UNAVAILABLE (fn) || DECL_DELETED_FN (fn)) return true; } diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index f0a7bd2..435f32b 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6549,7 +6549,7 @@ extern void validate_conversion_obstack (void); extern void mark_versions_used (tree); extern int unsafe_return_slot_p (tree); extern bool make_safe_copy_elision (tree, tree); -extern bool cp_warn_deprecated_use (tree, tsubst_flags_t = tf_warning_or_error); +extern bool cp_handle_deprecated_or_unavailable (tree, tsubst_flags_t = tf_warning_or_error); extern void cp_warn_deprecated_use_scopes (tree); extern tree get_function_version_dispatcher (tree); diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index e981ead..146979b 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -2418,6 +2418,10 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) if (TREE_DEPRECATED (newdecl)) TREE_DEPRECATED (olddecl) = 1; + /* Merge unavailability. */ + if (TREE_UNAVAILABLE (newdecl)) + TREE_UNAVAILABLE (olddecl) = 1; + /* Preserve function specific target and optimization options */ if (TREE_CODE (newdecl) == FUNCTION_DECL) { @@ -11757,20 +11761,24 @@ grokdeclarator (const cp_declarator *declarator, if (attrlist && *attrlist == error_mark_node) *attrlist = NULL_TREE; - /* An object declared as __attribute__((deprecated)) suppresses - warnings of uses of other deprecated items. */ + /* An object declared as __attribute__((unavailable)) suppresses + any reports of being declared with unavailable or deprecated + items. An object declared as __attribute__((deprecated)) + suppresses warnings of uses of other deprecated items. */ auto ds = make_temp_override (deprecated_state); - if (attrlist && lookup_attribute ("deprecated", *attrlist)) + if (attrlist && lookup_attribute ("unavailable", *attrlist)) + deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS; + else if (attrlist && lookup_attribute ("deprecated", *attrlist)) deprecated_state = DEPRECATED_SUPPRESS; - cp_warn_deprecated_use (type); + cp_handle_deprecated_or_unavailable (type); if (type && TREE_CODE (type) == TYPE_DECL) { cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type)); typedef_decl = type; type = TREE_TYPE (typedef_decl); if (DECL_ARTIFICIAL (typedef_decl)) - cp_warn_deprecated_use (type); + cp_handle_deprecated_or_unavailable (type); } /* No type at all: default to `int', and set DEFAULTED_INT because it was not a user-defined typedef. */ @@ -14417,6 +14425,43 @@ type_is_deprecated (tree type) return NULL_TREE; } +/* Returns an unavailable type used within TYPE, or NULL_TREE if none. */ + +static tree +type_is_unavailable (tree type) +{ + enum tree_code code; + if (TREE_UNAVAILABLE (type)) + return type; + if (TYPE_NAME (type)) + { + if (TREE_UNAVAILABLE (TYPE_NAME (type))) + return type; + else + { + cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); + return NULL_TREE; + } + } + + /* Do warn about using typedefs to a deprecated class. */ + if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) + return type_is_deprecated (TYPE_MAIN_VARIANT (type)); + + code = TREE_CODE (type); + + if (code == POINTER_TYPE || code == REFERENCE_TYPE + || code == OFFSET_TYPE || code == FUNCTION_TYPE + || code == METHOD_TYPE || code == ARRAY_TYPE) + return type_is_unavailable (TREE_TYPE (type)); + + if (TYPE_PTRMEMFUNC_P (type)) + return type_is_unavailable + (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); + + return NULL_TREE; +} + /* Decode the list of parameter types for a function type. Given the list of things declared inside the parens, return a list of types. @@ -14476,11 +14521,18 @@ grokparms (tree parmlist, tree *parms) if (type != error_mark_node) { - if (deprecated_state != DEPRECATED_SUPPRESS) + if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) + { + tree unavailtype = type_is_unavailable (type); + if (unavailtype) + cp_handle_deprecated_or_unavailable (unavailtype); + } + if (deprecated_state != DEPRECATED_SUPPRESS + && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) { tree deptype = type_is_deprecated (type); if (deptype) - cp_warn_deprecated_use (deptype); + cp_handle_deprecated_or_unavailable (deptype); } /* [dcl.fct] "A parameter with volatile-qualified type is diff --git a/gcc/cp/decl.h b/gcc/cp/decl.h index 387d807..4a9b399 100644 --- a/gcc/cp/decl.h +++ b/gcc/cp/decl.h @@ -44,7 +44,8 @@ extern void name_unnamed_type (tree, tree); enum deprecated_states { DEPRECATED_NORMAL, - DEPRECATED_SUPPRESS + DEPRECATED_SUPPRESS, + UNAVAILABLE_DEPRECATED_SUPPRESS }; extern enum deprecated_states deprecated_state; diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 0c9d2f4..107edca 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -1635,6 +1635,17 @@ cplus_decl_attributes (tree *decl, tree attributes, int flags) if (*decl == pattern) TREE_DEPRECATED (tmpl) = true; } + + /* Likewise, propagate unavailability out to the template. */ + if (TREE_UNAVAILABLE (*decl)) + if (tree ti = get_template_info (*decl)) + { + tree tmpl = TI_TEMPLATE (ti); + tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) + : DECL_TEMPLATE_RESULT (tmpl)); + if (*decl == pattern) + TREE_UNAVAILABLE (tmpl) = true; + } } /* Walks through the namespace- or function-scope anonymous union @@ -5498,14 +5509,47 @@ maybe_instantiate_decl (tree decl) } } -/* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or - not a warning was emitted. */ +/* Error if the DECL is unavailable (unless this is currently suppressed). + Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if + an error or warning was emitted. */ bool -cp_warn_deprecated_use (tree decl, tsubst_flags_t complain) +cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain) { - if (!(complain & tf_warning) || !decl - || deprecated_state == DEPRECATED_SUPPRESS) + if (!decl) + return false; + + if ((complain & tf_error) + && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) + { + if (TREE_UNAVAILABLE (decl)) + { + error_unavailable_use (decl, NULL_TREE); + return true; + } + else + { + /* Perhaps this is an unavailable typedef. */ + if (TYPE_P (decl) + && TYPE_NAME (decl) + && TREE_UNAVAILABLE (TYPE_NAME (decl))) + { + decl = TYPE_NAME (decl); + /* Don't error within members of a unavailable type. */ + if (TYPE_P (decl) + && currently_open_class (decl)) + return false; + + error_unavailable_use (decl, NULL_TREE); + return true; + } + } + /* Carry on to consider deprecatedness. */ + } + + if (!(complain & tf_warning) + || deprecated_state == DEPRECATED_SUPPRESS + || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS) return false; if (!TREE_DEPRECATED (decl)) @@ -5565,7 +5609,7 @@ cp_warn_deprecated_use_scopes (tree scope) && scope != global_namespace) { if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope)) - && cp_warn_deprecated_use (scope)) + && cp_handle_deprecated_or_unavailable (scope)) return; if (TYPE_P (scope)) scope = CP_TYPE_CONTEXT (scope); @@ -5677,7 +5721,7 @@ mark_used (tree decl, tsubst_flags_t complain) TREE_USED (decl) = true; } - cp_warn_deprecated_use (decl, complain); + cp_handle_deprecated_or_unavailable (decl, complain); /* We can only check DECL_ODR_USED on variables or functions with DECL_LANG_SPECIFIC set, and these are also the only decls that we diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d3c31be..ea71f9c 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -18428,18 +18428,26 @@ cp_parser_template_name (cp_parser* parser, /* If DECL is a template, then the name was a template-name. */ if (TREE_CODE (decl) == TEMPLATE_DECL) { - if (TREE_DEPRECATED (decl) - && deprecated_state != DEPRECATED_SUPPRESS) + if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl)) + && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) { tree d = DECL_TEMPLATE_RESULT (decl); tree attr; if (TREE_CODE (d) == TYPE_DECL) - attr = lookup_attribute ("deprecated", - TYPE_ATTRIBUTES (TREE_TYPE (d))); + attr = TYPE_ATTRIBUTES (TREE_TYPE (d)); else - attr = lookup_attribute ("deprecated", - DECL_ATTRIBUTES (d)); - warn_deprecated_use (decl, attr); + attr = DECL_ATTRIBUTES (d); + if (TREE_UNAVAILABLE (decl)) + { + attr = lookup_attribute ("unavailable", attr); + error_unavailable_use (decl, attr); + } + else if (TREE_DEPRECATED (decl) + && deprecated_state != DEPRECATED_SUPPRESS) + { + attr = lookup_attribute ("deprecated", attr); + warn_deprecated_use (decl, attr); + } } } else @@ -18690,7 +18698,9 @@ cp_parser_template_argument (cp_parser* parser) } if (cp_parser_parse_definitely (parser)) { - if (TREE_DEPRECATED (argument)) + if (TREE_UNAVAILABLE (argument)) + error_unavailable_use (argument, NULL_TREE); + else if (TREE_DEPRECATED (argument)) warn_deprecated_use (argument, NULL_TREE); return argument; } @@ -24351,9 +24361,9 @@ cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags) /*template_parm_p=*/false, &parenthesized_p); - /* We don't know yet if the enclosing context is deprecated, so wait - and warn in grokparms if appropriate. */ - deprecated_state = DEPRECATED_SUPPRESS; + /* We don't know yet if the enclosing context is unavailable or deprecated, + so wait and deal with it in grokparms if appropriate. */ + deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS; if (parameter && !cp_parser_error_occurred (parser)) { diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a46c6d2..cc61b50 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -2709,7 +2709,10 @@ build_class_member_access_expr (cp_expr object, tree member, member_scope = DECL_CLASS_CONTEXT (member); if (!mark_used (member, complain) && !(complain & tf_error)) return error_mark_node; - if (TREE_DEPRECATED (member)) + + if (TREE_UNAVAILABLE (member)) + error_unavailable_use (member, NULL_TREE); + else if (TREE_DEPRECATED (member)) warn_deprecated_use (member, NULL_TREE); } else @@ -3424,7 +3427,9 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, } } - if (TREE_DEPRECATED (member)) + if (TREE_UNAVAILABLE (member)) + error_unavailable_use (member, NULL_TREE); + else if (TREE_DEPRECATED (member)) warn_deprecated_use (member, NULL_TREE); if (template_p) diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 5e2c23c..f78dbf2 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -2163,7 +2163,7 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, type = TREE_TYPE (exp); if (DECL_ARTIFICIAL (exp)) - cp_warn_deprecated_use (type); + cp_handle_deprecated_or_unavailable (type); } else type = exp; |