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
, GCC_JIT_GLOBAL_IMPORTED
,
66 buf_type
, "test_buffer");
68 gcc_jit_block
*block
= gcc_jit_function_new_block(test_fn
, "entry");
70 /* snprintf(buffer, sizeof(buffer), "hello %s", name); */
71 gcc_jit_rvalue
*args
[4];
72 args
[0] = gcc_jit_context_new_cast (
74 /* Here's the difference with test-error-array-as-pointer.c: */
75 gcc_jit_lvalue_get_address (buffer
,
78 args
[1] = gcc_jit_context_new_rvalue_from_int (ctxt
,
81 args
[2] = gcc_jit_context_new_string_literal (ctxt
, "hello %s");
82 args
[3] = gcc_jit_param_as_rvalue (param_name
);
84 gcc_jit_block_add_eval (
86 gcc_jit_context_new_call (ctxt
, NULL
, snprintf
, 4, args
));
87 gcc_jit_block_end_with_void_return (block
, NULL
);
91 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
93 CHECK_NON_NULL (result
);
95 typedef void (*fn_type
) (const char *);
96 fn_type test_of_array_as_pointer
=
97 (fn_type
)gcc_jit_result_get_code (result
, "test_of_array_as_pointer");
98 CHECK_NON_NULL (test_of_array_as_pointer
);
100 test_of_array_as_pointer ("world");
101 CHECK_STRING_VALUE (test_buffer
, "hello world");