(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-error-bad-cast.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  
      13       int
      14       test_fn ()
      15       {
      16         struct foo f;
      17         return (int)f;
      18       }
      19  
      20       and verify that the API complains about the bad cast.
      21    */
      22    gcc_jit_type *int_type =
      23      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      24  
      25  
      26    gcc_jit_struct *struct_foo =
      27      gcc_jit_context_new_struct_type (ctxt, NULL, "foo",
      28  				     0, NULL);
      29  
      30    gcc_jit_function *test_fn =
      31      gcc_jit_context_new_function (ctxt, NULL,
      32                                    GCC_JIT_FUNCTION_EXPORTED,
      33                                    int_type,
      34                                    "test_fn",
      35                                    0, NULL,
      36                                    0);
      37    gcc_jit_lvalue *f =
      38      gcc_jit_function_new_local (
      39        test_fn,
      40        NULL,
      41        gcc_jit_struct_as_type (struct_foo), "f");
      42  
      43    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      44  
      45    gcc_jit_block_end_with_return (
      46      block, NULL,
      47      gcc_jit_context_new_cast (ctxt, NULL,
      48  			      gcc_jit_lvalue_as_rvalue (f),
      49  			      int_type));
      50  }
      51  
      52  void
      53  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      54  {
      55    CHECK_VALUE (result, NULL);
      56  
      57    /* Verify that the correct error message was emitted.  */
      58    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      59  		      "gcc_jit_context_new_cast:"
      60  		      " cannot cast f from type: struct foo"
      61  		      " to type: int");
      62  }
      63