test-error-dereference-field-of-non-pointer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #include "harness.h"
  5. struct foo
  6. {
  7. int x;
  8. int y;
  9. };
  10. void
  11. create_code (gcc_jit_context *ctxt, void *user_data)
  12. {
  13. /* Let's try to inject the equivalent of:
  14. void
  15. test_bogus_dereference ()
  16. {
  17. struct foo tmp;
  18. tmp->x = tmp->y;
  19. }
  20. i.e. where tmp is *not* a pointer.
  21. */
  22. gcc_jit_type *void_type =
  23. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  24. gcc_jit_type *int_type =
  25. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  26. /* Map "struct foo". */
  27. gcc_jit_field *x =
  28. gcc_jit_context_new_field (ctxt,
  29. NULL,
  30. int_type,
  31. "x");
  32. gcc_jit_field *y =
  33. gcc_jit_context_new_field (ctxt,
  34. NULL,
  35. int_type,
  36. "y");
  37. gcc_jit_field *foo_fields[] = {x, y};
  38. gcc_jit_struct *struct_foo =
  39. gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);
  40. /* Build the test function. */
  41. gcc_jit_function *test_fn =
  42. gcc_jit_context_new_function (ctxt, NULL,
  43. GCC_JIT_FUNCTION_EXPORTED,
  44. void_type,
  45. "test_bogus_dereference",
  46. 0, NULL,
  47. 0);
  48. gcc_jit_lvalue *tmp =
  49. gcc_jit_function_new_local (test_fn, NULL,
  50. gcc_jit_struct_as_type (struct_foo),
  51. "tmp");
  52. gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
  53. /* Erroneous: tmp->x = ... */
  54. gcc_jit_lvalue *lvalue =
  55. gcc_jit_rvalue_dereference_field (
  56. gcc_jit_lvalue_as_rvalue (tmp),
  57. NULL,
  58. x);
  59. /* Erroneous: ... = tmp->y; */
  60. gcc_jit_rvalue *rvalue =
  61. gcc_jit_lvalue_as_rvalue (
  62. gcc_jit_rvalue_dereference_field (
  63. gcc_jit_lvalue_as_rvalue (tmp),
  64. NULL,
  65. y));
  66. gcc_jit_block_add_assignment (
  67. block,
  68. NULL,
  69. lvalue, rvalue);
  70. gcc_jit_block_end_with_void_return (block, NULL);
  71. }
  72. void
  73. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  74. {
  75. CHECK_VALUE (result, NULL);
  76. /* Verify that the correct error message was emitted. */
  77. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  78. ("gcc_jit_rvalue_dereference_field:"
  79. " dereference of non-pointer tmp (type: struct foo)"
  80. " when accessing ->x"));
  81. }