test-error-local-used-from-other-function.c 1.7 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. fn_one ()
  11. {
  12. int i;
  13. }
  14. int
  15. fn_two ()
  16. {
  17. return i;
  18. }
  19. and verify that the API complains about the use of the local
  20. from the other function. */
  21. gcc_jit_type *void_type =
  22. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  23. gcc_jit_type *int_type =
  24. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  25. gcc_jit_function *fn_one =
  26. gcc_jit_context_new_function (ctxt, NULL,
  27. GCC_JIT_FUNCTION_EXPORTED,
  28. void_type,
  29. "fn_one",
  30. 0, NULL,
  31. 0);
  32. gcc_jit_lvalue *local =
  33. gcc_jit_function_new_local (fn_one, NULL, int_type, "i");
  34. gcc_jit_function *fn_two =
  35. gcc_jit_context_new_function (ctxt, NULL,
  36. GCC_JIT_FUNCTION_EXPORTED,
  37. int_type,
  38. "fn_two",
  39. 0, NULL,
  40. 0);
  41. gcc_jit_block *block = gcc_jit_function_new_block (fn_two, NULL);
  42. /* "return i;", using local i from the wrong function. */
  43. gcc_jit_block_end_with_return (block,
  44. NULL,
  45. gcc_jit_lvalue_as_rvalue (local));
  46. }
  47. void
  48. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  49. {
  50. CHECK_VALUE (result, NULL);
  51. /* Verify that the correct error message was emitted. */
  52. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  53. ("gcc_jit_block_end_with_return:"
  54. " rvalue i (type: int)"
  55. " has scope limited to function fn_one"
  56. " but was used within function fn_two"
  57. " (in statement: return i;)"));
  58. }