Basic support for use of C++ modules via interop. (#7241)

Create a multiplex source to pull information from both the AST reader
and from our custom source.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-05-21 00:21:54 +00:00
committed by GitHub
parent 022995e412
commit ad97b9e3a5
2 changed files with 13 additions and 21 deletions
+12 -8
View File
@@ -323,7 +323,7 @@ class ShallowCopyCompilerInvocation : public clang::CompilerInvocation {
};
// Provides clang AST nodes representing Carbon SemIR entities.
class CarbonExternalASTSource : public clang::ExternalASTSource {
class CarbonExternalASTSource : public clang::ExternalSemaSource {
public:
explicit CarbonExternalASTSource(Context* context) : context_(context) {}
@@ -896,13 +896,17 @@ auto GenerateAst(Context& context,
}
auto& ast = clang_instance.getASTContext();
// TODO: Clang's modules support is implemented as an ExternalASTSource
// (ASTReader) and there's no multiplexing support for ExternalASTSources at
// the moment - so registering CarbonExternalASTSource breaks Clang modules
// support. Implement multiplexing support (possibly in Clang) to restore
// modules functionality.
ast.setExternalSource(
llvm::makeIntrusiveRefCnt<CarbonExternalASTSource>(&context));
llvm::IntrusiveRefCntPtr<clang::ExternalSemaSource> carbon_source =
llvm::makeIntrusiveRefCnt<CarbonExternalASTSource>(&context);
if (auto* existing_source = llvm::cast_or_null<clang::ExternalSemaSource>(
ast.getExternalSource())) {
auto multiplex_source =
llvm::makeIntrusiveRefCnt<clang::MultiplexExternalSemaSource>(
existing_source, std::move(carbon_source));
ast.setExternalSource(std::move(multiplex_source));
} else {
ast.setExternalSource(std::move(carbon_source));
}
if (llvm::Error error = action.Execute()) {
// `Execute` currently never fails, but its contract allows it to.