# Is it possible for MultiplexExternalSemaSource to own the Sources?

**URL:** <https://discourse.llvm.org/t/is-it-possible-for-multiplexexternalsemasource-to-own-the-sources/64990>\
**Category:** Clang Frontend\
**Created:** [September 1, 2022, 4:49am UTC](https://discourse.llvm.org/t/is-it-possible-for-multiplexexternalsemasource-to-own-the-sources/64990 "2022-09-01T04:49:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![python3kgae](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/python3kgae/32/14405_2.png) [@python3kgae](https://discourse.llvm.org/u/python3kgae)\
**Post date:** [September 1, 2022, 4:49am UTC](https://discourse.llvm.org/t/is-it-possible-for-multiplexexternalsemasource-to-own-the-sources/64990/1 "2022-09-01T04:49:15Z")

</div>

MultiplexExternalSemaSource doesn’t own the Sources.  
`SmallVector<ExternalSemaSource *, 2> Sources; // doesn't own them.`

So when using it, one has to take care of the ownership of the Sources.

One solution I found in clang is in ChainedIncludesSource.  
It creates a struct to hold the Sources and inherited the struct.

```auto
/// Members of ChainedIncludesSource, factored out so we can initialize
/// them before we initialize the ExternalSemaSource base class.
struct ChainedIncludesSourceMembers {
  ChainedIncludesSourceMembers(
      std::vector<std::unique_ptr<CompilerInstance>> CIs,
      IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
      : Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
  ChainedIncludesSourceImpl Impl;
  IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
};

/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
/// calls to the final reader.
class ChainedIncludesSource
    : private ChainedIncludesSourceMembers,
      public MultiplexExternalSemaSource {
public:
  ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
                        IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
      : ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
        MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
};

```

Is there another solution to take care of the ownership of Sources?

Is it possible for MultiplexExternalSemaSource to own the Sources?

Thanks  
Xiang

---

<div class="post-metadata">

**Author:** ![beanz](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/beanz/32/1939_2.png) [@beanz](https://discourse.llvm.org/u/beanz)\
**Post date:** [September 1, 2022, 11:02pm UTC](https://discourse.llvm.org/t/is-it-possible-for-multiplexexternalsemasource-to-own-the-sources/64990/2 "2022-09-01T23:02:19Z")

</div>

Posted a change for review: [⚙ D133158 [NFC] Make MultiplexExternalSemaSource own sources](https://reviews.llvm.org/D133158)
