1  /*
       2  
       3    Test that the proper error is triggered when we initialize
       4    a global with another non-const global's rvalue.
       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_lvalue *foo;
      23    { /* int bar; */
      24      foo =  gcc_jit_context_new_global (
      25        ctxt, NULL,
      26        GCC_JIT_GLOBAL_EXPORTED,
      27        int_type,
      28        "global_lvalueinit_int1");
      29      gcc_jit_rvalue *rval = gcc_jit_context_new_rvalue_from_int (
      30        ctxt, int_type, 3);
      31      gcc_jit_global_set_initializer_rvalue (foo,
      32        rval);
      33    }
      34    { /* int foo = bar; */
      35  
      36      gcc_jit_lvalue *bar =  gcc_jit_context_new_global (
      37        ctxt, NULL,
      38        GCC_JIT_GLOBAL_EXPORTED,
      39        int_type,
      40        "global_lvalueinit_int2");
      41      gcc_jit_global_set_initializer_rvalue (bar,
      42  					   gcc_jit_lvalue_as_rvalue (foo));
      43    }
      44  }
      45  
      46  void
      47  verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
      48  {
      49    /* Ensure that the bad API usage prevents the API giving a bogus
      50       result back.  */
      51    CHECK_VALUE (result, NULL);
      52  
      53    /* Verify that the correct error message was emitted. */
      54    CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
      55  		      "unable to convert initial value for the global variable"
      56  		      " global_lvalueinit_int2 to a compile-time constant");
      57    CHECK_STRING_VALUE (gcc_jit_context_get_last_error (ctxt),
      58  		      "unable to convert initial value for the global variable"
      59  		      " global_lvalueinit_int2 to a compile-time constant");
      60  }