c++: Lost deprecated/unavailable attr in class tmpl [PR104682]
commit430c89274d7f82810724126637ffdc5507d442f0
authorMarek Polacek <polacek@redhat.com>
Fri, 25 Feb 2022 19:56:13 +0000 (25 14:56 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 28 Feb 2022 16:37:49 +0000 (28 11:37 -0500)
treebc4c5c862fc47548e3a9e77f62f9e5739965005e
parentc8b0571e334792c0c789438617cfb7faf86ab599
c++: Lost deprecated/unavailable attr in class tmpl [PR104682]

When looking into the other PR I noticed that we fail to give a warning
for a deprecated enumerator when the enum is in a class template.  This
only happens when the attribute doesn't have an argument.  The reason is
that when we tsubst_enum, we create a new enumerator:

      build_enumerator (DECL_NAME (decl), value, newtag,
           DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));

but DECL_ATTRIBUTES (decl) is null when the attribute was provided
without an argument -- in that case it simply melts into a tree flag.
handle_deprecated_attribute has:

      if (!args)
         *no_add_attrs = true;

so the attribute isn't retained and we lose it when tsubsting.  Same
thing when the attribute is on the enum itself.

Attribute unavailable is a similar case, but it's different in that
it can be a late attribute whereas "deprecated" can't:
is_late_template_attribute has

                /* But some attributes specifically apply to templates.  */
                && !is_attribute_p ("abi_tag", name)
                && !is_attribute_p ("deprecated", name)
                && !is_attribute_p ("visibility", name))
         return true;
       else
         return false;

which looks strange, but attr-unavailable-9.C tests that we don't error when
the attribute is applied on a template.

PR c++/104682

gcc/cp/ChangeLog:

* cp-tree.h (build_enumerator): Adjust.
* decl.cc (finish_enum): Make it return the new decl.
* pt.cc (tsubst_enum): Propagate TREE_DEPRECATED and TREE_UNAVAILABLE.

gcc/testsuite/ChangeLog:

* g++.dg/ext/attr-unavailable-10.C: New test.
* g++.dg/ext/attr-unavailable-11.C: New test.
* g++.dg/warn/deprecated-17.C: New test.
* g++.dg/warn/deprecated-18.C: New test.
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/ext/attr-unavailable-10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/attr-unavailable-11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/deprecated-17.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/deprecated-18.C [new file with mode: 0644]