test-error-return-within-void-function.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ()
  11. {
  12. return 42;
  13. }
  14. and verify that the API complains about the return
  15. of a value within a function with "void" return.
  16. */
  17. gcc_jit_type *void_type =
  18. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  19. gcc_jit_type *int_type =
  20. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  21. gcc_jit_function *test_fn =
  22. gcc_jit_context_new_function (ctxt, NULL,
  23. GCC_JIT_FUNCTION_EXPORTED,
  24. void_type, /* "void" return */
  25. "test_fn",
  26. 0, NULL,
  27. 0);
  28. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  29. /* "return 42;" (i.e. non-void) */
  30. gcc_jit_block_end_with_return (
  31. block, NULL,
  32. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
  33. }
  34. void
  35. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  36. {
  37. /* Ensure that the "return 42" leads to the API giving a NULL
  38. result back. */
  39. CHECK_VALUE (result, NULL);
  40. /* Verify that the correct error message was emitted. */
  41. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  42. "gcc_jit_block_end_with_return:"
  43. " mismatching types: return of (int)42 (type: int)"
  44. " in function test_fn (return type: void)");
  45. }