test-error-call-through-ptr-with-too-many-args.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. extern void
  9. called_function (void);
  10. #ifdef __cplusplus
  11. }
  12. #endif
  13. void
  14. create_code (gcc_jit_context *ctxt, void *user_data)
  15. {
  16. /* Let's try to inject the equivalent of:
  17. void
  18. test_caller (void (*some_fn_ptr) (void), int a)
  19. {
  20. some_fn_ptr (a);
  21. }
  22. and verify that the API complains about the mismatching arg
  23. counts.
  24. */
  25. gcc_jit_type *void_type =
  26. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  27. gcc_jit_type *int_type =
  28. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  29. /* Build the function ptr type. */
  30. gcc_jit_type *fn_ptr_type =
  31. gcc_jit_context_new_function_ptr_type (ctxt, NULL,
  32. void_type,
  33. 0, NULL, 0);
  34. /* Build the test_fn. */
  35. gcc_jit_param *param_fn_ptr =
  36. gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
  37. gcc_jit_param *param_a =
  38. gcc_jit_context_new_param (ctxt, NULL, int_type, "a");
  39. gcc_jit_param *params[2];
  40. params[0] = param_fn_ptr;
  41. params[1] = param_a;
  42. gcc_jit_function *test_fn =
  43. gcc_jit_context_new_function (ctxt, NULL,
  44. GCC_JIT_FUNCTION_EXPORTED,
  45. void_type,
  46. "test_caller",
  47. 2, params,
  48. 0);
  49. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  50. /* some_fn_ptr (a); */
  51. gcc_jit_rvalue *arg = gcc_jit_param_as_rvalue (param_a);
  52. gcc_jit_block_add_eval (
  53. block, NULL,
  54. gcc_jit_context_new_call_through_ptr (
  55. ctxt,
  56. NULL,
  57. gcc_jit_param_as_rvalue (param_fn_ptr),
  58. 1, &arg));
  59. /* the above has too many args. */
  60. gcc_jit_block_end_with_void_return (block, NULL);
  61. }
  62. void
  63. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  64. {
  65. /* Ensure that mismatching arg count leads to the API giving a NULL
  66. result back. */
  67. CHECK_VALUE (result, NULL);
  68. /* Verify that the correct error message was emitted. */
  69. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  70. "gcc_jit_context_new_call_through_ptr:"
  71. " too many arguments to fn_ptr:"
  72. " some_fn_ptr (got 1 args, expected 0)");
  73. }