1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #include "harness.h"
       7  
       8  #ifdef __cplusplus
       9  extern "C" {
      10  #endif
      11  
      12    extern void
      13    called_function (void *ptr);
      14  
      15  #ifdef __cplusplus
      16  }
      17  #endif
      18  
      19  void
      20  create_code (gcc_jit_context *ctxt, void *user_data)
      21  {
      22    /* Let's try to inject the equivalent of:
      23       extern void called_function (void *ptr);
      24  
      25       void
      26       test_fn ()
      27       {
      28          called_function (42);
      29       }
      30  
      31       and verify that the API complains about the mismatching argument
      32       type ("int" vs "void *").  */
      33    gcc_jit_type *void_type =
      34      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
      35    gcc_jit_type *void_ptr_type =
      36      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
      37    gcc_jit_type *int_type =
      38      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
      39  
      40    /* Declare the imported function.  */
      41    gcc_jit_param *param =
      42      gcc_jit_context_new_param (ctxt, NULL, void_ptr_type, "ptr");
      43    gcc_jit_function *called_fn =
      44      gcc_jit_context_new_function (ctxt, NULL,
      45                                    GCC_JIT_FUNCTION_IMPORTED,
      46                                    void_type,
      47                                    "called_function",
      48                                    1, ¶m,
      49                                    0);
      50  
      51    /* Build the test_fn.  */
      52    gcc_jit_function *test_fn =
      53      gcc_jit_context_new_function (ctxt, NULL,
      54                                    GCC_JIT_FUNCTION_EXPORTED,
      55                                    void_type,
      56                                    "test_fn",
      57                                    0, NULL,
      58                                    0);
      59    /* called_function (42);  */
      60    gcc_jit_rvalue *arg =
      61      gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
      62  
      63    gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
      64    gcc_jit_block_add_eval (
      65      block, NULL,
      66      gcc_jit_context_new_call (ctxt,
      67                                NULL,
      68                                called_fn,
      69                                1, &arg));
      70    /* the above has the wrong type for argument 1.  */
      71    gcc_jit_block_end_with_void_return (block, NULL);
      72  }
      73  
      74  void
      75  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      76  {
      77    CHECK_VALUE (result, NULL);
      78  
      79    /* Verify that the correct error message was emitted.  */
      80    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      81  		      ("gcc_jit_context_new_call:"
      82  		       " mismatching types for argument 1"
      83  		       " of function \"called_function\":"
      84  		       " assignment to param ptr (type: void *)"
      85  		       " from (int)42 (type: int)"));
      86  }
      87