9 #define BUFFER_SIZE (1024)
11 char test_buffer
[1024];
14 create_code (gcc_jit_context
*ctxt
, void *user_data
)
16 /* Let's try to inject the equivalent of:
17 void test_of_array_as_pointer (const char *name)
19 snprintf (test_buffer, sizeof (test_buffer),
23 gcc_jit_type
*void_type
=
24 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
25 gcc_jit_type
*const_char_ptr_type
=
26 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CONST_CHAR_PTR
);
27 gcc_jit_type
*char_type
=
28 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CHAR
);
29 gcc_jit_type
*char_ptr_type
=
30 gcc_jit_type_get_pointer (char_type
);
31 gcc_jit_type
*int_type
=
32 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
33 gcc_jit_type
*size_t_type
=
34 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_SIZE_T
);
35 gcc_jit_type
*buf_type
=
36 gcc_jit_context_new_array_type (ctxt
, NULL
, char_type
, BUFFER_SIZE
);
38 /* extern int snprintf(char *str, size_t size, const char *format, ...); */
39 gcc_jit_param
*param_s
=
40 gcc_jit_context_new_param (ctxt
, NULL
, char_ptr_type
, "s");
41 gcc_jit_param
*param_n
=
42 gcc_jit_context_new_param (ctxt
, NULL
, size_t_type
, "n");
43 gcc_jit_param
*param_format
=
44 gcc_jit_context_new_param (ctxt
, NULL
, const_char_ptr_type
, "format");
45 gcc_jit_param
*snprintf_params
[3] = {param_s
, param_n
, param_format
};
46 gcc_jit_function
*snprintf
=
47 gcc_jit_context_new_function (ctxt
, NULL
,
48 GCC_JIT_FUNCTION_IMPORTED
,
54 gcc_jit_param
*param_name
=
55 gcc_jit_context_new_param (ctxt
, NULL
, const_char_ptr_type
, "name");
56 gcc_jit_function
*test_fn
=
57 gcc_jit_context_new_function (ctxt
, NULL
,
58 GCC_JIT_FUNCTION_EXPORTED
,
60 "test_of_array_as_pointer",
64 gcc_jit_lvalue
*buffer
=
65 gcc_jit_context_new_global (ctxt
, NULL
,
66 GCC_JIT_GLOBAL_IMPORTED
,
70 gcc_jit_block
*block
= gcc_jit_function_new_block(test_fn
, "entry");
72 /* snprintf(buffer, sizeof(buffer), "hello %s", name); */
73 gcc_jit_rvalue
*args
[4];
74 args
[0] = gcc_jit_context_new_cast (
76 /* Here's the difference with test-array-as-pointer.c: */
77 gcc_jit_lvalue_as_rvalue (buffer
),
79 args
[1] = gcc_jit_context_new_rvalue_from_int (ctxt
,
82 args
[2] = gcc_jit_context_new_string_literal (ctxt
, "hello %s");
83 args
[3] = gcc_jit_param_as_rvalue (param_name
);
85 gcc_jit_block_add_eval (
87 gcc_jit_context_new_call (ctxt
, NULL
, snprintf
, 4, args
));
88 gcc_jit_block_end_with_void_return (block
, NULL
);
92 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
94 CHECK_VALUE (result
, NULL
);
96 /* Verify that the correct error message was emitted. */
97 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt
),
98 "gcc_jit_context_new_cast:"
99 " cannot cast test_buffer"
100 " from type: char[1024]"