test-error-call-through-ptr-with-mismatching-args.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. void
  6. create_code (gcc_jit_context *ctxt, void *user_data)
  7. {
  8. /* Let's try to inject the equivalent of:
  9. void
  10. test_fn (void (*some_fn_ptr) (void *))
  11. {
  12. some_fn_ptr (42);
  13. }
  14. and verify that the API complains about the mismatching argument
  15. type ("int" vs "void *"). */
  16. gcc_jit_type *void_type =
  17. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  18. gcc_jit_type *void_ptr_type =
  19. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
  20. gcc_jit_type *int_type =
  21. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  22. /* Build the function ptr type. */
  23. gcc_jit_type *fn_ptr_type =
  24. gcc_jit_context_new_function_ptr_type (ctxt, NULL,
  25. void_type,
  26. 1, &void_ptr_type, 0);
  27. /* Build the test_fn. */
  28. gcc_jit_param *param_fn_ptr =
  29. gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
  30. gcc_jit_function *test_fn =
  31. gcc_jit_context_new_function (ctxt, NULL,
  32. GCC_JIT_FUNCTION_EXPORTED,
  33. void_type,
  34. "test_fn",
  35. 1, &param_fn_ptr,
  36. 0);
  37. /* some_fn_ptr (42); */
  38. gcc_jit_rvalue *arg =
  39. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
  40. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  41. gcc_jit_block_add_eval (
  42. block, NULL,
  43. gcc_jit_context_new_call_through_ptr (
  44. ctxt,
  45. NULL,
  46. gcc_jit_param_as_rvalue (param_fn_ptr),
  47. 1, &arg));
  48. /* the above has the wrong type for argument 1. */
  49. gcc_jit_block_end_with_void_return (block, NULL);
  50. }
  51. void
  52. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  53. {
  54. CHECK_VALUE (result, NULL);
  55. /* Verify that the correct error message was emitted. */
  56. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  57. ("gcc_jit_context_new_call_through_ptr:"
  58. " mismatching types for argument 1 of fn_ptr:"
  59. " some_fn_ptr:"
  60. " assignment to param 1 (type: void *)"
  61. " from (int)42 (type: int)"));
  62. }