(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-error-bad-assignment.c
       1  #include <stdlib.h>
       2  #include <stdio.h>
       3  
       4  #include "libgccjit.h"
       5  
       6  #include "harness.h"
       7  
       8  void
       9  create_code (gcc_jit_context *ctxt, void *user_data)
      10  {
      11    /* Let's try to inject the equivalent of:
      12  
      13       void
      14       test_fn ()
      15       {
      16          long integer;
      17          volatile const void *variable;
      18          variable = &integer;
      19          long aligned_integer __attribute__((aligned(4)));
      20          variable = &aligned_integer;
      21       }
      22  
      23       and verify that the API complains about the mismatching types
      24       in the assignments.
      25    */
      26    gcc_jit_type *void_type =
      27      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
      28    gcc_jit_type *long_type =
      29      gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
      30    gcc_jit_type *const_void_type =
      31      gcc_jit_type_get_const (void_type);
      32    gcc_jit_type *volatile_void_ptr =
      33      gcc_jit_type_get_pointer (gcc_jit_type_get_volatile (const_void_type));
      34  
      35    gcc_jit_function *func =
      36      gcc_jit_context_new_function (ctxt, NULL,
      37                                    GCC_JIT_FUNCTION_EXPORTED,
      38                                    void_type,
      39                                    "test_fn",
      40                                    0, NULL,
      41                                    0);
      42  
      43    gcc_jit_lvalue *integer = gcc_jit_function_new_local (func, NULL, long_type, "integer");
      44    gcc_jit_rvalue *address = gcc_jit_lvalue_get_address(integer, NULL);
      45  
      46    gcc_jit_lvalue *variable = gcc_jit_function_new_local (func, NULL, volatile_void_ptr, "variable");
      47    gcc_jit_block *initial =
      48      gcc_jit_function_new_block (func, "initial");
      49    gcc_jit_block_add_assignment(initial, NULL, variable, address);
      50  
      51    gcc_jit_type *aligned_long_type = gcc_jit_type_get_aligned (long_type, 4);
      52    gcc_jit_lvalue *aligned_integer = gcc_jit_function_new_local (func, NULL, aligned_long_type, "aligned_integer");
      53    gcc_jit_rvalue *aligned_address = gcc_jit_lvalue_get_address(aligned_integer, NULL);
      54  
      55    gcc_jit_block_add_assignment(initial, NULL, variable, aligned_address);
      56  
      57    gcc_jit_block_end_with_void_return (initial, NULL);
      58  }
      59  
      60  void
      61  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      62  {
      63    CHECK_VALUE (result, NULL);
      64  
      65    /* Verify that the correct error messages were emitted.  */
      66    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      67  		      "gcc_jit_block_add_assignment:"
      68  		      " mismatching types:"
      69  		      " assignment to variable (type: volatile const void *)"
      70  		      " from &integer (type: long *)");
      71  
      72    CHECK_STRING_VALUE (gcc_jit_context_get_last_error (ctxt),
      73  		      "gcc_jit_block_add_assignment:"
      74  		      " mismatching types:"
      75  		      " assignment to variable (type: volatile const void *)"
      76  		      " from &aligned_integer (type: long  __attribute__((aligned(4))) *)");
      77  }
      78