2017-05-26 20:26:51 +08:00
|
|
|
//===-- ClangdTests.cpp - Clangd unit tests ---------------------*- 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
|
2017-05-26 20:26:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
#include "Annotations.h"
|
2017-09-28 11:14:40 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "ClangdServer.h"
|
2019-05-21 21:40:31 +08:00
|
|
|
#include "CodeComplete.h"
|
2019-01-22 17:10:20 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2018-02-09 21:51:57 +08:00
|
|
|
#include "Matchers.h"
|
2018-02-12 19:37:28 +08:00
|
|
|
#include "SyncAPI.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "TestFS.h"
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
#include "Threading.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "URI.h"
|
2017-05-28 07:19:28 +08:00
|
|
|
#include "clang/Config/config.h"
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
2018-01-25 22:32:21 +08:00
|
|
|
#include "gmock/gmock.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <algorithm>
|
2017-08-01 23:51:38 +08:00
|
|
|
#include <chrono>
|
2017-05-26 20:26:51 +08:00
|
|
|
#include <iostream>
|
2017-08-14 16:45:47 +08:00
|
|
|
#include <random>
|
2017-05-26 20:26:51 +08:00
|
|
|
#include <string>
|
2017-09-20 20:58:55 +08:00
|
|
|
#include <thread>
|
2017-05-26 20:26:51 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-01-25 22:32:21 +08:00
|
|
|
|
2018-11-28 18:30:42 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
using ::testing::ElementsAre;
|
2018-07-09 17:10:22 +08:00
|
|
|
using ::testing::Field;
|
2018-01-25 22:32:21 +08:00
|
|
|
using ::testing::Gt;
|
|
|
|
using ::testing::IsEmpty;
|
|
|
|
using ::testing::Pair;
|
|
|
|
using ::testing::UnorderedElementsAre;
|
|
|
|
|
[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
|
|
|
MATCHER_P2(DeclAt, File, Range, "") {
|
|
|
|
return arg.PreferredDeclaration ==
|
|
|
|
Location{URIForFile::canonicalize(File, testRoot()), Range};
|
2018-11-28 18:30:42 +08:00
|
|
|
}
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-03-23 18:39:15 +08:00
|
|
|
bool diagsContainErrors(const std::vector<Diag> &Diagnostics) {
|
2018-03-12 23:28:22 +08:00
|
|
|
for (auto D : Diagnostics) {
|
|
|
|
if (D.Severity == DiagnosticsEngine::Error ||
|
|
|
|
D.Severity == DiagnosticsEngine::Fatal)
|
2017-08-14 16:45:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
class ErrorCheckingDiagConsumer : public DiagnosticsConsumer {
|
|
|
|
public:
|
2018-03-12 23:28:22 +08:00
|
|
|
void onDiagnosticsReady(PathRef File,
|
2018-03-13 07:22:35 +08:00
|
|
|
std::vector<Diag> Diagnostics) override {
|
|
|
|
bool HadError = diagsContainErrors(Diagnostics);
|
2017-05-26 20:26:51 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
HadErrorInLastDiags = HadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hadErrorInLastDiags() {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return HadErrorInLastDiags;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex Mutex;
|
|
|
|
bool HadErrorInLastDiags = false;
|
|
|
|
};
|
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
/// For each file, record whether the last published diagnostics contained at
|
|
|
|
/// least one error.
|
|
|
|
class MultipleErrorCheckingDiagConsumer : public DiagnosticsConsumer {
|
|
|
|
public:
|
2018-03-12 23:28:22 +08:00
|
|
|
void onDiagnosticsReady(PathRef File,
|
2018-03-13 07:22:35 +08:00
|
|
|
std::vector<Diag> Diagnostics) override {
|
|
|
|
bool HadError = diagsContainErrors(Diagnostics);
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
LastDiagsHadError[File] = HadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exposes all files consumed by onDiagnosticsReady in an unspecified order.
|
|
|
|
/// For each file, a bool value indicates whether the last diagnostics
|
|
|
|
/// contained an error.
|
|
|
|
std::vector<std::pair<Path, bool>> filesWithDiags() const {
|
|
|
|
std::vector<std::pair<Path, bool>> Result;
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2019-05-20 20:17:36 +08:00
|
|
|
for (const auto &It : LastDiagsHadError)
|
|
|
|
Result.emplace_back(It.first(), It.second);
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
LastDiagsHadError.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex Mutex;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<bool> LastDiagsHadError;
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
/// Replaces all patterns of the form 0x123abc with spaces
|
2017-05-26 22:34:34 +08:00
|
|
|
std::string replacePtrsInDump(std::string const &Dump) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Regex RE("0x[0-9a-fA-F]+");
|
|
|
|
llvm::SmallVector<llvm::StringRef, 1> Matches;
|
|
|
|
llvm::StringRef Pending = Dump;
|
2017-05-26 22:34:34 +08:00
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
while (RE.match(Pending, &Matches)) {
|
2017-05-26 20:26:51 +08:00
|
|
|
assert(Matches.size() == 1 && "Exactly one match expected");
|
2017-05-26 22:34:34 +08:00
|
|
|
auto MatchPos = Matches[0].data() - Pending.data();
|
|
|
|
|
|
|
|
Result += Pending.take_front(MatchPos);
|
|
|
|
Pending = Pending.drop_front(MatchPos + Matches[0].size());
|
2017-05-26 20:26:51 +08:00
|
|
|
}
|
2017-05-26 22:34:34 +08:00
|
|
|
Result += Pending;
|
|
|
|
|
|
|
|
return Result;
|
2017-05-26 20:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string dumpASTWithoutMemoryLocs(ClangdServer &Server, PathRef File) {
|
2018-02-15 21:15:47 +08:00
|
|
|
auto DumpWithMemLocs = runDumpAST(Server, File);
|
2017-05-26 22:34:34 +08:00
|
|
|
return replacePtrsInDump(DumpWithMemLocs);
|
2017-05-26 20:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClangdVFSTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
std::string parseSourceAndDumpAST(
|
2019-01-07 23:45:19 +08:00
|
|
|
PathRef SourceFileRelPath, llvm::StringRef SourceContents,
|
|
|
|
std::vector<std::pair<PathRef, llvm::StringRef>> ExtraFiles = {},
|
2017-05-26 20:26:51 +08:00
|
|
|
bool ExpectErrors = false) {
|
2017-06-13 23:59:43 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-05-26 20:26:51 +08:00
|
|
|
for (const auto &FileWithContents : ExtraFiles)
|
2018-02-16 17:41:43 +08:00
|
|
|
FS.Files[testPath(FileWithContents.first)] = FileWithContents.second;
|
2017-06-14 17:46:44 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto SourceFilename = testPath(SourceFileRelPath);
|
2018-02-13 16:59:23 +08:00
|
|
|
Server.addDocument(SourceFilename, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto Result = dumpASTWithoutMemoryLocs(Server, SourceFilename);
|
2018-02-13 16:59:23 +08:00
|
|
|
EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_EQ(ExpectErrors, DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ClangdVFSTest, Parse) {
|
|
|
|
// FIXME: figure out a stable format for AST dumps, so that we can check the
|
|
|
|
// output of the dump itself is equal to the expected one, not just that it's
|
|
|
|
// different.
|
|
|
|
auto Empty = parseSourceAndDumpAST("foo.cpp", "", {});
|
|
|
|
auto OneDecl = parseSourceAndDumpAST("foo.cpp", "int a;", {});
|
|
|
|
auto SomeDecls = parseSourceAndDumpAST("foo.cpp", "int a; int b; int c;", {});
|
|
|
|
EXPECT_NE(Empty, OneDecl);
|
|
|
|
EXPECT_NE(Empty, SomeDecls);
|
|
|
|
EXPECT_NE(SomeDecls, OneDecl);
|
|
|
|
|
|
|
|
auto Empty2 = parseSourceAndDumpAST("foo.cpp", "");
|
|
|
|
auto OneDecl2 = parseSourceAndDumpAST("foo.cpp", "int a;");
|
|
|
|
auto SomeDecls2 = parseSourceAndDumpAST("foo.cpp", "int a; int b; int c;");
|
|
|
|
EXPECT_EQ(Empty, Empty2);
|
|
|
|
EXPECT_EQ(OneDecl, OneDecl2);
|
|
|
|
EXPECT_EQ(SomeDecls, SomeDecls2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangdVFSTest, ParseWithHeader) {
|
|
|
|
parseSourceAndDumpAST("foo.cpp", "#include \"foo.h\"", {},
|
|
|
|
/*ExpectErrors=*/true);
|
|
|
|
parseSourceAndDumpAST("foo.cpp", "#include \"foo.h\"", {{"foo.h", ""}},
|
|
|
|
/*ExpectErrors=*/false);
|
|
|
|
|
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
int b = a;
|
|
|
|
)cpp";
|
|
|
|
parseSourceAndDumpAST("foo.cpp", SourceContents, {{"foo.h", ""}},
|
|
|
|
/*ExpectErrors=*/true);
|
|
|
|
parseSourceAndDumpAST("foo.cpp", SourceContents, {{"foo.h", "int a;"}},
|
|
|
|
/*ExpectErrors=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangdVFSTest, Reparse) {
|
2017-06-13 23:59:43 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
int b = a;
|
|
|
|
)cpp";
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
FS.Files[testPath("foo.h")] = "int a;";
|
2017-06-13 23:59:43 +08:00
|
|
|
FS.Files[FooCpp] = SourceContents;
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
Server.addDocument(FooCpp, "");
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParseEmpty = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParse2 = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(DumpParse1, DumpParse2);
|
|
|
|
EXPECT_NE(DumpParse1, DumpParseEmpty);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ClangdVFSTest, ReparseOnHeaderChange) {
|
2017-06-13 23:59:43 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
int b = a;
|
|
|
|
)cpp";
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
auto FooH = testPath("foo.h");
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
FS.Files[FooH] = "int a;";
|
|
|
|
FS.Files[FooCpp] = SourceContents;
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
FS.Files[FooH] = "";
|
2018-03-15 01:08:41 +08:00
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParseDifferent = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
FS.Files[FooH] = "int a;";
|
2018-03-15 01:08:41 +08:00
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
2017-05-26 20:26:51 +08:00
|
|
|
auto DumpParse2 = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
2018-02-13 16:59:23 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
2017-06-13 23:59:43 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2017-05-26 20:26:51 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(DumpParse1, DumpParse2);
|
|
|
|
EXPECT_NE(DumpParse1, DumpParseDifferent);
|
|
|
|
}
|
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
TEST_F(ClangdVFSTest, PropagatesContexts) {
|
|
|
|
static Key<int> Secret;
|
|
|
|
struct FSProvider : public FileSystemProvider {
|
2019-01-07 23:45:19 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem() const override {
|
2018-03-13 07:22:35 +08:00
|
|
|
Got = Context::current().getExisting(Secret);
|
|
|
|
return buildTestFS({});
|
|
|
|
}
|
2018-10-10 15:46:15 +08:00
|
|
|
mutable int Got;
|
2018-03-13 07:22:35 +08:00
|
|
|
} FS;
|
|
|
|
struct DiagConsumer : public DiagnosticsConsumer {
|
|
|
|
void onDiagnosticsReady(PathRef File,
|
|
|
|
std::vector<Diag> Diagnostics) override {
|
|
|
|
Got = Context::current().getExisting(Secret);
|
|
|
|
}
|
|
|
|
int Got;
|
|
|
|
} DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2017-05-30 23:11:02 +08:00
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
// Verify that the context is plumbed to the FS provider and diagnostics.
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
{
|
|
|
|
WithContextValue Entrypoint(Secret, 42);
|
|
|
|
Server.addDocument(testPath("foo.cpp"), "void main(){}");
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
|
|
|
EXPECT_EQ(FS.Got, 42);
|
|
|
|
EXPECT_EQ(DiagConsumer.Got, 42);
|
2017-05-30 23:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 17:22:32 +08:00
|
|
|
// Only enable this test on Unix
|
|
|
|
#ifdef LLVM_ON_UNIX
|
2017-06-29 16:43:51 +08:00
|
|
|
TEST_F(ClangdVFSTest, SearchLibDir) {
|
|
|
|
// Checks that searches for GCC installation is done through vfs.
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2017-07-21 16:20:47 +08:00
|
|
|
CDB.ExtraClangFlags.insert(CDB.ExtraClangFlags.end(),
|
|
|
|
{"-xc++", "-target", "x86_64-linux-unknown",
|
2017-08-03 21:30:43 +08:00
|
|
|
"-m64", "--gcc-toolchain=/randomusr",
|
|
|
|
"-stdlib=libstdc++"});
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-06-29 16:43:51 +08:00
|
|
|
|
|
|
|
// Just a random gcc version string
|
|
|
|
SmallString<8> Version("4.9.3");
|
|
|
|
|
|
|
|
// A lib dir for gcc installation
|
2017-07-21 16:20:47 +08:00
|
|
|
SmallString<64> LibDir("/randomusr/lib/gcc/x86_64-linux-gnu");
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::path::append(LibDir, Version);
|
2017-06-29 16:43:51 +08:00
|
|
|
|
|
|
|
// Put crtbegin.o into LibDir/64 to trick clang into thinking there's a gcc
|
|
|
|
// installation there.
|
|
|
|
SmallString<64> DummyLibFile;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::path::append(DummyLibFile, LibDir, "64", "crtbegin.o");
|
2017-06-29 16:43:51 +08:00
|
|
|
FS.Files[DummyLibFile] = "";
|
|
|
|
|
2017-07-21 16:20:47 +08:00
|
|
|
SmallString<64> IncludeDir("/randomusr/include/c++");
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::path::append(IncludeDir, Version);
|
2017-06-29 16:43:51 +08:00
|
|
|
|
|
|
|
SmallString<64> StringPath;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::path::append(StringPath, IncludeDir, "string");
|
2017-06-29 16:43:51 +08:00
|
|
|
FS.Files[StringPath] = "class mock_string {};";
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2017-06-29 16:43:51 +08:00
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
#include <string>
|
|
|
|
mock_string x;
|
|
|
|
)cpp";
|
|
|
|
FS.Files[FooCpp] = SourceContents;
|
|
|
|
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents);
|
2017-06-29 16:43:51 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
|
|
|
|
const auto SourceContentsWithError = R"cpp(
|
|
|
|
#include <string>
|
|
|
|
std::string x;
|
|
|
|
)cpp";
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContentsWithError);
|
2017-06-29 16:43:51 +08:00
|
|
|
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
}
|
2017-06-29 17:22:32 +08:00
|
|
|
#endif // LLVM_ON_UNIX
|
2017-06-29 16:43:51 +08:00
|
|
|
|
2017-08-14 16:37:32 +08:00
|
|
|
TEST_F(ClangdVFSTest, ForceReparseCompileCommand) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-08-14 16:37:32 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2017-08-14 16:37:32 +08:00
|
|
|
const auto SourceContents1 = R"cpp(
|
|
|
|
template <class T>
|
|
|
|
struct foo { T x; };
|
|
|
|
)cpp";
|
|
|
|
const auto SourceContents2 = R"cpp(
|
|
|
|
template <class T>
|
|
|
|
struct bar { T x; };
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
FS.Files[FooCpp] = "";
|
|
|
|
|
|
|
|
// First parse files in C mode and check they produce errors.
|
|
|
|
CDB.ExtraClangFlags = {"-xc"};
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents1);
|
2017-08-14 16:37:32 +08:00
|
|
|
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents2);
|
2017-08-14 16:37:32 +08:00
|
|
|
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
|
|
|
|
// Now switch to C++ mode.
|
|
|
|
CDB.ExtraClangFlags = {"-xc++"};
|
2018-06-13 17:20:41 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents2, WantDiagnostics::Auto);
|
2017-08-14 16:37:32 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
// Subsequent addDocument calls should finish without errors too.
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents1);
|
2017-08-14 16:37:32 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents2);
|
2017-08-14 16:37:32 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
}
|
|
|
|
|
2018-02-20 02:18:49 +08:00
|
|
|
TEST_F(ClangdVFSTest, ForceReparseCompileCommandDefines) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2018-02-20 02:18:49 +08:00
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
#ifdef WITH_ERROR
|
|
|
|
this
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main() { return 0; }
|
|
|
|
)cpp";
|
|
|
|
FS.Files[FooCpp] = "";
|
|
|
|
|
|
|
|
// Parse with define, we expect to see the errors.
|
|
|
|
CDB.ExtraClangFlags = {"-DWITH_ERROR"};
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents);
|
2018-02-20 02:18:49 +08:00
|
|
|
EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
|
|
|
|
// Parse without the define, no errors should be produced.
|
|
|
|
CDB.ExtraClangFlags = {};
|
2018-06-13 17:20:41 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents, WantDiagnostics::Auto);
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
2018-02-20 02:18:49 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
// Subsequent addDocument call should finish without errors too.
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, SourceContents);
|
2018-02-20 02:18:49 +08:00
|
|
|
EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
|
|
|
|
}
|
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
// Test ClangdServer.reparseOpenedFiles.
|
|
|
|
TEST_F(ClangdVFSTest, ReparseOpenedFiles) {
|
|
|
|
Annotations FooSource(R"cpp(
|
|
|
|
#ifdef MACRO
|
2018-03-09 22:00:34 +08:00
|
|
|
static void $one[[bob]]() {}
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
#else
|
2018-03-09 22:00:34 +08:00
|
|
|
static void $two[[bob]]() {}
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int main () { bo^b (); return 0; }
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
Annotations BarSource(R"cpp(
|
|
|
|
#ifdef MACRO
|
|
|
|
this is an error
|
|
|
|
#endif
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
Annotations BazSource(R"cpp(
|
|
|
|
int hello;
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
MockFSProvider FS;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
MultipleErrorCheckingDiagConsumer DiagConsumer;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
auto BarCpp = testPath("bar.cpp");
|
|
|
|
auto BazCpp = testPath("baz.cpp");
|
|
|
|
|
|
|
|
FS.Files[FooCpp] = "";
|
|
|
|
FS.Files[BarCpp] = "";
|
|
|
|
FS.Files[BazCpp] = "";
|
|
|
|
|
|
|
|
CDB.ExtraClangFlags = {"-DMACRO=1"};
|
|
|
|
Server.addDocument(FooCpp, FooSource.code());
|
|
|
|
Server.addDocument(BarCpp, BarSource.code());
|
|
|
|
Server.addDocument(BazCpp, BazSource.code());
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(DiagConsumer.filesWithDiags(),
|
|
|
|
UnorderedElementsAre(Pair(FooCpp, false), Pair(BarCpp, true),
|
|
|
|
Pair(BazCpp, false)));
|
|
|
|
|
[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
|
|
|
auto Locations = runLocateSymbolAt(Server, FooCpp, FooSource.point());
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
EXPECT_TRUE(bool(Locations));
|
[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
|
|
|
EXPECT_THAT(*Locations, ElementsAre(DeclAt(FooCpp, FooSource.range("one"))));
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
|
|
|
|
// Undefine MACRO, close baz.cpp.
|
|
|
|
CDB.ExtraClangFlags.clear();
|
|
|
|
DiagConsumer.clear();
|
|
|
|
Server.removeDocument(BazCpp);
|
2018-06-13 17:20:41 +08:00
|
|
|
Server.addDocument(FooCpp, FooSource.code(), WantDiagnostics::Auto);
|
|
|
|
Server.addDocument(BarCpp, BarSource.code(), WantDiagnostics::Auto);
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(DiagConsumer.filesWithDiags(),
|
|
|
|
UnorderedElementsAre(Pair(FooCpp, false), Pair(BarCpp, false)));
|
|
|
|
|
[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
|
|
|
Locations = runLocateSymbolAt(Server, FooCpp, FooSource.point());
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
EXPECT_TRUE(bool(Locations));
|
[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
|
|
|
EXPECT_THAT(*Locations, ElementsAre(DeclAt(FooCpp, FooSource.range("two"))));
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
TEST_F(ClangdVFSTest, MemoryUsage) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2018-01-25 22:32:21 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
Path FooCpp = testPath("foo.cpp");
|
2018-01-25 22:32:21 +08:00
|
|
|
const auto SourceContents = R"cpp(
|
|
|
|
struct Something {
|
|
|
|
int method();
|
|
|
|
};
|
|
|
|
)cpp";
|
2018-02-16 17:41:43 +08:00
|
|
|
Path BarCpp = testPath("bar.cpp");
|
2018-01-25 22:32:21 +08:00
|
|
|
|
|
|
|
FS.Files[FooCpp] = "";
|
|
|
|
FS.Files[BarCpp] = "";
|
|
|
|
|
|
|
|
EXPECT_THAT(Server.getUsedBytesPerFile(), IsEmpty());
|
|
|
|
|
[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
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
|
|
|
Server.addDocument(BarCpp, SourceContents);
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
2018-01-25 22:32:21 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(Server.getUsedBytesPerFile(),
|
|
|
|
UnorderedElementsAre(Pair(FooCpp, Gt(0u)), Pair(BarCpp, Gt(0u))));
|
|
|
|
|
[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
|
|
|
Server.removeDocument(FooCpp);
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
2018-01-25 22:32:21 +08:00
|
|
|
EXPECT_THAT(Server.getUsedBytesPerFile(), ElementsAre(Pair(BarCpp, Gt(0u))));
|
|
|
|
|
[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
|
|
|
Server.removeDocument(BarCpp);
|
2018-03-06 01:28:54 +08:00
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
2018-01-25 22:32:21 +08:00
|
|
|
EXPECT_THAT(Server.getUsedBytesPerFile(), IsEmpty());
|
|
|
|
}
|
|
|
|
|
2018-02-09 21:51:57 +08:00
|
|
|
TEST_F(ClangdVFSTest, InvalidCompileCommand) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2018-02-09 21:51:57 +08:00
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2018-02-09 21:51:57 +08:00
|
|
|
// clang cannot create CompilerInvocation if we pass two files in the
|
|
|
|
// CompileCommand. We pass the file in ExtraFlags once and CDB adds another
|
|
|
|
// one in getCompileCommand().
|
2018-02-16 17:41:43 +08:00
|
|
|
CDB.ExtraClangFlags.push_back(FooCpp);
|
2018-02-09 21:51:57 +08:00
|
|
|
|
|
|
|
// Clang can't parse command args in that case, but we shouldn't crash.
|
2018-03-06 01:28:54 +08:00
|
|
|
runAddDocument(Server, FooCpp, "int main() {}");
|
2018-02-09 21:51:57 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
EXPECT_EQ(runDumpAST(Server, FooCpp), "<no-ast>");
|
[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
|
|
|
EXPECT_ERROR(runLocateSymbolAt(Server, FooCpp, Position()));
|
2018-02-15 21:15:47 +08:00
|
|
|
EXPECT_ERROR(runFindDocumentHighlights(Server, FooCpp, Position()));
|
|
|
|
EXPECT_ERROR(runRename(Server, FooCpp, Position(), "new_name"));
|
2019-04-11 17:36:36 +08:00
|
|
|
// Identifier-based fallback completion.
|
2018-03-13 07:22:35 +08:00
|
|
|
EXPECT_THAT(cantFail(runCodeComplete(Server, FooCpp, Position(),
|
|
|
|
clangd::CodeCompleteOptions()))
|
2018-07-02 19:13:16 +08:00
|
|
|
.Completions,
|
2019-04-11 17:36:36 +08:00
|
|
|
ElementsAre(Field(&CodeCompletion::Name, "int"),
|
|
|
|
Field(&CodeCompletion::Name, "main")));
|
2018-02-15 21:15:47 +08:00
|
|
|
auto SigHelp = runSignatureHelp(Server, FooCpp, Position());
|
2018-02-09 23:11:07 +08:00
|
|
|
ASSERT_TRUE(bool(SigHelp)) << "signatureHelp returned an error";
|
2018-03-13 07:22:35 +08:00
|
|
|
EXPECT_THAT(SigHelp->signatures, IsEmpty());
|
2018-02-09 21:51:57 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
class ClangdThreadingTest : public ClangdVFSTest {};
|
|
|
|
|
|
|
|
TEST_F(ClangdThreadingTest, StressTest) {
|
|
|
|
// Without 'static' clang gives an error for a usage inside TestDiagConsumer.
|
|
|
|
static const unsigned FilesCount = 5;
|
|
|
|
const unsigned RequestsCount = 500;
|
|
|
|
// Blocking requests wait for the parsing to complete, they slow down the test
|
|
|
|
// dramatically, so they are issued rarely. Each
|
|
|
|
// BlockingRequestInterval-request will be a blocking one.
|
|
|
|
const unsigned BlockingRequestInterval = 40;
|
|
|
|
|
|
|
|
const auto SourceContentsWithoutErrors = R"cpp(
|
|
|
|
int a;
|
|
|
|
int b;
|
|
|
|
int c;
|
|
|
|
int d;
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
const auto SourceContentsWithErrors = R"cpp(
|
|
|
|
int a = x;
|
|
|
|
int b;
|
|
|
|
int c;
|
|
|
|
int d;
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
// Giving invalid line and column number should not crash ClangdServer, but
|
|
|
|
// just to make sure we're sometimes hitting the bounds inside the file we
|
|
|
|
// limit the intervals of line and column number that are generated.
|
|
|
|
unsigned MaxLineForFileRequests = 7;
|
|
|
|
unsigned MaxColumnForFileRequests = 10;
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
std::vector<std::string> FilePaths;
|
2018-02-14 23:19:20 +08:00
|
|
|
MockFSProvider FS;
|
2018-02-16 17:41:43 +08:00
|
|
|
for (unsigned I = 0; I < FilesCount; ++I) {
|
|
|
|
std::string Name = std::string("Foo") + std::to_string(I) + ".cpp";
|
|
|
|
FS.Files[Name] = "";
|
|
|
|
FilePaths.push_back(testPath(Name));
|
|
|
|
}
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
struct FileStat {
|
|
|
|
unsigned HitsWithoutErrors = 0;
|
|
|
|
unsigned HitsWithErrors = 0;
|
|
|
|
bool HadErrorsInLastDiags = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestDiagConsumer : public DiagnosticsConsumer {
|
|
|
|
public:
|
|
|
|
TestDiagConsumer() : Stats(FilesCount, FileStat()) {}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
void onDiagnosticsReady(PathRef File,
|
2018-03-13 07:22:35 +08:00
|
|
|
std::vector<Diag> Diagnostics) override {
|
2019-01-07 23:45:19 +08:00
|
|
|
StringRef FileIndexStr = llvm::sys::path::stem(File);
|
2017-08-14 16:45:47 +08:00
|
|
|
ASSERT_TRUE(FileIndexStr.consume_front("Foo"));
|
|
|
|
|
|
|
|
unsigned long FileIndex = std::stoul(FileIndexStr.str());
|
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
bool HadError = diagsContainErrors(Diagnostics);
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
if (HadError)
|
|
|
|
Stats[FileIndex].HitsWithErrors++;
|
|
|
|
else
|
|
|
|
Stats[FileIndex].HitsWithoutErrors++;
|
|
|
|
Stats[FileIndex].HadErrorsInLastDiags = HadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<FileStat> takeFileStats() {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return std::move(Stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex Mutex;
|
|
|
|
std::vector<FileStat> Stats;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RequestStats {
|
|
|
|
unsigned RequestsWithoutErrors = 0;
|
|
|
|
unsigned RequestsWithErrors = 0;
|
|
|
|
bool LastContentsHadErrors = false;
|
|
|
|
bool FileIsRemoved = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<RequestStats> ReqStats;
|
|
|
|
ReqStats.reserve(FilesCount);
|
|
|
|
for (unsigned FileIndex = 0; FileIndex < FilesCount; ++FileIndex)
|
|
|
|
ReqStats.emplace_back();
|
|
|
|
|
|
|
|
TestDiagConsumer DiagConsumer;
|
|
|
|
{
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
// Prepare some random distributions for the test.
|
|
|
|
std::random_device RandGen;
|
|
|
|
|
|
|
|
std::uniform_int_distribution<unsigned> FileIndexDist(0, FilesCount - 1);
|
|
|
|
// Pass a text that contains compiler errors to addDocument in about 20% of
|
|
|
|
// all requests.
|
|
|
|
std::bernoulli_distribution ShouldHaveErrorsDist(0.2);
|
|
|
|
// Line and Column numbers for requests that need them.
|
|
|
|
std::uniform_int_distribution<int> LineDist(0, MaxLineForFileRequests);
|
|
|
|
std::uniform_int_distribution<int> ColumnDist(0, MaxColumnForFileRequests);
|
|
|
|
|
|
|
|
// Some helpers.
|
2018-02-08 15:37:35 +08:00
|
|
|
auto UpdateStatsOnAddDocument = [&](unsigned FileIndex, bool HadErrors) {
|
2017-08-14 16:45:47 +08:00
|
|
|
auto &Stats = ReqStats[FileIndex];
|
|
|
|
|
|
|
|
if (HadErrors)
|
|
|
|
++Stats.RequestsWithErrors;
|
|
|
|
else
|
|
|
|
++Stats.RequestsWithoutErrors;
|
|
|
|
Stats.LastContentsHadErrors = HadErrors;
|
|
|
|
Stats.FileIsRemoved = false;
|
|
|
|
};
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
auto UpdateStatsOnRemoveDocument = [&](unsigned FileIndex) {
|
2017-08-14 16:45:47 +08:00
|
|
|
auto &Stats = ReqStats[FileIndex];
|
|
|
|
|
|
|
|
Stats.FileIsRemoved = true;
|
|
|
|
};
|
|
|
|
|
2018-03-15 01:08:41 +08:00
|
|
|
auto AddDocument = [&](unsigned FileIndex, bool SkipCache) {
|
2017-08-14 16:45:47 +08:00
|
|
|
bool ShouldHaveErrors = ShouldHaveErrorsDist(RandGen);
|
2018-02-08 15:37:35 +08:00
|
|
|
Server.addDocument(FilePaths[FileIndex],
|
|
|
|
ShouldHaveErrors ? SourceContentsWithErrors
|
2018-03-15 01:08:41 +08:00
|
|
|
: SourceContentsWithoutErrors,
|
2018-06-13 17:20:41 +08:00
|
|
|
WantDiagnostics::Auto);
|
2018-02-08 15:37:35 +08:00
|
|
|
UpdateStatsOnAddDocument(FileIndex, ShouldHaveErrors);
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Various requests that we would randomly run.
|
|
|
|
auto AddDocumentRequest = [&]() {
|
|
|
|
unsigned FileIndex = FileIndexDist(RandGen);
|
2018-03-15 01:08:41 +08:00
|
|
|
AddDocument(FileIndex, /*SkipCache=*/false);
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto ForceReparseRequest = [&]() {
|
|
|
|
unsigned FileIndex = FileIndexDist(RandGen);
|
2018-03-15 01:08:41 +08:00
|
|
|
AddDocument(FileIndex, /*SkipCache=*/true);
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto RemoveDocumentRequest = [&]() {
|
|
|
|
unsigned FileIndex = FileIndexDist(RandGen);
|
|
|
|
// Make sure we don't violate the ClangdServer's contract.
|
|
|
|
if (ReqStats[FileIndex].FileIsRemoved)
|
2018-03-15 01:08:41 +08:00
|
|
|
AddDocument(FileIndex, /*SkipCache=*/false);
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
Server.removeDocument(FilePaths[FileIndex]);
|
|
|
|
UpdateStatsOnRemoveDocument(FileIndex);
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto CodeCompletionRequest = [&]() {
|
|
|
|
unsigned FileIndex = FileIndexDist(RandGen);
|
|
|
|
// Make sure we don't violate the ClangdServer's contract.
|
|
|
|
if (ReqStats[FileIndex].FileIsRemoved)
|
2018-03-15 01:08:41 +08:00
|
|
|
AddDocument(FileIndex, /*SkipCache=*/false);
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2018-02-14 18:52:04 +08:00
|
|
|
Position Pos;
|
|
|
|
Pos.line = LineDist(RandGen);
|
|
|
|
Pos.character = ColumnDist(RandGen);
|
2017-10-06 01:04:13 +08:00
|
|
|
// FIXME(ibiryukov): Also test async completion requests.
|
|
|
|
// Simply putting CodeCompletion into async requests now would make
|
|
|
|
// tests slow, since there's no way to cancel previous completion
|
|
|
|
// requests as opposed to AddDocument/RemoveDocument, which are implicitly
|
|
|
|
// cancelled by any subsequent AddDocument/RemoveDocument request to the
|
|
|
|
// same file.
|
2018-03-13 07:22:35 +08:00
|
|
|
cantFail(runCodeComplete(Server, FilePaths[FileIndex], Pos,
|
|
|
|
clangd::CodeCompleteOptions()));
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
[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
|
|
|
auto LocateSymbolRequest = [&]() {
|
2017-08-14 16:45:47 +08:00
|
|
|
unsigned FileIndex = FileIndexDist(RandGen);
|
|
|
|
// Make sure we don't violate the ClangdServer's contract.
|
|
|
|
if (ReqStats[FileIndex].FileIsRemoved)
|
2018-03-15 01:08:41 +08:00
|
|
|
AddDocument(FileIndex, /*SkipCache=*/false);
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2018-02-14 18:52:04 +08:00
|
|
|
Position Pos;
|
|
|
|
Pos.line = LineDist(RandGen);
|
|
|
|
Pos.character = ColumnDist(RandGen);
|
|
|
|
|
[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
|
|
|
ASSERT_TRUE(!!runLocateSymbolAt(Server, FilePaths[FileIndex], Pos));
|
2017-08-14 16:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::function<void()>> AsyncRequests = {
|
|
|
|
AddDocumentRequest, ForceReparseRequest, RemoveDocumentRequest};
|
|
|
|
std::vector<std::function<void()>> BlockingRequests = {
|
[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
|
|
|
CodeCompletionRequest, LocateSymbolRequest};
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
// Bash requests to ClangdServer in a loop.
|
|
|
|
std::uniform_int_distribution<int> AsyncRequestIndexDist(
|
|
|
|
0, AsyncRequests.size() - 1);
|
|
|
|
std::uniform_int_distribution<int> BlockingRequestIndexDist(
|
|
|
|
0, BlockingRequests.size() - 1);
|
|
|
|
for (unsigned I = 1; I <= RequestsCount; ++I) {
|
|
|
|
if (I % BlockingRequestInterval != 0) {
|
|
|
|
// Issue an async request most of the time. It should be fast.
|
|
|
|
unsigned RequestIndex = AsyncRequestIndexDist(RandGen);
|
|
|
|
AsyncRequests[RequestIndex]();
|
|
|
|
} else {
|
|
|
|
// Issue a blocking request once in a while.
|
|
|
|
auto RequestIndex = BlockingRequestIndexDist(RandGen);
|
|
|
|
BlockingRequests[RequestIndex]();
|
|
|
|
}
|
|
|
|
}
|
[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(Server.blockUntilIdleForTest());
|
|
|
|
}
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
// Check some invariants about the state of the program.
|
|
|
|
std::vector<FileStat> Stats = DiagConsumer.takeFileStats();
|
|
|
|
for (unsigned I = 0; I < FilesCount; ++I) {
|
2017-08-14 19:05:56 +08:00
|
|
|
if (!ReqStats[I].FileIsRemoved) {
|
2017-08-14 16:45:47 +08:00
|
|
|
ASSERT_EQ(Stats[I].HadErrorsInLastDiags,
|
|
|
|
ReqStats[I].LastContentsHadErrors);
|
2017-08-14 19:05:56 +08:00
|
|
|
}
|
2017-08-14 16:45:47 +08:00
|
|
|
|
|
|
|
ASSERT_LE(Stats[I].HitsWithErrors, ReqStats[I].RequestsWithErrors);
|
|
|
|
ASSERT_LE(Stats[I].HitsWithoutErrors, ReqStats[I].RequestsWithoutErrors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 11:14:40 +08:00
|
|
|
TEST_F(ClangdVFSTest, CheckSourceHeaderSwitch) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
2017-09-28 11:14:40 +08:00
|
|
|
|
|
|
|
auto SourceContents = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
int b = a;
|
|
|
|
)cpp";
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
auto FooH = testPath("foo.h");
|
|
|
|
auto Invalid = testPath("main.cpp");
|
2017-09-28 11:14:40 +08:00
|
|
|
|
|
|
|
FS.Files[FooCpp] = SourceContents;
|
|
|
|
FS.Files[FooH] = "int a;";
|
|
|
|
FS.Files[Invalid] = "int main() { \n return 0; \n }";
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Path> PathResult = Server.switchSourceHeader(FooCpp);
|
2017-09-28 11:14:40 +08:00
|
|
|
EXPECT_TRUE(PathResult.hasValue());
|
|
|
|
ASSERT_EQ(PathResult.getValue(), FooH);
|
|
|
|
|
|
|
|
PathResult = Server.switchSourceHeader(FooH);
|
|
|
|
EXPECT_TRUE(PathResult.hasValue());
|
|
|
|
ASSERT_EQ(PathResult.getValue(), FooCpp);
|
|
|
|
|
|
|
|
SourceContents = R"c(
|
|
|
|
#include "foo.HH"
|
|
|
|
int b = a;
|
|
|
|
)c";
|
|
|
|
|
|
|
|
// Test with header file in capital letters and different extension, source
|
|
|
|
// file with different extension
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooC = testPath("bar.c");
|
|
|
|
auto FooHH = testPath("bar.HH");
|
2017-09-28 11:14:40 +08:00
|
|
|
|
|
|
|
FS.Files[FooC] = SourceContents;
|
|
|
|
FS.Files[FooHH] = "int a;";
|
|
|
|
|
|
|
|
PathResult = Server.switchSourceHeader(FooC);
|
|
|
|
EXPECT_TRUE(PathResult.hasValue());
|
|
|
|
ASSERT_EQ(PathResult.getValue(), FooHH);
|
|
|
|
|
|
|
|
// Test with both capital letters
|
2018-02-16 17:41:43 +08:00
|
|
|
auto Foo2C = testPath("foo2.C");
|
|
|
|
auto Foo2HH = testPath("foo2.HH");
|
2017-09-28 11:14:40 +08:00
|
|
|
FS.Files[Foo2C] = SourceContents;
|
|
|
|
FS.Files[Foo2HH] = "int a;";
|
|
|
|
|
|
|
|
PathResult = Server.switchSourceHeader(Foo2C);
|
|
|
|
EXPECT_TRUE(PathResult.hasValue());
|
|
|
|
ASSERT_EQ(PathResult.getValue(), Foo2HH);
|
|
|
|
|
|
|
|
// Test with source file as capital letter and .hxx header file
|
2018-02-16 17:41:43 +08:00
|
|
|
auto Foo3C = testPath("foo3.C");
|
|
|
|
auto Foo3HXX = testPath("foo3.hxx");
|
2017-09-28 11:14:40 +08:00
|
|
|
|
|
|
|
SourceContents = R"c(
|
|
|
|
#include "foo3.hxx"
|
|
|
|
int b = a;
|
|
|
|
)c";
|
|
|
|
|
|
|
|
FS.Files[Foo3C] = SourceContents;
|
|
|
|
FS.Files[Foo3HXX] = "int a;";
|
|
|
|
|
|
|
|
PathResult = Server.switchSourceHeader(Foo3C);
|
|
|
|
EXPECT_TRUE(PathResult.hasValue());
|
|
|
|
ASSERT_EQ(PathResult.getValue(), Foo3HXX);
|
|
|
|
|
|
|
|
// Test if asking for a corresponding file that doesn't exist returns an empty
|
|
|
|
// string.
|
|
|
|
PathResult = Server.switchSourceHeader(Invalid);
|
|
|
|
EXPECT_FALSE(PathResult.hasValue());
|
|
|
|
}
|
|
|
|
|
2017-09-20 20:58:55 +08:00
|
|
|
TEST_F(ClangdThreadingTest, NoConcurrentDiagnostics) {
|
|
|
|
class NoConcurrentAccessDiagConsumer : public DiagnosticsConsumer {
|
|
|
|
public:
|
2018-02-13 16:59:23 +08:00
|
|
|
std::atomic<int> Count = {0};
|
2017-09-20 20:58:55 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
NoConcurrentAccessDiagConsumer(std::promise<void> StartSecondReparse)
|
|
|
|
: StartSecondReparse(std::move(StartSecondReparse)) {}
|
2017-09-20 20:58:55 +08:00
|
|
|
|
2018-03-13 07:22:35 +08:00
|
|
|
void onDiagnosticsReady(PathRef, std::vector<Diag>) override {
|
2018-02-13 16:59:23 +08:00
|
|
|
++Count;
|
2017-09-20 20:58:55 +08:00
|
|
|
std::unique_lock<std::mutex> Lock(Mutex, std::try_to_lock_t());
|
|
|
|
ASSERT_TRUE(Lock.owns_lock())
|
|
|
|
<< "Detected concurrent onDiagnosticsReady calls for the same file.";
|
2018-02-13 16:59:23 +08:00
|
|
|
|
|
|
|
// If we started the second parse immediately, it might cancel the first.
|
|
|
|
// So we don't allow it to start until the first has delivered diags...
|
2017-09-20 20:58:55 +08:00
|
|
|
if (FirstRequest) {
|
|
|
|
FirstRequest = false;
|
|
|
|
StartSecondReparse.set_value();
|
2018-02-13 16:59:23 +08:00
|
|
|
// ... but then we wait long enough that the callbacks would overlap.
|
2017-09-20 20:58:55 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex Mutex;
|
|
|
|
bool FirstRequest = true;
|
|
|
|
std::promise<void> StartSecondReparse;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto SourceContentsWithoutErrors = R"cpp(
|
|
|
|
int a;
|
|
|
|
int b;
|
|
|
|
int c;
|
|
|
|
int d;
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
const auto SourceContentsWithErrors = R"cpp(
|
|
|
|
int a = x;
|
|
|
|
int b;
|
|
|
|
int c;
|
|
|
|
int d;
|
|
|
|
)cpp";
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2018-02-13 16:59:23 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
FS.Files[FooCpp] = "";
|
2017-09-20 20:58:55 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
std::promise<void> StartSecondPromise;
|
|
|
|
std::future<void> StartSecond = StartSecondPromise.get_future();
|
2017-09-20 20:58:55 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
NoConcurrentAccessDiagConsumer DiagConsumer(std::move(StartSecondPromise));
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
[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
|
|
|
Server.addDocument(FooCpp, SourceContentsWithErrors);
|
2018-02-13 16:59:23 +08:00
|
|
|
StartSecond.wait();
|
|
|
|
Server.addDocument(FooCpp, SourceContentsWithoutErrors);
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
|
|
|
ASSERT_EQ(DiagConsumer.Count, 2); // Sanity check - we actually ran both?
|
2017-09-20 20:58:55 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 18:42:50 +08:00
|
|
|
TEST_F(ClangdVFSTest, FormatCode) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto Path = testPath("foo.cpp");
|
|
|
|
std::string Code = R"cpp(
|
|
|
|
#include "x.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "y.h"
|
2018-03-06 18:42:50 +08:00
|
|
|
|
|
|
|
void f( ) {}
|
|
|
|
)cpp";
|
|
|
|
std::string Expected = R"cpp(
|
|
|
|
#include "x.h"
|
|
|
|
#include "y.h"
|
|
|
|
|
|
|
|
void f() {}
|
|
|
|
)cpp";
|
|
|
|
FS.Files[Path] = Code;
|
|
|
|
runAddDocument(Server, Path, Code);
|
|
|
|
|
|
|
|
auto Replaces = Server.formatFile(Code, Path);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Replaces));
|
|
|
|
auto Changed = tooling::applyAllReplacements(Code, *Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Changed));
|
|
|
|
EXPECT_EQ(Expected, *Changed);
|
2018-02-16 22:15:55 +08:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:10:22 +08:00
|
|
|
TEST_F(ClangdVFSTest, ChangedHeaderFromISystem) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto SourcePath = testPath("source/foo.cpp");
|
|
|
|
auto HeaderPath = testPath("headers/foo.h");
|
|
|
|
FS.Files[HeaderPath] = "struct X { int bar; };";
|
|
|
|
Annotations Code(R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
X().ba^
|
|
|
|
})cpp");
|
|
|
|
CDB.ExtraClangFlags.push_back("-xc++");
|
|
|
|
CDB.ExtraClangFlags.push_back("-isystem" + testPath("headers"));
|
|
|
|
|
|
|
|
runAddDocument(Server, SourcePath, Code.code());
|
|
|
|
auto Completions = cantFail(runCodeComplete(Server, SourcePath, Code.point(),
|
|
|
|
clangd::CodeCompleteOptions()))
|
|
|
|
.Completions;
|
|
|
|
EXPECT_THAT(Completions, ElementsAre(Field(&CodeCompletion::Name, "bar")));
|
|
|
|
// Update the header and rerun addDocument to make sure we get the updated
|
|
|
|
// files.
|
|
|
|
FS.Files[HeaderPath] = "struct X { int bar; int baz; };";
|
|
|
|
runAddDocument(Server, SourcePath, Code.code());
|
|
|
|
Completions = cantFail(runCodeComplete(Server, SourcePath, Code.point(),
|
|
|
|
clangd::CodeCompleteOptions()))
|
|
|
|
.Completions;
|
|
|
|
// We want to make sure we see the updated version.
|
|
|
|
EXPECT_THAT(Completions, ElementsAre(Field(&CodeCompletion::Name, "bar"),
|
|
|
|
Field(&CodeCompletion::Name, "baz")));
|
|
|
|
}
|
|
|
|
|
2018-10-03 05:47:41 +08:00
|
|
|
// FIXME(ioeric): make this work for windows again.
|
|
|
|
#ifndef _WIN32
|
2018-10-02 18:43:55 +08:00
|
|
|
// Check that running code completion doesn't stat() a bunch of files from the
|
|
|
|
// preamble again. (They should be using the preamble's stat-cache)
|
|
|
|
TEST(ClangdTests, PreambleVFSStatCache) {
|
|
|
|
class ListenStatsFSProvider : public FileSystemProvider {
|
|
|
|
public:
|
2019-01-07 23:45:19 +08:00
|
|
|
ListenStatsFSProvider(llvm::StringMap<unsigned> &CountStats)
|
2018-10-02 18:43:55 +08:00
|
|
|
: CountStats(CountStats) {}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem() const override {
|
|
|
|
class ListenStatVFS : public llvm::vfs::ProxyFileSystem {
|
2018-10-02 18:43:55 +08:00
|
|
|
public:
|
2019-01-07 23:45:19 +08:00
|
|
|
ListenStatVFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
|
|
|
|
llvm::StringMap<unsigned> &CountStats)
|
2018-10-02 18:43:55 +08:00
|
|
|
: ProxyFileSystem(std::move(FS)), CountStats(CountStats) {}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
|
2018-10-02 18:43:55 +08:00
|
|
|
openFileForRead(const Twine &Path) override {
|
2019-01-07 23:45:19 +08:00
|
|
|
++CountStats[llvm::sys::path::filename(Path.str())];
|
2018-10-02 18:43:55 +08:00
|
|
|
return ProxyFileSystem::openFileForRead(Path);
|
|
|
|
}
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
|
|
|
|
++CountStats[llvm::sys::path::filename(Path.str())];
|
2018-10-02 18:43:55 +08:00
|
|
|
return ProxyFileSystem::status(Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<unsigned> &CountStats;
|
2018-10-02 18:43:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return IntrusiveRefCntPtr<ListenStatVFS>(
|
|
|
|
new ListenStatVFS(buildTestFS(Files), CountStats));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If relative paths are used, they are resolved with testPath().
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<std::string> Files;
|
|
|
|
llvm::StringMap<unsigned> &CountStats;
|
2018-10-02 18:43:55 +08:00
|
|
|
};
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<unsigned> CountStats;
|
2018-10-02 18:43:55 +08:00
|
|
|
ListenStatsFSProvider FS(CountStats);
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto SourcePath = testPath("foo.cpp");
|
|
|
|
auto HeaderPath = testPath("foo.h");
|
|
|
|
FS.Files[HeaderPath] = "struct TestSym {};";
|
|
|
|
Annotations Code(R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
TestSy^
|
|
|
|
})cpp");
|
|
|
|
|
|
|
|
runAddDocument(Server, SourcePath, Code.code());
|
|
|
|
|
2018-10-03 04:00:32 +08:00
|
|
|
unsigned Before = CountStats["foo.h"];
|
|
|
|
EXPECT_GT(Before, 0u);
|
2018-10-02 18:43:55 +08:00
|
|
|
auto Completions = cantFail(runCodeComplete(Server, SourcePath, Code.point(),
|
|
|
|
clangd::CodeCompleteOptions()))
|
|
|
|
.Completions;
|
2018-10-03 04:00:32 +08:00
|
|
|
EXPECT_EQ(CountStats["foo.h"], Before);
|
2018-10-02 18:43:55 +08:00
|
|
|
EXPECT_THAT(Completions,
|
|
|
|
ElementsAre(Field(&CodeCompletion::Name, "TestSym")));
|
|
|
|
}
|
2018-10-03 05:47:41 +08:00
|
|
|
#endif
|
2018-10-02 18:43:55 +08:00
|
|
|
|
2019-01-22 17:10:20 +08:00
|
|
|
TEST_F(ClangdVFSTest, FlagsWithPlugins) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
CDB.ExtraClangFlags = {
|
|
|
|
"-Xclang",
|
|
|
|
"-add-plugin",
|
|
|
|
"-Xclang",
|
|
|
|
"random-plugin",
|
|
|
|
};
|
|
|
|
OverlayCDB OCDB(&CDB);
|
|
|
|
ClangdServer Server(OCDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
const auto SourceContents = "int main() { return 0; }";
|
|
|
|
FS.Files[FooCpp] = FooCpp;
|
|
|
|
Server.addDocument(FooCpp, SourceContents);
|
|
|
|
auto Result = dumpASTWithoutMemoryLocs(Server, FooCpp);
|
|
|
|
EXPECT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for diagnostics";
|
|
|
|
EXPECT_NE(Result, "<no-ast>");
|
|
|
|
}
|
|
|
|
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
TEST_F(ClangdVFSTest, FallbackWhenPreambleIsNotReady) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
2019-04-11 17:36:36 +08:00
|
|
|
Annotations Code(R"cpp(
|
|
|
|
namespace ns { int xyz; }
|
|
|
|
using namespace ns;
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
int main() {
|
2019-04-11 17:36:36 +08:00
|
|
|
xy^
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
})cpp");
|
|
|
|
FS.Files[FooCpp] = FooCpp;
|
|
|
|
|
|
|
|
auto Opts = clangd::CodeCompleteOptions();
|
2019-05-21 21:40:31 +08:00
|
|
|
Opts.RunParser = CodeCompleteOptions::ParseIfReady;
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
|
|
|
|
// This will make compile command broken and preamble absent.
|
|
|
|
CDB.ExtraClangFlags = {"yolo.cc"};
|
|
|
|
Server.addDocument(FooCpp, Code.code());
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
|
|
|
auto Res = cantFail(runCodeComplete(Server, FooCpp, Code.point(), Opts));
|
|
|
|
EXPECT_EQ(Res.Context, CodeCompletionContext::CCC_Recovery);
|
2019-04-11 17:36:36 +08:00
|
|
|
// Identifier-based fallback completion doesn't know about "symbol" scope.
|
|
|
|
EXPECT_THAT(Res.Completions,
|
|
|
|
ElementsAre(AllOf(Field(&CodeCompletion::Name, "xyz"),
|
|
|
|
Field(&CodeCompletion::Scope, ""))));
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
|
|
|
|
// Make the compile command work again.
|
|
|
|
CDB.ExtraClangFlags = {"-std=c++11"};
|
|
|
|
Server.addDocument(FooCpp, Code.code());
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
2019-05-21 21:40:31 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
cantFail(runCodeComplete(Server, FooCpp, Code.point(), Opts)).Completions,
|
|
|
|
ElementsAre(AllOf(Field(&CodeCompletion::Name, "xyz"),
|
|
|
|
Field(&CodeCompletion::Scope, "ns::"))));
|
|
|
|
|
|
|
|
// Now force identifier-based completion.
|
|
|
|
Opts.RunParser = CodeCompleteOptions::NeverParse;
|
|
|
|
EXPECT_THAT(
|
|
|
|
cantFail(runCodeComplete(Server, FooCpp, Code.point(), Opts)).Completions,
|
|
|
|
ElementsAre(AllOf(Field(&CodeCompletion::Name, "xyz"),
|
|
|
|
Field(&CodeCompletion::Scope, ""))));
|
[clangd] Add fallback mode for code completion when compile command or preamble is not ready.
Summary:
When calling TUScehduler::runWithPreamble (e.g. in code compleiton), allow
entering a fallback mode when compile command or preamble is not ready, instead of
waiting. This allows clangd to perform naive code completion e.g. using identifiers
in the current file or symbols in the index.
This patch simply returns empty result for code completion in fallback mode. Identifier-based
plus more advanced index-based completion will be added in followup patches.
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: sammccall, javed.absar, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59811
llvm-svn: 357916
2019-04-08 22:53:16 +08:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:32:28 +08:00
|
|
|
TEST_F(ClangdVFSTest, FallbackWhenWaitingForCompileCommand) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
ErrorCheckingDiagConsumer DiagConsumer;
|
|
|
|
// Returns compile command only when notified.
|
|
|
|
class DelayedCompilationDatabase : public GlobalCompilationDatabase {
|
|
|
|
public:
|
|
|
|
DelayedCompilationDatabase(Notification &CanReturnCommand)
|
|
|
|
: CanReturnCommand(CanReturnCommand) {}
|
|
|
|
|
|
|
|
llvm::Optional<tooling::CompileCommand>
|
|
|
|
getCompileCommand(PathRef File, ProjectInfo * = nullptr) const override {
|
|
|
|
// FIXME: make this timeout and fail instead of waiting forever in case
|
|
|
|
// something goes wrong.
|
|
|
|
CanReturnCommand.wait();
|
|
|
|
auto FileName = llvm::sys::path::filename(File);
|
|
|
|
std::vector<std::string> CommandLine = {"clangd", "-ffreestanding", File};
|
|
|
|
return {tooling::CompileCommand(llvm::sys::path::parent_path(File),
|
|
|
|
FileName, std::move(CommandLine), "")};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ExtraClangFlags;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Notification &CanReturnCommand;
|
|
|
|
};
|
|
|
|
|
|
|
|
Notification CanReturnCommand;
|
|
|
|
DelayedCompilationDatabase CDB(CanReturnCommand);
|
|
|
|
ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
|
|
|
|
|
|
|
|
auto FooCpp = testPath("foo.cpp");
|
|
|
|
Annotations Code(R"cpp(
|
|
|
|
namespace ns { int xyz; }
|
|
|
|
using namespace ns;
|
|
|
|
int main() {
|
|
|
|
xy^
|
|
|
|
})cpp");
|
|
|
|
FS.Files[FooCpp] = FooCpp;
|
|
|
|
Server.addDocument(FooCpp, Code.code());
|
|
|
|
|
|
|
|
// Sleep for some time to make sure code completion is not run because update
|
|
|
|
// hasn't been scheduled.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
auto Opts = clangd::CodeCompleteOptions();
|
2019-05-21 21:40:31 +08:00
|
|
|
Opts.RunParser = CodeCompleteOptions::ParseIfReady;
|
2019-04-15 20:32:28 +08:00
|
|
|
|
|
|
|
auto Res = cantFail(runCodeComplete(Server, FooCpp, Code.point(), Opts));
|
|
|
|
EXPECT_EQ(Res.Context, CodeCompletionContext::CCC_Recovery);
|
|
|
|
|
|
|
|
CanReturnCommand.notify();
|
|
|
|
ASSERT_TRUE(Server.blockUntilIdleForTest());
|
|
|
|
EXPECT_THAT(cantFail(runCodeComplete(Server, FooCpp, Code.point(),
|
|
|
|
clangd::CodeCompleteOptions()))
|
|
|
|
.Completions,
|
|
|
|
ElementsAre(AllOf(Field(&CodeCompletion::Name, "xyz"),
|
|
|
|
Field(&CodeCompletion::Scope, "ns::"))));
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
} // namespace
|
2017-05-26 20:26:51 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|