Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/source/expr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,18 @@ in C++14, but not C++11). It also removes C++11 structural constraints for
``constexpr`` functions and constructors (e.g., it allows multiple statements
in definitions).

The global variable ``constexpr_exceptions_enabled`` is TRUE when exceptions
can be thrown and caught during constant evaluation (a C++26 feature; it can
also be controlled with ``--set_flag`` and ``--clear_flag`` options naming
``constexpr_exceptions``, and it is FALSE whenever ``exceptions_enabled`` is
FALSE). The interpreter then evaluates ``throw`` expressions and ``try``
blocks: A thrown exception unwinds the interpreted evaluation, destroying
objects along the way, until a matching handler is found. An exception that
is not caught within the evaluation, or that causes ``std::terminate`` to be
called, makes the interpretation fail. The section "Control Transfers and
Exceptions" in the comments at the start of ``interpret.c`` describes the
implementation.

To avoid burdening the IL or the rest of the front end, the interpreter does
not record state information in the IL or other front end structures. Instead,
it maintains its own data structures linked to the IL through efficient hash
Expand Down
26 changes: 26 additions & 0 deletions src/cmd_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ STATIC_THREAD a_boolean
value of FALSE. */
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */

STATIC_THREAD a_boolean
constexpr_exceptions_flag_used;
/* TRUE if constexpr_exceptions_enabled was set or
cleared by a --set_flag or --clear_flag option, so
that the language mode does not determine it. */


static void add_config_dependent_option_description(
an_option_kind kind,
Expand Down Expand Up @@ -2546,6 +2552,7 @@ STATIC_THREAD a_flag_name
#endif /* IL_SHOULD_BE_WRITTEN_TO_FILE */
{ "extended_float_types", &extended_float_types },
{ "use_predefined_macro_file", &use_predefined_macro_file },
{ "constexpr_exceptions", &constexpr_exceptions_enabled },
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{ NULL, NULL } /* must be last */
};

Expand All @@ -2569,6 +2576,9 @@ be given to the flag.
str_command_line_error(ec_cl_invalid_flag_name, flag_name);
} /* if */
*flag_var = value;
if (flag_var == &constexpr_exceptions_enabled) {
constexpr_exceptions_flag_used = TRUE;
} /* if */
} /* set_flag_value */


Expand Down Expand Up @@ -4449,6 +4459,9 @@ default mode (e.g., exception handling).
embed_enabled = TRUE;
struct_binding_packs_enabled = TRUE;
pack_indexing_enabled = TRUE;
if (!constexpr_exceptions_flag_used) {
constexpr_exceptions_enabled = TRUE;
} /* if */
} /* if */
/* Disable "false" as a null pointer constant in C++11 mode (as per Core
issue 903). */
Expand Down Expand Up @@ -12387,6 +12400,17 @@ Process the arguments on the command line that invoked the compiler.
} /* if */
nonclass_prototype_instantiations = TRUE;
} /* if */
if (!exceptions_enabled) {
/* Exceptions cannot be thrown during constant evaluation if they cannot be
thrown at all. */
constexpr_exceptions_enabled = FALSE;
} /* if */
if (constexpr_exceptions_enabled) {
/* Catching exceptions during constant evaluation requires try-blocks in
constexpr functions (e.g., with --set_flag=constexpr_exceptions in
C++17 mode). */
constexpr_try_enabled = TRUE;
} /* if */
if (lambdas_enabled) {
/* If lambdas are allowed, enable local types as template arguments too. */
local_types_as_template_args_enabled = TRUE;
Expand Down Expand Up @@ -13862,6 +13886,8 @@ variables declared in cmd_line.h.
embed_enabled = FALSE;
struct_binding_packs_enabled = FALSE;
pack_indexing_enabled = FALSE;
constexpr_exceptions_enabled = FALSE;
constexpr_exceptions_flag_used = FALSE;
} /* cmd_line_static_var_init */


Expand Down
5 changes: 5 additions & 0 deletions src/cmd_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,11 @@ EXTERN_THREAD a_boolean
pack_indexing_enabled;
/* TRUE if C++26 pack indexing is supported. */

EXTERN_THREAD a_boolean
constexpr_exceptions_enabled;
/* TRUE if exceptions can be thrown and caught during
constant evaluation (a C++26 feature). */

/*
Macro that determines whether CTAD for alias templates should be accepted.
Alias-template CTAD is a standard feature in C++20 mode (and the global
Expand Down
23 changes: 23 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -7740,6 +7740,29 @@ list is freed and the list passed in is reset.
} /* discard_more_info_list */


void truncate_more_info_list(a_diag_list_ptr dlp,
a_diagnostic_ptr last_kept)
/*
Discard the "more information" diagnostics that follow the entry last_kept on
the list specified by dlp (all of them, if last_kept is NULL). last_kept must
be an entry of the list (or NULL).
*/
{
a_diag_list discarded;

if (last_kept == NULL) {
discarded = *dlp;
clear_diag_list(dlp);
} else {
discarded.head = last_kept->next;
discarded.tail = dlp->tail;
last_kept->next = NULL;
dlp->tail = last_kept;
} /* if */
if (discarded.head != NULL) free_diag_list_elements(&discarded);
} /* truncate_more_info_list */


void more_info_diagnostic(an_error_code error_code,
a_source_position *error_pos,
a_diag_list_ptr diag_list)
Expand Down
3 changes: 3 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@ extern void splice_diag_list(a_diag_list_ptr src,

extern void discard_more_info_list(a_diag_list_ptr dlp);

extern void truncate_more_info_list(a_diag_list_ptr dlp,
a_diagnostic_ptr last_kept);

extern void pch_message(an_error_code error_code,
a_const_char *fill_in_str);

Expand Down
6 changes: 6 additions & 0 deletions src/error_msg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3683,5 +3683,11 @@ ec_bad_gcc_unroll_pragma_value;;"the argument of \"#pragma GCC unroll\" must be
ec_array_comparison_deprecated;;"comparison between two arrays is deprecated"
ec_array_comparison_not_allowed;;"comparison between two arrays is not allowed"
ec_array_comparison_compares_addresses;;"comparison between two arrays compares their addresses"
ec_constexpr_uncaught_exception;;"an exception of type %t is thrown and not caught during constant evaluation"
ec_constexpr_rethrow_without_exception;;"a rethrow (\"throw;\") is evaluated when no exception is being handled"
ec_constexpr_exception_escapes_nonthrowing;;"std::terminate is called because an exception escapes from non-throwing %nd"
ec_constexpr_exception_during_unwinding;;"std::terminate is called because an exception escapes from a destructor during stack unwinding"
ec_constexpr_exception_in_handler_init;;"std::terminate is called because an exception escapes from the initialization of a handler parameter"
ec_constexpr_exception_from_exception_dtor;;"std::terminate is called because the destructor of an exception object exits by throwing an exception"
###############################################################################

Loading
Loading