I know P2758R5 didn't make it into C++26, but on IRC Ville said it would be
useful anyway, so here is a quick attempt at implementing it.
Not adding anything on the libstdc++ side, because I don't know where
experimental stuff like that should go, whether it would be in the
implementation namespace etc.
Tags are currently parsed and printed in the diagnostics, but -Wconstexpr-msg=
option and being able to #pragma GCC diagnostic ignore -Wconstexpr-msg=blah
will need to wait for GCC 17.
In any case, the compiler side is just one new builtin,
__builtin_constexpr_diag, whose first argument is 0 for print,
or 1 for warning or 2 for error (or that ored with 16 if location shouldn't
be caller's location but caller's caller location - useful when adding some
wrappers around the builtin), second argument is string_view of the tag
(or "" for no tag) and third argument is string_view or u8string_view
with the message. The builtin also handles string literals.
2026-02-17 Jakub Jelinek <jakub@redhat.com>
gcc/cp/
* cp-tree.h (enum cp_built_in_function): Add
CP_BUILT_IN_CONSTEXPR_DIAG.
(cexpr_str::cexpr_str): Add new default ctor.
(cexpr_str::type_check): Add optional allow_char8_t arg.
(cexpr_str::extract): Add optional ctx, non_constant_p, overflow_p
and jump_target arguments.
* cp-gimplify.cc (cp_gimplify_expr): Throw away
__builtin_constexpr_diag calls after gimplification of
their arguments.
* decl.cc (cxx_init_decl_processing): Create __builtin_constexpr_diag
FE builtin decl.
* constexpr.cc (call_stack, call_stack_tick, last_cx_error_tick):
Moved earlier.
(cxx_eval_constexpr_diag): New function.
(cxx_eval_builtin_function_call): Handle __builtin_constexpr_diag
calls.
* tree.cc (builtin_valid_in_constant_expr_p): Return true for
CP_BUILT_IN_CONSTEXPR_DIAG.
* semantics.cc (cexpr_str::type_check): Add allow_char8_t argument,
if true, allow data to return const char8_t *.
(cexpr_str::extract): Add ctx, non_constant_p, overflow_p and
jump_target arguments, if they are non-NULL, evalute expressions
in that context rather than using cxx_constant_value and translate
back to SOURCE_CHARSET even if message_data or message_sz are NULL.
gcc/testsuite/
* g++.dg/ext/constexpr-diag1.C: New test.
* g++.dg/ext/constexpr-diag2.C: New test.
* g++.dg/ext/constexpr-diag3.C: New test.
* g++.dg/ext/constexpr-diag4.C: New test.
* g++.dg/ext/constexpr-diag5.C: New test.
* g++.dg/ext/constexpr-diag6.C: New test.