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