blob: 599a7d1e66d7da9ceee0d2fe0ec3e53878143244 [file] [log] [blame]
Reid Kleckner9f497332016-05-10 21:00:03 +00001// RUN: %clang_cc1 -fms-extensions -triple i686-pc-windows-msvc -Wcast-calling-convention -DMSVC -Wno-pointer-bool-conversion -verify -x c %s
2// RUN: %clang_cc1 -fms-extensions -triple i686-pc-windows-msvc -Wcast-calling-convention -DMSVC -Wno-pointer-bool-conversion -verify -x c++ %s
3// RUN: %clang_cc1 -fms-extensions -triple i686-pc-windows-msvc -Wcast-calling-convention -DMSVC -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s --check-prefix=MSFIXIT
4// RUN: %clang_cc1 -triple i686-pc-windows-gnu -Wcast-calling-convention -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s --check-prefix=GNUFIXIT
5
6// expected-note@+1 {{consider defining 'mismatched_before_winapi' with the 'stdcall' calling convention}}
7void mismatched_before_winapi(int x) {}
8
9#ifdef MSVC
10#define WINAPI __stdcall
11#else
12#define WINAPI __attribute__((stdcall))
13#endif
14
15// expected-note@+1 3 {{consider defining 'mismatched' with the 'stdcall' calling convention}}
16void mismatched(int x) {}
17
Reid Kleckner0b009e82017-01-31 19:37:45 +000018// expected-note@+1 {{consider defining 'mismatched_declaration' with the 'stdcall' calling convention}}
19void mismatched_declaration(int x);
20
21// expected-note@+1 {{consider defining 'suggest_fix_first_redecl' with the 'stdcall' calling convention}}
22void suggest_fix_first_redecl(int x);
23void suggest_fix_first_redecl(int x);
24
Reid Kleckner9f497332016-05-10 21:00:03 +000025typedef void (WINAPI *callback_t)(int);
26void take_callback(callback_t callback);
27
Reid Kleckner43be52a2016-05-11 17:43:13 +000028void WINAPI mismatched_stdcall(int x) {}
29
30void take_opaque_fn(void (*callback)(int));
31
Reid Kleckner9f497332016-05-10 21:00:03 +000032int main() {
33 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
34 take_callback((callback_t)mismatched);
35
36 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
37 callback_t callback = (callback_t)mismatched; // warns
38 (void)callback;
39
40 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
41 callback = (callback_t)&mismatched; // warns
42
43 // No warning, just to show we don't drill through other kinds of unary operators.
44 callback = (callback_t)!mismatched;
45
46 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
47 callback = (callback_t)&mismatched_before_winapi; // warns
48
49 // Probably a bug, but we don't warn.
50 void (*callback2)(int) = mismatched;
51 take_callback((callback_t)callback2);
52
53 // Another way to suppress the warning.
54 take_callback((callback_t)(void*)mismatched);
Reid Kleckner43be52a2016-05-11 17:43:13 +000055
Reid Kleckner0b009e82017-01-31 19:37:45 +000056 // Warn on declarations as well as definitions.
57 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
58 take_callback((callback_t)mismatched_declaration);
59 // expected-warning@+1 {{cast between incompatible calling conventions 'cdecl' and 'stdcall'}}
60 take_callback((callback_t)suggest_fix_first_redecl);
61
Reid Kleckner43be52a2016-05-11 17:43:13 +000062 // Don't warn, because we're casting from stdcall to cdecl. Usually that means
63 // the programmer is rinsing the function pointer through some kind of opaque
64 // API.
65 take_opaque_fn((void (*)(int))mismatched_stdcall);
Reid Kleckner9f497332016-05-10 21:00:03 +000066}
67
Reid Klecknera6fd8652016-05-17 17:33:31 +000068// MSFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
69// MSFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
70// MSFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
71// MSFIXIT: fix-it:"{{.*}}callingconv-cast.c":{7:6-7:6}:"__stdcall "
Reid Kleckner9f497332016-05-10 21:00:03 +000072
Reid Klecknera6fd8652016-05-17 17:33:31 +000073// GNUFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
74// GNUFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
75// GNUFIXIT: fix-it:"{{.*}}callingconv-cast.c":{16:6-16:6}:"WINAPI "
76// GNUFIXIT: fix-it:"{{.*}}callingconv-cast.c":{7:6-7:6}:"__attribute__((stdcall)) "