]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/contracts: ICE in C++ Contracts with '-fno-exceptions' [PR 110159]
authorNina Ranns <dinka.ranns@gmail.com>
Thu, 11 Jul 2024 16:47:34 +0000 (17:47 +0100)
committerJason Merrill <jason@redhat.com>
Tue, 16 Jul 2024 18:51:52 +0000 (14:51 -0400)
We currently only initialise terminate_fn if exceptions are enabled.
However, contract handling requires terminate_fn when building the
contract because a contract failure may result in std::terminate call
regardless of whether the exceptions are enabled. Refactored
init_exception_processing to extract the initialisation of
terminate_fn. New function init_terminate_fn added that initialises
terminate_fn if it hasn't already been initialised. Call to terminate_fn
added in cxx_init_decl_processing if contracts are enabled.

PR c++/110159

gcc/cp/ChangeLog:

* cp-tree.h (init_terminate_fn): Declaration of a new function.
* decl.cc (cxx_init_decl_processing): If contracts are enabled,
call init_terminate_fn.
* except.cc (init_exception_processing): Function refactored to
call init_terminate_fn.
(init_terminate_fn): Added new function that initializes
terminate_fn if it hasn't already been initialised.

gcc/testsuite/ChangeLog:

* g++.dg/contracts/pr110159.C: New test.

Signed-off-by: Nina Ranns <dinka.ranns@gmail.com>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/except.cc
gcc/testsuite/g++.dg/contracts/pr110159.C [new file with mode: 0644]

index c1a371bc72184f1a4e948df1f6bb81f9f28c3b34..c6f102564ce0d6230e50dd9e0169e4b6cc3e74d1 100644 (file)
@@ -7194,6 +7194,7 @@ extern void qualified_name_lookup_error           (tree, tree, tree,
                                                 location_t);
 
 /* in except.cc */
+extern void init_terminate_fn                  (void);
 extern void init_exception_processing          (void);
 extern tree expand_start_catch_block           (tree);
 extern void expand_end_catch_block             (void);
index d64b993329dd243a809562cc5c50266ca0e738c2..66e8a973cce56316248a00d96c24a87dd2e30fe1 100644 (file)
@@ -5172,6 +5172,9 @@ cxx_init_decl_processing (void)
   if (flag_exceptions)
     init_exception_processing ();
 
+  if (flag_contracts)
+    init_terminate_fn ();
+
   if (modules_p ())
     init_modules (parse_in);
 
index 1eb3ba53b4b57207391b7bb2bc2bc85cdcff14c5..3c69ab6950283295471a8ea7f45154fa498e4ffa 100644 (file)
@@ -42,15 +42,17 @@ static tree wrap_cleanups_r (tree *, int *, void *);
 static bool is_admissible_throw_operand_or_catch_parameter (tree, bool,
                                                            tsubst_flags_t);
 
-/* Sets up all the global eh stuff that needs to be initialized at the
-   start of compilation.  */
+/* Initializes the node to std::terminate, which is used in exception
+  handling and contract handling.  */
 
 void
-init_exception_processing (void)
+init_terminate_fn (void)
 {
+  if (terminate_fn)
+    return;
+
   tree tmp;
 
-  /* void std::terminate (); */
   push_nested_namespace (std_node);
   tmp = build_function_type_list (void_type_node, NULL_TREE);
   terminate_fn = build_cp_library_fn_ptr ("terminate", tmp,
@@ -60,6 +62,19 @@ init_exception_processing (void)
                       && TREE_NOTHROW (terminate_fn));
   pop_nested_namespace (std_node);
 
+}
+
+/* Sets up all the global eh stuff that needs to be initialized at the
+   start of compilation.  */
+
+void
+init_exception_processing (void)
+{
+  tree tmp;
+
+  /* void std::terminate (); */
+  init_terminate_fn ();
+
   /* void __cxa_call_unexpected(void *); */
   tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
   call_unexpected_fn
diff --git a/gcc/testsuite/g++.dg/contracts/pr110159.C b/gcc/testsuite/g++.dg/contracts/pr110159.C
new file mode 100644 (file)
index 0000000..614b466
--- /dev/null
@@ -0,0 +1,27 @@
+// check that contracts can be handled even when exceptions are disabled
+// { dg-do run }
+// { dg-options "-std=c++2a -fcontracts -fno-exceptions " }
+// { dg-output "contract violation in function f at .* a<5" }
+
+#include <exception>
+#include <cstdlib>
+
+int terminate_called = 0;
+void my_term()
+{
+    std::exit(0);
+}
+
+
+void f(int a)
+  [[ pre : a<5 ]]
+  {
+  }
+
+int
+main ()
+{
+  std::set_terminate (my_term);
+  f(3);
+  f(10);
+}