@@ -63,27 +63,36 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
63
63
ClangdServer::Callbacks *ServerCallbacks,
64
64
const ThreadsafeFS &TFS, AsyncTaskRunner *Tasks)
65
65
: FIndex(FIndex), ServerCallbacks(ServerCallbacks), TFS(TFS),
66
- Tasks (Tasks) {}
66
+ Stdlib{std::make_shared<StdLibSet>()}, Tasks(Tasks) {}
67
67
68
68
void onPreambleAST (PathRef Path, llvm::StringRef Version,
69
69
const CompilerInvocation &CI, ASTContext &Ctx,
70
70
Preprocessor &PP,
71
71
const CanonicalIncludes &CanonIncludes) override {
72
72
// If this preamble uses a standard library we haven't seen yet, index it.
73
73
if (FIndex)
74
- if (auto Loc = Stdlib. add (*CI.getLangOpts (), PP.getHeaderSearchInfo ()))
74
+ if (auto Loc = Stdlib-> add (*CI.getLangOpts (), PP.getHeaderSearchInfo ()))
75
75
indexStdlib (CI, std::move (*Loc));
76
76
77
77
if (FIndex)
78
78
FIndex->updatePreamble (Path, Version, Ctx, PP, CanonIncludes);
79
79
}
80
80
81
81
void indexStdlib (const CompilerInvocation &CI, StdLibLocation Loc) {
82
- auto Task = [this , LO (*CI.getLangOpts ()), Loc (std::move (Loc)),
83
- CI (std::make_unique<CompilerInvocation>(CI))]() mutable {
82
+ // This task is owned by Tasks, which outlives the TUScheduler and
83
+ // therefore the UpdateIndexCallbacks.
84
+ // We must be careful that the references we capture outlive TUScheduler.
85
+ auto Task = [LO (*CI.getLangOpts ()), Loc (std::move (Loc)),
86
+ CI (std::make_unique<CompilerInvocation>(CI)),
87
+ // External values that outlive ClangdServer
88
+ TFS (&TFS),
89
+ // Index outlives TUScheduler (declared first)
90
+ FIndex (FIndex),
91
+ // shared_ptr extends lifetime
92
+ Stdlib (Stdlib)]() mutable {
84
93
IndexFileIn IF;
85
- IF.Symbols = indexStandardLibrary (std::move (CI), Loc, TFS);
86
- if (Stdlib. isBest (LO))
94
+ IF.Symbols = indexStandardLibrary (std::move (CI), Loc, * TFS);
95
+ if (Stdlib-> isBest (LO))
87
96
FIndex->updatePreamble (std::move (IF));
88
97
};
89
98
if (Tasks)
@@ -128,7 +137,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
128
137
FileIndex *FIndex;
129
138
ClangdServer::Callbacks *ServerCallbacks;
130
139
const ThreadsafeFS &TFS;
131
- StdLibSet Stdlib;
140
+ std::shared_ptr< StdLibSet> Stdlib;
132
141
AsyncTaskRunner *Tasks;
133
142
};
134
143
@@ -233,9 +242,6 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
233
242
}
234
243
235
244
ClangdServer::~ClangdServer () {
236
- // Wait for stdlib indexing to finish.
237
- if (IndexTasks)
238
- IndexTasks->wait ();
239
245
// Destroying TUScheduler first shuts down request threads that might
240
246
// otherwise access members concurrently.
241
247
// (Nobody can be using TUScheduler because we're on the main thread).
0 commit comments