mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:40:12 +01:00
In generate_ast.cpp, an `CarbonExternalASTSource` is installed that has a `Check::Context` pointer. During lowering, this `ExternalASTSource` is still installed, and using it can cause a crash if the now-invalid pointer is dereferenced. Fix by adding a new `ReadOnlyASTSource` in sem_ir, and using that during lowering. `CarbonExternalASTSource` now inherits from `ReadOnlyASTSource` to avoid some code duplication. In generate_ast.cpp, we now always install a multiplex source, even if there's only one child source. Clang internally keeps pointers to the top-level `ExternalASTSource` installed via `setExternalSource`, and those pointers aren't updated if `setExternalSource` is called again. By using `MultiplexExternalSemaSource`, we can keep the top-level `ExternalASTSource` pointer the same, and only update its children. Using `MultiplexExternalSemaSource` this way requires a new constructor and a method to modify its child sources; added a new LLVM patch adding those. Originally landed in #7335, reverted in #7353 due to ASAN errors. Changes since original: * Use LLVM RTTI to make `Lower::Context::Finalize` less brittle. Add LLVM RTTI to `ReadOnlyASTSource` (and `CarbonExternalASTSource`). Change Finalize so that instead of just deleting the last multiplex child source, it erases any multiplex child sources that match `ReadOnlyASTSource`; this includes `CarbonExternalASTSource` since it's a subclass. * Fix ASAN error by updating the `MultiplexExternalSemaSource` earlier in lowering. It is sometimes accessed during PrepareToLower, so update it in `Context::GetFileContext` rather than `Context::Finalize`. Fixes https://github.com/carbon-language/carbon-lang/issues/7142