2018-02-08 15:37:35 +08:00
|
|
|
//===--- TUScheduler.cpp -----------------------------------------*-C++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-02-08 15:37:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// For each file, managed by TUScheduler, we create a single ASTWorker that
|
|
|
|
// manages an AST for that file. All operations that modify or read the AST are
|
|
|
|
// run on a separate dedicated thread asynchronously in FIFO order.
|
|
|
|
//
|
|
|
|
// We start processing each update immediately after we receive it. If two or
|
|
|
|
// more updates come subsequently without reads in-between, we attempt to drop
|
|
|
|
// an older one to not waste time building the ASTs we don't need.
|
|
|
|
//
|
|
|
|
// The processing thread of the ASTWorker is also responsible for building the
|
|
|
|
// preamble. However, unlike AST, the same preamble can be read concurrently, so
|
|
|
|
// we run each of async preamble reads on its own thread.
|
|
|
|
//
|
2019-01-03 21:28:05 +08:00
|
|
|
// To limit the concurrent load that clangd produces we maintain a semaphore
|
|
|
|
// that keeps more than a fixed number of threads from running concurrently.
|
2018-02-08 15:37:35 +08:00
|
|
|
//
|
|
|
|
// Rationale for cancelling updates.
|
|
|
|
// LSP clients can send updates to clangd on each keystroke. Some files take
|
|
|
|
// significant time to parse (e.g. a few seconds) and clangd can get starved by
|
|
|
|
// the updates to those files. Therefore we try to process only the last update,
|
|
|
|
// if possible.
|
|
|
|
// Our current strategy to do that is the following:
|
|
|
|
// - For each update we immediately schedule rebuild of the AST.
|
|
|
|
// - Rebuild of the AST checks if it was cancelled before doing any actual work.
|
|
|
|
// If it was, it does not do an actual rebuild, only reports llvm::None to the
|
|
|
|
// callback
|
|
|
|
// - When adding an update, we cancel the last update in the queue if it didn't
|
|
|
|
// have any reads.
|
|
|
|
// There is probably a optimal ways to do that. One approach we might take is
|
|
|
|
// the following:
|
|
|
|
// - For each update we remember the pending inputs, but delay rebuild of the
|
|
|
|
// AST for some timeout.
|
|
|
|
// - If subsequent updates come before rebuild was started, we replace the
|
|
|
|
// pending inputs and reset the timer.
|
|
|
|
// - If any reads of the AST are scheduled, we start building the AST
|
|
|
|
// immediately.
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "TUScheduler.h"
|
[clangd] Respect task cancellation in TUScheduler.
Summary:
- Reads are never executed if canceled before ready-to run.
In practice, we finalize cancelled reads eagerly and out-of-order.
- Cancelled reads don't prevent prior updates from being elided, as they don't
actually depend on the result of the update.
- Updates are downgraded from WantDiagnostics::Yes to WantDiagnostics::Auto when
cancelled, which allows them to be elided when all dependent reads are
cancelled and there are subsequent writes. (e.g. when the queue is backed up
with cancelled requests).
The queue operations aren't optimal (we scan the whole queue for cancelled
tasks every time the scheduler runs, and check cancellation twice in the end).
However I believe these costs are still trivial in practice (compared to any
AST operation) and the logic can be cleanly separated from the rest of the
scheduler.
Reviewers: ilya-biryukov
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54746
llvm-svn: 347450
2018-11-22 18:22:16 +08:00
|
|
|
#include "Cancellation.h"
|
2019-04-15 20:32:28 +08:00
|
|
|
#include "Compiler.h"
|
2019-10-23 16:18:09 +08:00
|
|
|
#include "Context.h"
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
#include "Diagnostics.h"
|
2019-04-15 20:32:28 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2018-02-08 15:37:35 +08:00
|
|
|
#include "Logger.h"
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "ParsedAST.h"
|
2019-09-04 15:35:00 +08:00
|
|
|
#include "Preamble.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "Trace.h"
|
2019-02-05 00:19:57 +08:00
|
|
|
#include "index/CanonicalIncludes.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2019-04-15 20:32:28 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
clangd: use -j for background index pool
Summary:
clangd supports a -j option to limit the amount of threads to use for parsing
TUs. However, when using -background-index (the default in later versions of
clangd), the parallelism used by clangd defaults to the hardware_parallelisn,
i.e. number of physical cores.
On shared hardware environments, with large projects, this can significantly
affect performance with no way to tune it down.
This change makes the -j parameter apply equally to parsing and background
index. It's not perfect, because the total number of threads is 2x the -j value,
which may still be unexpected. But at least this change allows users to prevent
clangd using all CPU cores.
Reviewers: kadircet, sammccall
Reviewed By: sammccall
Subscribers: javed.absar, jfb, sammccall, ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66031
llvm-svn: 368498
2019-08-10 07:03:32 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include <algorithm>
|
2018-02-08 15:37:35 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <thread>
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-03-02 16:56:37 +08:00
|
|
|
using std::chrono::steady_clock;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ASTWorker;
|
2019-01-25 23:14:03 +08:00
|
|
|
} // namespace
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
|
2018-08-09 17:25:26 +08:00
|
|
|
static clang::clangd::Key<std::string> kFileBeingProcessed;
|
2018-08-09 17:05:45 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<llvm::StringRef> TUScheduler::getFileBeingProcessedInContext() {
|
2018-08-09 17:05:45 +08:00
|
|
|
if (auto *File = Context::current().get(kFileBeingProcessed))
|
2019-01-07 23:45:19 +08:00
|
|
|
return llvm::StringRef(*File);
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2018-08-09 17:05:45 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// An LRU cache of idle ASTs.
|
|
|
|
/// Because we want to limit the overall number of these we retain, the cache
|
|
|
|
/// owns ASTs (and may evict them) while their workers are idle.
|
|
|
|
/// Workers borrow ASTs when active, and return them when done.
|
|
|
|
class TUScheduler::ASTCache {
|
|
|
|
public:
|
|
|
|
using Key = const ASTWorker *;
|
|
|
|
|
|
|
|
ASTCache(unsigned MaxRetainedASTs) : MaxRetainedASTs(MaxRetainedASTs) {}
|
|
|
|
|
|
|
|
/// Returns result of getUsedBytes() for the AST cached by \p K.
|
|
|
|
/// If no AST is cached, 0 is returned.
|
2018-06-01 22:44:57 +08:00
|
|
|
std::size_t getUsedBytes(Key K) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
|
|
|
auto It = findByKey(K);
|
|
|
|
if (It == LRU.end() || !It->second)
|
|
|
|
return 0;
|
|
|
|
return It->second->getUsedBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store the value in the pool, possibly removing the last used AST.
|
|
|
|
/// The value should not be in the pool when this function is called.
|
|
|
|
void put(Key K, std::unique_ptr<ParsedAST> V) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
assert(findByKey(K) == LRU.end());
|
|
|
|
|
|
|
|
LRU.insert(LRU.begin(), {K, std::move(V)});
|
|
|
|
if (LRU.size() <= MaxRetainedASTs)
|
|
|
|
return;
|
|
|
|
// We're past the limit, remove the last element.
|
|
|
|
std::unique_ptr<ParsedAST> ForCleanup = std::move(LRU.back().second);
|
|
|
|
LRU.pop_back();
|
|
|
|
// Run the expensive destructor outside the lock.
|
|
|
|
Lock.unlock();
|
|
|
|
ForCleanup.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cached value for \p K, or llvm::None if the value is not in
|
|
|
|
/// the cache anymore. If nullptr was cached for \p K, this function will
|
|
|
|
/// return a null unique_ptr wrapped into an optional.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> take(Key K) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
auto Existing = findByKey(K);
|
|
|
|
if (Existing == LRU.end())
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::unique_ptr<ParsedAST> V = std::move(Existing->second);
|
|
|
|
LRU.erase(Existing);
|
2018-06-01 20:03:16 +08:00
|
|
|
// GCC 4.8 fails to compile `return V;`, as it tries to call the copy
|
|
|
|
// constructor of unique_ptr, so we call the move ctor explicitly to avoid
|
|
|
|
// this miscompile.
|
2019-01-07 23:45:19 +08:00
|
|
|
return llvm::Optional<std::unique_ptr<ParsedAST>>(std::move(V));
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using KVPair = std::pair<Key, std::unique_ptr<ParsedAST>>;
|
|
|
|
|
|
|
|
std::vector<KVPair>::iterator findByKey(Key K) {
|
2018-10-07 22:49:41 +08:00
|
|
|
return llvm::find_if(LRU, [K](const KVPair &P) { return P.first == K; });
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex Mut;
|
|
|
|
unsigned MaxRetainedASTs;
|
|
|
|
/// Items sorted in LRU order, i.e. first item is the most recently accessed
|
|
|
|
/// one.
|
|
|
|
std::vector<KVPair> LRU; /* GUARDED_BY(Mut) */
|
|
|
|
};
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
namespace {
|
|
|
|
class ASTWorkerHandle;
|
|
|
|
|
|
|
|
/// Owns one instance of the AST, schedules updates and reads of it.
|
|
|
|
/// Also responsible for building and providing access to the preamble.
|
|
|
|
/// Each ASTWorker processes the async requests sent to it on a separate
|
|
|
|
/// dedicated thread.
|
|
|
|
/// The ASTWorker that manages the AST is shared by both the processing thread
|
|
|
|
/// and the TUScheduler. The TUScheduler should discard an ASTWorker when
|
|
|
|
/// remove() is called, but its thread may be busy and we don't want to block.
|
|
|
|
/// So the workers are accessed via an ASTWorkerHandle. Destroying the handle
|
|
|
|
/// signals the worker to exit its run loop and gives up shared ownership of the
|
|
|
|
/// worker.
|
|
|
|
class ASTWorker {
|
|
|
|
friend class ASTWorkerHandle;
|
2019-04-15 20:32:28 +08:00
|
|
|
ASTWorker(PathRef FileName, const GlobalCompilationDatabase &CDB,
|
|
|
|
TUScheduler::ASTCache &LRUCache, Semaphore &Barrier, bool RunSync,
|
|
|
|
steady_clock::duration UpdateDebounce, bool StorePreamblesInMemory,
|
|
|
|
ParsingCallbacks &Callbacks);
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create a new ASTWorker and return a handle to it.
|
|
|
|
/// The processing thread is spawned using \p Tasks. However, when \p Tasks
|
|
|
|
/// is null, all requests will be processed on the calling thread
|
|
|
|
/// synchronously instead. \p Barrier is acquired when processing each
|
2018-09-14 08:56:11 +08:00
|
|
|
/// request, it is used to limit the number of actively running threads.
|
2019-04-15 20:32:28 +08:00
|
|
|
static ASTWorkerHandle
|
|
|
|
create(PathRef FileName, const GlobalCompilationDatabase &CDB,
|
|
|
|
TUScheduler::ASTCache &IdleASTs, AsyncTaskRunner *Tasks,
|
|
|
|
Semaphore &Barrier, steady_clock::duration UpdateDebounce,
|
|
|
|
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks);
|
2018-02-08 15:37:35 +08:00
|
|
|
~ASTWorker();
|
|
|
|
|
2018-11-23 01:27:08 +08:00
|
|
|
void update(ParseInputs Inputs, WantDiagnostics);
|
2019-01-07 23:45:19 +08:00
|
|
|
void
|
|
|
|
runWithAST(llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action);
|
2018-02-13 16:59:23 +08:00
|
|
|
bool blockUntilIdle(Deadline Timeout) const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData> getPossiblyStalePreamble() const;
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
|
2018-08-30 23:07:34 +08:00
|
|
|
/// Obtain a preamble reflecting all updates so far. Threadsafe.
|
|
|
|
/// It may be delivered immediately, or later on the worker thread.
|
|
|
|
void getCurrentPreamble(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::unique_function<void(std::shared_ptr<const PreambleData>)>);
|
2019-04-15 20:32:28 +08:00
|
|
|
/// Returns compile command from the current file inputs.
|
|
|
|
tooling::CompileCommand getCurrentCompileCommand() const;
|
|
|
|
|
2018-07-09 18:45:33 +08:00
|
|
|
/// Wait for the first build of preamble to finish. Preamble itself can be
|
2018-09-14 08:56:11 +08:00
|
|
|
/// accessed via getPossiblyStalePreamble(). Note that this function will
|
2018-07-09 18:45:33 +08:00
|
|
|
/// return after an unsuccessful build of the preamble too, i.e. result of
|
|
|
|
/// getPossiblyStalePreamble() can be null even after this function returns.
|
|
|
|
void waitForFirstPreamble() const;
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::size_t getUsedBytes() const;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
bool isASTCached() const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Must be called exactly once on processing thread. Will return after
|
|
|
|
// stop() is called on a separate thread and all pending requests are
|
|
|
|
// processed.
|
|
|
|
void run();
|
|
|
|
/// Signal that run() should finish processing pending requests and exit.
|
|
|
|
void stop();
|
|
|
|
/// Adds a new task to the end of the request queue.
|
2019-01-07 23:45:19 +08:00
|
|
|
void startTask(llvm::StringRef Name, llvm::unique_function<void()> Task,
|
|
|
|
llvm::Optional<WantDiagnostics> UpdateType);
|
2018-12-06 17:41:04 +08:00
|
|
|
/// Updates the TUStatus and emits it. Only called in the worker thread.
|
|
|
|
void emitTUStatus(TUAction FAction,
|
|
|
|
const TUStatus::BuildDetails *Detail = nullptr);
|
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
/// Determines the next action to perform.
|
2018-09-14 08:56:11 +08:00
|
|
|
/// All actions that should never run are discarded.
|
2018-03-02 16:56:37 +08:00
|
|
|
/// Returns a deadline for the next action. If it's expired, run now.
|
|
|
|
/// scheduleLocked() is called again at the deadline, or if requests arrive.
|
|
|
|
Deadline scheduleLocked();
|
2018-02-22 21:11:12 +08:00
|
|
|
/// Should the first task in the queue be skipped instead of run?
|
|
|
|
bool shouldSkipHeadLocked() const;
|
2019-04-15 20:32:28 +08:00
|
|
|
/// This is private because `FileInputs.FS` is not thread-safe and thus not
|
|
|
|
/// safe to share. Callers should make sure not to expose `FS` via a public
|
|
|
|
/// interface.
|
|
|
|
std::shared_ptr<const ParseInputs> getCurrentFileInputs() const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
struct Request {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::unique_function<void()> Action;
|
2018-02-19 17:56:28 +08:00
|
|
|
std::string Name;
|
2018-03-02 16:56:37 +08:00
|
|
|
steady_clock::time_point AddTime;
|
2018-02-19 17:56:28 +08:00
|
|
|
Context Ctx;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType;
|
2018-02-19 17:56:28 +08:00
|
|
|
};
|
2018-02-08 15:37:35 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Handles retention of ASTs.
|
|
|
|
TUScheduler::ASTCache &IdleASTs;
|
2018-02-08 15:37:35 +08:00
|
|
|
const bool RunSync;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Time to wait after an update to see whether another update obsoletes it.
|
2018-03-02 16:56:37 +08:00
|
|
|
const steady_clock::duration UpdateDebounce;
|
2018-09-14 08:56:11 +08:00
|
|
|
/// File that ASTWorker is responsible for.
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
const Path FileName;
|
2019-04-15 20:32:28 +08:00
|
|
|
const GlobalCompilationDatabase &CDB;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Whether to keep the built preambles in memory or on disk.
|
|
|
|
const bool StorePreambleInMemory;
|
2018-11-23 01:27:08 +08:00
|
|
|
/// Callback invoked when preamble or main file AST is built.
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
ParsingCallbacks &Callbacks;
|
2018-12-06 17:41:04 +08:00
|
|
|
/// Only accessed by the worker thread.
|
|
|
|
TUStatus Status;
|
2018-03-02 16:56:37 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
Semaphore &Barrier;
|
2019-07-19 21:51:01 +08:00
|
|
|
/// Whether the 'onMainAST' callback ran for the current FileInputs.
|
|
|
|
bool RanASTCallback = false;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Guards members used by both TUScheduler and the worker thread.
|
2018-02-08 15:37:35 +08:00
|
|
|
mutable std::mutex Mutex;
|
2019-04-15 20:32:28 +08:00
|
|
|
/// File inputs, currently being used by the worker.
|
|
|
|
/// Inputs are written and read by the worker thread, compile command can also
|
|
|
|
/// be consumed by clients of ASTWorker.
|
|
|
|
std::shared_ptr<const ParseInputs> FileInputs; /* GUARDED_BY(Mutex) */
|
2018-02-09 18:17:23 +08:00
|
|
|
std::shared_ptr<const PreambleData> LastBuiltPreamble; /* GUARDED_BY(Mutex) */
|
2018-07-09 18:45:33 +08:00
|
|
|
/// Becomes ready when the first preamble build finishes.
|
|
|
|
Notification PreambleWasBuilt;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Set to true to signal run() to finish processing.
|
2018-03-12 23:28:22 +08:00
|
|
|
bool Done; /* GUARDED_BY(Mutex) */
|
|
|
|
std::deque<Request> Requests; /* GUARDED_BY(Mutex) */
|
2018-02-13 16:59:23 +08:00
|
|
|
mutable std::condition_variable RequestsCV;
|
2019-07-19 21:51:01 +08:00
|
|
|
/// Guards the callback that publishes results of AST-related computations
|
|
|
|
/// (diagnostics, highlightings) and file statuses.
|
|
|
|
std::mutex PublishMu;
|
|
|
|
// Used to prevent remove document + add document races that lead to
|
|
|
|
// out-of-order callbacks for publishing results of onMainAST callback.
|
|
|
|
//
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
// The lifetime of the old/new ASTWorkers will overlap, but their handles
|
|
|
|
// don't. When the old handle is destroyed, the old worker will stop reporting
|
2019-07-19 21:51:01 +08:00
|
|
|
// any results to the user.
|
|
|
|
bool CanPublishResults = true; /* GUARDED_BY(PublishMu) */
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A smart-pointer-like class that points to an active ASTWorker.
|
|
|
|
/// In destructor, signals to the underlying ASTWorker that no new requests will
|
|
|
|
/// be sent and the processing loop may exit (after running all pending
|
|
|
|
/// requests).
|
|
|
|
class ASTWorkerHandle {
|
|
|
|
friend class ASTWorker;
|
|
|
|
ASTWorkerHandle(std::shared_ptr<ASTWorker> Worker)
|
|
|
|
: Worker(std::move(Worker)) {
|
|
|
|
assert(this->Worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ASTWorkerHandle(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle &operator=(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle(ASTWorkerHandle &&) = default;
|
|
|
|
ASTWorkerHandle &operator=(ASTWorkerHandle &&) = default;
|
|
|
|
|
|
|
|
~ASTWorkerHandle() {
|
|
|
|
if (Worker)
|
|
|
|
Worker->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker &operator*() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return *Worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker *operator->() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return Worker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an owning reference to the underlying ASTWorker that can outlive
|
|
|
|
/// the ASTWorkerHandle. However, no new requests to an active ASTWorker can
|
|
|
|
/// be schedule via the returned reference, i.e. only reads of the preamble
|
|
|
|
/// are possible.
|
|
|
|
std::shared_ptr<const ASTWorker> lock() { return Worker; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<ASTWorker> Worker;
|
|
|
|
};
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
ASTWorkerHandle
|
|
|
|
ASTWorker::create(PathRef FileName, const GlobalCompilationDatabase &CDB,
|
|
|
|
TUScheduler::ASTCache &IdleASTs, AsyncTaskRunner *Tasks,
|
|
|
|
Semaphore &Barrier, steady_clock::duration UpdateDebounce,
|
|
|
|
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks) {
|
2019-04-04 20:56:03 +08:00
|
|
|
std::shared_ptr<ASTWorker> Worker(
|
2019-04-15 20:32:28 +08:00
|
|
|
new ASTWorker(FileName, CDB, IdleASTs, Barrier, /*RunSync=*/!Tasks,
|
2019-04-04 20:56:03 +08:00
|
|
|
UpdateDebounce, StorePreamblesInMemory, Callbacks));
|
2018-02-08 15:37:35 +08:00
|
|
|
if (Tasks)
|
2019-01-07 23:45:19 +08:00
|
|
|
Tasks->runAsync("worker:" + llvm::sys::path::filename(FileName),
|
2018-02-19 17:56:28 +08:00
|
|
|
[Worker]() { Worker->run(); });
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
return ASTWorkerHandle(std::move(Worker));
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
ASTWorker::ASTWorker(PathRef FileName, const GlobalCompilationDatabase &CDB,
|
|
|
|
TUScheduler::ASTCache &LRUCache, Semaphore &Barrier,
|
|
|
|
bool RunSync, steady_clock::duration UpdateDebounce,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks)
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
: IdleASTs(LRUCache), RunSync(RunSync), UpdateDebounce(UpdateDebounce),
|
2019-04-15 20:32:28 +08:00
|
|
|
FileName(FileName), CDB(CDB),
|
|
|
|
StorePreambleInMemory(StorePreamblesInMemory),
|
2019-04-04 20:56:03 +08:00
|
|
|
Callbacks(Callbacks), Status{TUAction(TUAction::Idle, ""),
|
|
|
|
TUStatus::BuildDetails()},
|
2019-04-15 20:32:28 +08:00
|
|
|
Barrier(Barrier), Done(false) {
|
|
|
|
auto Inputs = std::make_shared<ParseInputs>();
|
|
|
|
// Set a fallback command because compile command can be accessed before
|
|
|
|
// `Inputs` is initialized. Other fields are only used after initialization
|
|
|
|
// from client inputs.
|
|
|
|
Inputs->CompileCommand = CDB.getFallbackCommand(FileName);
|
|
|
|
FileInputs = std::move(Inputs);
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
ASTWorker::~ASTWorker() {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Make sure we remove the cached AST, if any.
|
|
|
|
IdleASTs.take(this);
|
2018-02-08 15:37:35 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(Done && "handle was not destroyed");
|
|
|
|
assert(Requests.empty() && "unprocessed requests when destroying ASTWorker");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-23 01:27:08 +08:00
|
|
|
void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef TaskName = "Update";
|
2018-11-23 01:27:08 +08:00
|
|
|
auto Task = [=]() mutable {
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
auto RunPublish = [&](llvm::function_ref<void()> Publish) {
|
|
|
|
// Ensure we only publish results from the worker if the file was not
|
|
|
|
// removed, making sure there are not race conditions.
|
|
|
|
std::lock_guard<std::mutex> Lock(PublishMu);
|
|
|
|
if (CanPublishResults)
|
|
|
|
Publish();
|
|
|
|
};
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
// Get the actual command as `Inputs` does not have a command.
|
|
|
|
// FIXME: some build systems like Bazel will take time to preparing
|
|
|
|
// environment to build the file, it would be nice if we could emit a
|
|
|
|
// "PreparingBuild" status to inform users, it is non-trivial given the
|
|
|
|
// current implementation.
|
|
|
|
if (auto Cmd = CDB.getCompileCommand(FileName))
|
|
|
|
Inputs.CompileCommand = *Cmd;
|
|
|
|
else
|
|
|
|
// FIXME: consider using old command if it's not a fallback one.
|
|
|
|
Inputs.CompileCommand = CDB.getFallbackCommand(FileName);
|
|
|
|
auto PrevInputs = getCurrentFileInputs();
|
2018-07-26 17:21:07 +08:00
|
|
|
// Will be used to check if we can avoid rebuilding the AST.
|
|
|
|
bool InputsAreTheSame =
|
2019-04-15 20:32:28 +08:00
|
|
|
std::tie(PrevInputs->CompileCommand, PrevInputs->Contents) ==
|
2018-07-26 17:21:07 +08:00
|
|
|
std::tie(Inputs.CompileCommand, Inputs.Contents);
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
tooling::CompileCommand OldCommand = PrevInputs->CompileCommand;
|
2019-07-19 21:51:01 +08:00
|
|
|
bool RanCallbackForPrevInputs = RanASTCallback;
|
2019-04-15 20:32:28 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
FileInputs = std::make_shared<ParseInputs>(Inputs);
|
|
|
|
}
|
2019-07-19 21:51:01 +08:00
|
|
|
RanASTCallback = false;
|
2018-12-06 17:41:04 +08:00
|
|
|
emitTUStatus({TUAction::BuildingPreamble, TaskName});
|
2019-04-11 16:17:15 +08:00
|
|
|
log("Updating file {0} with command {1}\n[{2}]\n{3}", FileName,
|
|
|
|
Inputs.CompileCommand.Heuristic,
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
Inputs.CompileCommand.Directory,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::join(Inputs.CompileCommand.CommandLine, " "));
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Rebuild the preamble and the AST.
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
StoreDiags CompilerInvocationDiagConsumer;
|
2019-11-29 02:22:50 +08:00
|
|
|
std::vector<std::string> CC1Args;
|
2019-11-29 19:05:00 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> Invocation = buildCompilerInvocation(
|
|
|
|
Inputs, CompilerInvocationDiagConsumer, &CC1Args);
|
2019-11-29 02:22:50 +08:00
|
|
|
// Log cc1 args even (especially!) if creating invocation failed.
|
|
|
|
if (!CC1Args.empty())
|
2019-11-29 19:05:00 +08:00
|
|
|
vlog("Driver produced command: cc1 {0}", llvm::join(CC1Args, " "));
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
std::vector<Diag> CompilerInvocationDiags =
|
|
|
|
CompilerInvocationDiagConsumer.take();
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
if (!Invocation) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
elog("Could not build CompilerInvocation for file {0}", FileName);
|
2018-07-30 23:30:45 +08:00
|
|
|
// Remove the old AST if it's still in cache.
|
|
|
|
IdleASTs.take(this);
|
2018-12-06 17:41:04 +08:00
|
|
|
TUStatus::BuildDetails Details;
|
|
|
|
Details.BuildFailed = true;
|
|
|
|
emitTUStatus({TUAction::BuildingPreamble, TaskName}, &Details);
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
// Report the diagnostics we collected when parsing the command line.
|
|
|
|
Callbacks.onFailedAST(FileName, std::move(CompilerInvocationDiags),
|
|
|
|
RunPublish);
|
2018-07-09 18:45:33 +08:00
|
|
|
// Make sure anyone waiting for the preamble gets notified it could not
|
|
|
|
// be built.
|
|
|
|
PreambleWasBuilt.notify();
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 18:17:23 +08:00
|
|
|
|
2018-07-26 17:21:07 +08:00
|
|
|
std::shared_ptr<const PreambleData> OldPreamble =
|
|
|
|
getPossiblyStalePreamble();
|
2018-09-04 00:37:59 +08:00
|
|
|
std::shared_ptr<const PreambleData> NewPreamble = buildPreamble(
|
2019-04-04 20:56:03 +08:00
|
|
|
FileName, *Invocation, OldPreamble, OldCommand, Inputs,
|
2018-09-04 00:37:59 +08:00
|
|
|
StorePreambleInMemory,
|
2019-02-05 00:19:57 +08:00
|
|
|
[this](ASTContext &Ctx, std::shared_ptr<clang::Preprocessor> PP,
|
|
|
|
const CanonicalIncludes &CanonIncludes) {
|
|
|
|
Callbacks.onPreambleAST(FileName, Ctx, std::move(PP), CanonIncludes);
|
2018-09-04 00:37:59 +08:00
|
|
|
});
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
bool CanReuseAST = InputsAreTheSame && (OldPreamble == NewPreamble);
|
2018-02-09 18:17:23 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-08-17 16:15:22 +08:00
|
|
|
LastBuiltPreamble = NewPreamble;
|
2018-02-09 18:17:23 +08:00
|
|
|
}
|
2018-07-26 17:21:07 +08:00
|
|
|
// Before doing the expensive AST reparse, we want to release our reference
|
|
|
|
// to the old preamble, so it can be freed if there are no other references
|
|
|
|
// to it.
|
|
|
|
OldPreamble.reset();
|
2018-07-09 18:45:33 +08:00
|
|
|
PreambleWasBuilt.notify();
|
2018-12-06 17:41:04 +08:00
|
|
|
emitTUStatus({TUAction::BuildingFile, TaskName});
|
2018-07-31 19:47:52 +08:00
|
|
|
if (!CanReuseAST) {
|
|
|
|
IdleASTs.take(this); // Remove the old AST if it's still in cache.
|
|
|
|
} else {
|
2019-07-19 21:51:01 +08:00
|
|
|
// We don't need to rebuild the AST, check if we need to run the callback.
|
|
|
|
if (RanCallbackForPrevInputs) {
|
|
|
|
RanASTCallback = true;
|
2018-07-31 19:47:52 +08:00
|
|
|
// Take a shortcut and don't report the diagnostics, since they should
|
|
|
|
// not changed. All the clients should handle the lack of OnUpdated()
|
|
|
|
// call anyway to handle empty result from buildAST.
|
|
|
|
// FIXME(ibiryukov): the AST could actually change if non-preamble
|
|
|
|
// includes changed, but we choose to ignore it.
|
|
|
|
// FIXME(ibiryukov): should we refresh the cache in IdleASTs for the
|
|
|
|
// current file at this point?
|
|
|
|
log("Skipping rebuild of the AST for {0}, inputs are the same.",
|
|
|
|
FileName);
|
2018-12-06 17:41:04 +08:00
|
|
|
TUStatus::BuildDetails Details;
|
|
|
|
Details.ReuseAST = true;
|
|
|
|
emitTUStatus({TUAction::BuildingFile, TaskName}, &Details);
|
2018-07-31 19:47:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:45:37 +08:00
|
|
|
// We only need to build the AST if diagnostics were requested.
|
|
|
|
if (WantDiags == WantDiagnostics::No)
|
|
|
|
return;
|
|
|
|
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
{
|
2019-07-19 21:51:01 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(PublishMu);
|
2019-12-16 17:33:56 +08:00
|
|
|
// No need to rebuild the AST if we won't send the diagnostics. However,
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
// note that we don't prevent preamble rebuilds.
|
2019-07-19 21:51:01 +08:00
|
|
|
if (!CanPublishResults)
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 19:47:52 +08:00
|
|
|
// Get the AST for diagnostics.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
|
2018-07-31 19:47:52 +08:00
|
|
|
if (!AST) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<ParsedAST> NewAST =
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
buildAST(FileName, std::move(Invocation), CompilerInvocationDiags,
|
|
|
|
Inputs, NewPreamble);
|
2019-08-15 07:52:23 +08:00
|
|
|
AST = NewAST ? std::make_unique<ParsedAST>(std::move(*NewAST)) : nullptr;
|
2018-12-06 17:41:04 +08:00
|
|
|
if (!(*AST)) { // buildAST fails.
|
|
|
|
TUStatus::BuildDetails Details;
|
|
|
|
Details.BuildFailed = true;
|
|
|
|
emitTUStatus({TUAction::BuildingFile, TaskName}, &Details);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We are reusing the AST.
|
|
|
|
TUStatus::BuildDetails Details;
|
|
|
|
Details.ReuseAST = true;
|
|
|
|
emitTUStatus({TUAction::BuildingFile, TaskName}, &Details);
|
2018-07-26 17:21:07 +08:00
|
|
|
}
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
// We want to report the diagnostics even if this update was cancelled.
|
|
|
|
// It seems more useful than making the clients wait indefinitely if they
|
|
|
|
// spam us with updates.
|
2018-09-14 08:56:11 +08:00
|
|
|
// Note *AST can still be null if buildAST fails.
|
2018-07-31 21:45:37 +08:00
|
|
|
if (*AST) {
|
2018-08-28 18:57:45 +08:00
|
|
|
trace::Span Span("Running main AST callback");
|
2019-07-19 21:51:01 +08:00
|
|
|
|
|
|
|
Callbacks.onMainAST(FileName, **AST, RunPublish);
|
|
|
|
RanASTCallback = true;
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
} else {
|
|
|
|
// Failed to build the AST, at least report diagnostics from the command
|
|
|
|
// line if there were any.
|
|
|
|
// FIXME: we might have got more errors while trying to build the AST,
|
|
|
|
// surface them too.
|
|
|
|
Callbacks.onFailedAST(FileName, CompilerInvocationDiags, RunPublish);
|
2018-07-31 19:47:52 +08:00
|
|
|
}
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Stash the AST in the cache for further use.
|
2018-07-31 19:47:52 +08:00
|
|
|
IdleASTs.put(this, std::move(*AST));
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
2018-12-06 17:41:04 +08:00
|
|
|
startTask(TaskName, std::move(Task), WantDiags);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::runWithAST(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
|
2019-08-15 22:16:06 +08:00
|
|
|
auto Task = [=, Action = std::move(Action)]() mutable {
|
[clangd] Respect task cancellation in TUScheduler.
Summary:
- Reads are never executed if canceled before ready-to run.
In practice, we finalize cancelled reads eagerly and out-of-order.
- Cancelled reads don't prevent prior updates from being elided, as they don't
actually depend on the result of the update.
- Updates are downgraded from WantDiagnostics::Yes to WantDiagnostics::Auto when
cancelled, which allows them to be elided when all dependent reads are
cancelled and there are subsequent writes. (e.g. when the queue is backed up
with cancelled requests).
The queue operations aren't optimal (we scan the whole queue for cancelled
tasks every time the scheduler runs, and check cancellation twice in the end).
However I believe these costs are still trivial in practice (compared to any
AST operation) and the logic can be cleanly separated from the rest of the
scheduler.
Reviewers: ilya-biryukov
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54746
llvm-svn: 347450
2018-11-22 18:22:16 +08:00
|
|
|
if (isCancelled())
|
2019-01-07 23:45:19 +08:00
|
|
|
return Action(llvm::make_error<CancelledError>());
|
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
|
2019-04-15 20:32:28 +08:00
|
|
|
auto CurrentInputs = getCurrentFileInputs();
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
if (!AST) {
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
StoreDiags CompilerInvocationDiagConsumer;
|
|
|
|
std::unique_ptr<CompilerInvocation> Invocation = buildCompilerInvocation(
|
|
|
|
*CurrentInputs, CompilerInvocationDiagConsumer);
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Try rebuilding the AST.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<ParsedAST> NewAST =
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
Invocation
|
|
|
|
? buildAST(FileName,
|
2019-08-15 07:52:23 +08:00
|
|
|
std::make_unique<CompilerInvocation>(*Invocation),
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
CompilerInvocationDiagConsumer.take(), *CurrentInputs,
|
|
|
|
getPossiblyStalePreamble())
|
2018-10-20 23:30:37 +08:00
|
|
|
: None;
|
2019-08-15 07:52:23 +08:00
|
|
|
AST = NewAST ? std::make_unique<ParsedAST>(std::move(*NewAST)) : nullptr;
|
2018-02-09 18:17:23 +08:00
|
|
|
}
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Make sure we put the AST back into the LRU cache.
|
2019-01-07 23:45:19 +08:00
|
|
|
auto _ = llvm::make_scope_exit(
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
[&AST, this]() { IdleASTs.put(this, std::move(*AST)); });
|
|
|
|
// Run the user-provided action.
|
|
|
|
if (!*AST)
|
2019-01-07 23:45:19 +08:00
|
|
|
return Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"invalid AST", llvm::errc::invalid_argument));
|
2019-04-15 20:32:28 +08:00
|
|
|
Action(InputsAndAST{*CurrentInputs, **AST});
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
2019-08-15 22:16:06 +08:00
|
|
|
startTask(Name, std::move(Task), /*UpdateType=*/None);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData>
|
|
|
|
ASTWorker::getPossiblyStalePreamble() const {
|
2018-02-09 18:17:23 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return LastBuiltPreamble;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 23:07:34 +08:00
|
|
|
void ASTWorker::getCurrentPreamble(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::unique_function<void(std::shared_ptr<const PreambleData>)> Callback) {
|
2018-08-30 23:07:34 +08:00
|
|
|
// We could just call startTask() to throw the read on the queue, knowing
|
|
|
|
// it will run after any updates. But we know this task is cheap, so to
|
|
|
|
// improve latency we cheat: insert it on the queue after the last update.
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
auto LastUpdate =
|
|
|
|
std::find_if(Requests.rbegin(), Requests.rend(),
|
|
|
|
[](const Request &R) { return R.UpdateType.hasValue(); });
|
|
|
|
// If there were no writes in the queue, the preamble is ready now.
|
|
|
|
if (LastUpdate == Requests.rend()) {
|
|
|
|
Lock.unlock();
|
|
|
|
return Callback(getPossiblyStalePreamble());
|
|
|
|
}
|
|
|
|
assert(!RunSync && "Running synchronously, but queue is non-empty!");
|
|
|
|
Requests.insert(LastUpdate.base(),
|
2019-08-15 22:16:06 +08:00
|
|
|
Request{[Callback = std::move(Callback), this]() mutable {
|
|
|
|
Callback(getPossiblyStalePreamble());
|
|
|
|
},
|
2018-08-30 23:07:34 +08:00
|
|
|
"GetPreamble", steady_clock::now(),
|
|
|
|
Context::current().clone(),
|
2018-10-20 23:30:37 +08:00
|
|
|
/*UpdateType=*/None});
|
2018-08-30 23:07:34 +08:00
|
|
|
Lock.unlock();
|
|
|
|
RequestsCV.notify_all();
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:28:05 +08:00
|
|
|
void ASTWorker::waitForFirstPreamble() const { PreambleWasBuilt.wait(); }
|
2018-07-09 18:45:33 +08:00
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
std::shared_ptr<const ParseInputs> ASTWorker::getCurrentFileInputs() const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
return FileInputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompileCommand ASTWorker::getCurrentCompileCommand() const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
return FileInputs->CompileCommand;
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::size_t ASTWorker::getUsedBytes() const {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Note that we don't report the size of ASTs currently used for processing
|
|
|
|
// the in-flight requests. We used this information for debugging purposes
|
|
|
|
// only, so this should be fine.
|
|
|
|
std::size_t Result = IdleASTs.getUsedBytes(this);
|
|
|
|
if (auto Preamble = getPossiblyStalePreamble())
|
|
|
|
Result += Preamble->Preamble.getSize();
|
|
|
|
return Result;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
bool ASTWorker::isASTCached() const { return IdleASTs.getUsedBytes(this) != 0; }
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void ASTWorker::stop() {
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
{
|
2019-07-19 21:51:01 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(PublishMu);
|
|
|
|
CanPublishResults = false;
|
[clangd] Cleanup: make diagnostics callbacks from TUScheduler non-racy
Summary:
Previously, removeDoc followed by an addDoc to TUScheduler resulted in
racy diagnostic responses, i.e. the old dianostics could be delivered
to the client after the new ones by TUScheduler.
To workaround this, we tracked a version number in ClangdServer and
discarded stale diagnostics. After this commit, the TUScheduler will
stop delivering diagnostics for removed files and the workaround in
ClangdServer is not required anymore.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54829
llvm-svn: 347468
2018-11-22 23:39:54 +08:00
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "stop() called twice");
|
|
|
|
Done = true;
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void ASTWorker::startTask(llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void()> Task,
|
|
|
|
llvm::Optional<WantDiagnostics> UpdateType) {
|
2018-02-08 15:37:35 +08:00
|
|
|
if (RunSync) {
|
|
|
|
assert(!Done && "running a task after stop()");
|
2019-01-07 23:45:19 +08:00
|
|
|
trace::Span Tracer(Name + ":" + llvm::sys::path::filename(FileName));
|
2018-02-08 15:37:35 +08:00
|
|
|
Task();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "running a task after stop()");
|
2018-08-09 17:05:45 +08:00
|
|
|
Requests.push_back(
|
|
|
|
{std::move(Task), Name, steady_clock::now(),
|
|
|
|
Context::current().derive(kFileBeingProcessed, FileName), UpdateType});
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:41:04 +08:00
|
|
|
void ASTWorker::emitTUStatus(TUAction Action,
|
|
|
|
const TUStatus::BuildDetails *Details) {
|
|
|
|
Status.Action = std::move(Action);
|
|
|
|
if (Details)
|
|
|
|
Status.Details = *Details;
|
2019-07-19 21:51:01 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(PublishMu);
|
2018-12-06 17:41:04 +08:00
|
|
|
// Do not emit TU statuses when the ASTWorker is shutting down.
|
2019-07-19 21:51:01 +08:00
|
|
|
if (CanPublishResults) {
|
2018-12-06 17:41:04 +08:00
|
|
|
Callbacks.onFileUpdated(FileName, Status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void ASTWorker::run() {
|
|
|
|
while (true) {
|
2018-02-19 17:56:28 +08:00
|
|
|
Request Req;
|
2018-02-08 15:37:35 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
2018-03-02 16:56:37 +08:00
|
|
|
for (auto Wait = scheduleLocked(); !Wait.expired();
|
|
|
|
Wait = scheduleLocked()) {
|
|
|
|
if (Done) {
|
|
|
|
if (Requests.empty())
|
|
|
|
return;
|
|
|
|
else // Even though Done is set, finish pending requests.
|
|
|
|
break; // However, skip delays to shutdown fast.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracing: we have a next request, attribute this sleep to it.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<WithContext> Ctx;
|
|
|
|
llvm::Optional<trace::Span> Tracer;
|
2018-03-02 16:56:37 +08:00
|
|
|
if (!Requests.empty()) {
|
|
|
|
Ctx.emplace(Requests.front().Ctx.clone());
|
|
|
|
Tracer.emplace("Debounce");
|
|
|
|
SPAN_ATTACH(*Tracer, "next_request", Requests.front().Name);
|
2018-12-06 17:41:04 +08:00
|
|
|
if (!(Wait == Deadline::infinity())) {
|
|
|
|
emitTUStatus({TUAction::Queued, Req.Name});
|
2018-03-02 16:56:37 +08:00
|
|
|
SPAN_ATTACH(*Tracer, "sleep_ms",
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
Wait.time() - steady_clock::now())
|
|
|
|
.count());
|
2018-12-06 17:41:04 +08:00
|
|
|
}
|
2018-03-02 16:56:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wait(Lock, RequestsCV, Wait);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
Req = std::move(Requests.front());
|
2018-02-13 16:59:23 +08:00
|
|
|
// Leave it on the queue for now, so waiters don't see an empty queue.
|
2018-02-08 15:37:35 +08:00
|
|
|
} // unlock Mutex
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
{
|
2018-12-13 21:09:50 +08:00
|
|
|
std::unique_lock<Semaphore> Lock(Barrier, std::try_to_lock);
|
|
|
|
if (!Lock.owns_lock()) {
|
|
|
|
emitTUStatus({TUAction::Queued, Req.Name});
|
|
|
|
Lock.lock();
|
|
|
|
}
|
2018-02-19 17:56:28 +08:00
|
|
|
WithContext Guard(std::move(Req.Ctx));
|
|
|
|
trace::Span Tracer(Req.Name);
|
2018-12-06 17:41:04 +08:00
|
|
|
emitTUStatus({TUAction::RunningAction, Req.Name});
|
2018-02-19 17:56:28 +08:00
|
|
|
Req.Action();
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
2018-12-06 17:41:04 +08:00
|
|
|
bool IsEmpty = false;
|
2018-02-13 16:59:23 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-02-22 21:11:12 +08:00
|
|
|
Requests.pop_front();
|
2018-12-06 17:41:04 +08:00
|
|
|
IsEmpty = Requests.empty();
|
2018-02-13 16:59:23 +08:00
|
|
|
}
|
2018-12-06 17:41:04 +08:00
|
|
|
if (IsEmpty)
|
|
|
|
emitTUStatus({TUAction::Idle, /*Name*/ ""});
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
Deadline ASTWorker::scheduleLocked() {
|
|
|
|
if (Requests.empty())
|
|
|
|
return Deadline::infinity(); // Wait for new requests.
|
[clangd] Respect task cancellation in TUScheduler.
Summary:
- Reads are never executed if canceled before ready-to run.
In practice, we finalize cancelled reads eagerly and out-of-order.
- Cancelled reads don't prevent prior updates from being elided, as they don't
actually depend on the result of the update.
- Updates are downgraded from WantDiagnostics::Yes to WantDiagnostics::Auto when
cancelled, which allows them to be elided when all dependent reads are
cancelled and there are subsequent writes. (e.g. when the queue is backed up
with cancelled requests).
The queue operations aren't optimal (we scan the whole queue for cancelled
tasks every time the scheduler runs, and check cancellation twice in the end).
However I believe these costs are still trivial in practice (compared to any
AST operation) and the logic can be cleanly separated from the rest of the
scheduler.
Reviewers: ilya-biryukov
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54746
llvm-svn: 347450
2018-11-22 18:22:16 +08:00
|
|
|
// Handle cancelled requests first so the rest of the scheduler doesn't.
|
|
|
|
for (auto I = Requests.begin(), E = Requests.end(); I != E; ++I) {
|
|
|
|
if (!isCancelled(I->Ctx)) {
|
|
|
|
// Cancellations after the first read don't affect current scheduling.
|
|
|
|
if (I->UpdateType == None)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Cancelled reads are moved to the front of the queue and run immediately.
|
|
|
|
if (I->UpdateType == None) {
|
|
|
|
Request R = std::move(*I);
|
|
|
|
Requests.erase(I);
|
|
|
|
Requests.push_front(std::move(R));
|
|
|
|
return Deadline::zero();
|
|
|
|
}
|
|
|
|
// Cancelled updates are downgraded to auto-diagnostics, and may be elided.
|
|
|
|
if (I->UpdateType == WantDiagnostics::Yes)
|
|
|
|
I->UpdateType = WantDiagnostics::Auto;
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
while (shouldSkipHeadLocked())
|
|
|
|
Requests.pop_front();
|
|
|
|
assert(!Requests.empty() && "skipped the whole queue");
|
|
|
|
// Some updates aren't dead yet, but never end up being used.
|
|
|
|
// e.g. the first keystroke is live until obsoleted by the second.
|
|
|
|
// We debounce "maybe-unused" writes, sleeping 500ms in case they become dead.
|
|
|
|
// But don't delay reads (including updates where diagnostics are needed).
|
|
|
|
for (const auto &R : Requests)
|
|
|
|
if (R.UpdateType == None || R.UpdateType == WantDiagnostics::Yes)
|
|
|
|
return Deadline::zero();
|
|
|
|
// Front request needs to be debounced, so determine when we're ready.
|
|
|
|
Deadline D(Requests.front().AddTime + UpdateDebounce);
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
// Returns true if Requests.front() is a dead update that can be skipped.
|
|
|
|
bool ASTWorker::shouldSkipHeadLocked() const {
|
|
|
|
assert(!Requests.empty());
|
|
|
|
auto Next = Requests.begin();
|
|
|
|
auto UpdateType = Next->UpdateType;
|
|
|
|
if (!UpdateType) // Only skip updates.
|
|
|
|
return false;
|
|
|
|
++Next;
|
|
|
|
// An update is live if its AST might still be read.
|
|
|
|
// That is, if it's not immediately followed by another update.
|
|
|
|
if (Next == Requests.end() || !Next->UpdateType)
|
|
|
|
return false;
|
|
|
|
// The other way an update can be live is if its diagnostics might be used.
|
|
|
|
switch (*UpdateType) {
|
|
|
|
case WantDiagnostics::Yes:
|
|
|
|
return false; // Always used.
|
|
|
|
case WantDiagnostics::No:
|
|
|
|
return true; // Always dead.
|
|
|
|
case WantDiagnostics::Auto:
|
|
|
|
// Used unless followed by an update that generates diagnostics.
|
|
|
|
for (; Next != Requests.end(); ++Next)
|
|
|
|
if (Next->UpdateType == WantDiagnostics::Yes ||
|
|
|
|
Next->UpdateType == WantDiagnostics::Auto)
|
|
|
|
return true; // Prefer later diagnostics.
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-23 00:12:27 +08:00
|
|
|
llvm_unreachable("Unknown WantDiagnostics");
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
bool ASTWorker::blockUntilIdle(Deadline Timeout) const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
return wait(Lock, RequestsCV, Timeout, [&] { return Requests.empty(); });
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:39:12 +08:00
|
|
|
// Render a TUAction to a user-facing string representation.
|
|
|
|
// TUAction represents clangd-internal states, we don't intend to expose them
|
|
|
|
// to users (say C++ programmers) directly to avoid confusion, we use terms that
|
|
|
|
// are familiar by C++ programmers.
|
|
|
|
std::string renderTUAction(const TUAction &Action) {
|
|
|
|
std::string Result;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_string_ostream OS(Result);
|
2018-12-20 23:39:12 +08:00
|
|
|
switch (Action.S) {
|
|
|
|
case TUAction::Queued:
|
|
|
|
OS << "file is queued";
|
|
|
|
break;
|
|
|
|
case TUAction::RunningAction:
|
|
|
|
OS << "running " << Action.Name;
|
|
|
|
break;
|
|
|
|
case TUAction::BuildingPreamble:
|
|
|
|
OS << "parsing includes";
|
|
|
|
break;
|
|
|
|
case TUAction::BuildingFile:
|
|
|
|
OS << "parsing main file";
|
|
|
|
break;
|
|
|
|
case TUAction::Idle:
|
|
|
|
OS << "idle";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
unsigned getDefaultAsyncThreadsCount() {
|
clangd: use -j for background index pool
Summary:
clangd supports a -j option to limit the amount of threads to use for parsing
TUs. However, when using -background-index (the default in later versions of
clangd), the parallelism used by clangd defaults to the hardware_parallelisn,
i.e. number of physical cores.
On shared hardware environments, with large projects, this can significantly
affect performance with no way to tune it down.
This change makes the -j parameter apply equally to parsing and background
index. It's not perfect, because the total number of threads is 2x the -j value,
which may still be unexpected. But at least this change allows users to prevent
clangd using all CPU cores.
Reviewers: kadircet, sammccall
Reviewed By: sammccall
Subscribers: javed.absar, jfb, sammccall, ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66031
llvm-svn: 368498
2019-08-10 07:03:32 +08:00
|
|
|
unsigned HardwareConcurrency = llvm::heavyweight_hardware_concurrency();
|
|
|
|
// heavyweight_hardware_concurrency may fall back to hardware_concurrency.
|
|
|
|
// C++ standard says that hardware_concurrency() may return 0; fallback to 1
|
|
|
|
// worker thread in that case.
|
2018-01-31 16:51:16 +08:00
|
|
|
if (HardwareConcurrency == 0)
|
|
|
|
return 1;
|
|
|
|
return HardwareConcurrency;
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:39:12 +08:00
|
|
|
FileStatus TUStatus::render(PathRef File) const {
|
|
|
|
FileStatus FStatus;
|
|
|
|
FStatus.uri = URIForFile::canonicalize(File, /*TUPath=*/File);
|
|
|
|
FStatus.state = renderTUAction(Action);
|
|
|
|
return FStatus;
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
struct TUScheduler::FileData {
|
|
|
|
/// Latest inputs, passed to TUScheduler::update().
|
2018-03-15 01:46:52 +08:00
|
|
|
std::string Contents;
|
2018-02-08 15:37:35 +08:00
|
|
|
ASTWorkerHandle Worker;
|
|
|
|
};
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
TUScheduler::TUScheduler(const GlobalCompilationDatabase &CDB,
|
|
|
|
unsigned AsyncThreadsCount,
|
2018-01-31 16:51:16 +08:00
|
|
|
bool StorePreamblesInMemory,
|
2018-09-04 00:37:59 +08:00
|
|
|
std::unique_ptr<ParsingCallbacks> Callbacks,
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::chrono::steady_clock::duration UpdateDebounce,
|
|
|
|
ASTRetentionPolicy RetentionPolicy)
|
2019-04-15 20:32:28 +08:00
|
|
|
: CDB(CDB), StorePreamblesInMemory(StorePreamblesInMemory),
|
2018-09-04 00:37:59 +08:00
|
|
|
Callbacks(Callbacks ? move(Callbacks)
|
2019-08-15 07:52:23 +08:00
|
|
|
: std::make_unique<ParsingCallbacks>()),
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
Barrier(AsyncThreadsCount),
|
2019-08-15 07:52:23 +08:00
|
|
|
IdleASTs(std::make_unique<ASTCache>(RetentionPolicy.MaxRetainedASTs)),
|
2018-03-02 16:56:37 +08:00
|
|
|
UpdateDebounce(UpdateDebounce) {
|
2018-02-13 16:59:23 +08:00
|
|
|
if (0 < AsyncThreadsCount) {
|
|
|
|
PreambleTasks.emplace();
|
|
|
|
WorkerThreads.emplace();
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
TUScheduler::~TUScheduler() {
|
|
|
|
// Notify all workers that they need to stop.
|
|
|
|
Files.clear();
|
|
|
|
|
|
|
|
// Wait for all in-flight tasks to finish.
|
2018-02-13 16:59:23 +08:00
|
|
|
if (PreambleTasks)
|
|
|
|
PreambleTasks->wait();
|
|
|
|
if (WorkerThreads)
|
|
|
|
WorkerThreads->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TUScheduler::blockUntilIdle(Deadline D) const {
|
|
|
|
for (auto &File : Files)
|
|
|
|
if (!File.getValue()->Worker->blockUntilIdle(D))
|
|
|
|
return false;
|
|
|
|
if (PreambleTasks)
|
|
|
|
if (!PreambleTasks->wait(D))
|
|
|
|
return false;
|
|
|
|
return true;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2019-07-12 18:18:42 +08:00
|
|
|
bool TUScheduler::update(PathRef File, ParseInputs Inputs,
|
2018-11-23 01:27:08 +08:00
|
|
|
WantDiagnostics WantDiags) {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::unique_ptr<FileData> &FD = Files[File];
|
2019-07-12 18:18:42 +08:00
|
|
|
bool NewFile = FD == nullptr;
|
2018-02-08 15:37:35 +08:00
|
|
|
if (!FD) {
|
|
|
|
// Create a new worker to process the AST-related tasks.
|
2018-07-26 20:05:31 +08:00
|
|
|
ASTWorkerHandle Worker = ASTWorker::create(
|
2019-04-15 20:32:28 +08:00
|
|
|
File, CDB, *IdleASTs,
|
|
|
|
WorkerThreads ? WorkerThreads.getPointer() : nullptr, Barrier,
|
|
|
|
UpdateDebounce, StorePreamblesInMemory, *Callbacks);
|
|
|
|
FD = std::unique_ptr<FileData>(
|
|
|
|
new FileData{Inputs.Contents, std::move(Worker)});
|
2018-02-08 15:37:35 +08:00
|
|
|
} else {
|
2018-03-15 01:46:52 +08:00
|
|
|
FD->Contents = Inputs.Contents;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-11-23 01:27:08 +08:00
|
|
|
FD->Worker->update(std::move(Inputs), WantDiags);
|
2019-07-12 18:18:42 +08:00
|
|
|
return NewFile;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-02-07 03:22:40 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void TUScheduler::remove(PathRef File) {
|
|
|
|
bool Removed = Files.erase(File);
|
|
|
|
if (!Removed)
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
elog("Trying to remove file from TUScheduler that is not tracked: {0}",
|
|
|
|
File);
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 15:29:05 +08:00
|
|
|
llvm::StringRef TUScheduler::getContents(PathRef File) const {
|
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
|
|
|
elog("getContents() for untracked file: {0}", File);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return It->second->Contents;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:40:20 +08:00
|
|
|
llvm::StringMap<std::string> TUScheduler::getAllFileContents() const {
|
|
|
|
llvm::StringMap<std::string> Results;
|
|
|
|
for (auto &It : Files)
|
|
|
|
Results.try_emplace(It.getKey(), It.getValue()->Contents);
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void TUScheduler::run(llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void()> Action) {
|
2018-10-25 22:19:14 +08:00
|
|
|
if (!PreambleTasks)
|
|
|
|
return Action();
|
2019-10-23 16:18:09 +08:00
|
|
|
PreambleTasks->runAsync(Name, [Ctx = Context::current().clone(),
|
|
|
|
Action = std::move(Action)]() mutable {
|
|
|
|
WithContext WC(std::move(Ctx));
|
|
|
|
Action();
|
|
|
|
});
|
2018-10-25 22:19:14 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
void TUScheduler::runWithAST(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Name, PathRef File,
|
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2019-01-07 23:45:19 +08:00
|
|
|
Action(llvm::make_error<LSPError>(
|
|
|
|
"trying to get AST for non-added document", ErrorCode::InvalidParams));
|
2018-01-31 16:51:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
It->second->Worker->runWithAST(Name, std::move(Action));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
void TUScheduler::runWithPreamble(llvm::StringRef Name, PathRef File,
|
|
|
|
PreambleConsistency Consistency,
|
|
|
|
Callback<InputsAndPreamble> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2019-01-07 23:45:19 +08:00
|
|
|
Action(llvm::make_error<LSPError>(
|
|
|
|
"trying to get preamble for non-added document",
|
|
|
|
ErrorCode::InvalidParams));
|
2018-01-31 16:51:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
if (!PreambleTasks) {
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble =
|
|
|
|
It->second->Worker->getPossiblyStalePreamble();
|
2019-04-15 20:32:28 +08:00
|
|
|
Action(InputsAndPreamble{It->second->Contents,
|
|
|
|
It->second->Worker->getCurrentCompileCommand(),
|
2018-03-15 01:46:52 +08:00
|
|
|
Preamble.get()});
|
2018-02-08 15:37:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-08-30 23:07:34 +08:00
|
|
|
// Future is populated if the task needs a specific preamble.
|
|
|
|
std::future<std::shared_ptr<const PreambleData>> ConsistentPreamble;
|
|
|
|
if (Consistency == Consistent) {
|
|
|
|
std::promise<std::shared_ptr<const PreambleData>> Promise;
|
|
|
|
ConsistentPreamble = Promise.get_future();
|
2019-08-15 22:16:06 +08:00
|
|
|
It->second->Worker->getCurrentPreamble(
|
|
|
|
[Promise = std::move(Promise)](
|
|
|
|
std::shared_ptr<const PreambleData> Preamble) mutable {
|
2018-08-30 23:07:34 +08:00
|
|
|
Promise.set_value(std::move(Preamble));
|
2019-08-15 22:16:06 +08:00
|
|
|
});
|
2018-08-30 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const ASTWorker> Worker = It->second->Worker.lock();
|
2019-08-15 22:16:06 +08:00
|
|
|
auto Task = [Worker, Consistency, Name = Name.str(), File = File.str(),
|
|
|
|
Contents = It->second->Contents,
|
|
|
|
Command = Worker->getCurrentCompileCommand(),
|
|
|
|
Ctx = Context::current().derive(kFileBeingProcessed, File),
|
|
|
|
ConsistentPreamble = std::move(ConsistentPreamble),
|
|
|
|
Action = std::move(Action), this]() mutable {
|
2018-08-30 23:07:34 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble;
|
|
|
|
if (ConsistentPreamble.valid()) {
|
|
|
|
Preamble = ConsistentPreamble.get();
|
|
|
|
} else {
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
if (Consistency != PreambleConsistency::StaleOrAbsent) {
|
|
|
|
// Wait until the preamble is built for the first time, if preamble is
|
|
|
|
// required. This avoids extra work of processing the preamble headers
|
|
|
|
// in parallel multiple times.
|
|
|
|
Worker->waitForFirstPreamble();
|
|
|
|
}
|
2018-08-30 23:07:34 +08:00
|
|
|
Preamble = Worker->getPossiblyStalePreamble();
|
|
|
|
}
|
2018-07-09 18:45:33 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::lock_guard<Semaphore> BarrierLock(Barrier);
|
|
|
|
WithContext Guard(std::move(Ctx));
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-03-15 01:46:52 +08:00
|
|
|
Action(InputsAndPreamble{Contents, Command, Preamble.get()});
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2019-08-15 22:16:06 +08:00
|
|
|
PreambleTasks->runAsync("task:" + llvm::sys::path::filename(File),
|
|
|
|
std::move(Task));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<Path, std::size_t>>
|
|
|
|
TUScheduler::getUsedBytesPerFile() const {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::vector<std::pair<Path, std::size_t>> Result;
|
|
|
|
Result.reserve(Files.size());
|
|
|
|
for (auto &&PathAndFile : Files)
|
|
|
|
Result.push_back(
|
|
|
|
{PathAndFile.first(), PathAndFile.second->Worker->getUsedBytes()});
|
|
|
|
return Result;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::vector<Path> TUScheduler::getFilesWithCachedAST() const {
|
|
|
|
std::vector<Path> Result;
|
|
|
|
for (auto &&PathAndFile : Files) {
|
|
|
|
if (!PathAndFile.second->Worker->isASTCached())
|
|
|
|
continue;
|
|
|
|
Result.push_back(PathAndFile.first());
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|