test-error-dereferencing-void-ptr.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <libgccjit.h>
  2. #include "harness.h"
  3. void
  4. create_code (gcc_jit_context *ctxt, void *user_data)
  5. {
  6. /* Replay of API calls for ctxt. */
  7. gcc_jit_type *type_long_long =
  8. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG);
  9. gcc_jit_type *type_void =
  10. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  11. gcc_jit_type *type_void_ptr =
  12. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
  13. gcc_jit_field *field_u_signed =
  14. gcc_jit_context_new_field (ctxt,
  15. NULL, /* gcc_jit_location *loc */
  16. type_long_long, /* gcc_jit_type *type, */
  17. "u_signed"); /* const char *name */
  18. gcc_jit_field *field_u_ptr =
  19. gcc_jit_context_new_field (ctxt,
  20. NULL, /* gcc_jit_location *loc */
  21. type_void_ptr, /* gcc_jit_type *type, */
  22. "u_ptr"); /* const char *name */
  23. gcc_jit_field *fields_for_union_any[2] = {
  24. field_u_signed,
  25. field_u_ptr,
  26. };
  27. gcc_jit_type *union_any =
  28. gcc_jit_context_new_union_type (ctxt,
  29. NULL, /* gcc_jit_location *loc */
  30. "any", /* const char *name */
  31. 2, /* int num_fields */
  32. fields_for_union_any);
  33. gcc_jit_function *func =
  34. gcc_jit_context_new_function (ctxt, /* gcc_jit_context *ctxt */
  35. NULL, /* gcc_jit_location *loc */
  36. GCC_JIT_FUNCTION_EXPORTED,
  37. type_void, /* gcc_jit_type *return_type */
  38. "anonloop_0", /* const char *name */
  39. 0, /* int num_params */
  40. NULL, /* gcc_jit_param **params */
  41. 0); /* int is_variadic */
  42. gcc_jit_block *block_initial =
  43. gcc_jit_function_new_block (func, "initial");
  44. gcc_jit_lvalue *local_tmp =
  45. gcc_jit_function_new_local (func, /* gcc_jit_function *func */
  46. NULL, /* gcc_jit_location *loc */
  47. union_any, /* gcc_jit_type *type */
  48. "tmp"); /* const char *name */
  49. /* "tmp.u_signed = 0x213d640;" */
  50. gcc_jit_block_add_assignment (
  51. block_initial, /*gcc_jit_block *block */
  52. NULL, /* gcc_jit_location *loc */
  53. gcc_jit_lvalue_access_field (local_tmp, /*gcc_jit_lvalue *struct_or_union */
  54. NULL, /*gcc_jit_location *loc */
  55. field_u_signed),
  56. gcc_jit_context_new_rvalue_from_long (
  57. ctxt, /* gcc_jit_context *ctxt */
  58. type_long_long, /* gcc_jit_type *numeric_type */
  59. 0x213d640)); /* long value */
  60. /* "(*tmp.u_ptr) += 1;" which can't be done since u_ptr is a (void *). */
  61. gcc_jit_block_add_assignment_op (
  62. block_initial, /*gcc_jit_block *block */
  63. NULL, /* gcc_jit_location *loc */
  64. /* "(*tmp.u_ptr)". */
  65. gcc_jit_rvalue_dereference (
  66. gcc_jit_lvalue_as_rvalue (
  67. gcc_jit_lvalue_access_field (
  68. local_tmp, /*gcc_jit_lvalue *struct_or_union */
  69. NULL, /*gcc_jit_location *loc */
  70. field_u_ptr)),
  71. NULL), /* gcc_jit_location *loc */
  72. GCC_JIT_BINARY_OP_PLUS, /* enum gcc_jit_binary_op op */
  73. gcc_jit_context_new_rvalue_from_int (
  74. ctxt, /* gcc_jit_context *ctxt */
  75. type_long_long, /* gcc_jit_type *numeric_type */
  76. 1)); /* int value */
  77. gcc_jit_block_end_with_void_return (block_initial, /*gcc_jit_block *block */
  78. NULL);
  79. }
  80. void
  81. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  82. {
  83. CHECK_VALUE (result, NULL);
  84. /* Verify that the correct error message was emitted. */
  85. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  86. "gcc_jit_rvalue_dereference:"
  87. " dereference of void pointer tmp.u_ptr"
  88. " (type: void *)");
  89. }