xref: /netbsd-src/tests/usr.bin/xlint/lint1/gcc_typeof.c (revision 039b010016da489b3c993f4814255a8bc72125df)
1 /*	$NetBSD: gcc_typeof.c,v 1.7 2023/08/02 18:51:25 rillig Exp $	*/
2 # 3 "gcc_typeof.c"
3 
4 /*
5  * Tests for the GCC extension 'typeof'.
6  *
7  * https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
8  */
9 
10 /* lint1-extra-flags: -X 351 */
11 
12 void take_double(typeof(0.0));
13 
14 void take_function_double_returning_double(
15     typeof(0.0)(
16 	typeof(0.0)
17     )
18 );
19 
20 void
cast(double (* fn)(double))21 cast(double(*fn)(double))
22 {
23 	take_double(0.0);
24 
25 	/* expect+1: warning: passing 'pointer to function(double) returning double' to incompatible 'double', arg #1 [155] */
26 	take_double(fn);
27 
28 	/* expect+1: warning: passing 'double' to incompatible 'pointer to function(double) returning double', arg #1 [155] */
29 	take_function_double_returning_double(0.0);
30 
31 	take_function_double_returning_double(fn);
32 
33 	/* identity cast */
34 	take_function_double_returning_double((double (*)(double))fn);
35 }
36 
37 /*
38  * Since cgram 1.58 from 2014-02-18, when support for __typeof__ was added,
39  * and before cgram.y 1.394 from 2022-04-10, lint ran into an assertion
40  * failure when encountering a redundant type qualifier 'const' or 'volatile'
41  * in a declaration using __typeof__.  Seen in sys/sys/lwp.h on x86_64, in
42  * the call atomic_load_consume(&l->l_mutex).
43  */
44 int *volatile lock;
45 const volatile __typeof__(lock) *lock_pointer = &lock;
46 
47 /*
48  * Before cgram.y 1.427 from 2023-01-21, lint crashed due to a null pointer
49  * dereference if the __typeof__ operator had an invalid operand.  Seen in
50  * _fc_atomic_ptr_cmpexch from fontconfig, which uses <stdatomic.h> provided
51  * by GCC, which in turn uses __auto_type and __typeof__, and lint doesn't
52  * know about __auto_type.
53  */
_fc_atomic_ptr_cmpexch(void)54 void _fc_atomic_ptr_cmpexch(void)
55 {
56 	/* expect+1: error: 'expr' undefined [99] */
57 	__typeof__ (expr) var = 0;
58 }
59