-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc++] Fix C++23 standard modules when using with clang-cl
on Windows
#148992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libcxx Author: Adam (siradam7th) ChangesThe following PR fixes a couple of issues and compilation errors when using Issues:
Fixes:
This pull request fixes the following issues: Full diff: https://github.com/llvm/llvm-project/pull/148992.diff 4 Files Affected:
diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index 05f4e846c3ef9..2350f9ae9bc19 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -17,6 +17,11 @@
#if defined(_LIBCPP_ABI_VCRUNTIME)
# include <new.h>
+// <new.h> does not define 'get_new_handler'
+// which makes the 'std' module build fail, this fixes it
+namespace std {
+_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+}
#else
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
typedef void (*new_handler)();
diff --git a/libcxx/modules/std.compat/ctime.inc b/libcxx/modules/std.compat/ctime.inc
index eba8234a08969..7547b055b6807 100644
--- a/libcxx/modules/std.compat/ctime.inc
+++ b/libcxx/modules/std.compat/ctime.inc
@@ -14,15 +14,17 @@ export {
using ::timespec _LIBCPP_USING_IF_EXISTS;
using ::tm _LIBCPP_USING_IF_EXISTS;
-
using ::asctime _LIBCPP_USING_IF_EXISTS;
using ::clock _LIBCPP_USING_IF_EXISTS;
+ using ::strftime _LIBCPP_USING_IF_EXISTS;
+
+#ifndef _LIBCPP_ABI_VCRUNTIME
using ::ctime _LIBCPP_USING_IF_EXISTS;
using ::difftime _LIBCPP_USING_IF_EXISTS;
using ::gmtime _LIBCPP_USING_IF_EXISTS;
using ::localtime _LIBCPP_USING_IF_EXISTS;
using ::mktime _LIBCPP_USING_IF_EXISTS;
- using ::strftime _LIBCPP_USING_IF_EXISTS;
using ::time _LIBCPP_USING_IF_EXISTS;
using ::timespec_get _LIBCPP_USING_IF_EXISTS;
+#endif
} // export
diff --git a/libcxx/modules/std.cppm.in b/libcxx/modules/std.cppm.in
index 984b18321923c..f19dc07169fda 100644
--- a/libcxx/modules/std.cppm.in
+++ b/libcxx/modules/std.cppm.in
@@ -51,6 +51,9 @@ module;
#include <cstdio>
#include <cstdlib>
#include <cstring>
+#ifdef _LIBCPP_ABI_VCRUNTIME
+#define _BUILD_STD_MODULE
+#endif
#include <ctime>
#include <cuchar>
#include <cwchar>
diff --git a/libcxx/modules/std/ctime.inc b/libcxx/modules/std/ctime.inc
index 5bfa61917e5f2..e9f8ecb6ec63e 100644
--- a/libcxx/modules/std/ctime.inc
+++ b/libcxx/modules/std/ctime.inc
@@ -14,15 +14,17 @@ export namespace std {
using std::timespec _LIBCPP_USING_IF_EXISTS;
using std::tm _LIBCPP_USING_IF_EXISTS;
-
using std::asctime _LIBCPP_USING_IF_EXISTS;
using std::clock _LIBCPP_USING_IF_EXISTS;
+ using std::strftime _LIBCPP_USING_IF_EXISTS;
+
+#ifndef _LIBCPP_ABI_VCRUNTIME
using std::ctime _LIBCPP_USING_IF_EXISTS;
using std::difftime _LIBCPP_USING_IF_EXISTS;
using std::gmtime _LIBCPP_USING_IF_EXISTS;
using std::localtime _LIBCPP_USING_IF_EXISTS;
using std::mktime _LIBCPP_USING_IF_EXISTS;
- using std::strftime _LIBCPP_USING_IF_EXISTS;
using std::time _LIBCPP_USING_IF_EXISTS;
using std::timespec_get _LIBCPP_USING_IF_EXISTS;
+#endif
} // namespace std
|
|
I have moved the macro definition to the file |
using ::asctime _LIBCPP_USING_IF_EXISTS; | ||
using ::clock _LIBCPP_USING_IF_EXISTS; | ||
using ::strftime _LIBCPP_USING_IF_EXISTS; | ||
|
||
#ifndef _LIBCPP_ABI_VCRUNTIME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you simply leaving these functions missing when using UCRT?
Also, it seem that we should modify this line libcxx/utils/libcxx/test/features.py
to enable the tests:
or ("_WIN32" in compilerMacros(cfg) and not _mingwSupportsModules(cfg)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking into it, however it seems that moving the definition '_BUILD_STD_MODULE' triggered some other weird errors, I'll do more testing tomorrow and see...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, these lines should be removed. Now get_new_handler.pass.cpp
should always pass.
Lines 11 to 17 in 653872f
// FIXME: When libc++ is linked against vcruntime (i.e. the default config in | |
// MSVC mode), the declarations of std::set_new_handler and std::get_new_handler | |
// are provided by vcruntime/UCRT's new.h. However, that header actually only | |
// declares set_new_handler - it's missing a declaration of get_new_handler. | |
// XFAIL: msvc && stdlib=libc++ | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like just defining the macro is not enough, I have resorted to defining the actual functions as inline, I'll push the changes once I made sure that everything works with both std
and std.compact
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _LIBCPP_USING_IF_EXISTS
attribute is intended to make things work when the declarations are not available, and avoid precisely these #ifdef
blocks. What's the issue here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue on these <ctime>
functions is that UCRT incorrectly adds static
to them, so we can't directly export
them. At this time we might simply skip export
for them to make the standard modules buildable. MSVC STL chooses to reimplement them in the std
namespace as a workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it seem that we should modify this line
libcxx/utils/libcxx/test/features.py
to enable the tests:
or ("_WIN32" in compilerMacros(cfg) and not _mingwSupportsModules(cfg))
Is just removing that line enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can try this:
or ("_WIN32" in compilerMacros(cfg) and "__MINGW32__" in compilerMacros(cfg) and not _mingwSupportsModules(cfg))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with how the tests are run/generated yet, I'll see...
✅ With the latest revision this PR passed the Python code formatter. |
libcxx/include/__new/new_handler.h
Outdated
@@ -17,6 +17,11 @@ | |||
|
|||
#if defined(_LIBCPP_ABI_VCRUNTIME) | |||
# include <new.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does the MSVC STL get their own definition for get_new_handler()
? It seems really brittle to include <new.h>
but have our definition for get_new_handler()
. We should either declare all of them (set_new_handler()
, the new_handler
alias, etc) or rely on <new.h>
to provide them, but not a mixture of both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MSVC STL just (additionally) declares it in <new>
. The declaration is not available from <new.h>
or <vcruntime_new.h>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siradam7th I think it would be better to follow what MSVC STL does. We can just avoid special-casing _LIBCPP_ABI_VCRUNTIME
, and consistently declare these 3 things ourselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siradam7th I think it would be better to follow what MSVC STL does. We can just avoid special-casing
_LIBCPP_ABI_VCRUNTIME
, and consistently declare these 3 things ourselves.
I'll give it a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed the _LIBCPP_ABI_VCRUNTIME
macro conditional from libcxx/include/__new/new_handler.h
.
and fixed the missing symbols which are now defined by libcxx/src/new_handler.cpp
, and everything is working correctly.
apart from a couple of warnings of redeclaration since some of the cpp
files include <new>
:
llvm-project\build\include\c++\v1\__new/new_handler.h(20,39): warning: redeclaration of 'std::set_new_handler' should not add 'dllexport' attribute [-Wdll-attribute-on-redeclaration]
20 | _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\new.h(32,42): note: previous declaration is here
32 | _CRTIMP2 new_handler __cdecl set_new_handler(_In_opt_ new_handler _NewHandler) throw();
| ^
1 warning generated.
using ::asctime _LIBCPP_USING_IF_EXISTS; | ||
using ::clock _LIBCPP_USING_IF_EXISTS; | ||
using ::strftime _LIBCPP_USING_IF_EXISTS; | ||
|
||
#ifndef _LIBCPP_ABI_VCRUNTIME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _LIBCPP_USING_IF_EXISTS
attribute is intended to make things work when the declarations are not available, and avoid precisely these #ifdef
blocks. What's the issue here?
libcxx/modules/std.cppm.in
Outdated
@@ -7,6 +7,10 @@ | |||
// | |||
//===----------------------------------------------------------------------===// | |||
|
|||
#ifdef _LIBCPP_ABI_VCRUNTIME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this change is temporary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was
bec4906
to
4e7a4c9
Compare
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp,inc,h -- libcxx/include/__new/new_handler.h libcxx/modules/std.compat/ctime.inc libcxx/modules/std/ctime.inc libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp View the diff from clang-format here.diff --git a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
index 2c0081703..d20a65d8b 100644
--- a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
@@ -8,7 +8,6 @@
// test get_new_handler
-
#include <new>
#include <cassert>
|
// A workaround for UCRT because it defines these functions | ||
// as static and that causes the error "internal linkage cannot be exported" | ||
#ifdef _LIBCPP_ABI_VCRUNTIME | ||
|
||
template <int = 0> | ||
inline char* __CRTDECL ctime(_In_ time_t const* const _Time) noexcept { | ||
return _ctime64(_Time); | ||
} | ||
|
||
template <int = 0> | ||
inline double __CRTDECL difftime(_In_ time_t const _Time1, _In_ time_t const _Time2) noexcept { | ||
return _difftime64(_Time1, _Time2); | ||
} | ||
|
||
template <int = 0> | ||
inline tm* __CRTDECL gmtime(_In_ time_t const* const _Time) noexcept { | ||
return _gmtime64(_Time); | ||
} | ||
|
||
template <int = 0> | ||
inline tm* __CRTDECL localtime(_In_ time_t const* const _Time) noexcept { | ||
return _localtime64(_Time); | ||
} | ||
|
||
template <int = 0> | ||
inline time_t __CRTDECL mktime(_Inout_ tm* const _Tm) noexcept { | ||
return _mktime64(_Tm); | ||
} | ||
|
||
template <int = 0> | ||
inline time_t __CRTDECL time(_Out_opt_ time_t* const _Time) noexcept { | ||
return _time64(_Time); | ||
} | ||
|
||
template <int = 0> | ||
inline int __CRTDECL timespec_get(_Out_ timespec* const _Ts, _In_ int const _Base) noexcept { | ||
return _timespec64_get(reinterpret_cast<_timespec64*>(_Ts), _Base); | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like this. It makes us clearly non-conforming and working around an issue that is trivial to fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it is trivial? even MSVC's STL has the same workaround because there has been no changes in UCRT in the past years, because of those few functions modules cannot be compiled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it is trivial?
The fix in UCRT would be very trivial. UCRT team can just remove static
on these functions in less than one minute, as UCRT expects the __inline
extension to behave same as inline
in C++.
The crux is that nobody in the UCRT team seems to be able to work on this, and contribution from open source community is still impossible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The crux is that nobody in the UCRT team seems to be able to work on this, and contribution from open source community is still impossible.
This is one of the main reasons for the existance of this PR, if they would've done it, they at least would have done it for MSVC's sake, but here we are...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not a good reason to add a comparatively complex and fragile workaround, but a reason to complain to them to get their asses moving. I don't know what Microsoft's reasons are, but so far this is a very clear "we don't care" and not "we can't" to me. I have no interest in supporting anybodys "we don't care".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, but IMO we should at least to skip export
of these functions to build standard modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is definitely more palatable. I'm not sure I'd approve it; I'll have to think about that a bit more I think.
#if defined(_LIBCPP_ABI_VCRUNTIME) | ||
# include <new.h> | ||
#else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we split this into a separate patch? This doesn't seem to be directly related to modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is, because <new>
does not define get_new_handler()
and that stops standard modules from building, the change was requested by @frederick-vs-ja to simplify the previous solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also fixes other tests IIUC, so this isn't specific to modules. It just happens to surface with modules as well.
The following PR fixes a couple of issues and compilation errors when using
libcxx
withclang-cl
on Windows.Issues:
get_new_handler()
in theucrt/new.h
that is included bylibcxx/include/__new
static inline
in functions definition forctime
insidestd.cppm.in
andstd.compat.cppm.in
(std.cppm
,std.compat.cppm
).Fixes:
get_new_handler()
function under thestd
namespace when using_LIBCPP_ABI_VCRUNTIME
._BUILD_STD_MODULE
before including<ctime>
insidestd.cppm.in
because MSVC's headers already have a fix in place.std.cppm
andstd.compat.cppm
are fixed.This pull request fixes the following issues: