Skip to content

Commit f40819e

Browse files
author
Eric Liu
committedAug 9, 2018
[clangd] Record the file being processed in a TUScheduler thread in context.
Summary: This allows implementations like different symbol indexes to know what the current active file is. For example, some customized index implementation might decide to only return results for some files. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: javed.absar, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D50446 llvm-svn: 339320
1 parent 5df524f commit f40819e

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed
 

‎clang-tools-extra/clangd/TUScheduler.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ namespace {
6363
class ASTWorker;
6464
}
6565

66+
static const clang::clangd::Key<std::string> kFileBeingProcessed;
67+
68+
llvm::Optional<llvm::StringRef> TUScheduler::getFileBeingProcessedInContext() {
69+
if (auto *File = Context::current().get(kFileBeingProcessed))
70+
return StringRef(*File);
71+
return llvm::None;
72+
}
73+
6674
/// An LRU cache of idle ASTs.
6775
/// Because we want to limit the overall number of these we retain, the cache
6876
/// owns ASTs (and may evict them) while their workers are idle.
@@ -491,8 +499,9 @@ void ASTWorker::startTask(llvm::StringRef Name,
491499
{
492500
std::lock_guard<std::mutex> Lock(Mutex);
493501
assert(!Done && "running a task after stop()");
494-
Requests.push_back({std::move(Task), Name, steady_clock::now(),
495-
Context::current().clone(), UpdateType});
502+
Requests.push_back(
503+
{std::move(Task), Name, steady_clock::now(),
504+
Context::current().derive(kFileBeingProcessed, FileName), UpdateType});
496505
}
497506
RequestsCV.notify_all();
498507
}
@@ -734,10 +743,12 @@ void TUScheduler::runWithPreamble(
734743
Action(InputsAndPreamble{Contents, Command, Preamble.get()});
735744
};
736745

737-
PreambleTasks->runAsync("task:" + llvm::sys::path::filename(File),
738-
Bind(Task, std::string(Name), std::string(File),
739-
It->second->Contents, It->second->Command,
740-
Context::current().clone(), std::move(Action)));
746+
PreambleTasks->runAsync(
747+
"task:" + llvm::sys::path::filename(File),
748+
Bind(Task, std::string(Name), std::string(File), It->second->Contents,
749+
It->second->Command,
750+
Context::current().derive(kFileBeingProcessed, File),
751+
std::move(Action)));
741752
}
742753

743754
std::vector<std::pair<Path, std::size_t>>

‎clang-tools-extra/clangd/TUScheduler.h

+8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ class TUScheduler {
122122
/// an LRU cache.
123123
class ASTCache;
124124

125+
// The file being built/processed in the current thread. This is a hack in
126+
// order to get the file name into the index implementations. Do not depend on
127+
// this inside clangd.
128+
// FIXME: remove this when there is proper index support via build system
129+
// integration.
130+
static llvm::Optional<llvm::StringRef> getFileBeingProcessedInContext();
131+
125132
private:
126133
const bool StorePreamblesInMemory;
127134
const std::shared_ptr<PCHContainerOperations> PCHOps;
@@ -135,6 +142,7 @@ class TUScheduler {
135142
llvm::Optional<AsyncTaskRunner> WorkerThreads;
136143
std::chrono::steady_clock::duration UpdateDebounce;
137144
};
145+
138146
} // namespace clangd
139147
} // namespace clang
140148

‎clang-tools-extra/unittests/clangd/TUSchedulerTests.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,22 @@ TEST_F(TUSchedulerTests, ManyUpdates) {
197197
{
198198
WithContextValue WithNonce(NonceKey, ++Nonce);
199199
S.update(File, Inputs, WantDiagnostics::Auto,
200-
[Nonce, &Mut,
200+
[File, Nonce, &Mut,
201201
&TotalUpdates](llvm::Optional<std::vector<Diag>> Diags) {
202202
EXPECT_THAT(Context::current().get(NonceKey),
203203
Pointee(Nonce));
204204

205205
std::lock_guard<std::mutex> Lock(Mut);
206206
++TotalUpdates;
207+
EXPECT_EQ(File,
208+
*TUScheduler::getFileBeingProcessedInContext());
207209
});
208210
}
209211

210212
{
211213
WithContextValue WithNonce(NonceKey, ++Nonce);
212214
S.runWithAST("CheckAST", File,
213-
[Inputs, Nonce, &Mut,
215+
[File, Inputs, Nonce, &Mut,
214216
&TotalASTReads](llvm::Expected<InputsAndAST> AST) {
215217
EXPECT_THAT(Context::current().get(NonceKey),
216218
Pointee(Nonce));
@@ -221,23 +223,27 @@ TEST_F(TUSchedulerTests, ManyUpdates) {
221223

222224
std::lock_guard<std::mutex> Lock(Mut);
223225
++TotalASTReads;
226+
EXPECT_EQ(
227+
File,
228+
*TUScheduler::getFileBeingProcessedInContext());
224229
});
225230
}
226231

227232
{
228233
WithContextValue WithNonce(NonceKey, ++Nonce);
229-
S.runWithPreamble("CheckPreamble", File,
230-
[Inputs, Nonce, &Mut, &TotalPreambleReads](
231-
llvm::Expected<InputsAndPreamble> Preamble) {
232-
EXPECT_THAT(Context::current().get(NonceKey),
233-
Pointee(Nonce));
234-
235-
ASSERT_TRUE((bool)Preamble);
236-
EXPECT_EQ(Preamble->Contents, Inputs.Contents);
237-
238-
std::lock_guard<std::mutex> Lock(Mut);
239-
++TotalPreambleReads;
240-
});
234+
S.runWithPreamble(
235+
"CheckPreamble", File,
236+
[File, Inputs, Nonce, &Mut, &TotalPreambleReads](
237+
llvm::Expected<InputsAndPreamble> Preamble) {
238+
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
239+
240+
ASSERT_TRUE((bool)Preamble);
241+
EXPECT_EQ(Preamble->Contents, Inputs.Contents);
242+
243+
std::lock_guard<std::mutex> Lock(Mut);
244+
++TotalPreambleReads;
245+
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
246+
});
241247
}
242248
}
243249
}

0 commit comments

Comments
 (0)
Please sign in to comment.