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: getFileSystem()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 78