Skip to content

Commit 5b5329b

Browse files
committed
[NFC] Make MultiplexExternalSemaSource own sources
This change refactors the MuiltiplexExternalSemaSource to take ownership of the underlying sources. As a result it makes a larger cleanup of external source ownership in Sema and the ChainedIncludesSource. Reviewed By: aaron.ballman, aprantl Differential Revision: https://reviews.llvm.org/D133158
1 parent 1491282 commit 5b5329b

File tree

8 files changed

+86
-108
lines changed

8 files changed

+86
-108
lines changed

clang-tools-extra/clang-include-fixer/IncludeFixer.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ namespace {
2727
class Action : public clang::ASTFrontendAction {
2828
public:
2929
explicit Action(SymbolIndexManager &SymbolIndexMgr, bool MinimizeIncludePaths)
30-
: SemaSource(SymbolIndexMgr, MinimizeIncludePaths,
31-
/*GenerateDiagnostics=*/false) {}
30+
: SemaSource(new IncludeFixerSemaSource(SymbolIndexMgr,
31+
MinimizeIncludePaths,
32+
/*GenerateDiagnostics=*/false)) {}
3233

3334
std::unique_ptr<clang::ASTConsumer>
3435
CreateASTConsumer(clang::CompilerInstance &Compiler,
3536
StringRef InFile) override {
36-
SemaSource.setFilePath(InFile);
37+
SemaSource->setFilePath(InFile);
3738
return std::make_unique<clang::ASTConsumer>();
3839
}
3940

@@ -51,8 +52,8 @@ class Action : public clang::ASTFrontendAction {
5152
CompletionConsumer = &Compiler->getCodeCompletionConsumer();
5253

5354
Compiler->createSema(getTranslationUnitKind(), CompletionConsumer);
54-
SemaSource.setCompilerInstance(Compiler);
55-
Compiler->getSema().addExternalSource(&SemaSource);
55+
SemaSource->setCompilerInstance(Compiler);
56+
Compiler->getSema().addExternalSource(SemaSource.get());
5657

5758
clang::ParseAST(Compiler->getSema(), Compiler->getFrontendOpts().ShowStats,
5859
Compiler->getFrontendOpts().SkipFunctionBodies);
@@ -61,12 +62,12 @@ class Action : public clang::ASTFrontendAction {
6162
IncludeFixerContext
6263
getIncludeFixerContext(const clang::SourceManager &SourceManager,
6364
clang::HeaderSearch &HeaderSearch) const {
64-
return SemaSource.getIncludeFixerContext(SourceManager, HeaderSearch,
65-
SemaSource.getMatchedSymbols());
65+
return SemaSource->getIncludeFixerContext(SourceManager, HeaderSearch,
66+
SemaSource->getMatchedSymbols());
6667
}
6768

6869
private:
69-
IncludeFixerSemaSource SemaSource;
70+
IntrusiveRefCntPtr<IncludeFixerSemaSource> SemaSource;
7071
};
7172

7273
} // namespace

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,24 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
4040
static char ID;
4141

4242
private:
43-
SmallVector<ExternalSemaSource *, 2> Sources; // doesn't own them.
43+
SmallVector<ExternalSemaSource *, 2> Sources;
4444

4545
public:
46-
47-
///Constructs a new multiplexing external sema source and appends the
46+
/// Constructs a new multiplexing external sema source and appends the
4847
/// given element to it.
4948
///
50-
///\param[in] s1 - A non-null (old) ExternalSemaSource.
51-
///\param[in] s2 - A non-null (new) ExternalSemaSource.
49+
///\param[in] S1 - A non-null (old) ExternalSemaSource.
50+
///\param[in] S2 - A non-null (new) ExternalSemaSource.
5251
///
53-
MultiplexExternalSemaSource(ExternalSemaSource& s1, ExternalSemaSource& s2);
52+
MultiplexExternalSemaSource(ExternalSemaSource *S1, ExternalSemaSource *S2);
5453

5554
~MultiplexExternalSemaSource() override;
5655

57-
///Appends new source to the source list.
56+
/// Appends new source to the source list.
5857
///
5958
///\param[in] source - An ExternalSemaSource.
6059
///
61-
void addSource(ExternalSemaSource &source);
60+
void AddSource(ExternalSemaSource *Source);
6261

6362
//===--------------------------------------------------------------------===//
6463
// ExternalASTSource.

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ class Sema final {
360360
void operator=(const Sema &) = delete;
361361

362362
///Source of additional semantic information.
363-
ExternalSemaSource *ExternalSource;
364-
365-
///Whether Sema has generated a multiplexer and has to delete it.
366-
bool isMultiplexExternalSource;
363+
IntrusiveRefCntPtr<ExternalSemaSource> ExternalSource;
367364

368365
static bool mightHaveNonExternalLinkage(const DeclaratorDecl *FD);
369366

@@ -1637,7 +1634,7 @@ class Sema final {
16371634
ASTContext &getASTContext() const { return Context; }
16381635
ASTConsumer &getASTConsumer() const { return Consumer; }
16391636
ASTMutationListener *getASTMutationListener() const;
1640-
ExternalSemaSource* getExternalSource() const { return ExternalSource; }
1637+
ExternalSemaSource *getExternalSource() const { return ExternalSource.get(); }
16411638

16421639
DarwinSDKInfo *getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc,
16431640
StringRef Platform);

clang/lib/Frontend/ChainedIncludesSource.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
using namespace clang;
2828

2929
namespace {
30-
class ChainedIncludesSourceImpl : public ExternalSemaSource {
30+
class ChainedIncludesSource : public ExternalSemaSource {
3131
public:
32-
ChainedIncludesSourceImpl(std::vector<std::unique_ptr<CompilerInstance>> CIs)
32+
ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs)
3333
: CIs(std::move(CIs)) {}
3434

3535
protected:
36-
//===----------------------------------------------------------------------===//
36+
//===--------------------------------------------------------------------===//
3737
// ExternalASTSource interface.
38-
//===----------------------------------------------------------------------===//
38+
//===--------------------------------------------------------------------===//
3939

4040
/// Return the amount of memory used by memory buffers, breaking down
4141
/// by heap-backed versus mmap'ed memory.
@@ -51,30 +51,7 @@ class ChainedIncludesSourceImpl : public ExternalSemaSource {
5151
private:
5252
std::vector<std::unique_ptr<CompilerInstance>> CIs;
5353
};
54-
55-
/// Members of ChainedIncludesSource, factored out so we can initialize
56-
/// them before we initialize the ExternalSemaSource base class.
57-
struct ChainedIncludesSourceMembers {
58-
ChainedIncludesSourceMembers(
59-
std::vector<std::unique_ptr<CompilerInstance>> CIs,
60-
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
61-
: Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
62-
ChainedIncludesSourceImpl Impl;
63-
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
64-
};
65-
66-
/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
67-
/// calls to the final reader.
68-
class ChainedIncludesSource
69-
: private ChainedIncludesSourceMembers,
70-
public MultiplexExternalSemaSource {
71-
public:
72-
ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
73-
IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
74-
: ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
75-
MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
76-
};
77-
}
54+
} // end anonymous namespace
7855

7956
static ASTReader *
8057
createASTReader(CompilerInstance &CI, StringRef pchFile,
@@ -214,6 +191,8 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
214191
if (!Reader)
215192
return nullptr;
216193

217-
return IntrusiveRefCntPtr<ChainedIncludesSource>(
218-
new ChainedIncludesSource(std::move(CIs), Reader));
194+
auto ChainedSrc =
195+
llvm::makeIntrusiveRefCnt<ChainedIncludesSource>(std::move(CIs));
196+
return llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>(
197+
ChainedSrc.get(), Reader.get());
219198
}

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,30 @@ using namespace clang;
1616

1717
char MultiplexExternalSemaSource::ID;
1818

19-
///Constructs a new multiplexing external sema source and appends the
19+
/// Constructs a new multiplexing external sema source and appends the
2020
/// given element to it.
2121
///
22-
MultiplexExternalSemaSource::MultiplexExternalSemaSource(ExternalSemaSource &s1,
23-
ExternalSemaSource &s2){
24-
Sources.push_back(&s1);
25-
Sources.push_back(&s2);
22+
MultiplexExternalSemaSource::MultiplexExternalSemaSource(
23+
ExternalSemaSource *S1, ExternalSemaSource *S2) {
24+
S1->Retain();
25+
S2->Retain();
26+
Sources.push_back(S1);
27+
Sources.push_back(S2);
2628
}
2729

2830
// pin the vtable here.
29-
MultiplexExternalSemaSource::~MultiplexExternalSemaSource() {}
31+
MultiplexExternalSemaSource::~MultiplexExternalSemaSource() {
32+
for (auto *S : Sources)
33+
S->Release();
34+
}
3035

31-
///Appends new source to the source list.
36+
/// Appends new source to the source list.
3237
///
3338
///\param[in] source - An ExternalSemaSource.
3439
///
35-
void MultiplexExternalSemaSource::addSource(ExternalSemaSource &source) {
36-
Sources.push_back(&source);
40+
void MultiplexExternalSemaSource::AddSource(ExternalSemaSource *Source) {
41+
Source->Retain();
42+
Sources.push_back(Source);
3743
}
3844

3945
//===----------------------------------------------------------------------===//

clang/lib/Sema/Sema.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,10 @@ const uint64_t Sema::MaximumAlignment;
185185

186186
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
187187
TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter)
188-
: ExternalSource(nullptr), isMultiplexExternalSource(false),
189-
CurFPFeatures(pp.getLangOpts()), LangOpts(pp.getLangOpts()), PP(pp),
190-
Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()),
191-
SourceMgr(PP.getSourceManager()), CollectStats(false),
192-
CodeCompleter(CodeCompleter), CurContext(nullptr),
188+
: ExternalSource(nullptr), CurFPFeatures(pp.getLangOpts()),
189+
LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
190+
Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
191+
CollectStats(false), CodeCompleter(CodeCompleter), CurContext(nullptr),
193192
OriginalLexicalContext(nullptr), MSStructPragmaOn(false),
194193
MSPointerToMemberRepresentationMethod(
195194
LangOpts.getMSPointerToMemberRepresentationMethod()),
@@ -473,10 +472,6 @@ Sema::~Sema() {
473472
= dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
474473
ExternalSema->ForgetSema();
475474

476-
// If Sema's ExternalSource is the multiplexer - we own it.
477-
if (isMultiplexExternalSource)
478-
delete ExternalSource;
479-
480475
// Delete cached satisfactions.
481476
std::vector<ConstraintSatisfaction *> Satisfactions;
482477
Satisfactions.reserve(Satisfactions.size());
@@ -550,12 +545,10 @@ void Sema::addExternalSource(ExternalSemaSource *E) {
550545
return;
551546
}
552547

553-
if (isMultiplexExternalSource)
554-
static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E);
555-
else {
556-
ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E);
557-
isMultiplexExternalSource = true;
558-
}
548+
if (auto *Ex = dyn_cast<MultiplexExternalSemaSource>(ExternalSource))
549+
Ex->AddSource(E);
550+
else
551+
ExternalSource = new MultiplexExternalSemaSource(ExternalSource.get(), E);
559552
}
560553

561554
/// Print out statistics about the semantic analysis.
@@ -1291,8 +1284,8 @@ void Sema::ActOnEndOfTranslationUnit() {
12911284
// translation unit, with an initializer equal to 0.
12921285
llvm::SmallSet<VarDecl *, 32> Seen;
12931286
for (TentativeDefinitionsType::iterator
1294-
T = TentativeDefinitions.begin(ExternalSource),
1295-
TEnd = TentativeDefinitions.end();
1287+
T = TentativeDefinitions.begin(ExternalSource.get()),
1288+
TEnd = TentativeDefinitions.end();
12961289
T != TEnd; ++T) {
12971290
VarDecl *VD = (*T)->getActingDefinition();
12981291

@@ -1336,8 +1329,9 @@ void Sema::ActOnEndOfTranslationUnit() {
13361329
if (!Diags.hasErrorOccurred() && TUKind != TU_Module) {
13371330
// Output warning for unused file scoped decls.
13381331
for (UnusedFileScopedDeclsType::iterator
1339-
I = UnusedFileScopedDecls.begin(ExternalSource),
1340-
E = UnusedFileScopedDecls.end(); I != E; ++I) {
1332+
I = UnusedFileScopedDecls.begin(ExternalSource.get()),
1333+
E = UnusedFileScopedDecls.end();
1334+
I != E; ++I) {
13411335
if (ShouldRemoveFromUnused(this, *I))
13421336
continue;
13431337

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18183,8 +18183,8 @@ void Sema::CheckDelegatingCtorCycles() {
1818318183
llvm::SmallPtrSet<CXXConstructorDecl*, 4> Valid, Invalid, Current;
1818418184

1818518185
for (DelegatingCtorDeclsType::iterator
18186-
I = DelegatingCtorDecls.begin(ExternalSource),
18187-
E = DelegatingCtorDecls.end();
18186+
I = DelegatingCtorDecls.begin(ExternalSource.get()),
18187+
E = DelegatingCtorDecls.end();
1818818188
I != E; ++I)
1818918189
DelegatingCycleHelper(*I, Valid, Invalid, Current, *this);
1819018190

0 commit comments

Comments
 (0)