2017-12-05 15:20:26 +08:00
|
|
|
//===-- TestFS.h ------------------------------------------------*- 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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Allows setting up fake filesystem environments for tests.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-30 22:21:31 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTFS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTFS_H
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "ClangdServer.h"
|
2019-07-11 17:54:31 +08:00
|
|
|
#include "GlobalCompilationDatabase.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"
|
2020-06-17 17:53:32 +08:00
|
|
|
#include "support/ThreadsafeFS.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
2020-06-16 18:16:24 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
// Builds a VFS that provides access to the provided files, plus temporary
|
|
|
|
// directories.
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
|
2018-07-26 17:21:07 +08:00
|
|
|
buildTestFS(llvm::StringMap<std::string> const &Files,
|
|
|
|
llvm::StringMap<time_t> const &Timestamps = {});
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
// A VFS provider that returns TestFSes containing a provided set of files.
|
2020-06-17 17:53:32 +08:00
|
|
|
class MockFS : public ThreadsafeFS {
|
2017-12-05 15:20:26 +08:00
|
|
|
public:
|
2020-06-30 01:52:09 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
|
2020-08-13 23:24:22 +08:00
|
|
|
auto MemFS = buildTestFS(Files, Timestamps);
|
|
|
|
if (!OverlayRealFileSystemForModules)
|
|
|
|
return MemFS;
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem =
|
|
|
|
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem());
|
|
|
|
OverlayFileSystem->pushOverlay(MemFS);
|
|
|
|
return OverlayFileSystem;
|
2020-06-16 18:16:24 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:41:43 +08:00
|
|
|
// If relative paths are used, they are resolved with testPath().
|
2017-12-05 15:20:26 +08:00
|
|
|
llvm::StringMap<std::string> Files;
|
2020-06-05 00:26:52 +08:00
|
|
|
llvm::StringMap<time_t> Timestamps;
|
2020-08-13 23:24:22 +08:00
|
|
|
// If true, real file system will be used as fallback for the in-memory one.
|
|
|
|
// This is useful for testing module support.
|
|
|
|
bool OverlayRealFileSystemForModules = false;
|
2017-12-05 15:20:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// A Compilation database that returns a fixed set of compile flags.
|
|
|
|
class MockCompilationDatabase : public GlobalCompilationDatabase {
|
|
|
|
public:
|
[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 \p Directory is not empty, use that as the Directory field of the
|
[clangd] Auto-index watches global CDB for changes.
Summary:
Instead of receiving compilation commands, auto-index is triggered by just
filenames to reindex, and gets commands from the global comp DB internally.
This has advantages:
- more of the work can be done asynchronously (fetching compilation commands
upfront can be slow for large CDBs)
- we get access to the CDB which can be used to retrieve interpolated commands
for headers (useful in some cases where the original TU goes away)
- fits nicely with the filename-only change observation from r347297
The interface to GlobalCompilationDatabase gets extended: when retrieving a
compile command, the GCDB can optionally report the project the file belongs to.
This naturally fits together with getCompileCommand: it's hard to implement one
without the other. But because most callers don't care, I've ended up with an
awkward optional-out-param-in-virtual method pattern - maybe there's a better
one.
This is the main missing integration point between ClangdServer and
BackgroundIndex, after this we should be able to add an auto-index flag.
Reviewers: ioeric, kadircet
Subscribers: MaskRay, jkorous, arphaman, cfe-commits, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D54865
llvm-svn: 347538
2018-11-26 17:51:50 +08:00
|
|
|
/// CompileCommand, and as project SourceRoot.
|
[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 \p RelPathPrefix is not empty, use that as a prefix in front of the
|
|
|
|
/// source file name, instead of using an absolute path.
|
|
|
|
MockCompilationDatabase(StringRef Directory = StringRef(),
|
|
|
|
StringRef RelPathPrefix = StringRef());
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
llvm::Optional<tooling::CompileCommand>
|
2019-07-11 17:54:31 +08:00
|
|
|
getCompileCommand(PathRef File) const override;
|
|
|
|
|
|
|
|
llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
std::vector<std::string> ExtraClangFlags;
|
[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
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Directory;
|
|
|
|
StringRef RelPathPrefix;
|
2017-12-05 15:20:26 +08:00
|
|
|
};
|
|
|
|
|
2018-01-30 06:28:08 +08:00
|
|
|
// Returns an absolute (fake) test directory for this OS.
|
2018-02-16 17:41:43 +08:00
|
|
|
const char *testRoot();
|
2018-01-30 06:28:08 +08:00
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
// Returns a suitable absolute path for this OS.
|
2020-07-01 22:30:57 +08:00
|
|
|
std::string testPath(PathRef File,
|
|
|
|
llvm::sys::path::Style = llvm::sys::path::Style::native);
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2018-06-15 16:55:00 +08:00
|
|
|
// unittest: is a scheme that refers to files relative to testRoot()
|
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
|
|
|
// and thus register unittest: URI scheme plugin.
|
|
|
|
extern volatile int UnittestSchemeAnchorSource;
|
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
2018-05-30 22:21:31 +08:00
|
|
|
#endif
|