Debian Codegraph @

gcc/testsuite/gcc.dg/c23-mismatching-fn-ptr.c

raw
1 /* Verify that when we complain about incompatible pointer types
2 involving function pointers, we show the declaration of the
3 function. */
4
5 /* { dg-do compile } */
6 /* { dg-options "-std=c23 -pedantic-errors" } */
7
8 /* We're pretending that this is legacy code that was written before C23,
9 hence it uses NULL rather than nullptr. */
10 #define NULL ((void*)0)
11
12 typedef void (*void_int_fnptr_t) (int);
13
14 extern void takes_void_int_fnptr (void_int_fnptr_t fn); /* { dg-message "expected 'void_int_fnptr_t' \\{aka '\[^\n\r\]*'\\} but argument is of type '\[^\n\r\]*'" } */
15 extern void fn_argpass(); /* { dg-message "declared here" } */
16 void test_argpass ()
17 {
18 takes_void_int_fnptr (&fn_argpass); /* { dg-error "passing argument 1 of 'takes_void_int_fnptr' from incompatible pointer type" } */
19 }
20
21 extern void fn_assign(); /* { dg-message "declared here" } */
22 void test_assign ()
23 {
24 void (*assigned_to) (int);
25 assigned_to = &fn_assign; /* { dg-error "assignment to '\[^\n\r\]*' from incompatible pointer type '\[^\n\r\]*'" } */
26 }
27
28 extern void fn_init(); /* { dg-message "declared here" } */
29 void test_init ()
30 {
31 void (*initialized) (int) = &fn_init; /* { dg-error "initialization of '\[^\n\r\]*' from incompatible pointer type '\[^\n\r\]*'" } */
32 }
33
34 extern void fn_return(); /* { dg-message "declared here" } */
35 void_int_fnptr_t
36 test_return ()
37 {
38 return &fn_return; /* { dg-error "returning '\[^\n\r\]*' from a function with incompatible return type '\[^\n\r\]*'" } */
39 }
40
41 /* Test of storing a sighandler_t with a function signature mismatch.
42 In particular, verify that we show the locations of typedefs. */
43
44 typedef void (*sighandler_t)(int); /* { dg-message "'sighandler_t' declared here" } */
45 sighandler_t signal(int signum, sighandler_t handler);
46
47 typedef void (*wrong_sighandler_t)(void); /* { dg-message "'wrong_sighandler_t' declared here" } */
48 extern void takes_wrong_sighandler_type (wrong_sighandler_t fn); /* { dg-message "expected 'wrong_sighandler_t' \\{aka '\[^\n\r\]*'\\} but argument is of type 'sighandler_t' \\{aka '\[^\n\r\]*'\\}" } */
49
50 void test_argpass_from_signal_result ()
51 {
52 takes_wrong_sighandler_type (signal (42, NULL)); /* { dg-error "passing argument 1 of 'takes_wrong_sighandler_type' from incompatible pointer type" } */
53 }
54
55 void test_assign_from_signal_result ()
56 {
57 wrong_sighandler_t assigned_to;
58 assigned_to = signal (42, NULL); /* { dg-error "assignment to 'wrong_sighandler_t' \\{aka '\[^\n\r\]*'\\} from incompatible pointer type 'sighandler_t' \\{aka '\[^\n\r\]*'\\}" } */
59 }
60
61 void test_init_from_signal_result ()
62 {
63 wrong_sighandler_t initialized = signal (42, NULL); /* { dg-error "initialization of 'wrong_sighandler_t' \\{aka '\[^\n\r\]*'\\} from incompatible pointer type 'sighandler_t' \\{aka '\[^\n\r\]*'\\}" } */
64 }
65
66 wrong_sighandler_t
67 test_return_signal_result ()
68 {
69 return signal (42, NULL); /* { dg-error "returning 'sighandler_t' \\{aka '\[^\n\r\]*'\\} from a function with incompatible return type 'wrong_sighandler_t' \\{aka '\[^\n\r\]*'\\}" } */
70 }