-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[C++23] [Modules] [std module] Skip including standard headers if the std module is imported #80663
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
Comments
@llvm/issue-subscribers-clang-modules Author: Chuanqi Xu (ChuanqiXu9)
While there is an annoying issue called "include after import": https://clang.llvm.org/docs/StandardCPlusPlusModules.html#including-headers-after-import-is-problematic, it should still be problematic to increase the compilation time (or the size of BMIs, if it exists). And this is fundamental to clang and it looks hard to fix it completely and perfectly in the side side.
And I feel this is straightforward for people to understand:
may have larger BMI and compile slower than:
While the example looks silly, it is actually pretty common since the standard library can be used in other headers. In a private meeting with MSVC developer, he mentioned MSVC have extensions (or plan to?) to skip the standard headers if the std module is imported. I feel this sounds good and can be pretty helpful to end users. For the implementation, I don't have a complete design now. In my mind, the immediate idea may be:
The idea to implement this in the library may require the library to provide an additional header to include all the controlling macros. So that the user (manually) can import std in a way like:
The idea in the compiler side may need to hardcode all the filenames for the standard headers and skip entering such headers. The idea to implement this in the compiler side and the library side is the library provides such header and the compiler can insert it automatically. The idea to implement this is still in the early phase. Any comments are welcomed. |
I think the option1 may be best since it can be extended to other libraries. I document it in #80687. |
I typed a long reply last week, but it seems I forgot to press comment :-/ I think there might be something possible on the library side. However the tricky part would be the macros. For example, feature-test macros, errno, and assert all require proper macro support. So I think Maybe it would be possible to add a special pragma to a header to tell the compiler to stop processing. Something along the lines of
In that case the compiler can stop processing when the module Would this be feasible to implement in the compiler? |
I don't understand this. In my mind the
Yeah, as you said, it looks not easy due to we need to find the last |
For example
Inside the module users can now use FTM available in
This still requires Clang to parse the entire file, but the pre-processor will "remove" the implementation when the proper module is imported. If this works it might be made more generic by using something like This is trivial to do in libc++ and I expect this shouldn't be too hard to implement in Clang. What do you think of this approach? |
(IIUC, Got your point. But this is not related to Clang in my mind. If we choose the option 1, libc++ will provide the (BTW, maybe we can include headers like in controlling_macros_for_std_headers too, it is not decided.)
On the one hand, it should be easy for the compiler to leak the macro definition On the other hand, however, it requires the BMI to be present before preprocessing. This is an old fixed bug in clang. That is:
And the command
will complain things like "failed to find module And this is the reason why I prefer this to be a library solution. A pure library solution is more flexible and generalized to other libraries. Maybe it is also an idea for the libc++ library to provide a header I guess you may feel it may not be convinient for the users to include an additional header but I guess it may be reasonable for users to understand. And users only need this if they want to mix includes and imports. |
While there is an annoying issue called "include after import": https://clang.llvm.org/docs/StandardCPlusPlusModules.html#including-headers-after-import-is-problematic, it should still be problematic even after we fix that. Since such style may increase the compilation time (or the size of BMIs, if it exists). And this is fundamental to clang and it looks hard to fix it completely and perfectly in the side side.
And I feel this is straightforward for people to understand:
may have larger BMI and compile slower than:
While the example looks silly, it is actually pretty common since the standard library can be used in other headers.
In a private meeting with MSVC developer, he mentioned MSVC have extensions (or plan to?) to skip the standard headers if the std module is imported.
I feel this sounds good and can be pretty helpful to end users.
For the implementation, I don't have a complete design now. In my mind, the immediate idea may be:
The idea to implement this in the library may require the library to provide an additional header to include all the controlling macros. So that the user (manually) can import std in a way like:
The idea in the compiler side may need to hardcode all the filenames for the standard headers and skip entering such headers.
The idea to implement this in the compiler side and the library side is the library provides such header and the compiler can insert it automatically.
The idea to implement this is still in the early phase. Any comments are welcomed.
The text was updated successfully, but these errors were encountered: