xref: /netbsd-src/tests/usr.bin/xlint/lint1/d_fold_test.c (revision fe7ce87087f280c6ec3a05c8e6c372792bed4a2a)
1 # 2 "d_fold_test.c"
2 
3 /*
4  * Test how expressions are handled in a context where they are tested for
5  * truthiness, such as in the condition of an if statement.
6  */
7 
8 /* lint1-extra-flags: -X 351 */
9 
10 struct s {
11 	int member;
12 };
13 
14 union u {
15 	int member;
16 };
17 
18 enum e {
19 	E
20 };
21 
22 struct arr {
23 	int arr[4];
24 };
25 
26 /* C99 6.2.5p2 */
if_Bool(_Bool b)27 void if_Bool(_Bool b)			{ if (b) return; }
28 
29 /* C99 6.2.5p3 */
if_char(char c)30 void if_char(char c)			{ if (c) return; }
31 
32 /* C99 6.2.5p4 */
if_signed_char(signed char sc)33 void if_signed_char(signed char sc)	{ if (sc) return; }
if_short_int(short s)34 void if_short_int(short s)		{ if (s) return; }
if_int(int i)35 void if_int(int i)			{ if (i) return; }
if_long_int(long int l)36 void if_long_int(long int l)		{ if (l) return; }
if_long_long_int(long long int ll)37 void if_long_long_int(long long int ll)	{ if (ll) return; }
38 
39 /* C99 6.2.5p6 */
if_unsigned_char(unsigned char uc)40 void if_unsigned_char(unsigned char uc)			{ if (uc) return; }
if_unsigned_short_int(unsigned short us)41 void if_unsigned_short_int(unsigned short us)		{ if (us) return; }
if_unsigned_int(unsigned int ui)42 void if_unsigned_int(unsigned int ui)			{ if (ui) return; }
if_unsigned_long_int(unsigned long int ul)43 void if_unsigned_long_int(unsigned long int ul)		{ if (ul) return; }
if_unsigned_long_long_int(unsigned long long int ull)44 void if_unsigned_long_long_int(unsigned long long int ull) { if (ull) return; }
45 
46 /* C99 6.2.5p10 */
if_float(float f)47 void if_float(float f)			{ if (f) return; }
if_double(double d)48 void if_double(double d)		{ if (d) return; }
if_long_double(long double ld)49 void if_long_double(long double ld)	{ if (ld) return; }
50 
51 /* C99 6.2.5p11 */
if_float_Complex(float _Complex fc)52 void if_float_Complex(float _Complex fc)		{ if (fc) return; }
if_double_Complex(double _Complex dc)53 void if_double_Complex(double _Complex dc)		{ if (dc) return; }
if_long_double_Complex(long double _Complex ldc)54 void if_long_double_Complex(long double _Complex ldc)	{ if (ldc) return; }
55 
56 /* C99 6.2.5p16 */
if_enum(enum e e)57 void if_enum(enum e e)			{ if (e) return; }
58 
59 /* C99 6.2.5p20 */
if_array(struct arr arr)60 void if_array(struct arr arr)		{ if (arr.arr) return; }
61 /* expect+2: error: controlling expressions must have scalar type [204] */
62 /* expect+1: warning: parameter 's' unused in function 'if_struct' [231] */
if_struct(struct s s)63 void if_struct(struct s s)		{ if (s) return; }
64 /* expect+2: error: controlling expressions must have scalar type [204] */
65 /* expect+1: warning: parameter 'u' unused in function 'if_union' [231] */
if_union(union u u)66 void if_union(union u u)		{ if (u) return; }
if_function(void)67 void if_function(void)			{ if (if_function) return; }
if_pointer(void * p)68 void if_pointer(void *p)		{ if (p) return; }
69 
70 /* C99 6.8.5 */
71 /* expect+2: error: controlling expressions must have scalar type [204] */
72 /* expect+1: warning: parameter 's' unused in function 'while_struct' [231] */
while_struct(struct s s)73 void while_struct(struct s s)		{ while (s) return; }
74 /* expect+3: error: controlling expressions must have scalar type [204] */
75 /* expect+2: warning: end-of-loop code not reached [223] */
76 /* expect+1: warning: parameter 's' unused in function 'for_struct' [231] */
for_struct(struct s s)77 void for_struct(struct s s)		{ for (;s;) return; }
78 /* expect+2: error: controlling expressions must have scalar type [204] */
79 /* expect+1: warning: parameter 's' unused in function 'do_while_struct' [231] */
do_while_struct(struct s s)80 void do_while_struct(struct s s)	{ do { return; } while (s); }
81 
82 /* C99 6.5.15 does not require a scalar type, curiously. */
83 /* expect+3: error: first operand of '?' must have scalar type [170] */
84 /* expect+2: error: function 'conditional_struct' expects to return value [214] */
85 /* expect+1: warning: parameter 's' unused in function 'conditional_struct' [231] */
conditional_struct(struct s s)86 int conditional_struct(struct s s)	{ return s ? 1 : 2; }
87