test-error-gcc_jit_lvalue_access_field-wrong-struct.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. struct bar
  11. {
  12. int p;
  13. int q;
  14. };
  15. void
  16. create_code (gcc_jit_context *ctxt, void *user_data)
  17. {
  18. /* Let's try to inject the equivalent of:
  19. void
  20. test_bogus_access (struct foo f)
  21. {
  22. f.p = f.x;
  23. }
  24. i.e. using the wrong struct for the LHS.
  25. */
  26. gcc_jit_type *void_type =
  27. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  28. gcc_jit_type *int_type =
  29. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  30. /* Map "struct foo". */
  31. gcc_jit_field *x =
  32. gcc_jit_context_new_field (ctxt,
  33. NULL,
  34. int_type,
  35. "x");
  36. gcc_jit_field *y =
  37. gcc_jit_context_new_field (ctxt,
  38. NULL,
  39. int_type,
  40. "y");
  41. gcc_jit_field *foo_fields[] = {x, y};
  42. gcc_jit_struct *struct_foo =
  43. gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);
  44. /* Map "struct bar". */
  45. gcc_jit_field *p =
  46. gcc_jit_context_new_field (ctxt,
  47. NULL,
  48. int_type,
  49. "p");
  50. gcc_jit_field *q =
  51. gcc_jit_context_new_field (ctxt,
  52. NULL,
  53. int_type,
  54. "q");
  55. /* We don't actually need a gcc_jit_type for "struct bar" for the test. */
  56. gcc_jit_field *bar_fields[] = {p, q};
  57. (void)gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, bar_fields);
  58. /* Build the test function. */
  59. gcc_jit_param *param_f =
  60. gcc_jit_context_new_param (ctxt, NULL,
  61. gcc_jit_struct_as_type (struct_foo), "f");
  62. gcc_jit_function *test_fn =
  63. gcc_jit_context_new_function (ctxt, NULL,
  64. GCC_JIT_FUNCTION_EXPORTED,
  65. void_type,
  66. "test_bogus_access",
  67. 1, &param_f,
  68. 0);
  69. /* Erroneous: f.p = ... */
  70. gcc_jit_lvalue *lvalue =
  71. gcc_jit_lvalue_access_field (
  72. gcc_jit_param_as_lvalue (param_f),
  73. NULL,
  74. p);
  75. /* OK: ... = f.x; */
  76. gcc_jit_rvalue *rvalue =
  77. gcc_jit_lvalue_as_rvalue (
  78. gcc_jit_lvalue_access_field (
  79. gcc_jit_param_as_lvalue (param_f),
  80. NULL,
  81. x));
  82. gcc_jit_block *block =
  83. gcc_jit_function_new_block (test_fn, NULL);
  84. gcc_jit_block_add_assignment (
  85. block,
  86. NULL,
  87. lvalue, rvalue);
  88. gcc_jit_block_end_with_void_return (block, NULL);
  89. }
  90. void
  91. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  92. {
  93. CHECK_VALUE (result, NULL);
  94. /* Verify that the correct error message was emitted. */
  95. CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
  96. "gcc_jit_lvalue_access_field:"
  97. " p is not a field of struct foo");
  98. }