Skip to content

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

Merged
merged 13 commits into from
May 26, 2025

Conversation

cjdb
Copy link

@cjdb cjdb commented May 23, 2025

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.

@cjdb
Copy link
Author

cjdb commented May 23, 2025

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.
Copy link
Owner

@jeremy-rifkin jeremy-rifkin left a 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

@@ -25,6 +25,7 @@ install(
ARCHIVE #
COMPONENT ${package_name}_development
INCLUDES #
FILE_SET CXX_MODULES
Copy link
Owner

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 :)

Copy link
Author

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 :)

# check_cxx_source_compiles doesn't support modules (yet?) so we need to drop
# to a raw try_compile.
try_compile(
HAS_CXX20_MODULES
Copy link
Owner

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.

Copy link
Author

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?

Copy link
Owner

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)


export module cpptrace;

namespace cpptrace::inline v1 {
Copy link
Owner

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?

Copy link
Author

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
Copy link
Owner

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.

Copy link
Author

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).

@@ -0,0 +1,102 @@
module;

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.

Copy link
Author

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?

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.

Copy link
Owner

@jeremy-rifkin jeremy-rifkin May 26, 2025

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.

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.

Copy link
Author

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.

Copy link
Owner

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.

@@ -0,0 +1,4 @@
export module cpptrace;

int main()
Copy link

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.

Copy link
Author

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.

Copy link
Owner

@jeremy-rifkin jeremy-rifkin left a 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!

@jeremy-rifkin jeremy-rifkin merged commit 2b6cf03 into jeremy-rifkin:dev May 26, 2025
99 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants