clang-tools 20.0.0git
TestFS.h
Go to the documentation of this file.
1//===-- TestFS.h ------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Allows setting up fake filesystem environments for tests.
10//
11//===----------------------------------------------------------------------===//
12#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTFS_H
13#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TESTFS_H
15#include "support/Path.h"
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/Support/Path.h"
19#include "llvm/Support/VirtualFileSystem.h"
20#include <optional>
21
22namespace clang {
23namespace clangd {
24
25// Builds a VFS that provides access to the provided files, plus temporary
26// directories.
27llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
28buildTestFS(llvm::StringMap<std::string> const &Files,
29 llvm::StringMap<time_t> const &Timestamps = {});
30
31// A VFS provider that returns TestFSes containing a provided set of files.
32class MockFS : public ThreadsafeFS {
33public:
34 IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
35 auto MemFS = buildTestFS(Files, Timestamps);
37 return MemFS;
38 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem =
39 new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem());
40 OverlayFileSystem->pushOverlay(MemFS);
41 return OverlayFileSystem;
42 }
43
44 // If relative paths are used, they are resolved with testPath().
45 llvm::StringMap<std::string> Files;
46 llvm::StringMap<time_t> Timestamps;
47 // If true, real file system will be used as fallback for the in-memory one.
48 // This is useful for testing module support.
50};
51
52// A Compilation database that returns a fixed set of compile flags.
54public:
55 /// If \p Directory is not empty, use that as the Directory field of the
56 /// CompileCommand, and as project SourceRoot.
57 ///
58 /// If \p RelPathPrefix is not empty, use that as a prefix in front of the
59 /// source file name, instead of using an absolute path.
60 MockCompilationDatabase(StringRef Directory = StringRef(),
61 StringRef RelPathPrefix = StringRef());
62
63 std::optional<tooling::CompileCommand>
64 getCompileCommand(PathRef File) const override;
65
66 std::optional<ProjectInfo> getProjectInfo(PathRef File) const override;
67
68 std::vector<std::string> ExtraClangFlags;
69
70protected:
71 StringRef Directory;
72 StringRef RelPathPrefix;
73};
74
75// Returns an absolute (fake) test directory for this OS.
76const char *testRoot();
77
78// Returns a suitable absolute path for this OS.
79std::string testPath(PathRef File,
80 llvm::sys::path::Style = llvm::sys::path::Style::native);
81
82// unittest: is a scheme that refers to files relative to testRoot()
83// This anchor is used to force the linker to link in the generated object file
84// and thus register unittest: URI scheme plugin.
85extern volatile int UnittestSchemeAnchorSource;
86
87} // namespace clangd
88} // namespace clang
89#endif
Provides compilation arguments used for parsing C and C++ files.
std::optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
If there are any known-good commands for building this file, returns one.
Definition: TestFS.cpp:60
std::vector< std::string > ExtraClangFlags
Definition: TestFS.h:68
std::optional< ProjectInfo > getProjectInfo(PathRef File) const override
Finds the closest project to File.
Definition: TestFS.cpp:55
llvm::StringMap< std::string > Files
Definition: TestFS.h:45
llvm::StringMap< time_t > Timestamps
Definition: TestFS.h:46
bool OverlayRealFileSystemForModules
Definition: TestFS.h:49
IntrusiveRefCntPtr< llvm::vfs::FileSystem > viewImpl() const override
Overridden by implementations to provide a vfs::FileSystem.
Definition: TestFS.h:34
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:26
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:93
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
Definition: TestFS.cpp:33
volatile int UnittestSchemeAnchorSource
Definition: TestFS.cpp:137
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
const char * testRoot()
Definition: TestFS.cpp:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//