2017-12-05 15:20:26 +08:00
|
|
|
//===-- TestFS.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
|
2017-12-05 15:20:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "TestFS.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
|
|
|
#include "Path.h"
|
2018-06-15 16:55:00 +08:00
|
|
|
#include "URI.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-06-15 16:55:00 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-06-15 16:55:00 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-02-16 17:41:43 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
|
|
|
|
buildTestFS(llvm::StringMap<std::string> const &Files,
|
|
|
|
llvm::StringMap<time_t> const &Timestamps) {
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2018-10-02 18:43:55 +08:00
|
|
|
MemFS->setCurrentWorkingDirectory(testRoot());
|
2018-02-16 17:41:43 +08:00
|
|
|
for (auto &FileAndContents : Files) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef File = FileAndContents.first();
|
2018-07-26 17:21:07 +08:00
|
|
|
MemFS->addFile(
|
|
|
|
File, Timestamps.lookup(File),
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(FileAndContents.second, File));
|
2018-02-16 17:41:43 +08:00
|
|
|
}
|
2018-02-14 01:08:13 +08:00
|
|
|
return MemFS;
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
MockCompilationDatabase::MockCompilationDatabase(llvm::StringRef Directory,
|
|
|
|
llvm::StringRef RelPathPrefix)
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
: ExtraClangFlags({"-ffreestanding"}), Directory(Directory),
|
|
|
|
RelPathPrefix(RelPathPrefix) {
|
2018-02-14 01:47:16 +08:00
|
|
|
// -ffreestanding avoids implicit stdc-predef.h.
|
|
|
|
}
|
2017-12-05 15:34:35 +08:00
|
|
|
|
2019-07-11 17:54:31 +08:00
|
|
|
llvm::Optional<ProjectInfo>
|
|
|
|
MockCompilationDatabase::getProjectInfo(PathRef File) const {
|
|
|
|
return ProjectInfo{Directory};
|
2019-07-11 20:31:18 +08:00
|
|
|
}
|
2019-07-11 17:54:31 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
MockCompilationDatabase::getCompileCommand(PathRef File) const {
|
2017-12-05 15:20:26 +08:00
|
|
|
if (ExtraClangFlags.empty())
|
2018-02-16 17:41:43 +08:00
|
|
|
return None;
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
auto FileName = llvm::sys::path::filename(File);
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
|
|
|
|
// Build the compile command.
|
|
|
|
auto CommandLine = ExtraClangFlags;
|
2017-12-05 15:20:26 +08:00
|
|
|
CommandLine.insert(CommandLine.begin(), "clang");
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
if (RelPathPrefix.empty()) {
|
|
|
|
// Use the absolute path in the compile command.
|
|
|
|
CommandLine.push_back(File);
|
|
|
|
} else {
|
|
|
|
// Build a relative path using RelPathPrefix.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<32> RelativeFilePath(RelPathPrefix);
|
|
|
|
llvm::sys::path::append(RelativeFilePath, FileName);
|
[clangd] Avoid duplicates in findDefinitions response
Summary:
When compile_commands.json contains some source files expressed as
relative paths, we can get duplicate responses to findDefinitions. The
responses only differ by the URI, which are different versions of the
same file:
"result": [
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/build/../src/first.h"
},
{
...
"uri": "file:///home/emaisin/src/ls-interact/cpp-test/src/first.h"
}
]
In getAbsoluteFilePath, we try to obtain the realpath of the FileEntry
by calling tryGetRealPathName. However, this can fail and return an
empty string. It may be bug a bug in clang, but in any case we should
fall back to computing it ourselves if it happens.
I changed getAbsoluteFilePath so that if tryGetRealPathName succeeds, we
return right away (a real path is always absolute). Otherwise, we try
to build an absolute path, as we did before, but we also call
VFS->getRealPath to make sure to get the canonical path (e.g. without
any ".." in it).
Reviewers: malaperle
Subscribers: hokein, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D48687
llvm-svn: 339483
2018-08-11 06:27:53 +08:00
|
|
|
CommandLine.push_back(RelativeFilePath.str());
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
return {tooling::CompileCommand(Directory != llvm::StringRef()
|
|
|
|
? Directory
|
|
|
|
: llvm::sys::path::parent_path(File),
|
|
|
|
FileName, std::move(CommandLine), "")};
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
const char *testRoot() {
|
2018-04-10 21:14:03 +08:00
|
|
|
#ifdef _WIN32
|
2017-12-05 15:20:26 +08:00
|
|
|
return "C:\\clangd-test";
|
|
|
|
#else
|
|
|
|
return "/clangd-test";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
std::string testPath(PathRef File) {
|
2019-01-07 23:45:19 +08:00
|
|
|
assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<32> NativeFile = File;
|
|
|
|
llvm::sys::path::native(NativeFile);
|
|
|
|
llvm::SmallString<32> Path;
|
|
|
|
llvm::sys::path::append(Path, testRoot(), NativeFile);
|
2018-02-16 17:41:43 +08:00
|
|
|
return Path.str();
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:33:53 +08:00
|
|
|
/// unittest: is a scheme that refers to files relative to testRoot().
|
|
|
|
/// URI body is a path relative to testRoot() e.g. unittest:///x.h for
|
|
|
|
/// /clangd-test/x.h.
|
2018-06-15 16:55:00 +08:00
|
|
|
class TestScheme : public URIScheme {
|
|
|
|
public:
|
|
|
|
static const char *Scheme;
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Expected<std::string>
|
|
|
|
getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
|
|
|
|
llvm::StringRef HintPath) const override {
|
2018-11-28 18:30:42 +08:00
|
|
|
if (!HintPath.startswith(testRoot()))
|
2019-01-07 23:45:19 +08:00
|
|
|
return llvm::make_error<llvm::StringError>(
|
2018-11-28 18:30:42 +08:00
|
|
|
"Hint path doesn't start with test root: " + HintPath,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::inconvertibleErrorCode());
|
2018-06-19 17:33:53 +08:00
|
|
|
if (!Body.consume_front("/"))
|
2019-01-07 23:45:19 +08:00
|
|
|
return llvm::make_error<llvm::StringError>(
|
2018-06-19 17:33:53 +08:00
|
|
|
"Body of an unittest: URI must start with '/'",
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
llvm::SmallString<16> Path(Body.begin(), Body.end());
|
|
|
|
llvm::sys::path::native(Path);
|
2018-06-15 16:55:00 +08:00
|
|
|
return testPath(Path);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Expected<URI>
|
|
|
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
|
|
|
|
llvm::StringRef Body = AbsolutePath;
|
2018-06-15 16:55:00 +08:00
|
|
|
if (!Body.consume_front(testRoot()))
|
2019-01-07 23:45:19 +08:00
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
AbsolutePath + "does not start with " + testRoot(),
|
|
|
|
llvm::inconvertibleErrorCode());
|
2018-06-15 16:55:00 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
return URI(Scheme, /*Authority=*/"",
|
|
|
|
llvm::sys::path::convert_to_slash(Body));
|
2018-06-15 16:55:00 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *TestScheme::Scheme = "unittest";
|
|
|
|
|
|
|
|
static URISchemeRegistry::Add<TestScheme> X(TestScheme::Scheme, "Test schema");
|
|
|
|
|
|
|
|
volatile int UnittestSchemeAnchorSource = 0;
|
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|