[clang] Silence -Wcast-function-type warnings on idiomatic Windows code (PR #135660)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 15 04:28:22 PDT 2025
================
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 %s -triple x86_64-windows -fsyntax-only -Wcast-function-type -Wno-cast-function-type-strict -verify=windows
+// RUN: %clang_cc1 %s -triple x86_64-windows -fsyntax-only -Wcast-function-type -Wno-cast-function-type-strict -x c++ -verify=windows
+// RUN: %clang_cc1 %s -triple x86_64-pc-linux -fsyntax-only -Wcast-function-type -Wno-cast-function-type-strict -verify=linux
+// RUN: %clang_cc1 %s -triple x86_64-pc-linux -fsyntax-only -Wcast-function-type -Wno-cast-function-type-strict -x c++ -verify=linux,linux-cpp
+// RUN: %clang_cc1 %s -triple x86_64-windows -fsyntax-only -Wcast-function-type -Wcast-function-type-strict -x c++ -verify=strict
+// windows-no-diagnostics
+
+// On Windows targets, this is expected to compile fine, and on non-Windows
+// targets, this should diagnose the mismatch. This is to allow for idiomatic
+// use of GetProcAddress, similar to what we do for dlsym. On non-Windows
+// targets, this should be diagnosed.
+typedef int (*FARPROC1)();
+typedef unsigned long long (*FARPROC2)();
+
+FARPROC1 GetProcAddress1(void);
+FARPROC2 GetProcAddress2(void);
+
+typedef int (*test1_type)(int);
+typedef float(*test2_type)();
+
+void test(void) {
+ // This does not diagnose on Linux in C mode because FARPROC1 has a matching
+ // return type to test1_type, but FARPROC1 has no prototype and so checking
+ // is disabled for further compatibility issues. In C++ mode, all functions
+ // have a prototype and so the check happens.
+ test1_type t1 = (test1_type)GetProcAddress1(); // linux-cpp-warning {{cast from 'FARPROC1' (aka 'int (*)()') to 'test1_type' (aka 'int (*)(int)') converts to incompatible function type}} \
----------------
AaronBallman wrote:
I went ahead and made the change (personally, I prefer line splicing over @-1 @-2 just because it makes future changes somewhat easier, but I don't feel that strongly in such a small file).
https://github.com/llvm/llvm-project/pull/135660
More information about the cfe-commits
mailing list