-
Notifications
You must be signed in to change notification settings - Fork 111
adds experimental support for C++20 modules #248
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
Conversation
Blocked by #249. |
Programmers can now use `import cpptrace;` instead of needing to include headers. Support is currently experimental. Since macros aren't exported by modules, users need to include either `<cpptrace/exceptions_macros.hpp>` or `<from_current_macros.hpp>`, depending on their needs.
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.
Thanks so much for taking the time to do this. I think everything looks good to me, a couple quick comments
cmake/InstallRules.cmake
Outdated
@@ -25,6 +25,7 @@ install( | |||
ARCHIVE # | |||
COMPONENT ${package_name}_development | |||
INCLUDES # | |||
FILE_SET CXX_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.
I'm not familiar with this option but I'll trust this does something reasonable :)
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 installs the module interface unit :)
cmake/Autoconfig.cmake
Outdated
# check_cxx_source_compiles doesn't support modules (yet?) so we need to drop | ||
# to a raw try_compile. | ||
try_compile( | ||
HAS_CXX20_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 looks like this test will pass for much older versions of clang than I was expecting, all the way back to clang 10 while I'm under the impression the module implementation only got more feature-complete and stable recently https://gcc.godbolt.org/z/4r73zGxd6. On the other hand, it won't work for any version of gcc because of -fmodules
(which might be ok, not sure how stable gcc is yet but I'm under the impression they're at least close to feature-complete / stability). Thoughts on how best to approach this? I'm not sure what the best default behavior is.
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, that's going too far back. CMake support for modules is only very recent, so I guess we could gate on compiler versions?
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 thinking compiler version might be more robust yeah (but more of a hassle to check)
src/cpptrace_module.cpp
Outdated
|
||
export module cpptrace; | ||
|
||
namespace cpptrace::inline v1 { |
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.
Is it possible to rely on the begin/end namespace macros 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.
Yep, I'll get on that.
@@ -0,0 +1,54 @@ | |||
#ifndef CPPTRACE_FROM_CURRENT_MACROS_HPP |
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.
Out of curiosity, is there a sensible way to make either of the following work instead of a new macros header?
#include <cpptrace/from_current.hpp>
import cpptrace;
// or
import cpptrace;
#include <cpptrace/from_current.hpp>
If a macros header is the best way to do this I think it's good with me - and I'm guessing if so other libraries might follow the same pattern. But I'd love to be able to keep #include <cpptrace/from_current.hpp>
under modules if possible as that is currently in a sense part of the library interface.
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.
We can do the former, but not the latter (yet). Having said that, including cpptrace/from_current.hpp
will undo some or all of the hard work that modules is accomplishing. I'm not aware of a better way to import macros from a module library than to do
#include <cpptrace/from_current_macros.hpp>
import cpptrace;
Once support for modules stabilises, I'd recommend the docs encourage header users not include the _macros.hpp
headers, and instead just stick with the original headers (which export the macros).
src/cpptrace_module.cpp
Outdated
@@ -0,0 +1,102 @@ | |||
module; |
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 suggest module interface files to end with .cppm
instead of .cpp
. It improves the readability slightly.
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 convinced that .cppm
improves the readability. Could you elaborate, please?
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 uniform convention can help people to identify things more easily. E.g., I can search for *.cppm
in github to find the repos supporting modules. Or we can count the number of module interfaces more easily.
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.
Unfortunately, as is the way of C++, it looks like people are already using a wide variety of extensions for modules https://github.com/search?type=code&q=%2F%5E%28%3F-i%29module%3B%2F+language%3Ac%2B%2B+-is%3Afork&p=1. I'm not super opinionated on this issue.
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.
Yeah, this is not a forcement but a suggestion.
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 reason I'm avoidant is because the original GCC modules implementer told me that he was discouraging a different file extension after some experience. I don't think LLVM has that same aversion.
Having said that, I've literally named the file cpptrace_module.cpp
to disambiguate it from cpptrace.cpp
. I think cpptrace.cppm
might be the better of the two, even if I'd rather stick with just .cpp
.
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.
After discussion I went ahead and updated it to .cppm - since this file will be installed to share/
I think it's good to make it clear it's a module file and not just some random .cpp file.
cmake/has_modules.cpp
Outdated
@@ -0,0 +1,4 @@ | |||
export module cpptrace; | |||
|
|||
int main() |
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.
Note main is not by current standard allowed to be attached to a named module and at least GCC15 will diagnose this.
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 was surprised this worked at all. The correct thing to do was to add -fsyntax-only
to the try_compile
job, but I think this file is redundant now anyway.
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.
LGTM, thanks so much for putting this together!
Programmers can now use
import cpptrace;
instead of needing to include headers. Support is currently experimental.Since macros aren't exported by modules, users need to include either
<cpptrace/exceptions_macros.hpp>
or<from_current_macros.hpp>
, depending on their needs.