xref: /llvm-project/lldb/test/API/types/basic_type.cpp (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1 // This file must have the following defined before it is included:
2 // T defined to the type to test (int, float, etc)
3 // T_CSTR a C string representation of the type T ("int", "float")
4 // T_VALUE_1 defined to a valid initializer value for TEST_TYPE (7 for int, 2.0 for float)
5 // T_VALUE_2, T_VALUE_3, T_VALUE_4 defined to a valid initializer value for TEST_TYPE that is different from TEST_VALUE_1
6 // T_PRINTF_FORMAT defined if T can be printed with printf
7 //
8 // An example for integers is below
9 #if 0
10 
11 #define T int
12 #define T_CSTR "int"
13 #define T_VALUE_1 11001110
14 #define T_VALUE_2 22002220
15 #define T_VALUE_3 33003330
16 #define T_VALUE_4 44044440
17 #define T_PRINTF_FORMAT "%i"
18 
19 #include "basic_type.cpp"
20 
21 #endif
22 
23 #ifdef TEST_BLOCK_CAPTURED_VARS
24 #include <dispatch/dispatch.h>
25 #endif
26 #include <cstdint>
27 #include <cstdio>
28 #include <cstdlib>
29 
30 class a_class
31 {
32 public:
a_class(const T & a,const T & b)33     a_class (const T& a, const T& b) :
34         m_a (a),
35         m_b (b)
36     {
37     }
38 
~a_class()39     ~a_class ()
40     {
41     }
42 
43     const T&
get_a() const44     get_a() const
45     {
46         return m_a;
47     }
48 
49     void
set_a(const T & a)50     set_a (const T& a)
51     {
52         m_a = a;
53     }
54 
55     const T&
get_b() const56     get_b() const
57     {
58         return m_b;
59     }
60 
61     void
set_b(const T & b)62     set_b (const T& b)
63     {
64         m_b = b;
65     }
66 
67 protected:
68     T m_a;
69     T m_b;
70 };
71 
72 typedef struct a_struct_tag {
73     T a;
74     T b;
75 } a_struct_t;
76 
77 
78 typedef union a_union_zero_tag {
79     T a;
80     double a_double;
81 } a_union_zero_t;
82 
83 typedef struct a_union_nonzero_tag {
84   double a_double;
85   a_union_zero_t u;
86 } a_union_nonzero_t;
87 
88 
89 int
main(int argc,char const * argv[])90 main (int argc, char const *argv[])
91 {
92     FILE *out = stdout;
93 
94     // By default, output to stdout
95     // If a filename is provided as the command line argument,
96     // output to that file.
97     if (argc == 2 && argv[1] && argv[1][0] != '\0')
98     {
99         out = fopen (argv[1], "w");
100     }
101 
102     T a = T_VALUE_1;
103     T* a_ptr = &a;
104     T& a_ref = a;
105     T a_array_bounded[2] = { T_VALUE_1, T_VALUE_2 };
106     T a_array_unbounded[] = { T_VALUE_1, T_VALUE_2 };
107 
108     a_class a_class_instance (T_VALUE_1, T_VALUE_2);
109     a_class *a_class_ptr = &a_class_instance;
110     a_class &a_class_ref = a_class_instance;
111 
112     a_struct_t a_struct = { T_VALUE_1, T_VALUE_2 };
113     a_struct_t *a_struct_ptr = &a_struct;
114     a_struct_t &a_struct_ref = a_struct;
115 
116     // Create a union with type T at offset zero
117     a_union_zero_t a_union_zero;
118     a_union_zero.a = T_VALUE_1;
119     a_union_zero_t *a_union_zero_ptr = &a_union_zero;
120     a_union_zero_t &a_union_zero_ref = a_union_zero;
121 
122     // Create a union with type T at a non-zero offset
123     a_union_nonzero_t a_union_nonzero;
124     a_union_nonzero.u.a = T_VALUE_1;
125     a_union_nonzero_t *a_union_nonzero_ptr = &a_union_nonzero;
126     a_union_nonzero_t &a_union_nonzero_ref = a_union_nonzero;
127 
128     a_struct_t a_struct_array_bounded[2]  = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }};
129     a_struct_t a_struct_array_unbounded[] = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }};
130     a_union_zero_t a_union_zero_array_bounded[2];
131     a_union_zero_array_bounded[0].a = T_VALUE_1;
132     a_union_zero_array_bounded[1].a = T_VALUE_2;
133     a_union_zero_t a_union_zero_array_unbounded[] = {{ T_VALUE_1 }, { T_VALUE_2 }};
134 
135 #ifdef T_PRINTF_FORMAT
136     fprintf (out, "%s: a = '" T_PRINTF_FORMAT "'\n", T_CSTR, a);
137     fprintf (out, "%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_ptr, *a_ptr);
138     fprintf (out, "%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n", T_CSTR, &a_ref, a_ref);
139 
140     fprintf (out, "%s[2]: a_array_bounded[0] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_bounded[0]);
141     fprintf (out, "%s[2]: a_array_bounded[1] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_bounded[1]);
142 
143     fprintf (out, "%s[]: a_array_unbounded[0] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_unbounded[0]);
144     fprintf (out, "%s[]: a_array_unbounded[1] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_unbounded[1]);
145 
146     fprintf (out, "(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_a());
147     fprintf (out, "(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_b());
148     fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_a());
149     fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_b());
150     fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_a());
151     fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_b());
152 
153     fprintf (out, "(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n", a_struct.a);
154     fprintf (out, "(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n", a_struct.b);
155     fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->a);
156     fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->b);
157     fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.a);
158     fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.b);
159 
160     fprintf (out, "(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n", a_union_zero.a);
161     fprintf (out, "(a_union_zero_t*) a_union_zero_ptr = %p, a_union_zero_ptr->a = '" T_PRINTF_FORMAT "'\n", a_union_zero_ptr, a_union_zero_ptr->a);
162     fprintf (out, "(a_union_zero_t&) a_union_zero_ref = %p, a_union_zero_ref.a = '" T_PRINTF_FORMAT "'\n", &a_union_zero_ref, a_union_zero_ref.a);
163 
164     fprintf (out, "(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero.u.a);
165     fprintf (out, "(a_union_nonzero_t*) a_union_nonzero_ptr = %p, a_union_nonzero_ptr->u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero_ptr, a_union_nonzero_ptr->u.a);
166     fprintf (out, "(a_union_nonzero_t&) a_union_nonzero_ref = %p, a_union_nonzero_ref.u.a = '" T_PRINTF_FORMAT "'\n", &a_union_nonzero_ref, a_union_nonzero_ref.u.a);
167 
168     fprintf (out, "(a_struct_t[2]) a_struct_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[0].a);
169     fprintf (out, "(a_struct_t[2]) a_struct_array_bounded[0].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[0].b);
170     fprintf (out, "(a_struct_t[2]) a_struct_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[1].a);
171     fprintf (out, "(a_struct_t[2]) a_struct_array_bounded[1].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[1].b);
172 
173     fprintf (out, "(a_struct_t[]) a_struct_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[0].a);
174     fprintf (out, "(a_struct_t[]) a_struct_array_unbounded[0].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[0].b);
175     fprintf (out, "(a_struct_t[]) a_struct_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[1].a);
176     fprintf (out, "(a_struct_t[]) a_struct_array_unbounded[1].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[1].b);
177 
178     fprintf (out, "(a_union_zero_t[2]) a_union_zero_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_bounded[0].a);
179     fprintf (out, "(a_union_zero_t[2]) a_union_zero_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_bounded[1].a);
180 
181     fprintf (out, "(a_union_zero_t[]) a_union_zero_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_unbounded[0].a);
182     fprintf (out, "(a_union_zero_t[]) a_union_zero_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_unbounded[1].a);
183 
184 #endif
185     puts("About to exit, break here to check values..."); // Here is the line we will break on to check variables.
186 
187 #ifdef TEST_BLOCK_CAPTURED_VARS
188     void (^myBlock)() = ^() {
189         fprintf (out, "%s: a = '" T_PRINTF_FORMAT "'\n", T_CSTR, a);
190         fprintf (out, "%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_ptr, *a_ptr);
191         fprintf (out, "%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n", T_CSTR, &a_ref, a_ref);
192 
193         fprintf (out, "(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_a());
194         fprintf (out, "(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_b());
195         fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_a());
196         fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_b());
197         fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_a());
198         fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_b());
199 
200         fprintf (out, "(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n", a_struct.a);
201         fprintf (out, "(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n", a_struct.b);
202         fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->a);
203         fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->b);
204         fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.a);
205         fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.b);
206 
207         fprintf (out, "(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n", a_union_zero.a);
208         fprintf (out, "(a_union_zero_t*) a_union_zero_ptr = %p, a_union_zero_ptr->a = '" T_PRINTF_FORMAT "'\n", a_union_zero_ptr, a_union_zero_ptr->a);
209         fprintf (out, "(a_union_zero_t&) a_union_zero_ref = %p, a_union_zero_ref.a = '" T_PRINTF_FORMAT "'\n", &a_union_zero_ref, a_union_zero_ref.a);
210 
211         fprintf (out, "(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero.u.a);
212         fprintf (out, "(a_union_nonzero_t*) a_union_nonzero_ptr = %p, a_union_nonzero_ptr->u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero_ptr, a_union_nonzero_ptr->u.a);
213         fprintf (out, "(a_union_nonzero_t&) a_union_nonzero_ref = %p, a_union_nonzero_ref.u.a = '" T_PRINTF_FORMAT "'\n", &a_union_nonzero_ref, a_union_nonzero_ref.u.a);
214 
215         fprintf (out, "That's All Folks!\n"); // Break here to test block captured variables.
216     };
217 
218     myBlock();
219 #endif
220 
221     if (out != stdout)
222         fclose (out);
223 
224     return 0;
225 }
226