]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Disallow [[deprecated]] on types other than class/enum definitions [PR110345]
authorJakub Jelinek <jakub@redhat.com>
Fri, 20 Dec 2024 09:12:08 +0000 (10:12 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 20 Dec 2024 09:12:08 +0000 (10:12 +0100)
commitcd647514a539943ade6461efbf056a7c3f4305c6
tree7782c5b1240cc7f18cd43e6fb570445b33575fc1
parenta25cc26884663244c3b936af785854abee8949dd
c++: Disallow [[deprecated]] on types other than class/enum definitions [PR110345]

For C++ 26 P2552R3 I went through all the spots (except modules) where
attribute-specifier-seq appears in the grammar and tried to construct
a testcase in all those spots, for now for [[deprecated]] attribute.

The patch below contains that testcase.  One needed change for this
particular attribute was that currently we handle [[deprecated]]
exactly the same as [[gnu::deprecated]], but for the latter unlike C++14
or later we allow it also on almost all types, while the standard
is strict and allows it only on
https://eel.is/c++draft/dcl.attr#deprecated-2
The attribute may be applied to the declaration of a class, a typedef-name,
a variable, a non-static data member, a function, a namespace,
an enumeration, an enumerator, a concept, or a template specialization.

The following patch just adds a pedwarn for the cases that gnu::deprecated
allows but C++14 disallows, so integral/floating/boolean types,
pointers/references, array types, function types etc.
Basically, for TYPE_P, if the attribute is applied in place (which means
the struct/union/class/enum definition), it is allowed, otherwise pedwarned.

I've tried to compile it also with latest clang and there is agreement in
most of the diagnostics, just at block scope (inside of foo) it doesn't
diagnose
  auto e = new int [n] [[deprecated]];
  auto e2 = new int [n] [[deprecated]] [42];
  [[deprecated]] lab:;
and at namespace scope
[[deprecated]];
I think that all feels like clang++ bug.

Also this pedwarns on
  [[deprecated]] int : 0;
at class scope, that isn't a non-static data member...

I guess to mark the paper as implemented (or what has been already voted
into C++23 earlier) we'll need to add similar testcase for all the other
standard attributes and make sure we check what the attributes can appertain
to and what they can't.

2024-12-19  Jakub Jelinek  <jakub@redhat.com>

PR c++/110345
* parser.cc (cp_parser_std_attribute): Don't transform
[[deprecated]] into [[gnu::deprecated]].
* tree.cc (handle_std_deprecated_attribute): New function.
(std_attributes): Add deprecated entry.

* g++.dg/cpp0x/attr-deprecated1.C: New test.
gcc/cp/parser.cc
gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp0x/attr-deprecated1.C [new file with mode: 0644]