mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:50:13 +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
61 lines
2.2 KiB
Diff
61 lines
2.2 KiB
Diff
From 2c7bb43e8800ef29d815f37422a2e56ee0e724b1 Mon Sep 17 00:00:00 2001
|
|
From: Nicholas Bishop <nicholsabishop@google.com>
|
|
Date: Wed, 10 Jun 2026 20:19:57 -0400
|
|
Subject: [PATCH] Add empty constructor and GetSources method to
|
|
MultiplexExternalSemaSource
|
|
|
|
---
|
|
.../include/clang/Sema/MultiplexExternalSemaSource.h | 11 ++++++++++-
|
|
clang/lib/Sema/MultiplexExternalSemaSource.cpp | 3 +++
|
|
2 files changed, 13 insertions(+), 1 deletion(-)
|
|
|
|
diff --git a/clang/include/clang/Sema/MultiplexExternalSemaSource.h b/clang/include/clang/Sema/MultiplexExternalSemaSource.h
|
|
index 12015724b39f..9230d843e923 100644
|
|
--- a/clang/include/clang/Sema/MultiplexExternalSemaSource.h
|
|
+++ b/clang/include/clang/Sema/MultiplexExternalSemaSource.h
|
|
@@ -39,10 +39,16 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
|
|
/// LLVM-style RTTI.
|
|
static char ID;
|
|
|
|
+public:
|
|
+ using SourceVector = SmallVector<llvm::IntrusiveRefCntPtr<ExternalSemaSource>, 2>;
|
|
+
|
|
private:
|
|
- SmallVector<llvm::IntrusiveRefCntPtr<ExternalSemaSource>, 2> Sources;
|
|
+ SourceVector Sources;
|
|
|
|
public:
|
|
+ /// Constructs an empty multiplexing external sema source.
|
|
+ MultiplexExternalSemaSource();
|
|
+
|
|
/// Constructs a new multiplexing external sema source and appends the
|
|
/// given element to it.
|
|
///
|
|
@@ -58,6 +64,9 @@ public:
|
|
///
|
|
void AddSource(llvm::IntrusiveRefCntPtr<ExternalSemaSource> Source);
|
|
|
|
+ /// Returns a reference to the sources vector.
|
|
+ SourceVector& GetSources() { return Sources; }
|
|
+
|
|
//===--------------------------------------------------------------------===//
|
|
// ExternalASTSource.
|
|
//===--------------------------------------------------------------------===//
|
|
diff --git a/clang/lib/Sema/MultiplexExternalSemaSource.cpp b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
|
|
index be9582ce501f..4306143025c8 100644
|
|
--- a/clang/lib/Sema/MultiplexExternalSemaSource.cpp
|
|
+++ b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
|
|
@@ -16,6 +16,9 @@ using namespace clang;
|
|
|
|
char MultiplexExternalSemaSource::ID;
|
|
|
|
+/// Constructs an empty multiplexing external sema source.
|
|
+MultiplexExternalSemaSource::MultiplexExternalSemaSource() {}
|
|
+
|
|
/// Constructs a new multiplexing external sema source and appends the
|
|
/// given element to it.
|
|
///
|
|
--
|
|
2.54.0.1136.gdb2ca164c4-goog
|
|
|