2018-01-31 16:51:16 +08:00
|
|
|
//===-- TUSchedulerTests.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-01-31 16:51:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-06 17:41:04 +08:00
|
|
|
#include "Annotations.h"
|
2020-04-08 02:53:56 +08:00
|
|
|
#include "ClangdServer.h"
|
2019-07-19 21:51:01 +08:00
|
|
|
#include "Diagnostics.h"
|
2018-11-23 00:20:12 +08:00
|
|
|
#include "Matchers.h"
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "ParsedAST.h"
|
2019-09-04 15:35:00 +08:00
|
|
|
#include "Preamble.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "TUScheduler.h"
|
|
|
|
#include "TestFS.h"
|
2021-01-10 23:23:03 +08:00
|
|
|
#include "TestIndex.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Cancellation.h"
|
|
|
|
#include "support/Context.h"
|
|
|
|
#include "support/Path.h"
|
2020-04-17 05:12:09 +08:00
|
|
|
#include "support/TestTracer.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Threading.h"
|
2020-06-17 17:53:32 +08:00
|
|
|
#include "support/ThreadsafeFS.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 "clang/Basic/DiagnosticDriver.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2020-04-08 03:18:00 +08:00
|
|
|
#include "llvm/ADT/FunctionExtras.h"
|
2019-07-19 21:51:01 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-12-06 17:41:04 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2020-04-08 03:18:00 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-01-03 21:28:05 +08:00
|
|
|
#include "gmock/gmock.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <algorithm>
|
2020-03-16 04:43:00 +08:00
|
|
|
#include <atomic>
|
2020-02-04 22:41:39 +08:00
|
|
|
#include <chrono>
|
2020-04-07 03:36:43 +08:00
|
|
|
#include <cstdint>
|
2020-04-08 03:18:00 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-01-31 16:51:16 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
[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
|
|
|
namespace {
|
2018-01-31 16:51:16 +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
|
|
|
using ::testing::AnyOf;
|
[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
|
|
|
using ::testing::Each;
|
2018-08-30 23:07:34 +08:00
|
|
|
using ::testing::ElementsAre;
|
[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
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::Field;
|
|
|
|
using ::testing::IsEmpty;
|
2021-01-10 23:23:03 +08:00
|
|
|
using ::testing::Pair;
|
2018-01-31 16:51:16 +08:00
|
|
|
using ::testing::Pointee;
|
2020-04-17 05:12:09 +08:00
|
|
|
using ::testing::SizeIs;
|
[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
|
|
|
using ::testing::UnorderedElementsAre;
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2020-03-18 02:08:23 +08:00
|
|
|
MATCHER_P2(TUState, PreambleActivity, ASTActivity, "") {
|
|
|
|
if (arg.PreambleActivity != PreambleActivity) {
|
|
|
|
*result_listener << "preamblestate is "
|
|
|
|
<< static_cast<uint8_t>(arg.PreambleActivity);
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-18 02:08:23 +08:00
|
|
|
if (arg.ASTActivity.K != ASTActivity) {
|
|
|
|
*result_listener << "aststate is " << arg.ASTActivity.K;
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-12-06 17:41:04 +08:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:09:25 +08:00
|
|
|
// Dummy ContextProvider to verify the provider is invoked & contexts are used.
|
|
|
|
static Key<std::string> BoundPath;
|
|
|
|
Context bindPath(PathRef F) {
|
|
|
|
return Context::current().derive(BoundPath, F.str());
|
|
|
|
}
|
|
|
|
llvm::StringRef boundPath() {
|
|
|
|
const std::string *V = Context::current().get(BoundPath);
|
|
|
|
return V ? *V : llvm::StringRef("");
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler::Options optsForTest() {
|
2020-07-03 05:09:25 +08:00
|
|
|
TUScheduler::Options Opts(ClangdServer::optsForTest());
|
|
|
|
Opts.ContextProvider = bindPath;
|
|
|
|
return Opts;
|
2020-02-03 18:18:13 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
class TUSchedulerTests : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
ParseInputs getInputs(PathRef File, std::string Contents) {
|
2019-01-28 22:01:55 +08:00
|
|
|
ParseInputs Inputs;
|
|
|
|
Inputs.CompileCommand = *CDB.getCompileCommand(File);
|
2020-06-18 00:09:54 +08:00
|
|
|
Inputs.TFS = &FS;
|
2019-01-28 22:01:55 +08:00
|
|
|
Inputs.Contents = std::move(Contents);
|
|
|
|
Inputs.Opts = ParseOptions();
|
|
|
|
return Inputs;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void updateWithCallback(TUScheduler &S, PathRef File,
|
|
|
|
llvm::StringRef Contents, WantDiagnostics WD,
|
2018-11-23 01:27:08 +08:00
|
|
|
llvm::unique_function<void()> CB) {
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
updateWithCallback(S, File, getInputs(File, std::string(Contents)), WD,
|
|
|
|
std::move(CB));
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateWithCallback(TUScheduler &S, PathRef File, ParseInputs Inputs,
|
|
|
|
WantDiagnostics WD,
|
|
|
|
llvm::unique_function<void()> CB) {
|
2018-11-23 01:27:08 +08:00
|
|
|
WithContextValue Ctx(llvm::make_scope_exit(std::move(CB)));
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
S.update(File, Inputs, WD);
|
2018-11-23 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Key<llvm::unique_function<void(PathRef File, std::vector<Diag>)>>
|
|
|
|
DiagsCallbackKey;
|
|
|
|
|
|
|
|
/// A diagnostics callback that should be passed to TUScheduler when it's used
|
|
|
|
/// in updateWithDiags.
|
|
|
|
static std::unique_ptr<ParsingCallbacks> captureDiags() {
|
|
|
|
class CaptureDiags : public ParsingCallbacks {
|
[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
|
|
|
public:
|
2019-07-19 21:51:01 +08:00
|
|
|
void onMainAST(PathRef File, ParsedAST &AST, PublishFn Publish) override {
|
[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
|
|
|
reportDiagnostics(File, AST.getDiagnostics(), Publish);
|
|
|
|
}
|
|
|
|
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
void onFailedAST(PathRef File, llvm::StringRef Version,
|
|
|
|
std::vector<Diag> Diags, PublishFn Publish) override {
|
[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
|
|
|
reportDiagnostics(File, Diags, Publish);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void reportDiagnostics(PathRef File, llvm::ArrayRef<Diag> Diags,
|
|
|
|
PublishFn Publish) {
|
2018-11-23 01:27:08 +08:00
|
|
|
auto D = Context::current().get(DiagsCallbackKey);
|
|
|
|
if (!D)
|
|
|
|
return;
|
2019-07-19 21:51:01 +08:00
|
|
|
Publish([&]() {
|
|
|
|
const_cast<
|
|
|
|
llvm::unique_function<void(PathRef, std::vector<Diag>)> &> (*D)(
|
|
|
|
File, std::move(Diags));
|
|
|
|
});
|
2018-11-23 01:27:08 +08:00
|
|
|
}
|
|
|
|
};
|
2019-08-15 07:52:23 +08:00
|
|
|
return std::make_unique<CaptureDiags>();
|
2018-11-23 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Schedule an update and call \p CB with the diagnostics it produces, if
|
|
|
|
/// any. The TUScheduler should be created with captureDiags as a
|
|
|
|
/// DiagsCallback for this to work.
|
|
|
|
void updateWithDiags(TUScheduler &S, PathRef File, ParseInputs Inputs,
|
|
|
|
WantDiagnostics WD,
|
|
|
|
llvm::unique_function<void(std::vector<Diag>)> CB) {
|
|
|
|
Path OrigFile = File.str();
|
2019-08-15 22:16:06 +08:00
|
|
|
WithContextValue Ctx(DiagsCallbackKey,
|
|
|
|
[OrigFile, CB = std::move(CB)](
|
|
|
|
PathRef File, std::vector<Diag> Diags) mutable {
|
|
|
|
assert(File == OrigFile);
|
|
|
|
CB(std::move(Diags));
|
|
|
|
});
|
2018-11-23 01:27:08 +08:00
|
|
|
S.update(File, std::move(Inputs), WD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateWithDiags(TUScheduler &S, PathRef File, llvm::StringRef Contents,
|
|
|
|
WantDiagnostics WD,
|
|
|
|
llvm::unique_function<void(std::vector<Diag>)> CB) {
|
2020-01-29 03:23:46 +08:00
|
|
|
return updateWithDiags(S, File, getInputs(File, std::string(Contents)), WD,
|
2018-11-23 01:27:08 +08:00
|
|
|
std::move(CB));
|
|
|
|
}
|
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
MockFS FS;
|
2018-01-31 16:51:16 +08:00
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
};
|
|
|
|
|
2018-11-23 01:27:08 +08:00
|
|
|
Key<llvm::unique_function<void(PathRef File, std::vector<Diag>)>>
|
|
|
|
TUSchedulerTests::DiagsCallbackKey;
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
TEST_F(TUSchedulerTests, MissingFiles) {
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest());
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto Added = testPath("added.cpp");
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[Added] = "x";
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto Missing = testPath("missing.cpp");
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[Missing] = "";
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2019-06-19 15:29:05 +08:00
|
|
|
S.update(Added, getInputs(Added, "x"), WantDiagnostics::No);
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2019-07-19 21:51:01 +08:00
|
|
|
// Assert each operation for missing file is an error (even if it's
|
|
|
|
// available in VFS).
|
2018-11-23 00:20:12 +08:00
|
|
|
S.runWithAST("", Missing,
|
|
|
|
[&](Expected<InputsAndAST> AST) { EXPECT_ERROR(AST); });
|
|
|
|
S.runWithPreamble(
|
|
|
|
"", Missing, TUScheduler::Stale,
|
|
|
|
[&](Expected<InputsAndPreamble> Preamble) { EXPECT_ERROR(Preamble); });
|
2018-02-08 15:37:35 +08:00
|
|
|
// remove() shouldn't crash on missing files.
|
|
|
|
S.remove(Missing);
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
// Assert there aren't any errors for added file.
|
2018-10-20 23:30:37 +08:00
|
|
|
S.runWithAST("", Added,
|
|
|
|
[&](Expected<InputsAndAST> AST) { EXPECT_TRUE(bool(AST)); });
|
2018-08-30 23:07:34 +08:00
|
|
|
S.runWithPreamble("", Added, TUScheduler::Stale,
|
2018-10-20 23:30:37 +08:00
|
|
|
[&](Expected<InputsAndPreamble> Preamble) {
|
2018-08-30 23:07:34 +08:00
|
|
|
EXPECT_TRUE(bool(Preamble));
|
|
|
|
});
|
2018-02-08 15:37:35 +08:00
|
|
|
S.remove(Added);
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
// Assert that all operations fail after removing the file.
|
2018-11-23 00:20:12 +08:00
|
|
|
S.runWithAST("", Added,
|
|
|
|
[&](Expected<InputsAndAST> AST) { EXPECT_ERROR(AST); });
|
2018-08-30 23:07:34 +08:00
|
|
|
S.runWithPreamble("", Added, TUScheduler::Stale,
|
2018-10-20 23:30:37 +08:00
|
|
|
[&](Expected<InputsAndPreamble> Preamble) {
|
2018-08-30 23:07:34 +08:00
|
|
|
ASSERT_FALSE(bool(Preamble));
|
2018-11-23 00:20:12 +08:00
|
|
|
llvm::consumeError(Preamble.takeError());
|
2018-08-30 23:07:34 +08:00
|
|
|
});
|
2018-02-08 15:37:35 +08:00
|
|
|
// remove() shouldn't crash on missing files.
|
|
|
|
S.remove(Added);
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
TEST_F(TUSchedulerTests, WantDiagnostics) {
|
|
|
|
std::atomic<int> CallbackCount(0);
|
|
|
|
{
|
2020-01-04 23:28:41 +08:00
|
|
|
// To avoid a racy test, don't allow tasks to actually run on the worker
|
2018-02-22 23:33:33 +08:00
|
|
|
// thread until we've scheduled them all.
|
|
|
|
Notification Ready;
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
2018-02-22 21:11:12 +08:00
|
|
|
auto Path = testPath("foo.cpp");
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, Path, "", WantDiagnostics::Yes,
|
|
|
|
[&](std::vector<Diag>) { Ready.wait(); });
|
|
|
|
updateWithDiags(S, Path, "request diags", WantDiagnostics::Yes,
|
|
|
|
[&](std::vector<Diag>) { ++CallbackCount; });
|
|
|
|
updateWithDiags(S, Path, "auto (clobbered)", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) {
|
|
|
|
ADD_FAILURE()
|
|
|
|
<< "auto should have been cancelled by auto";
|
|
|
|
});
|
|
|
|
updateWithDiags(S, Path, "request no diags", WantDiagnostics::No,
|
|
|
|
[&](std::vector<Diag>) {
|
|
|
|
ADD_FAILURE() << "no diags should not be called back";
|
|
|
|
});
|
|
|
|
updateWithDiags(S, Path, "auto (produces)", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) { ++CallbackCount; });
|
2018-02-22 21:11:12 +08:00
|
|
|
Ready.notify();
|
[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
|
|
|
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
|
|
|
EXPECT_EQ(2, CallbackCount);
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
TEST_F(TUSchedulerTests, Debounce) {
|
|
|
|
std::atomic<int> CallbackCount(0);
|
|
|
|
{
|
2020-02-03 18:18:13 +08:00
|
|
|
auto Opts = optsForTest();
|
2020-02-04 22:41:39 +08:00
|
|
|
Opts.UpdateDebounce = DebouncePolicy::fixed(std::chrono::seconds(1));
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, Opts, captureDiags());
|
2018-03-03 02:23:41 +08:00
|
|
|
// FIXME: we could probably use timeouts lower than 1 second here.
|
2018-03-02 16:56:37 +08:00
|
|
|
auto Path = testPath("foo.cpp");
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, Path, "auto (debounced)", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) {
|
|
|
|
ADD_FAILURE()
|
|
|
|
<< "auto should have been debounced and canceled";
|
|
|
|
});
|
2018-03-03 02:23:41 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, Path, "auto (timed out)", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) { ++CallbackCount; });
|
2018-03-03 02:23:41 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, Path, "auto (shut down)", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) { ++CallbackCount; });
|
[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
|
|
|
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-03-02 16:56:37 +08:00
|
|
|
}
|
|
|
|
EXPECT_EQ(2, CallbackCount);
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
TEST_F(TUSchedulerTests, Cancellation) {
|
|
|
|
// We have the following update/read sequence
|
|
|
|
// U0
|
|
|
|
// U1(WantDiags=Yes) <-- cancelled
|
|
|
|
// R1 <-- cancelled
|
|
|
|
// U2(WantDiags=Yes) <-- cancelled
|
|
|
|
// R2A <-- cancelled
|
|
|
|
// R2B
|
|
|
|
// U3(WantDiags=Yes)
|
|
|
|
// R3 <-- cancelled
|
|
|
|
std::vector<std::string> DiagsSeen, ReadsSeen, ReadsCanceled;
|
|
|
|
{
|
2018-11-22 20:54:25 +08:00
|
|
|
Notification Proceed; // Ensure we schedule everything.
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
[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
|
|
|
auto Path = testPath("foo.cpp");
|
|
|
|
// Helper to schedule a named update and return a function to cancel it.
|
|
|
|
auto Update = [&](std::string ID) -> Canceler {
|
|
|
|
auto T = cancelableTask();
|
|
|
|
WithContext C(std::move(T.first));
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(
|
|
|
|
S, Path, "//" + ID, WantDiagnostics::Yes,
|
|
|
|
[&, ID](std::vector<Diag> Diags) { DiagsSeen.push_back(ID); });
|
[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
|
|
|
return std::move(T.second);
|
|
|
|
};
|
|
|
|
// Helper to schedule a named read and return a function to cancel it.
|
|
|
|
auto Read = [&](std::string ID) -> Canceler {
|
|
|
|
auto T = cancelableTask();
|
|
|
|
WithContext C(std::move(T.first));
|
|
|
|
S.runWithAST(ID, Path, [&, ID](llvm::Expected<InputsAndAST> E) {
|
|
|
|
if (auto Err = E.takeError()) {
|
|
|
|
if (Err.isA<CancelledError>()) {
|
|
|
|
ReadsCanceled.push_back(ID);
|
|
|
|
consumeError(std::move(Err));
|
|
|
|
} else {
|
|
|
|
ADD_FAILURE() << "Non-cancelled error for " << ID << ": "
|
|
|
|
<< llvm::toString(std::move(Err));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ReadsSeen.push_back(ID);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return std::move(T.second);
|
|
|
|
};
|
|
|
|
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithCallback(S, Path, "", WantDiagnostics::Yes,
|
|
|
|
[&]() { Proceed.wait(); });
|
[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
|
|
|
// The second parens indicate cancellation, where present.
|
|
|
|
Update("U1")();
|
|
|
|
Read("R1")();
|
|
|
|
Update("U2")();
|
|
|
|
Read("R2A")();
|
|
|
|
Read("R2B");
|
|
|
|
Update("U3");
|
|
|
|
Read("R3")();
|
|
|
|
Proceed.notify();
|
[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
|
|
|
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
[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
|
|
|
}
|
|
|
|
EXPECT_THAT(DiagsSeen, ElementsAre("U2", "U3"))
|
|
|
|
<< "U1 and all dependent reads were cancelled. "
|
|
|
|
"U2 has a dependent read R2A. "
|
|
|
|
"U3 was not cancelled.";
|
|
|
|
EXPECT_THAT(ReadsSeen, ElementsAre("R2B"))
|
|
|
|
<< "All reads other than R2B were cancelled";
|
|
|
|
EXPECT_THAT(ReadsCanceled, ElementsAre("R1", "R2A", "R3"))
|
|
|
|
<< "All reads other than R2B were cancelled";
|
|
|
|
}
|
|
|
|
|
2020-03-10 23:52:31 +08:00
|
|
|
TEST_F(TUSchedulerTests, InvalidationNoCrash) {
|
|
|
|
auto Path = testPath("foo.cpp");
|
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
|
|
|
|
|
|
|
Notification StartedRunning;
|
|
|
|
Notification ScheduledChange;
|
|
|
|
// We expect invalidation logic to not crash by trying to invalidate a running
|
|
|
|
// request.
|
|
|
|
S.update(Path, getInputs(Path, ""), WantDiagnostics::Auto);
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
S.runWithAST(
|
|
|
|
"invalidatable-but-running", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
StartedRunning.notify();
|
|
|
|
ScheduledChange.wait();
|
|
|
|
ASSERT_TRUE(bool(AST));
|
|
|
|
},
|
|
|
|
TUScheduler::InvalidateOnUpdate);
|
|
|
|
StartedRunning.wait();
|
|
|
|
S.update(Path, getInputs(Path, ""), WantDiagnostics::Auto);
|
|
|
|
ScheduledChange.notify();
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
}
|
|
|
|
|
[clangd] Cancel certain operations if the file changes before we start.
Summary:
Otherwise they can force us to build lots of snapshots that we don't need.
Particularly, try to do this for operations that are frequently
generated by editors without explicit user interaction, and where
editing the file makes the result less useful. (Code action
enumeration is a good example).
https://github.com/clangd/clangd/issues/298
This doesn't return the "right" LSP error code (ContentModified) to the client,
we need to teach the cancellation API to distinguish between different causes.
Reviewers: kadircet
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75602
2020-03-04 21:04:17 +08:00
|
|
|
TEST_F(TUSchedulerTests, Invalidation) {
|
|
|
|
auto Path = testPath("foo.cpp");
|
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
|
|
|
std::atomic<int> Builds(0), Actions(0);
|
|
|
|
|
|
|
|
Notification Start;
|
|
|
|
updateWithDiags(S, Path, "a", WantDiagnostics::Yes, [&](std::vector<Diag>) {
|
|
|
|
++Builds;
|
|
|
|
Start.wait();
|
|
|
|
});
|
|
|
|
S.runWithAST(
|
|
|
|
"invalidatable", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
++Actions;
|
|
|
|
EXPECT_FALSE(bool(AST));
|
|
|
|
llvm::Error E = AST.takeError();
|
|
|
|
EXPECT_TRUE(E.isA<CancelledError>());
|
2020-04-12 00:19:50 +08:00
|
|
|
handleAllErrors(std::move(E), [&](const CancelledError &E) {
|
|
|
|
EXPECT_EQ(E.Reason, static_cast<int>(ErrorCode::ContentModified));
|
|
|
|
});
|
[clangd] Cancel certain operations if the file changes before we start.
Summary:
Otherwise they can force us to build lots of snapshots that we don't need.
Particularly, try to do this for operations that are frequently
generated by editors without explicit user interaction, and where
editing the file makes the result less useful. (Code action
enumeration is a good example).
https://github.com/clangd/clangd/issues/298
This doesn't return the "right" LSP error code (ContentModified) to the client,
we need to teach the cancellation API to distinguish between different causes.
Reviewers: kadircet
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75602
2020-03-04 21:04:17 +08:00
|
|
|
},
|
|
|
|
TUScheduler::InvalidateOnUpdate);
|
|
|
|
S.runWithAST(
|
|
|
|
"not-invalidatable", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
++Actions;
|
|
|
|
EXPECT_TRUE(bool(AST));
|
|
|
|
},
|
|
|
|
TUScheduler::NoInvalidation);
|
|
|
|
updateWithDiags(S, Path, "b", WantDiagnostics::Auto, [&](std::vector<Diag>) {
|
|
|
|
++Builds;
|
|
|
|
ADD_FAILURE() << "Shouldn't build, all dependents invalidated";
|
|
|
|
});
|
|
|
|
S.runWithAST(
|
|
|
|
"invalidatable", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
++Actions;
|
|
|
|
EXPECT_FALSE(bool(AST));
|
|
|
|
llvm::Error E = AST.takeError();
|
|
|
|
EXPECT_TRUE(E.isA<CancelledError>());
|
|
|
|
consumeError(std::move(E));
|
|
|
|
},
|
|
|
|
TUScheduler::InvalidateOnUpdate);
|
|
|
|
updateWithDiags(S, Path, "c", WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) { ++Builds; });
|
|
|
|
S.runWithAST(
|
|
|
|
"invalidatable", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
++Actions;
|
|
|
|
EXPECT_TRUE(bool(AST)) << "Shouldn't be invalidated, no update follows";
|
|
|
|
},
|
|
|
|
TUScheduler::InvalidateOnUpdate);
|
|
|
|
Start.notify();
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
|
|
|
|
EXPECT_EQ(2, Builds.load()) << "Middle build should be skipped";
|
|
|
|
EXPECT_EQ(4, Actions.load()) << "All actions should run (some with error)";
|
|
|
|
}
|
|
|
|
|
2020-12-19 08:55:26 +08:00
|
|
|
// We don't invalidate requests for updates that don't change the file content.
|
|
|
|
// These are mostly "refresh this file" events synthesized inside clangd itself.
|
|
|
|
// (Usually the AST rebuild is elided after verifying that all inputs are
|
|
|
|
// unchanged, but invalidation decisions happen earlier and so independently).
|
|
|
|
// See https://github.com/clangd/clangd/issues/620
|
|
|
|
TEST_F(TUSchedulerTests, InvalidationUnchanged) {
|
|
|
|
auto Path = testPath("foo.cpp");
|
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
|
|
|
std::atomic<int> Actions(0);
|
|
|
|
|
|
|
|
Notification Start;
|
|
|
|
updateWithDiags(S, Path, "a", WantDiagnostics::Yes, [&](std::vector<Diag>) {
|
|
|
|
Start.wait();
|
|
|
|
});
|
|
|
|
S.runWithAST(
|
|
|
|
"invalidatable", Path,
|
|
|
|
[&](llvm::Expected<InputsAndAST> AST) {
|
|
|
|
++Actions;
|
|
|
|
EXPECT_TRUE(bool(AST))
|
|
|
|
<< "Should not invalidate based on an update with same content: "
|
|
|
|
<< llvm::toString(AST.takeError());
|
|
|
|
},
|
|
|
|
TUScheduler::InvalidateOnUpdate);
|
|
|
|
updateWithDiags(S, Path, "a", WantDiagnostics::Yes, [&](std::vector<Diag>) {
|
|
|
|
ADD_FAILURE() << "Shouldn't build, identical to previous";
|
|
|
|
});
|
|
|
|
Start.notify();
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
|
|
|
|
EXPECT_EQ(1, Actions.load()) << "All actions should run";
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
TEST_F(TUSchedulerTests, ManyUpdates) {
|
|
|
|
const int FilesCount = 3;
|
|
|
|
const int UpdatesPerFile = 10;
|
|
|
|
|
|
|
|
std::mutex Mut;
|
|
|
|
int TotalASTReads = 0;
|
|
|
|
int TotalPreambleReads = 0;
|
|
|
|
int TotalUpdates = 0;
|
2020-04-08 03:18:00 +08:00
|
|
|
llvm::StringMap<int> LatestDiagVersion;
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
// Run TUScheduler and collect some stats.
|
|
|
|
{
|
2020-02-03 18:18:13 +08:00
|
|
|
auto Opts = optsForTest();
|
2020-02-04 22:41:39 +08:00
|
|
|
Opts.UpdateDebounce = DebouncePolicy::fixed(std::chrono::milliseconds(50));
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, Opts, captureDiags());
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
std::vector<std::string> Files;
|
|
|
|
for (int I = 0; I < FilesCount; ++I) {
|
2018-02-16 17:41:43 +08:00
|
|
|
std::string Name = "foo" + std::to_string(I) + ".cpp";
|
|
|
|
Files.push_back(testPath(Name));
|
2020-06-18 00:09:54 +08:00
|
|
|
this->FS.Files[Files.back()] = "";
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
StringRef Contents1 = R"cpp(int a;)cpp";
|
|
|
|
StringRef Contents2 = R"cpp(int main() { return 1; })cpp";
|
|
|
|
StringRef Contents3 = R"cpp(int a; int b; int sum() { return a + b; })cpp";
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
StringRef AllContents[] = {Contents1, Contents2, Contents3};
|
2018-01-31 16:51:16 +08:00
|
|
|
const int AllContentsSize = 3;
|
|
|
|
|
2019-07-19 21:51:01 +08:00
|
|
|
// Scheduler may run tasks asynchronously, but should propagate the
|
|
|
|
// context. We stash a nonce in the context, and verify it in the task.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
static Key<int> NonceKey;
|
|
|
|
int Nonce = 0;
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
for (int FileI = 0; FileI < FilesCount; ++FileI) {
|
|
|
|
for (int UpdateI = 0; UpdateI < UpdatesPerFile; ++UpdateI) {
|
|
|
|
auto Contents = AllContents[(FileI + UpdateI) % AllContentsSize];
|
|
|
|
|
|
|
|
auto File = Files[FileI];
|
|
|
|
auto Inputs = getInputs(File, Contents.str());
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
{
|
|
|
|
WithContextValue WithNonce(NonceKey, ++Nonce);
|
2020-04-08 03:18:00 +08:00
|
|
|
Inputs.Version = std::to_string(UpdateI);
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(
|
|
|
|
S, File, Inputs, WantDiagnostics::Auto,
|
2020-04-08 03:18:00 +08:00
|
|
|
[File, Nonce, Version(Inputs.Version), &Mut, &TotalUpdates,
|
|
|
|
&LatestDiagVersion](std::vector<Diag>) {
|
2018-11-23 01:27:08 +08:00
|
|
|
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
|
2020-07-03 05:09:25 +08:00
|
|
|
EXPECT_EQ(File, boundPath());
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
|
2018-11-23 01:27:08 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
|
|
|
++TotalUpdates;
|
|
|
|
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
|
2020-04-08 03:18:00 +08:00
|
|
|
// Make sure Diags are for a newer version.
|
|
|
|
auto It = LatestDiagVersion.try_emplace(File, -1);
|
|
|
|
const int PrevVersion = It.first->second;
|
|
|
|
int CurVersion;
|
|
|
|
ASSERT_TRUE(llvm::to_integer(Version, CurVersion, 10));
|
|
|
|
EXPECT_LT(PrevVersion, CurVersion);
|
|
|
|
It.first->getValue() = CurVersion;
|
2018-11-23 01:27:08 +08:00
|
|
|
});
|
|
|
|
}
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
{
|
|
|
|
WithContextValue WithNonce(NonceKey, ++Nonce);
|
2019-01-03 21:28:05 +08:00
|
|
|
S.runWithAST(
|
|
|
|
"CheckAST", File,
|
|
|
|
[File, Inputs, Nonce, &Mut,
|
|
|
|
&TotalASTReads](Expected<InputsAndAST> AST) {
|
|
|
|
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
|
2020-07-03 05:09:25 +08:00
|
|
|
EXPECT_EQ(File, boundPath());
|
2019-01-03 21:28:05 +08:00
|
|
|
|
|
|
|
ASSERT_TRUE((bool)AST);
|
|
|
|
EXPECT_EQ(AST->Inputs.Contents, Inputs.Contents);
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
EXPECT_EQ(AST->Inputs.Version, Inputs.Version);
|
|
|
|
EXPECT_EQ(AST->AST.version(), Inputs.Version);
|
2019-01-03 21:28:05 +08:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
|
|
|
++TotalASTReads;
|
|
|
|
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
|
|
|
|
});
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
WithContextValue WithNonce(NonceKey, ++Nonce);
|
2018-08-09 17:05:45 +08:00
|
|
|
S.runWithPreamble(
|
2018-08-30 23:07:34 +08:00
|
|
|
"CheckPreamble", File, TUScheduler::Stale,
|
2018-10-20 23:30:37 +08:00
|
|
|
[File, Inputs, Nonce, &Mut,
|
|
|
|
&TotalPreambleReads](Expected<InputsAndPreamble> Preamble) {
|
2018-08-09 17:05:45 +08:00
|
|
|
EXPECT_THAT(Context::current().get(NonceKey), Pointee(Nonce));
|
2020-07-03 05:09:25 +08:00
|
|
|
EXPECT_EQ(File, boundPath());
|
2018-08-09 17:05:45 +08:00
|
|
|
|
|
|
|
ASSERT_TRUE((bool)Preamble);
|
|
|
|
EXPECT_EQ(Preamble->Contents, Inputs.Contents);
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
|
|
|
++TotalPreambleReads;
|
|
|
|
EXPECT_EQ(File, *TUScheduler::getFileBeingProcessedInContext());
|
|
|
|
});
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
}
|
[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
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-01-31 16:51:16 +08:00
|
|
|
} // TUScheduler destructor waits for all operations to finish.
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
2020-04-08 03:18:00 +08:00
|
|
|
// Updates might get coalesced in preamble thread and result in dropping
|
|
|
|
// diagnostics for intermediate snapshots.
|
|
|
|
EXPECT_GE(TotalUpdates, FilesCount);
|
|
|
|
EXPECT_LE(TotalUpdates, FilesCount * UpdatesPerFile);
|
|
|
|
// We should receive diags for last update.
|
|
|
|
for (const auto &Entry : LatestDiagVersion)
|
|
|
|
EXPECT_EQ(Entry.second, UpdatesPerFile - 1);
|
2018-01-31 16:51:16 +08:00
|
|
|
EXPECT_EQ(TotalASTReads, FilesCount * UpdatesPerFile);
|
|
|
|
EXPECT_EQ(TotalPreambleReads, FilesCount * UpdatesPerFile);
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
TEST_F(TUSchedulerTests, EvictedAST) {
|
2018-07-21 02:45:25 +08:00
|
|
|
std::atomic<int> BuiltASTCounter(0);
|
2020-02-03 18:18:13 +08:00
|
|
|
auto Opts = optsForTest();
|
|
|
|
Opts.AsyncThreadsCount = 1;
|
|
|
|
Opts.RetentionPolicy.MaxRetainedASTs = 2;
|
2020-04-17 05:12:09 +08:00
|
|
|
trace::TestTracer Tracer;
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, Opts);
|
[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
|
|
|
|
|
|
|
llvm::StringLiteral SourceContents = R"cpp(
|
|
|
|
int* a;
|
|
|
|
double* b = a;
|
|
|
|
)cpp";
|
2018-07-26 17:21:07 +08:00
|
|
|
llvm::StringLiteral OtherSourceContents = R"cpp(
|
|
|
|
int* a;
|
|
|
|
double* b = a + 0;
|
|
|
|
)cpp";
|
[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
|
|
|
|
|
|
|
auto Foo = testPath("foo.cpp");
|
|
|
|
auto Bar = testPath("bar.cpp");
|
|
|
|
auto Baz = testPath("baz.cpp");
|
|
|
|
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
|
[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
|
|
|
// Build one file in advance. We will not access it later, so it will be the
|
|
|
|
// one that the cache will evict.
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithCallback(S, Foo, SourceContents, WantDiagnostics::Yes,
|
|
|
|
[&BuiltASTCounter]() { ++BuiltASTCounter; });
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
[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
|
|
|
ASSERT_EQ(BuiltASTCounter.load(), 1);
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(1));
|
[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
|
|
|
|
2019-07-19 21:51:01 +08:00
|
|
|
// Build two more files. Since we can retain only 2 ASTs, these should be
|
|
|
|
// the ones we see in the cache later.
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithCallback(S, Bar, SourceContents, WantDiagnostics::Yes,
|
|
|
|
[&BuiltASTCounter]() { ++BuiltASTCounter; });
|
|
|
|
updateWithCallback(S, Baz, SourceContents, WantDiagnostics::Yes,
|
|
|
|
[&BuiltASTCounter]() { ++BuiltASTCounter; });
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
[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
|
|
|
ASSERT_EQ(BuiltASTCounter.load(), 3);
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(2));
|
[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
|
|
|
|
|
|
|
// Check only the last two ASTs are retained.
|
|
|
|
ASSERT_THAT(S.getFilesWithCachedAST(), UnorderedElementsAre(Bar, Baz));
|
|
|
|
|
|
|
|
// Access the old file again.
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithCallback(S, Foo, OtherSourceContents, WantDiagnostics::Yes,
|
|
|
|
[&BuiltASTCounter]() { ++BuiltASTCounter; });
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
[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
|
|
|
ASSERT_EQ(BuiltASTCounter.load(), 4);
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(1));
|
[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
|
|
|
|
|
|
|
// Check the AST for foo.cpp is retained now and one of the others got
|
|
|
|
// evicted.
|
|
|
|
EXPECT_THAT(S.getFilesWithCachedAST(),
|
|
|
|
UnorderedElementsAre(Foo, AnyOf(Bar, Baz)));
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:07:12 +08:00
|
|
|
// We send "empty" changes to TUScheduler when we think some external event
|
|
|
|
// *might* have invalidated current state (e.g. a header was edited).
|
|
|
|
// Verify that this doesn't evict our cache entries.
|
|
|
|
TEST_F(TUSchedulerTests, NoopChangesDontThrashCache) {
|
|
|
|
auto Opts = optsForTest();
|
|
|
|
Opts.RetentionPolicy.MaxRetainedASTs = 1;
|
|
|
|
TUScheduler S(CDB, Opts);
|
|
|
|
|
|
|
|
auto Foo = testPath("foo.cpp");
|
|
|
|
auto FooInputs = getInputs(Foo, "int x=1;");
|
|
|
|
auto Bar = testPath("bar.cpp");
|
|
|
|
auto BarInputs = getInputs(Bar, "int x=2;");
|
|
|
|
|
|
|
|
// After opening Foo then Bar, AST cache contains Bar.
|
|
|
|
S.update(Foo, FooInputs, WantDiagnostics::Auto);
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
S.update(Bar, BarInputs, WantDiagnostics::Auto);
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
ASSERT_THAT(S.getFilesWithCachedAST(), ElementsAre(Bar));
|
|
|
|
|
|
|
|
// Any number of no-op updates to Foo don't dislodge Bar from the cache.
|
|
|
|
S.update(Foo, FooInputs, WantDiagnostics::Auto);
|
|
|
|
S.update(Foo, FooInputs, WantDiagnostics::Auto);
|
|
|
|
S.update(Foo, FooInputs, WantDiagnostics::Auto);
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
ASSERT_THAT(S.getFilesWithCachedAST(), ElementsAre(Bar));
|
|
|
|
// In fact each file has been built only once.
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Foo).ASTBuilds, 1u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Bar).ASTBuilds, 1u);
|
|
|
|
}
|
|
|
|
|
2018-08-17 22:55:57 +08:00
|
|
|
TEST_F(TUSchedulerTests, EmptyPreamble) {
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest());
|
2018-08-17 22:55:57 +08:00
|
|
|
|
|
|
|
auto Foo = testPath("foo.cpp");
|
|
|
|
auto Header = testPath("foo.h");
|
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[Header] = "void foo()";
|
|
|
|
FS.Timestamps[Header] = time_t(0);
|
2018-08-17 22:55:57 +08:00
|
|
|
auto WithPreamble = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
int main() {}
|
|
|
|
)cpp";
|
|
|
|
auto WithEmptyPreamble = R"cpp(int main() {})cpp";
|
2018-11-23 01:27:08 +08:00
|
|
|
S.update(Foo, getInputs(Foo, WithPreamble), WantDiagnostics::Auto);
|
2019-01-03 21:28:05 +08:00
|
|
|
S.runWithPreamble(
|
|
|
|
"getNonEmptyPreamble", Foo, TUScheduler::Stale,
|
|
|
|
[&](Expected<InputsAndPreamble> Preamble) {
|
|
|
|
// We expect to get a non-empty preamble.
|
|
|
|
EXPECT_GT(
|
|
|
|
cantFail(std::move(Preamble)).Preamble->Preamble.getBounds().Size,
|
|
|
|
0u);
|
|
|
|
});
|
2021-01-10 23:23:03 +08:00
|
|
|
// Wait while the preamble is being built.
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-08-17 22:55:57 +08:00
|
|
|
|
|
|
|
// Update the file which results in an empty preamble.
|
2018-11-23 01:27:08 +08:00
|
|
|
S.update(Foo, getInputs(Foo, WithEmptyPreamble), WantDiagnostics::Auto);
|
2021-01-10 23:23:03 +08:00
|
|
|
// Wait while the preamble is being built.
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2019-01-03 21:28:05 +08:00
|
|
|
S.runWithPreamble(
|
|
|
|
"getEmptyPreamble", Foo, TUScheduler::Stale,
|
|
|
|
[&](Expected<InputsAndPreamble> Preamble) {
|
|
|
|
// We expect to get an empty preamble.
|
|
|
|
EXPECT_EQ(
|
|
|
|
cantFail(std::move(Preamble)).Preamble->Preamble.getBounds().Size,
|
|
|
|
0u);
|
|
|
|
});
|
2018-08-17 22:55:57 +08:00
|
|
|
}
|
|
|
|
|
2021-01-10 23:23:03 +08:00
|
|
|
TEST_F(TUSchedulerTests, ASTSignalsSmokeTests) {
|
|
|
|
TUScheduler S(CDB, optsForTest());
|
|
|
|
auto Foo = testPath("foo.cpp");
|
|
|
|
auto Header = testPath("foo.h");
|
|
|
|
|
|
|
|
FS.Files[Header] = "namespace tar { int foo(); }";
|
|
|
|
const char *Contents = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
namespace ns {
|
|
|
|
int func() {
|
|
|
|
return tar::foo());
|
|
|
|
}
|
|
|
|
} // namespace ns
|
|
|
|
)cpp";
|
|
|
|
// Update the file which results in an empty preamble.
|
|
|
|
S.update(Foo, getInputs(Foo, Contents), WantDiagnostics::Yes);
|
|
|
|
// Wait while the preamble is being built.
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
Notification TaskRun;
|
|
|
|
S.runWithPreamble(
|
|
|
|
"ASTSignals", Foo, TUScheduler::Stale,
|
|
|
|
[&](Expected<InputsAndPreamble> IP) {
|
|
|
|
ASSERT_FALSE(!IP);
|
|
|
|
std::vector<std::pair<StringRef, int>> NS;
|
|
|
|
for (const auto &P : IP->Signals->RelatedNamespaces)
|
|
|
|
NS.emplace_back(P.getKey(), P.getValue());
|
|
|
|
EXPECT_THAT(NS,
|
|
|
|
UnorderedElementsAre(Pair("ns::", 1), Pair("tar::", 1)));
|
|
|
|
|
|
|
|
std::vector<std::pair<SymbolID, int>> Sym;
|
|
|
|
for (const auto &P : IP->Signals->ReferencedSymbols)
|
|
|
|
Sym.emplace_back(P.getFirst(), P.getSecond());
|
|
|
|
EXPECT_THAT(Sym, UnorderedElementsAre(Pair(ns("tar").ID, 1),
|
|
|
|
Pair(ns("ns").ID, 1),
|
|
|
|
Pair(func("tar::foo").ID, 1),
|
|
|
|
Pair(func("ns::func").ID, 1)));
|
|
|
|
TaskRun.notify();
|
|
|
|
});
|
|
|
|
TaskRun.wait();
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:45:33 +08:00
|
|
|
TEST_F(TUSchedulerTests, RunWaitsForPreamble) {
|
|
|
|
// Testing strategy: we update the file and schedule a few preamble reads at
|
|
|
|
// the same time. All reads should get the same non-null preamble.
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest());
|
2018-07-09 18:45:33 +08:00
|
|
|
auto Foo = testPath("foo.cpp");
|
|
|
|
auto NonEmptyPreamble = R"cpp(
|
|
|
|
#define FOO 1
|
|
|
|
#define BAR 2
|
|
|
|
|
|
|
|
int main() {}
|
|
|
|
)cpp";
|
|
|
|
constexpr int ReadsToSchedule = 10;
|
|
|
|
std::mutex PreamblesMut;
|
|
|
|
std::vector<const void *> Preambles(ReadsToSchedule, nullptr);
|
2018-11-23 01:27:08 +08:00
|
|
|
S.update(Foo, getInputs(Foo, NonEmptyPreamble), WantDiagnostics::Auto);
|
2018-07-09 18:45:33 +08:00
|
|
|
for (int I = 0; I < ReadsToSchedule; ++I) {
|
|
|
|
S.runWithPreamble(
|
2018-08-30 23:07:34 +08:00
|
|
|
"test", Foo, TUScheduler::Stale,
|
2018-10-20 23:30:37 +08:00
|
|
|
[I, &PreamblesMut, &Preambles](Expected<InputsAndPreamble> IP) {
|
2018-07-09 18:45:33 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(PreamblesMut);
|
|
|
|
Preambles[I] = cantFail(std::move(IP)).Preamble;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
// Check all actions got the same non-null preamble.
|
|
|
|
std::lock_guard<std::mutex> Lock(PreamblesMut);
|
|
|
|
ASSERT_NE(Preambles[0], nullptr);
|
|
|
|
ASSERT_THAT(Preambles, Each(Preambles[0]));
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:21:07 +08:00
|
|
|
TEST_F(TUSchedulerTests, NoopOnEmptyChanges) {
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
auto Source = testPath("foo.cpp");
|
|
|
|
auto Header = testPath("foo.h");
|
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[Header] = "int a;";
|
|
|
|
FS.Timestamps[Header] = time_t(0);
|
2018-07-26 17:21:07 +08:00
|
|
|
|
2020-04-14 04:07:12 +08:00
|
|
|
std::string SourceContents = R"cpp(
|
2018-07-26 17:21:07 +08:00
|
|
|
#include "foo.h"
|
|
|
|
int b = a;
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
// Return value indicates if the updated callback was received.
|
2018-11-23 01:27:08 +08:00
|
|
|
auto DoUpdate = [&](std::string Contents) -> bool {
|
2018-07-26 17:21:07 +08:00
|
|
|
std::atomic<bool> Updated(false);
|
|
|
|
Updated = false;
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, Source, Contents, WantDiagnostics::Yes,
|
|
|
|
[&Updated](std::vector<Diag>) { Updated = true; });
|
2018-08-23 18:25:07 +08:00
|
|
|
bool UpdateFinished = S.blockUntilIdle(timeoutSeconds(10));
|
2018-07-26 17:21:07 +08:00
|
|
|
if (!UpdateFinished)
|
|
|
|
ADD_FAILURE() << "Updated has not finished in one second. Threading bug?";
|
|
|
|
return Updated;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test that subsequent updates with the same inputs do not cause rebuilds.
|
2018-11-23 01:27:08 +08:00
|
|
|
ASSERT_TRUE(DoUpdate(SourceContents));
|
2020-04-14 04:07:12 +08:00
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 1u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 1u);
|
2018-11-23 01:27:08 +08:00
|
|
|
ASSERT_FALSE(DoUpdate(SourceContents));
|
2020-04-14 04:07:12 +08:00
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 1u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 1u);
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
// Update to a header should cause a rebuild, though.
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Timestamps[Header] = time_t(1);
|
2018-11-23 01:27:08 +08:00
|
|
|
ASSERT_TRUE(DoUpdate(SourceContents));
|
|
|
|
ASSERT_FALSE(DoUpdate(SourceContents));
|
2020-04-14 04:07:12 +08:00
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 2u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 2u);
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
// Update to the contents should cause a rebuild.
|
2020-04-14 04:07:12 +08:00
|
|
|
SourceContents += "\nint c = b;";
|
|
|
|
ASSERT_TRUE(DoUpdate(SourceContents));
|
|
|
|
ASSERT_FALSE(DoUpdate(SourceContents));
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 3u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 2u);
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
// Update to the compile commands should also cause a rebuild.
|
|
|
|
CDB.ExtraClangFlags.push_back("-DSOMETHING");
|
2020-04-14 04:07:12 +08:00
|
|
|
ASSERT_TRUE(DoUpdate(SourceContents));
|
|
|
|
ASSERT_FALSE(DoUpdate(SourceContents));
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).ASTBuilds, 4u);
|
|
|
|
ASSERT_EQ(S.fileStats().lookup(Source).PreambleBuilds, 3u);
|
2018-07-26 17:21:07 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 17:02:46 +08:00
|
|
|
// We rebuild if a completely missing header exists, but not if one is added
|
|
|
|
// on a higher-priority include path entry (for performance).
|
|
|
|
// (Previously we wouldn't automatically rebuild when files were added).
|
|
|
|
TEST_F(TUSchedulerTests, MissingHeader) {
|
|
|
|
CDB.ExtraClangFlags.push_back("-I" + testPath("a"));
|
|
|
|
CDB.ExtraClangFlags.push_back("-I" + testPath("b"));
|
|
|
|
// Force both directories to exist so they don't get pruned.
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files.try_emplace("a/__unused__");
|
|
|
|
FS.Files.try_emplace("b/__unused__");
|
2020-02-04 04:14:49 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
|
|
|
|
|
|
|
auto Source = testPath("foo.cpp");
|
2020-04-11 17:02:46 +08:00
|
|
|
auto HeaderA = testPath("a/foo.h");
|
|
|
|
auto HeaderB = testPath("b/foo.h");
|
2020-02-04 04:14:49 +08:00
|
|
|
|
|
|
|
auto SourceContents = R"cpp(
|
|
|
|
#include "foo.h"
|
2020-04-11 17:02:46 +08:00
|
|
|
int c = b;
|
2020-02-04 04:14:49 +08:00
|
|
|
)cpp";
|
|
|
|
|
|
|
|
ParseInputs Inputs = getInputs(Source, SourceContents);
|
2020-03-16 04:43:00 +08:00
|
|
|
std::atomic<size_t> DiagCount(0);
|
2020-02-04 04:14:49 +08:00
|
|
|
|
|
|
|
// Update the source contents, which should trigger an initial build with
|
|
|
|
// the header file missing.
|
2020-03-18 02:08:23 +08:00
|
|
|
updateWithDiags(
|
2020-03-16 04:43:00 +08:00
|
|
|
S, Source, Inputs, WantDiagnostics::Yes,
|
|
|
|
[&DiagCount](std::vector<Diag> Diags) {
|
|
|
|
++DiagCount;
|
2020-03-18 02:08:23 +08:00
|
|
|
EXPECT_THAT(Diags,
|
|
|
|
ElementsAre(Field(&Diag::Message, "'foo.h' file not found"),
|
|
|
|
Field(&Diag::Message,
|
2020-04-11 17:02:46 +08:00
|
|
|
"use of undeclared identifier 'b'")));
|
2020-03-18 02:08:23 +08:00
|
|
|
});
|
2020-06-05 00:26:52 +08:00
|
|
|
S.blockUntilIdle(timeoutSeconds(10));
|
2020-02-04 04:14:49 +08:00
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[HeaderB] = "int b;";
|
|
|
|
FS.Timestamps[HeaderB] = time_t(1);
|
2020-02-04 04:14:49 +08:00
|
|
|
|
2020-04-11 17:02:46 +08:00
|
|
|
// The addition of the missing header file triggers a rebuild, no errors.
|
2020-06-17 17:53:32 +08:00
|
|
|
updateWithDiags(S, Source, Inputs, WantDiagnostics::Yes,
|
|
|
|
[&DiagCount](std::vector<Diag> Diags) {
|
|
|
|
++DiagCount;
|
|
|
|
EXPECT_THAT(Diags, IsEmpty());
|
|
|
|
});
|
2020-02-04 04:14:49 +08:00
|
|
|
|
2020-04-11 17:02:46 +08:00
|
|
|
// Ensure previous assertions are done before we touch the FS again.
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
// Add the high-priority header file, which should reintroduce the error.
|
2020-06-18 00:09:54 +08:00
|
|
|
FS.Files[HeaderA] = "int a;";
|
|
|
|
FS.Timestamps[HeaderA] = time_t(1);
|
2020-04-11 17:02:46 +08:00
|
|
|
|
|
|
|
// This isn't detected: we don't stat a/foo.h to validate the preamble.
|
|
|
|
updateWithDiags(S, Source, Inputs, WantDiagnostics::Yes,
|
|
|
|
[&DiagCount](std::vector<Diag> Diags) {
|
|
|
|
++DiagCount;
|
|
|
|
ADD_FAILURE()
|
|
|
|
<< "Didn't expect new diagnostics when adding a/foo.h";
|
|
|
|
});
|
|
|
|
|
|
|
|
// Forcing the reload should should cause a rebuild.
|
2020-02-04 04:14:49 +08:00
|
|
|
Inputs.ForceRebuild = true;
|
2020-06-17 17:53:32 +08:00
|
|
|
updateWithDiags(
|
|
|
|
S, Source, Inputs, WantDiagnostics::Yes,
|
|
|
|
[&DiagCount](std::vector<Diag> Diags) {
|
|
|
|
++DiagCount;
|
|
|
|
ElementsAre(Field(&Diag::Message, "use of undeclared identifier 'b'"));
|
|
|
|
});
|
2020-02-04 04:14:49 +08:00
|
|
|
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2020-04-11 17:02:46 +08:00
|
|
|
EXPECT_EQ(DiagCount, 3U);
|
2020-02-04 04:14:49 +08:00
|
|
|
}
|
2020-04-11 17:02:46 +08:00
|
|
|
|
2018-07-31 19:47:52 +08:00
|
|
|
TEST_F(TUSchedulerTests, NoChangeDiags) {
|
2020-04-17 05:12:09 +08:00
|
|
|
trace::TestTracer Tracer;
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
2018-07-31 19:47:52 +08:00
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2020-04-17 05:12:09 +08:00
|
|
|
const auto *Contents = "int a; int b;";
|
2018-07-31 19:47:52 +08:00
|
|
|
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "miss"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(
|
|
|
|
S, FooCpp, Contents, WantDiagnostics::No,
|
|
|
|
[](std::vector<Diag>) { ADD_FAILURE() << "Should not be called."; });
|
2018-10-20 23:30:37 +08:00
|
|
|
S.runWithAST("touchAST", FooCpp, [](Expected<InputsAndAST> IA) {
|
2018-07-31 19:47:52 +08:00
|
|
|
// Make sure the AST was actually built.
|
|
|
|
cantFail(std::move(IA));
|
|
|
|
});
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "hit"), SizeIs(0));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_read", "miss"), SizeIs(1));
|
2018-07-31 19:47:52 +08:00
|
|
|
|
|
|
|
// Even though the inputs didn't change and AST can be reused, we need to
|
|
|
|
// report the diagnostics, as they were not reported previously.
|
|
|
|
std::atomic<bool> SeenDiags(false);
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(S, FooCpp, Contents, WantDiagnostics::Auto,
|
|
|
|
[&](std::vector<Diag>) { SeenDiags = true; });
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-07-31 19:47:52 +08:00
|
|
|
ASSERT_TRUE(SeenDiags);
|
2020-04-17 05:12:09 +08:00
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "hit"), SizeIs(1));
|
|
|
|
EXPECT_THAT(Tracer.takeMetric("ast_access_diag", "miss"), SizeIs(0));
|
2018-07-31 19:47:52 +08:00
|
|
|
|
|
|
|
// Subsequent request does not get any diagnostics callback because the same
|
|
|
|
// diags have previously been reported and the inputs didn't change.
|
2018-11-23 01:27:08 +08:00
|
|
|
updateWithDiags(
|
|
|
|
S, FooCpp, Contents, WantDiagnostics::Auto,
|
2018-07-31 19:47:52 +08:00
|
|
|
[&](std::vector<Diag>) { ADD_FAILURE() << "Should not be called."; });
|
2018-08-23 18:25:07 +08:00
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
2018-07-31 19:47:52 +08:00
|
|
|
}
|
|
|
|
|
2018-10-25 22:19:14 +08:00
|
|
|
TEST_F(TUSchedulerTests, Run) {
|
2020-07-15 02:24:32 +08:00
|
|
|
for (bool Sync : {false, true}) {
|
|
|
|
auto Opts = optsForTest();
|
|
|
|
if (Sync)
|
|
|
|
Opts.AsyncThreadsCount = 0;
|
|
|
|
TUScheduler S(CDB, Opts);
|
|
|
|
std::atomic<int> Counter(0);
|
|
|
|
S.run("add 1", /*Path=*/"", [&] { ++Counter; });
|
|
|
|
S.run("add 2", /*Path=*/"", [&] { Counter += 2; });
|
|
|
|
ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(10)));
|
|
|
|
EXPECT_EQ(Counter.load(), 3);
|
|
|
|
|
|
|
|
Notification TaskRun;
|
|
|
|
Key<int> TestKey;
|
|
|
|
WithContextValue CtxWithKey(TestKey, 10);
|
|
|
|
const char *Path = "somepath";
|
|
|
|
S.run("props context", Path, [&] {
|
|
|
|
EXPECT_EQ(Context::current().getExisting(TestKey), 10);
|
|
|
|
EXPECT_EQ(Path, boundPath());
|
|
|
|
TaskRun.notify();
|
|
|
|
});
|
|
|
|
TaskRun.wait();
|
|
|
|
}
|
2018-10-25 22:19:14 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:41:04 +08:00
|
|
|
TEST_F(TUSchedulerTests, TUStatus) {
|
2020-01-24 21:08:56 +08:00
|
|
|
class CaptureTUStatus : public ClangdServer::Callbacks {
|
2018-12-06 17:41:04 +08:00
|
|
|
public:
|
|
|
|
void onFileUpdated(PathRef File, const TUStatus &Status) override {
|
2020-04-08 02:53:56 +08:00
|
|
|
auto ASTAction = Status.ASTActivity.K;
|
|
|
|
auto PreambleAction = Status.PreambleActivity;
|
2018-12-06 17:41:04 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2020-04-08 02:53:56 +08:00
|
|
|
// Only push the action if it has changed. Since TUStatus can be published
|
|
|
|
// from either Preamble or AST thread and when one changes the other stays
|
|
|
|
// the same.
|
|
|
|
// Note that this can result in missing some updates when something other
|
|
|
|
// than action kind changes, e.g. when AST is built/reused the action kind
|
|
|
|
// stays as Building.
|
|
|
|
if (ASTActions.empty() || ASTActions.back() != ASTAction)
|
|
|
|
ASTActions.push_back(ASTAction);
|
|
|
|
if (PreambleActions.empty() || PreambleActions.back() != PreambleAction)
|
|
|
|
PreambleActions.push_back(PreambleAction);
|
2018-12-06 17:41:04 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:53:56 +08:00
|
|
|
std::vector<PreambleAction> preambleStatuses() {
|
2019-05-09 20:21:28 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2020-04-08 02:53:56 +08:00
|
|
|
return PreambleActions;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ASTAction::Kind> astStatuses() {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return ASTActions;
|
2019-05-09 20:21:28 +08:00
|
|
|
}
|
2018-12-06 17:41:04 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex Mutex;
|
2020-04-08 02:53:56 +08:00
|
|
|
std::vector<ASTAction::Kind> ASTActions;
|
|
|
|
std::vector<PreambleAction> PreambleActions;
|
2018-12-06 17:41:04 +08:00
|
|
|
} CaptureTUStatus;
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2018-12-06 17:41:04 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2020-01-24 21:08:56 +08:00
|
|
|
ClangdServer Server(CDB, FS, ClangdServer::optsForTest(), &CaptureTUStatus);
|
2018-12-06 17:41:04 +08:00
|
|
|
Annotations Code("int m^ain () {}");
|
|
|
|
|
|
|
|
// We schedule the following tasks in the queue:
|
|
|
|
// [Update] [GoToDefinition]
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
Server.addDocument(testPath("foo.cpp"), Code.code(), "1",
|
2020-04-08 02:53:56 +08:00
|
|
|
WantDiagnostics::Auto);
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
Server.locateSymbolAt(testPath("foo.cpp"), Code.point(),
|
|
|
|
[](Expected<std::vector<LocatedSymbol>> Result) {
|
|
|
|
ASSERT_TRUE((bool)Result);
|
|
|
|
});
|
2018-12-06 17:41:04 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
|
|
|
|
2020-04-08 02:53:56 +08:00
|
|
|
EXPECT_THAT(CaptureTUStatus.preambleStatuses(),
|
|
|
|
ElementsAre(
|
|
|
|
// PreambleThread starts idle, as the update is first handled
|
|
|
|
// by ASTWorker.
|
|
|
|
PreambleAction::Idle,
|
|
|
|
// Then it starts building first preamble and releases that to
|
|
|
|
// ASTWorker.
|
|
|
|
PreambleAction::Building,
|
|
|
|
// Then goes idle and stays that way as we don't receive any
|
|
|
|
// more update requests.
|
|
|
|
PreambleAction::Idle));
|
|
|
|
EXPECT_THAT(CaptureTUStatus.astStatuses(),
|
2018-12-06 17:41:04 +08:00
|
|
|
ElementsAre(
|
2020-04-08 02:53:56 +08:00
|
|
|
// Starts handling the update action and blocks until the
|
|
|
|
// first preamble is built.
|
|
|
|
ASTAction::RunningAction,
|
|
|
|
// Afterwqards it builds an AST for that preamble to publish
|
|
|
|
// diagnostics.
|
|
|
|
ASTAction::Building,
|
|
|
|
// Then goes idle.
|
|
|
|
ASTAction::Idle,
|
|
|
|
// Afterwards we start executing go-to-def.
|
|
|
|
ASTAction::RunningAction,
|
|
|
|
// Then go idle.
|
|
|
|
ASTAction::Idle));
|
2018-12-06 17:41:04 +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
|
|
|
TEST_F(TUSchedulerTests, CommandLineErrors) {
|
|
|
|
// We should see errors from command-line parsing inside the main file.
|
|
|
|
CDB.ExtraClangFlags = {"-fsome-unknown-flag"};
|
|
|
|
|
2019-09-03 21:56:03 +08:00
|
|
|
// (!) 'Ready' must live longer than TUScheduler.
|
|
|
|
Notification Ready;
|
|
|
|
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
[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> Diagnostics;
|
|
|
|
updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
|
2020-03-18 02:08:23 +08:00
|
|
|
WantDiagnostics::Yes, [&](std::vector<Diag> D) {
|
[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
|
|
|
Diagnostics = std::move(D);
|
|
|
|
Ready.notify();
|
|
|
|
});
|
|
|
|
Ready.wait();
|
|
|
|
|
|
|
|
EXPECT_THAT(
|
|
|
|
Diagnostics,
|
|
|
|
ElementsAre(AllOf(
|
|
|
|
Field(&Diag::ID, Eq(diag::err_drv_unknown_argument)),
|
|
|
|
Field(&Diag::Name, Eq("drv_unknown_argument")),
|
|
|
|
Field(&Diag::Message, "unknown argument: '-fsome-unknown-flag'"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TUSchedulerTests, CommandLineWarnings) {
|
|
|
|
// We should not see warnings from command-line parsing.
|
|
|
|
CDB.ExtraClangFlags = {"-Wsome-unknown-warning"};
|
|
|
|
|
2019-09-17 22:56:11 +08:00
|
|
|
// (!) 'Ready' must live longer than TUScheduler.
|
|
|
|
Notification Ready;
|
|
|
|
|
2020-02-03 18:18:13 +08:00
|
|
|
TUScheduler S(CDB, optsForTest(), captureDiags());
|
[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> Diagnostics;
|
|
|
|
updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
|
2020-03-18 02:08:23 +08:00
|
|
|
WantDiagnostics::Yes, [&](std::vector<Diag> D) {
|
[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
|
|
|
Diagnostics = std::move(D);
|
|
|
|
Ready.notify();
|
|
|
|
});
|
|
|
|
Ready.wait();
|
|
|
|
|
|
|
|
EXPECT_THAT(Diagnostics, IsEmpty());
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:41:39 +08:00
|
|
|
TEST(DebouncePolicy, Compute) {
|
|
|
|
namespace c = std::chrono;
|
2021-01-07 04:00:15 +08:00
|
|
|
DebouncePolicy::clock::duration History[] = {
|
2020-02-04 22:41:39 +08:00
|
|
|
c::seconds(0),
|
|
|
|
c::seconds(5),
|
|
|
|
c::seconds(10),
|
|
|
|
c::seconds(20),
|
|
|
|
};
|
|
|
|
DebouncePolicy Policy;
|
|
|
|
Policy.Min = c::seconds(3);
|
|
|
|
Policy.Max = c::seconds(25);
|
|
|
|
// Call Policy.compute(History) and return seconds as a float.
|
|
|
|
auto Compute = [&](llvm::ArrayRef<DebouncePolicy::clock::duration> History) {
|
2021-01-07 04:00:15 +08:00
|
|
|
return c::duration_cast<c::duration<float, c::seconds::period>>(
|
|
|
|
Policy.compute(History))
|
|
|
|
.count();
|
2020-02-04 22:41:39 +08:00
|
|
|
};
|
|
|
|
EXPECT_NEAR(10, Compute(History), 0.01) << "(upper) median = 10";
|
|
|
|
Policy.RebuildRatio = 1.5;
|
|
|
|
EXPECT_NEAR(15, Compute(History), 0.01) << "median = 10, ratio = 1.5";
|
|
|
|
Policy.RebuildRatio = 3;
|
|
|
|
EXPECT_NEAR(25, Compute(History), 0.01) << "constrained by max";
|
|
|
|
Policy.RebuildRatio = 0;
|
|
|
|
EXPECT_NEAR(3, Compute(History), 0.01) << "constrained by min";
|
|
|
|
EXPECT_NEAR(25, Compute({}), 0.01) << "no history -> max";
|
|
|
|
}
|
|
|
|
|
2020-04-08 03:18:00 +08:00
|
|
|
TEST_F(TUSchedulerTests, AsyncPreambleThread) {
|
|
|
|
// Blocks preamble thread while building preamble with \p BlockVersion until
|
|
|
|
// \p N is notified.
|
|
|
|
class BlockPreambleThread : public ParsingCallbacks {
|
|
|
|
public:
|
|
|
|
BlockPreambleThread(llvm::StringRef BlockVersion, Notification &N)
|
|
|
|
: BlockVersion(BlockVersion), N(N) {}
|
|
|
|
void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx,
|
|
|
|
std::shared_ptr<clang::Preprocessor> PP,
|
|
|
|
const CanonicalIncludes &) override {
|
|
|
|
if (Version == BlockVersion)
|
|
|
|
N.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::StringRef BlockVersion;
|
|
|
|
Notification &N;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr llvm::StringLiteral InputsV0 = "v0";
|
|
|
|
static constexpr llvm::StringLiteral InputsV1 = "v1";
|
|
|
|
Notification Ready;
|
|
|
|
TUScheduler S(CDB, optsForTest(),
|
|
|
|
std::make_unique<BlockPreambleThread>(InputsV1, Ready));
|
|
|
|
|
|
|
|
Path File = testPath("foo.cpp");
|
|
|
|
auto PI = getInputs(File, "");
|
|
|
|
PI.Version = InputsV0.str();
|
|
|
|
S.update(File, PI, WantDiagnostics::Auto);
|
|
|
|
S.blockUntilIdle(timeoutSeconds(10));
|
|
|
|
|
|
|
|
// Block preamble builds.
|
|
|
|
PI.Version = InputsV1.str();
|
|
|
|
// Issue second update which will block preamble thread.
|
|
|
|
S.update(File, PI, WantDiagnostics::Auto);
|
|
|
|
|
|
|
|
Notification RunASTAction;
|
|
|
|
// Issue an AST read, which shouldn't be blocked and see latest version of the
|
|
|
|
// file.
|
|
|
|
S.runWithAST("test", File, [&](Expected<InputsAndAST> AST) {
|
|
|
|
ASSERT_TRUE(bool(AST));
|
|
|
|
// Make sure preamble is built with stale inputs, but AST was built using
|
|
|
|
// new ones.
|
|
|
|
EXPECT_THAT(AST->AST.preambleVersion(), InputsV0);
|
|
|
|
EXPECT_THAT(AST->Inputs.Version, InputsV1.str());
|
|
|
|
RunASTAction.notify();
|
|
|
|
});
|
|
|
|
RunASTAction.wait();
|
|
|
|
Ready.notify();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
} // namespace
|
2018-01-31 16:51:16 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|