Skip to content

[libcxx] Add cast to avoid pointer casting warning on Windows #92738

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 20, 2024

Conversation

mstorsjo
Copy link
Member

This avoids the following build time warning, when building with the latest nightly Clang:

warning: cast from 'FARPROC' (aka 'int (*)() __attribute__((stdcall))') to
'GetSystemTimeAsFileTimePtr' (aka 'void (*)(_FILETIME *) __attribute__((stdcall))')
converts to incompatible function type [-Wcast-function-type-mismatch]

This warning seems to have appeared since Clang commit 999d4f8, which restructured.

The GetProcAddress function returns a FARPROC type, which is int (WINAPI *)(). Directly casting this to another function pointer type triggers this warning, but casting to a void* inbetween avoids this issue. (On Unix-like platforms, dlsym returns a void*, which doesn't exhibit this casting problem.)

@mstorsjo mstorsjo requested a review from a team as a code owner May 20, 2024 11:01
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 20, 2024
@llvmbot
Copy link
Member

llvmbot commented May 20, 2024

@llvm/pr-subscribers-libcxx

Author: Martin Storsjö (mstorsjo)

Changes

This avoids the following build time warning, when building with the latest nightly Clang:

warning: cast from 'FARPROC' (aka 'int (*)() __attribute__((stdcall))') to
'GetSystemTimeAsFileTimePtr' (aka 'void (*)(_FILETIME *) __attribute__((stdcall))')
converts to incompatible function type [-Wcast-function-type-mismatch]

This warning seems to have appeared since Clang commit 999d4f8, which restructured.

The GetProcAddress function returns a FARPROC type, which is int (WINAPI *)(). Directly casting this to another function pointer type triggers this warning, but casting to a void* inbetween avoids this issue. (On Unix-like platforms, dlsym returns a void*, which doesn't exhibit this casting problem.)


Full diff: https://github.com/llvm/llvm-project/pull/92738.diff

1 Files Affected:

  • (modified) libcxx/src/chrono.cpp (+2-2)
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index e7d6dfbc22924..009c210993ce8 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -77,8 +77,8 @@ typedef void(WINAPI* GetSystemTimeAsFileTimePtr)(LPFILETIME);
 class GetSystemTimeInit {
 public:
   GetSystemTimeInit() {
-    fp =
-        (GetSystemTimeAsFileTimePtr)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
+    fp = (GetSystemTimeAsFileTimePtr)(void *)GetProcAddress(
+        GetModuleHandleW(L"kernel32.dll"), "GetSystemTimePreciseAsFileTime");
     if (fp == nullptr)
       fp = GetSystemTimeAsFileTime;
   }

Copy link

github-actions bot commented May 20, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

This avoids the following build time warning, when building with
the latest nightly Clang:

    warning: cast from 'FARPROC' (aka 'int (*)() __attribute__((stdcall))') to
    'GetSystemTimeAsFileTimePtr' (aka 'void (*)(_FILETIME *) __attribute__((stdcall))')
    converts to incompatible function type [-Wcast-function-type-mismatch]

This warning seems to have appeared since Clang commit
999d4f8, which restructured.

The GetProcAddress function returns a FARPROC type, which is
"int (WINAPI *)()". Directly casting this to another function pointer
type triggers this warning, but casting to a void* inbetween
avoids this issue. (On Unix-like platforms, dlsym returns a
"void*", which doesn't exhibit this casting problem.)
@mstorsjo mstorsjo force-pushed the libcxx-func-casting branch from 42981a2 to fa65e83 Compare May 20, 2024 11:24
@mordante mordante self-assigned this May 20, 2024
Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. LGTM!

@mstorsjo mstorsjo merged commit 250c39c into llvm:main May 20, 2024
52 checks passed
@mstorsjo mstorsjo deleted the libcxx-func-casting branch May 20, 2024 20:30
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants