xref: /llvm-project/clang/test/SemaCXX/warn-cast-function-type.cpp (revision 2bc098b8aba089fe8328b3b8a8b6b6816cd5a908)
1 // RUN: %clang_cc1 %s -fblocks -fsyntax-only -Wcast-function-type -Wno-cast-function-type-strict -verify
2 // RUN: %clang_cc1 %s -fblocks -fsyntax-only -Wextra -Wno-cast-function-type-strict -verify
3 
4 int x(long);
5 
6 typedef int (f1)(long);
7 typedef int (f2)(void*);
8 typedef int (f3)(...);
9 typedef void (f4)(...);
10 typedef void (f5)(void);
11 typedef int (f6)(long, int);
12 typedef int (f7)(long,...);
13 typedef int (&f8)(long, int);
14 
15 f1 *a;
16 f2 *b;
17 f3 *c;
18 f4 *d;
19 f5 *e;
20 f6 *f;
21 f7 *g;
22 
23 struct S
24 {
25   void foo (int*);
26   void bar (int);
27 };
28 
29 typedef void (S::*mf)(int);
30 
31 enum E : long;
32 int efunc(E);
33 
34 // Produce the underlying `long` type implicitly.
35 enum E2 { big = __LONG_MAX__ };
36 int e2func(E2);
37 
foo()38 void foo() {
39   a = (f1 *)x;
40   a = (f1 *)efunc; // enum is just type system sugar, still passed as a long.
41   a = (f1 *)e2func; // enum is just type system sugar, still passed as a long.
42   b = (f2 *)x; // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
43   b = reinterpret_cast<f2 *>(x); // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
44   c = (f3 *)x;
45   d = (f4 *)x; // expected-warning {{cast from 'int (*)(long)' to 'f4 *' (aka 'void (*)(...)') converts to incompatible function type}}
46   e = (f5 *)x;
47   f = (f6 *)x; // expected-warning {{cast from 'int (*)(long)' to 'f6 *' (aka 'int (*)(long, int)') converts to incompatible function type}}
48   g = (f7 *)x;
49 
50   mf p1 = (mf)&S::foo; // expected-warning {{cast from 'void (S::*)(int *)' to 'mf' (aka 'void (S::*)(int)') converts to incompatible function type}}
51 
52   f8 f2 = (f8)x; // expected-warning {{cast from 'int (long)' to 'f8' (aka 'int (&)(long, int)') converts to incompatible function type}}
53   (void)f2;
54 
55   int (^y)(long);
56   f = (f6 *)y; // expected-warning {{cast from 'int (^)(long)' to 'f6 *' (aka 'int (*)(long, int)') converts to incompatible function type}}
57 }
58