1 /*
2  *	Testing for compile error in the cast helpers
3  *	written by Jan Engelhardt
4  *
5  *	This program is free software; you can redistribute it and/or
6  *	modify it under the terms of the WTF Public License version 2 or
7  *	(at your option) any later version.
8  */
9 #include <math.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <libHX/defs.h>
14 #include <libHX/init.h>
15 #include "internal.h"
16 #define UNUSED __attribute__((unused))
17 
18 static int *v_1 UNUSED = const_cast1(int *, (const int *)NULL);
19 
c_signed(void)20 static void c_signed(void)
21 {
22 	const char *si_00 = "foo";
23 	char *si_01 = const_cast1(char *, si_00);
24 	signed char *si_02 UNUSED = signed_cast(signed char *, si_01);
25 	unsigned char *si_03 UNUSED = signed_cast(unsigned char *, si_01);
26 	const signed char *si_04 UNUSED = signed_cast(const signed char *, si_00);
27 	const unsigned char *si_05 UNUSED = signed_cast(const unsigned char *, si_00);
28 	si_00 = signed_cast(const char *, si_05);
29 }
30 
c_reinterpret(void)31 static void c_reinterpret(void)
32 {
33 	const char *si_00 = "foo";
34 	void *sr_00 = reinterpret_cast(void *, static_cast(uintptr_t, 8));
35 	int sr_01 = reinterpret_cast(uintptr_t, sr_00);
36 	void *sr_02 = reinterpret_cast(void *, static_cast(uintptr_t, static_cast(unsigned int, reinterpret_cast(uintptr_t, &si_00))));
37 	printf("sr: %p %u; %p[%p]\n", sr_00, sr_01, sr_02, &si_00);
38 }
39 
c_static(void)40 static void c_static(void)
41 {
42 	double st_00 = sqrt(static_cast(int,
43 		10 * sqrt(static_cast(double, 3) / 4)));
44 	printf("st: %f\n", st_00);
45 }
46 
c_const1(void)47 static void c_const1(void)
48 {
49 	const int *co_00 = NULL;
50 	int *co_01 = const_cast1(int *, co_00);
51 	co_00 = co_01;
52 }
53 
c_const2(void)54 static void c_const2(void)
55 {
56 	const int **co_02 = NULL;
57 	int **co_03 UNUSED = const_cast2(int **, co_02);
58 	int *const *co_04 UNUSED = const_cast2(int *const *, co_02);
59 	const int *const *co_05 = const_cast2(const int *const *, co_02);
60 	co_02 = const_cast2(const int **, co_05);
61 	co_04 = const_cast2(int *const *, co_05);
62 }
63 
c_const3(void)64 static void c_const3(void)
65 {
66 	const int *const *const *co_06 = NULL;
67 	int ***co_07 UNUSED = const_cast3(int ***,
68 		(printf("If this line is only printed once when the program "
69 		"is run, __builtin_choose_expr works as desired.\n"), co_06));
70 }
71 
c_constA(void)72 static void c_constA(void)
73 {
74 	static const char r1[] = "static";
75 	char *w1 UNUSED = const_cast1(char *, r1);
76 
77 	static const char *const r2[] = {"static"};
78 	char **w2 UNUSED = const_cast2(char **, r2);
79 
80 	static const char *const *const r3[] = {NULL};
81 	char ***w3 UNUSED = const_cast3(char ***, r3);
82 }
83 
main(void)84 int main(void)
85 {
86 	if (HX_init() <= 0)
87 		abort();
88 	c_signed();
89 	c_reinterpret();
90 	c_static();
91 	c_const1();
92 	c_const2();
93 	c_const3();
94 	c_constA();
95 	HX_exit();
96 	return 0;
97 }
98