annotate gcc/testsuite/jit.dg/test-error-array-as-pointer.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 #include <stdlib.h>
kono
parents:
diff changeset
2 #include <stdio.h>
kono
parents:
diff changeset
3 #include <stddef.h>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 #include "libgccjit.h"
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 #include "harness.h"
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 #define BUFFER_SIZE (1024)
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 char test_buffer[1024];
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 void
kono
parents:
diff changeset
14 create_code (gcc_jit_context *ctxt, void *user_data)
kono
parents:
diff changeset
15 {
kono
parents:
diff changeset
16 /* Let's try to inject the equivalent of:
kono
parents:
diff changeset
17 void test_of_array_as_pointer (const char *name)
kono
parents:
diff changeset
18 {
kono
parents:
diff changeset
19 snprintf (test_buffer, sizeof (test_buffer),
kono
parents:
diff changeset
20 "hello %s", name);
kono
parents:
diff changeset
21 }
kono
parents:
diff changeset
22 */
kono
parents:
diff changeset
23 gcc_jit_type *void_type =
kono
parents:
diff changeset
24 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
kono
parents:
diff changeset
25 gcc_jit_type *const_char_ptr_type =
kono
parents:
diff changeset
26 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
kono
parents:
diff changeset
27 gcc_jit_type *char_type =
kono
parents:
diff changeset
28 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
kono
parents:
diff changeset
29 gcc_jit_type *char_ptr_type =
kono
parents:
diff changeset
30 gcc_jit_type_get_pointer (char_type);
kono
parents:
diff changeset
31 gcc_jit_type *int_type =
kono
parents:
diff changeset
32 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
kono
parents:
diff changeset
33 gcc_jit_type *size_t_type =
kono
parents:
diff changeset
34 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T);
kono
parents:
diff changeset
35 gcc_jit_type *buf_type =
kono
parents:
diff changeset
36 gcc_jit_context_new_array_type (ctxt, NULL, char_type, BUFFER_SIZE);
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 /* extern int snprintf(char *str, size_t size, const char *format, ...); */
kono
parents:
diff changeset
39 gcc_jit_param *param_s =
kono
parents:
diff changeset
40 gcc_jit_context_new_param (ctxt, NULL, char_ptr_type, "s");
kono
parents:
diff changeset
41 gcc_jit_param *param_n =
kono
parents:
diff changeset
42 gcc_jit_context_new_param (ctxt, NULL, size_t_type, "n");
kono
parents:
diff changeset
43 gcc_jit_param *param_format =
kono
parents:
diff changeset
44 gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
kono
parents:
diff changeset
45 gcc_jit_param *snprintf_params[3] = {param_s, param_n, param_format};
kono
parents:
diff changeset
46 gcc_jit_function *snprintf =
kono
parents:
diff changeset
47 gcc_jit_context_new_function (ctxt, NULL,
kono
parents:
diff changeset
48 GCC_JIT_FUNCTION_IMPORTED,
kono
parents:
diff changeset
49 int_type,
kono
parents:
diff changeset
50 "snprintf",
kono
parents:
diff changeset
51 3, snprintf_params,
kono
parents:
diff changeset
52 1);
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 gcc_jit_param *param_name =
kono
parents:
diff changeset
55 gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
kono
parents:
diff changeset
56 gcc_jit_function *test_fn =
kono
parents:
diff changeset
57 gcc_jit_context_new_function (ctxt, NULL,
kono
parents:
diff changeset
58 GCC_JIT_FUNCTION_EXPORTED,
kono
parents:
diff changeset
59 void_type,
kono
parents:
diff changeset
60 "test_of_array_as_pointer",
kono
parents:
diff changeset
61 1, &param_name,
kono
parents:
diff changeset
62 0);
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 gcc_jit_lvalue *buffer =
kono
parents:
diff changeset
65 gcc_jit_context_new_global (ctxt, NULL,
kono
parents:
diff changeset
66 GCC_JIT_GLOBAL_IMPORTED,
kono
parents:
diff changeset
67 buf_type,
kono
parents:
diff changeset
68 "test_buffer");
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 gcc_jit_block *block = gcc_jit_function_new_block(test_fn, "entry");
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 /* snprintf(buffer, sizeof(buffer), "hello %s", name); */
kono
parents:
diff changeset
73 gcc_jit_rvalue *args[4];
kono
parents:
diff changeset
74 args[0] = gcc_jit_context_new_cast (
kono
parents:
diff changeset
75 ctxt, NULL,
kono
parents:
diff changeset
76 /* Here's the difference with test-array-as-pointer.c: */
kono
parents:
diff changeset
77 gcc_jit_lvalue_as_rvalue (buffer),
kono
parents:
diff changeset
78 char_ptr_type);
kono
parents:
diff changeset
79 args[1] = gcc_jit_context_new_rvalue_from_int (ctxt,
kono
parents:
diff changeset
80 size_t_type,
kono
parents:
diff changeset
81 BUFFER_SIZE);
kono
parents:
diff changeset
82 args[2] = gcc_jit_context_new_string_literal (ctxt, "hello %s");
kono
parents:
diff changeset
83 args[3] = gcc_jit_param_as_rvalue (param_name);
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 gcc_jit_block_add_eval (
kono
parents:
diff changeset
86 block, NULL,
kono
parents:
diff changeset
87 gcc_jit_context_new_call (ctxt, NULL, snprintf, 4, args));
kono
parents:
diff changeset
88 gcc_jit_block_end_with_void_return (block, NULL);
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 void
kono
parents:
diff changeset
92 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
kono
parents:
diff changeset
93 {
kono
parents:
diff changeset
94 CHECK_VALUE (result, NULL);
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 /* Verify that the correct error message was emitted. */
kono
parents:
diff changeset
97 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
kono
parents:
diff changeset
98 "gcc_jit_context_new_cast:"
kono
parents:
diff changeset
99 " cannot cast test_buffer"
kono
parents:
diff changeset
100 " from type: char[1024]"
kono
parents:
diff changeset
101 " to type: char *");
kono
parents:
diff changeset
102 }