test-error-call-through-ptr-with-not-enough-args.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_caller (void (*some_fn_ptr) (int p))
  11. {
  12. called_function (); // missing arg
  13. }
  14. and verify that the API complains about the missing argument.
  15. */
  16. gcc_jit_type *void_type =
  17. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  18. gcc_jit_type *int_type =
  19. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  20. /* Build the function ptr type. */
  21. gcc_jit_type *fn_ptr_type =
  22. gcc_jit_context_new_function_ptr_type (ctxt, NULL,
  23. void_type,
  24. 1, &int_type, 0);
  25. /* Build the test_fn. */
  26. gcc_jit_param *param_fn_ptr =
  27. gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
  28. gcc_jit_function *test_fn =
  29. gcc_jit_context_new_function (ctxt, NULL,
  30. GCC_JIT_FUNCTION_EXPORTED,
  31. void_type,
  32. "test_caller",
  33. 1, &param_fn_ptr,
  34. 0);
  35. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  36. /* called_function (); */
  37. gcc_jit_block_add_eval (
  38. block, NULL,
  39. gcc_jit_context_new_call_through_ptr (
  40. ctxt,
  41. NULL,
  42. gcc_jit_param_as_rvalue (param_fn_ptr),
  43. 0, NULL));
  44. /* the above has not enough args. */
  45. gcc_jit_block_end_with_void_return (block, NULL);
  46. }
  47. void
  48. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  49. {
  50. /* Ensure that mismatching arg count leads to the API giving a NULL
  51. result back. */
  52. CHECK_VALUE (result, NULL);
  53. /* Verify that the correct error message was emitted. */
  54. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  55. ("gcc_jit_context_new_call_through_ptr:"
  56. " not enough arguments to fn_ptr: some_fn_ptr"
  57. " (got 0 args, expected 1)"));
  58. }