diff --git a/toolchain/check/cpp/generate_ast.cpp b/toolchain/check/cpp/generate_ast.cpp index c62b4bd1b0a2..a42e8c8a0351 100644 --- a/toolchain/check/cpp/generate_ast.cpp +++ b/toolchain/check/cpp/generate_ast.cpp @@ -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(&context)); + llvm::IntrusiveRefCntPtr carbon_source = + llvm::makeIntrusiveRefCnt(&context); + if (auto* existing_source = llvm::cast_or_null( + ast.getExternalSource())) { + auto multiplex_source = + llvm::makeIntrusiveRefCnt( + 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. diff --git a/toolchain/check/testdata/interop/cpp/modules/import.carbon b/toolchain/check/testdata/interop/cpp/modules/import.carbon index 6d2488439ad7..ba7a38f471e1 100644 --- a/toolchain/check/testdata/interop/cpp/modules/import.carbon +++ b/toolchain/check/testdata/interop/cpp/modules/import.carbon @@ -37,25 +37,13 @@ struct B {}; B *_Nonnull makeB(A *_Nonnull); -// --- fail_todo_use_a_b.carbon +// --- use_a_b.carbon library "[[@TEST_NAME]]"; import Cpp library "a.h"; import Cpp library "b.h"; -// CHECK:STDERR: fail_todo_use_a_b.carbon:[[@LINE+4]]:12: error: member name `B` not found in `Cpp` [MemberNameNotFoundInInstScope] -// CHECK:STDERR: fn Go() -> Cpp.B* { -// CHECK:STDERR: ^~~~~ -// CHECK:STDERR: fn Go() -> Cpp.B* { - // CHECK:STDERR: fail_todo_use_a_b.carbon:[[@LINE+8]]:10: error: member name `makeB` not found in `Cpp` [MemberNameNotFoundInInstScope] - // CHECK:STDERR: return Cpp.makeB(Cpp.makeA()); - // CHECK:STDERR: ^~~~~~~~~ - // CHECK:STDERR: - // CHECK:STDERR: fail_todo_use_a_b.carbon:[[@LINE+4]]:20: error: member name `makeA` not found in `Cpp` [MemberNameNotFoundInInstScope] - // CHECK:STDERR: return Cpp.makeB(Cpp.makeA()); - // CHECK:STDERR: ^~~~~~~~~ - // CHECK:STDERR: return Cpp.makeB(Cpp.makeA()); }