test-error-call-through-ptr-with-non-function.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_ptr)
  11. {
  12. ((some_unspecified_fn_ptr_type)some_ptr) (42);
  13. }
  14. and verify that the API complains about the 42 not being a
  15. function pointer. */
  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 test_fn. */
  23. gcc_jit_param *some_ptr =
  24. gcc_jit_context_new_param (ctxt, NULL, void_ptr_type, "some_ptr");
  25. gcc_jit_function *test_fn =
  26. gcc_jit_context_new_function (ctxt, NULL,
  27. GCC_JIT_FUNCTION_EXPORTED,
  28. void_type,
  29. "test_fn",
  30. 1, &some_ptr,
  31. 0);
  32. gcc_jit_rvalue *arg =
  33. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
  34. /* ((some_unspecified_fn_ptr_type)some_ptr) (42); */
  35. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  36. gcc_jit_block_add_eval (
  37. block, NULL,
  38. gcc_jit_context_new_call_through_ptr (
  39. ctxt,
  40. NULL,
  41. /* This is not a function pointer. */
  42. gcc_jit_param_as_rvalue (some_ptr),
  43. 1, &arg));
  44. gcc_jit_block_end_with_void_return (block, NULL);
  45. }
  46. void
  47. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  48. {
  49. CHECK_VALUE (result, NULL);
  50. /* Verify that the correct error message was emitted. */
  51. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  52. ("gcc_jit_context_new_call_through_ptr:"
  53. " fn_ptr is not a function ptr: some_ptr type: void *"));
  54. }