On 3/28/26 12:48 PM, Jakub Jelinek wrote:
On Fri, Mar 27, 2026 at 11:25:45AM +0100, Jakub Jelinek wrote:
On Thu, Mar 26, 2026 at 08:40:36AM +0100, Jakub Jelinek wrote:
The following patch attempts to implement the
P3842R1 - A conservative fix for constexpr uncaught_exceptions()
          and current_exception()
paper.  std::current_exception() and std::uncaught_exceptions() aren't
constexpr anymore (admittedly, they weren't declared that way in the
headers before either, but the FE magically treated them as such),
instead two new FE builtins are introduced so that one can get their
behavior before this patch when needed.  So, at constant evaluation
time they are constant evaluated and if they are not folded away,
they are transformed into std::current_exception() or
std::uncaught_exceptions() runtime calls during gimplification.

Compared to the paper, I had to also deal with nested_exception default
ctor which also calls current_exception.

Lightly tested on x86_64-linux so far.

Here is an updated version of the patch which implements P3842R2.
In particular, reverts all nested_exception.h changes over the last year
and removes it from the constexpr-eh3.C test.  The proposed
__builtin_current_exception() is I believe exactly the
<it>current-exception</i>() in the paper.

Here is a third version of the patch, no longer RFC PATCH as it has been
voted in.  Testing found a regression in
18_support/exception_ptr/exception_ptr_cast.cc
caused by the earlier version of the patch, so this patch adjusts it to
use __builtin_current_exception() if possible and fall back to not trying
to test test01 during constant evaluation otherwise.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2026-03-28  Jakub Jelinek  <[email protected]>

gcc/cp/
        * cp-tree.h (enum cp_built_in_function): Add
        CP_BUILT_IN_CURRENT_EXCEPTION and CP_BUILT_IN_UNCAUGHT_EXCEPTIONS.
        * tree.cc (builtin_valid_in_constant_expr_p): Handle
        those.
        * decl.cc (cxx_init_decl_processing): Build
        decls for CP_BUILT_IN_CURRENT_EXCEPTION and
        CP_BUILT_IN_UNCAUGHT_EXCEPTIONS.
        (require_deduced_type): Deduce CP_BUILT_IN_CURRENT_EXCEPTION
        return type.
        * cp-gimplify.cc (cp_gimplify_expr): Handle
        CP_BUILT_IN_CURRENT_EXCEPTION and CP_BUILT_IN_UNCAUGHT_EXCEPTIONS.
        * constexpr.cc (enum cxa_builtin): Remove STD_UNCAUGHT_EXCEPTIONS
        and STD_CURRENT_EXCEPTION, add BUILTIN_UNCAUGHT_EXCEPTIONS and
        BUILTIN_CURRENT_EXCEPTION, renumber some enumerators.
        (cxx_cxa_builtin_fn_p): Don't check for std::uncaught_exceptions
        nor std::current_exception.
        (cxx_eval_cxa_builtin_fn): Handle BUILTIN_UNCAUGHT_EXCEPTIONS
        and BUILTIN_CURRENT_EXCEPTION instead of STD_UNCAUGHT_EXCEPTIONS
        and STD_CURRENT_EXCEPTION.
        (cxx_eval_builtin_function_call): Handle
        CP_BUILT_IN_CURRENT_EXCEPTION and CP_BUILT_IN_UNCAUGHT_EXCEPTIONS.
        Do just one fndecl_builtin_p check for BUILT_IN_FRONTEND and
        switch on DECL_FE_FUNCTION_CODE.
gcc/testsuite/
        * g++.dg/cpp26/constexpr-eh3.C: Use __builtin_current_exception()
        instead of std::current_exception() and
        __builttin_uncaught_exceptions() instead of
        std::uncaught_exceptions().  Remove nested_exception related parts
        of the test.
        * g++.dg/cpp26/constexpr-eh8.C: Use __builtin_current_exception()
        instead of std::current_exception().
libstdc++-v3/
        * libsupc++/exception_ptr.h (std::make_exception_ptr): Use
        __builtin_current_exception() instead of current_exception() if
        the builtin exists.
        * libsupc++/nested_exception.h: Revert 2026-01-07 and 2025-07-10
        changes.
        * testsuite/18_support/exception_ptr/exception_ptr_cast.cc (test01):
        Use __builtin_current_exception instead of std::current_exception
        if __has_builtin(__builtin_current_exception).  Otherwise don't
        try to constant evaluate test01.

--- gcc/cp/cp-tree.h.jj 2026-03-27 10:17:14.017332648 +0100
+++ gcc/cp/cp-tree.h    2026-03-27 10:20:42.997381886 +0100
@@ -7135,6 +7135,8 @@ enum cp_built_in_function {
    CP_BUILT_IN_EH_PTR_ADJUST_REF,
    CP_BUILT_IN_IS_STRING_LITERAL,
    CP_BUILT_IN_CONSTEXPR_DIAG,
+  CP_BUILT_IN_CURRENT_EXCEPTION,
+  CP_BUILT_IN_UNCAUGHT_EXCEPTIONS,
    CP_BUILT_IN_LAST
  };
--- gcc/cp/tree.cc.jj 2026-03-27 10:17:14.036332338 +0100
+++ gcc/cp/tree.cc      2026-03-27 10:20:42.998245213 +0100
@@ -570,6 +570,8 @@ builtin_valid_in_constant_expr_p (const_
          case CP_BUILT_IN_EH_PTR_ADJUST_REF:
          case CP_BUILT_IN_IS_STRING_LITERAL:
          case CP_BUILT_IN_CONSTEXPR_DIAG:
+         case CP_BUILT_IN_CURRENT_EXCEPTION:
+         case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
            return true;
          default:
            break;
--- gcc/cp/decl.cc.jj   2026-03-27 10:17:14.019332616 +0100
+++ gcc/cp/decl.cc      2026-03-27 10:20:42.999379349 +0100
@@ -5627,6 +5627,20 @@ cxx_init_decl_processing (void)
                                   CP_BUILT_IN_EH_PTR_ADJUST_REF,
                                   BUILT_IN_FRONTEND, NULL, NULL_TREE);
        set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
+
+      /* Similar case to __builtin_source_locaiton above.  The concrete

"location"

OK with that typo fix.

Jason

Reply via email to