test-error-call-with-not-enough-args.c 2.3 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. extern void called_function (int p);
  18. void
  19. test_caller ()
  20. {
  21. called_function (); // missing arg
  22. }
  23. and verify that the API complains about the missing argument.
  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. /* Declare the imported function. */
  30. gcc_jit_param *param_p =
  31. gcc_jit_context_new_param (ctxt, NULL, int_type, "p");
  32. gcc_jit_function *called_fn =
  33. gcc_jit_context_new_function (ctxt, NULL,
  34. GCC_JIT_FUNCTION_IMPORTED,
  35. void_type,
  36. "called_function",
  37. 1, &param_p,
  38. 0);
  39. /* Build the test_fn. */
  40. gcc_jit_function *test_fn =
  41. gcc_jit_context_new_function (ctxt, NULL,
  42. GCC_JIT_FUNCTION_EXPORTED,
  43. void_type,
  44. "test_caller",
  45. 0, NULL,
  46. 0);
  47. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  48. /* called_function (); */
  49. gcc_jit_block_add_eval (
  50. block, NULL,
  51. gcc_jit_context_new_call (ctxt,
  52. NULL,
  53. called_fn,
  54. 0, NULL));
  55. /* the above has the wrong arg count. */
  56. gcc_jit_block_end_with_void_return (block, NULL);
  57. }
  58. extern void
  59. called_function (void)
  60. {
  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:"
  71. " not enough arguments to function \"called_function\""
  72. " (got 0 args, expected 1)"));
  73. }