clang-tools  9.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"
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.
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.
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
std::vector< std::string > ExtraClangFlags
Definition: TestFS.h:57
llvm::StringMap< std::string > Files
Definition: TestFS.h:38
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
Provides compilation arguments used for parsing C and C++ files.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
Definition: TestFS.cpp:22
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
const char * testRoot()
Definition: TestFS.cpp:74
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getFileSystem() const override
Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.
Definition: TestFS.h:33
llvm::StringRef Directory
llvm::StringMap< time_t > Timestamps
volatile int UnittestSchemeAnchorSource
Definition: TestFS.cpp:132
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringMap< std::string > Files