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"
|
2018-06-15 16:55:00 +08:00
|
|
|
#include "URI.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Path.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 {
|
2020-01-29 03:23:46 +08:00
|
|
|
return ProjectInfo{std::string(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.
|
2020-01-29 03:23:46 +08:00
|
|
|
CommandLine.push_back(std::string(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
|
|
|
} 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);
|
2020-01-29 03:23:46 +08:00
|
|
|
CommandLine.push_back(std::string(RelativeFilePath.str()));
|
[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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-01 22:30:57 +08:00
|
|
|
std::string testPath(PathRef File, llvm::sys::path::Style Style) {
|
2020-11-23 20:12:35 +08:00
|
|
|
assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
|
|
|
|
|
|
|
|
llvm::SmallString<32> NativeFile = File;
|
|
|
|
llvm::sys::path::native(NativeFile, Style);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<32> Path;
|
2020-11-23 20:12:35 +08:00
|
|
|
llvm::sys::path::append(Path, Style, testRoot(), NativeFile);
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(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 {
|
2020-12-18 20:14:15 +08:00
|
|
|
if (!HintPath.empty() && !HintPath.startswith(testRoot()))
|
|
|
|
return error("Hint path is not empty and doesn't start with {0}: {1}",
|
|
|
|
testRoot(), HintPath);
|
2018-06-19 17:33:53 +08:00
|
|
|
if (!Body.consume_front("/"))
|
2020-09-14 17:33:12 +08:00
|
|
|
return error("Body of an unittest: URI must start with '/'");
|
2019-01-07 23:45:19 +08:00
|
|
|
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()))
|
2020-09-14 17:33:12 +08:00
|
|
|
return error("{0} does not start with {1}", AbsolutePath, testRoot());
|
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
|