(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-error-return-within-void-function.c
       1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #include "harness.h"
       7  
       8  void
       9  create_code (gcc_jit_context *ctxt, void *user_data)
      10  {
      11    /* Let's try to inject the equivalent of:
      12       void
      13       test_fn ()
      14       {
      15          return 42;
      16       }
      17  
      18       and verify that the API complains about the return
      19       of a value within a function with "void" return.
      20    */
      21    gcc_jit_type *void_type =
      22      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
      23    gcc_jit_type *int_type =
      24      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      25  
      26    gcc_jit_function *test_fn =
      27      gcc_jit_context_new_function (ctxt, NULL,
      28                                    GCC_JIT_FUNCTION_EXPORTED,
      29                                    void_type, /* "void" return */
      30                                    "test_fn",
      31                                    0, NULL,
      32                                    0);
      33    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      34  
      35    /* "return 42;"  (i.e. non-void) */
      36    gcc_jit_block_end_with_return (
      37      block, NULL,
      38      gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
      39  }
      40  
      41  void
      42  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      43  {
      44    /* Ensure that the "return 42" leads to the API giving a NULL
      45       result back.  */
      46    CHECK_VALUE (result, NULL);
      47  
      48    /* Verify that the correct error message was emitted.  */
      49    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      50  		      "gcc_jit_block_end_with_return:"
      51  		      " mismatching types: return of (int)42 (type: int)"
      52  		      " in function test_fn (return type: void)");
      53  }
      54