]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Add __builtin_operator_{new,delete} support
authorJakub Jelinek <jakub@redhat.com>
Mon, 11 Nov 2024 18:54:32 +0000 (19:54 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 11 Nov 2024 19:04:33 +0000 (20:04 +0100)
commit417b4cc9bf218083838aeab458bbb7510e36375a
tree0250e834233b8fdbf772d8cce01a1ff1ff32e24c
parentdfc9062eca47c237953c88a5614ae792792d058d
c++: Add __builtin_operator_{new,delete} support

clang++ adds __builtin_operator_{new,delete} builtins which as documented
work similarly to ::operator {new,delete}, except that it is an error
if the called ::operator {new,delete} is not a replaceable global operator
and allow optimizations which C++ normally allows just when those are used
from new/delete expressions https://eel.is/c++draft/expr.new#14
When using these builtins, the same optimizations can be done even when
using those builtins.

For GCC we note that in the CALL_FROM_NEW_OR_DELETE_P flag on CALL_EXPRs.
The following patch implements it as a C++ FE keyword (because passing
references through ... changes the argument and so BUILT_IN_FRONTEND
builtin can't be used), just attempts to call the ::operator {new,delete}
and if it isn't replaceable, diagnoses it.

libstdc++ already uses the builtin in some cases.

2024-11-11  Jakub Jelinek  <jakub@redhat.com>

gcc/c-family/
* c-common.h (enum rid): Add RID_BUILTIN_OPERATOR_NEW
and RID_BUILTIN_OPERATOR_DELETE.
(names_builtin_p): Change return type from bool to int.
* c-common.cc (c_common_reswords): Add __builtin_operator_new
and __builtin_operator_delete.
gcc/c/
* c-decl.cc (names_builtin_p): Change return type from
bool to int, adjust return statments.
gcc/cp/
* parser.cc (cp_parser_postfix_expression): Handle
RID_BUILTIN_OPERATOR_NEW and RID_BUILTIN_OPERATOR_DELETE.
* cp-objcp-common.cc (names_builtin_p): Change return type from
bool to int, adjust return statments.  Handle
RID_BUILTIN_OPERATOR_NEW and RID_BUILTIN_OPERATOR_DELETE.
* pt.cc (tsubst_expr) <case CALL_EXPR>: Handle
CALL_FROM_NEW_OR_DELETE_P.
gcc/
* doc/extend.texi (New/Delete Builtins): Document
__builtin_operator_new and __builtin_operator_delete.
gcc/testsuite/
* g++.dg/ext/builtin-operator-new-1.C: New test.
* g++.dg/ext/builtin-operator-new-2.C: New test.
* g++.dg/ext/builtin-operator-new-3.C: New test.
gcc/c-family/c-common.cc
gcc/c-family/c-common.h
gcc/c/c-decl.cc
gcc/cp/cp-objcp-common.cc
gcc/cp/parser.cc
gcc/cp/pt.cc
gcc/doc/extend.texi
gcc/testsuite/g++.dg/ext/builtin-operator-new-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/builtin-operator-new-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/builtin-operator-new-3.C [new file with mode: 0644]