(root)/
gcc-13.2.0/
gcc/
testsuite/
jit.dg/
test-error-global-nonconst-init.c
       1  /*
       2  
       3    Test that the proper error is triggered when we initialize
       4    a global with a function call.
       5  
       6    Using gcc_jit_global_set_initializer_rvalue()
       7  
       8  */
       9  
      10  #include <stdlib.h>
      11  #include <stdio.h>
      12  
      13  #include "libgccjit.h"
      14  #include "harness.h"
      15  
      16  void
      17  create_code (gcc_jit_context *ctxt, void *user_data)
      18  {
      19    gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt,
      20      GCC_JIT_TYPE_INT);
      21  
      22    gcc_jit_function *fn_int_3;
      23    { /* int foo () { int local = 3; return local;} */
      24      fn_int_3 =
      25        gcc_jit_context_new_function (ctxt,
      26  				    0,
      27  				    GCC_JIT_FUNCTION_EXPORTED,
      28  				    int_type,
      29  				    "fn_int_3",
      30  				    0,
      31  				    0,
      32  				    0);
      33      gcc_jit_block *block = gcc_jit_function_new_block (fn_int_3, "start");
      34      gcc_jit_lvalue *local = gcc_jit_function_new_local (fn_int_3,
      35  							0,
      36  							int_type,
      37  							"local");
      38      gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
      39        ctxt, int_type, 3);
      40  
      41      gcc_jit_block_add_assignment (block, 0, local, rval);
      42  
      43      gcc_jit_block_end_with_return (block,
      44  				   0,
      45  				   gcc_jit_lvalue_as_rvalue(local));
      46  
      47    }
      48  
      49    { /* int bar = foo(); */
      50      gcc_jit_rvalue *rval =
      51        gcc_jit_context_new_call (ctxt,
      52  				0,
      53  				fn_int_3,
      54  				0,0);
      55  
      56      gcc_jit_lvalue *foo =  gcc_jit_context_new_global (
      57        ctxt, NULL,
      58        GCC_JIT_GLOBAL_EXPORTED,
      59        int_type,
      60        "global_nonconst_int");
      61      gcc_jit_global_set_initializer_rvalue (foo,
      62        rval);
      63    }
      64  }
      65  
      66  void
      67  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      68  {
      69    /* Ensure that the bad API usage prevents the API giving a bogus
      70       result back.  */
      71    CHECK_VALUE (result, NULL);
      72  
      73    /* Verify that the correct error message was emitted. */
      74    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      75  		      "unable to convert initial value for the global variable"
      76  		      " global_nonconst_int to a compile-time constant");
      77    CHECK_STRING_VALUE (gcc_jit_context_get_last_error (ctxt),
      78  		      "unable to convert initial value for the global variable"
      79  		      " global_nonconst_int to a compile-time constant");
      80  }