2017-12-05 07:20:26 +00:00
|
|
|
//===-- TestFS.h ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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 07:20:26 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Allows setting up fake filesystem environments for tests.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-12-02 13:43:13 +00:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTFS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTFS_H
|
2019-07-11 09:54:31 +00: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 17:49:17 +02:00
|
|
|
#include "support/Path.h"
|
2020-06-17 11:53:32 +02:00
|
|
|
#include "support/ThreadsafeFS.h"
|
2017-12-05 07:20:26 +00:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-10 13:27:25 +00:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2023-01-07 20:02:20 -08:00
|
|
|
#include <optional>
|
2017-12-05 07:20:26 +00:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
// Builds a VFS that provides access to the provided files, plus temporary
|
|
|
|
// directories.
|
2018-10-10 13:27:25 +00:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
|
2018-07-26 09:21:07 +00:00
|
|
|
buildTestFS(llvm::StringMap<std::string> const &Files,
|
|
|
|
llvm::StringMap<time_t> const &Timestamps = {});
|
2017-12-05 07:20:26 +00:00
|
|
|
|
|
|
|
// A VFS provider that returns TestFSes containing a provided set of files.
|
2020-06-17 11:53:32 +02:00
|
|
|
class MockFS : public ThreadsafeFS {
|
2017-12-05 07:20:26 +00:00
|
|
|
public:
|
2020-06-29 19:52:09 +02:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
|
2020-08-13 17:24:22 +02: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 12:16:24 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:41:43 +00:00
|
|
|
// If relative paths are used, they are resolved with testPath().
|
2017-12-05 07:20:26 +00:00
|
|
|
llvm::StringMap<std::string> Files;
|
2020-06-04 18:26:52 +02:00
|
|
|
llvm::StringMap<time_t> Timestamps;
|
2020-08-13 17:24:22 +02: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 07:20:26 +00: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-10 22:27:53 +00: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 09:51:50 +00: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-10 22:27:53 +00: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 07:20:26 +00:00
|
|
|
|
2023-01-07 20:19:42 -08:00
|
|
|
std::optional<tooling::CompileCommand>
|
2019-07-11 09:54:31 +00:00
|
|
|
getCompileCommand(PathRef File) const override;
|
|
|
|
|
2023-01-07 20:19:42 -08:00
|
|
|
std::optional<ProjectInfo> getProjectInfo(PathRef File) const override;
|
2017-12-05 07:20:26 +00: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-10 22:27:53 +00:00
|
|
|
|
[clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (#66462)
Alternatives to https://reviews.llvm.org/D153114.
Try to address https://github.com/clangd/clangd/issues/1293.
See the links for design ideas and the consensus so far. We want to have
some initial support in clang18.
This is the initial support for C++20 Modules in clangd.
As suggested by sammccall in https://reviews.llvm.org/D153114,
we should minimize the scope of the initial patch to make it easier
to review and understand so that every one are in the same page:
> Don't attempt any cross-file or cross-version coordination: i.e. don't
> try to reuse BMIs between different files, don't try to reuse BMIs
> between (preamble) reparses of the same file, don't try to persist the
> module graph. Instead, when building a preamble, synchronously scan
> for the module graph, build the required PCMs on the single preamble
> thread with filenames private to that preamble, and then proceed to
> build the preamble.
This patch reflects the above opinions.
# Testing in real-world project
I tested this with a modularized library:
https://github.com/alibaba/async_simple/tree/CXX20Modules. This library
has 3 modules (async_simple, std and asio) and 65 module units. (Note
that a module consists of multiple module units). Both `std` module and
`asio` module have 100k+ lines of code (maybe more, I didn't count). And
async_simple itself has 8k lines of code. This is the scale of the
project.
The result shows that it works pretty well, ..., well, except I need to
wait roughly 10s after opening/editing any file. And this falls in our
expectations. We know it is hard to make it perfect in the first move.
# What this patch does in detail
- Introduced an option `--experimental-modules-support` for the support
for C++20 Modules. So that no matter how bad this is, it wouldn't affect
current users. Following off the page, we'll assume the option is
enabled.
- Introduced two classes `ModuleFilesInfo` and
`ModuleDependencyScanner`. Now `ModuleDependencyScanner` is only used by
`ModuleFilesInfo`.
- The class `ModuleFilesInfo` records the built module files for
specific single source file. The module files can only be built by the
static member function `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef
File, ...)`.
- The class `PreambleData` adds a new member variable with type
`ModuleFilesInfo`. This refers to the needed module files for the
current file. It means the module files info is part of the preamble,
which is suggested in the first patch too.
- In `isPreambleCompatible()`, we add a call to
`ModuleFilesInfo::CanReuse()` to check if the built module files are
still up to date.
- When we build the AST for a source file, we will load the built module
files from ModuleFilesInfo.
# What we need to do next
Let's split the TODOs into clang part and clangd part to make things
more clear.
The TODOs in the clangd part include:
1. Enable reusing module files across source files. The may require us
to bring a ModulesManager like thing which need to handle `scheduling`,
`the possibility of BMI version conflicts` and `various events that can
invalidate the module graph`.
2. Get a more efficient method to get the `<module-name> ->
<module-unit-source>` map. Currently we always scan the whole project
during `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef File, ...)`.
This is clearly inefficient even if the scanning process is pretty fast.
I think the potential solutions include:
- Make a global scanner to monitor the state of every source file like I
did in the first patch. The pain point is that we need to take care of
the data races.
- Ask the build systems to provide the map just like we ask them to
provide the compilation database.
3. Persist the module files. So that we can reuse module files across
clangd invocations or even across clangd instances.
TODOs in the clang part include:
1. Clang should offer an option/mode to skip writing/reading the bodies
of the functions. Or even if we can requrie the parser to skip parsing
the function bodies.
And it looks like we can say the support for C++20 Modules is initially
workable after we made (1) and (2) (or even without (2)).
2024-07-18 10:10:22 +08:00
|
|
|
protected:
|
[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-10 22:27:53 +00:00
|
|
|
StringRef Directory;
|
|
|
|
StringRef RelPathPrefix;
|
2017-12-05 07:20:26 +00:00
|
|
|
};
|
|
|
|
|
2018-01-29 22:28:08 +00:00
|
|
|
// Returns an absolute (fake) test directory for this OS.
|
2018-02-16 09:41:43 +00:00
|
|
|
const char *testRoot();
|
2018-01-29 22:28:08 +00:00
|
|
|
|
2017-12-05 07:20:26 +00:00
|
|
|
// Returns a suitable absolute path for this OS.
|
2020-07-01 16:30:57 +02:00
|
|
|
std::string testPath(PathRef File,
|
|
|
|
llvm::sys::path::Style = llvm::sys::path::Style::native);
|
2017-12-05 07:20:26 +00:00
|
|
|
|
2018-06-15 08:55:00 +00: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 07:20:26 +00:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
2018-05-30 14:21:31 +00:00
|
|
|
#endif
|