1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stddef.h>
4 
5 #include "libgccjit.h"
6 
7 #include "harness.h"
8 
9 #define BUFFER_SIZE (1024)
10 
11 char test_buffer[1024];
12 
13 void
14 create_code (gcc_jit_context *ctxt, void *user_data)
15 {
16   /* Let's try to inject the equivalent of:
17         void test_of_array_as_pointer (const char *name)
18         {
19             snprintf (test_buffer, sizeof (test_buffer),
20 	              "hello %s", name);
21         }
22     */
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);
37 
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,
49 				  int_type,
50 				  "snprintf",
51 				  3, snprintf_params,
52 				  1);
53 
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,
59 				  void_type,
60 				  "test_of_array_as_pointer",
61 				  1, &param_name,
62 				  0);
63 
64   gcc_jit_lvalue *buffer =
65     gcc_jit_context_new_global (ctxt, NULL,
66 				GCC_JIT_GLOBAL_IMPORTED,
67 				buf_type,
68 				"test_buffer");
69 
70   gcc_jit_block *block = gcc_jit_function_new_block(test_fn, "entry");
71 
72   /* snprintf(buffer, sizeof(buffer), "hello %s", name); */
73   gcc_jit_rvalue *args[4];
74   args[0] = gcc_jit_context_new_cast (
75     ctxt, NULL,
76     /* Here's the difference with test-array-as-pointer.c: */
77     gcc_jit_lvalue_as_rvalue (buffer),
78     char_ptr_type);
79   args[1] = gcc_jit_context_new_rvalue_from_int (ctxt,
80 						 size_t_type,
81 						 BUFFER_SIZE);
82   args[2] = gcc_jit_context_new_string_literal (ctxt, "hello %s");
83   args[3] = gcc_jit_param_as_rvalue (param_name);
84 
85   gcc_jit_block_add_eval (
86     block, NULL,
87     gcc_jit_context_new_call (ctxt, NULL, snprintf, 4, args));
88   gcc_jit_block_end_with_void_return (block, NULL);
89 }
90 
91 void
92 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
93 {
94   CHECK_VALUE (result, NULL);
95 
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]"
101 		      " to type: char *");
102 }
103