clang-tools 10.0.0

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_UNITTESTS_CLANGD_TESTFS_H
13 #define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTFS_H
14 #include "ClangdServer.h"
15 #include "GlobalCompilationDatabase.h"
16 #include "Path.h"
17 #include "llvm/ADT/IntrusiveRefCntPtr.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/VirtualFileSystem.h"
20
21 namespace clang {
22 namespace clangd {
23
24 // Builds a VFS that provides access to the provided files, plus temporary
25 // directories.
26 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
27 buildTestFS(llvm::StringMap<std::string> const &Files,
28 llvm::StringMap<time_t> const &Timestamps = {});
29
30 // A VFS provider that returns TestFSes containing a provided set of files.
31 class MockFSProvider : public FileSystemProvider {
32 public:
33 IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem() const override {
34 return buildTestFS(Files);
35 }
36
37 // If relative paths are used, they are resolved with testPath().
38 llvm::StringMap<std::string> Files;
39 };
40
41 // A Compilation database that returns a fixed set of compile flags.
42 class MockCompilationDatabase : public GlobalCompilationDatabase {
43 public:
44 /// If \p Directory is not empty, use that as the Directory field of the
45 /// CompileCommand, and as project SourceRoot.
46 ///
47 /// If \p RelPathPrefix is not empty, use that as a prefix in front of the
48 /// source file name, instead of using an absolute path.
49 MockCompilationDatabase(StringRef Directory = StringRef(),
50 StringRef RelPathPrefix = StringRef());
51
52 llvm::Optional<tooling::CompileCommand>
53 getCompileCommand(PathRef File) const override;
54
55 llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override;
56
57 std::vector<std::string> ExtraClangFlags;
58
59 private:
60 StringRef Directory;
61 StringRef RelPathPrefix;
62 };
63
64 // Returns an absolute (fake) test directory for this OS.
65 const char *testRoot();
66
67 // Returns a suitable absolute path for this OS.
68 std::string testPath(PathRef File);
69
70 // unittest: is a scheme that refers to files relative to testRoot()
71 // This anchor is used to force the linker to link in the generated object file
72 // and thus register unittest: URI scheme plugin.
73 extern volatile int UnittestSchemeAnchorSource;
74
75 } // namespace clangd
76 } // namespace clang
77 #endif
clang::clangd::MockCompilationDatabase::ExtraClangFlags
std::vector< std::string > ExtraClangFlags
Definition: TestFS.h:57
ClangdServer.h
clang::clangd::MockFSProvider::Files
llvm::StringMap< std::string > Files
Definition: TestFS.h:38
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang::clangd::FileSystemProvider
Definition: FSProvider.h:20
GlobalCompilationDatabase.h
clang::clangd::MockFSProvider
Definition: TestFS.h:31
clang::clangd::GlobalCompilationDatabase
Provides compilation arguments used for parsing C and C++ files.
Definition: GlobalCompilationDatabase.h:35
clang::clangd::buildTestFS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
Definition: TestFS.cpp:22
clang::clangd::testPath
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
clang::clangd::testRoot
const char * testRoot()
Definition: TestFS.cpp:74
clang::clangd::MockFSProvider::getFileSystem
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getFileSystem() const override
Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.
Definition: TestFS.h:33
Directory
llvm::StringRef Directory
Definition: Serialization.cpp:387
Timestamps
llvm::StringMap< time_t > Timestamps
Definition: TUSchedulerTests.cpp:120
clang::clangd::UnittestSchemeAnchorSource
volatile int UnittestSchemeAnchorSource
Definition: TestFS.cpp:132
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Path.h
clang::clangd::MockCompilationDatabase
Definition: TestFS.h:42
clang::clangd::CompletionItemKind::File
Files
llvm::StringMap< std::string > Files
Definition: TUSchedulerTests.cpp:119