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 "support/Path.h" 17 #include "support/ThreadsafeFS.h" 18 #include "llvm/ADT/IntrusiveRefCntPtr.h" 19 #include "llvm/ADT/None.h" 20 #include "llvm/ADT/Optional.h" 21 #include "llvm/Support/Path.h" 22 #include "llvm/Support/VirtualFileSystem.h" 23 24 namespace clang { 25 namespace clangd { 26 27 // Builds a VFS that provides access to the provided files, plus temporary 28 // directories. 29 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> 30 buildTestFS(llvm::StringMap<std::string> const &Files, 31 llvm::StringMap<time_t> const &Timestamps = {}); 32 33 // A VFS provider that returns TestFSes containing a provided set of files. 34 class MockFS : public ThreadsafeFS { 35 public: viewImpl()36 IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override { 37 return buildTestFS(Files, Timestamps); 38 } 39 40 // If relative paths are used, they are resolved with testPath(). 41 llvm::StringMap<std::string> Files; 42 llvm::StringMap<time_t> Timestamps; 43 }; 44 45 // A Compilation database that returns a fixed set of compile flags. 46 class MockCompilationDatabase : public GlobalCompilationDatabase { 47 public: 48 /// If \p Directory is not empty, use that as the Directory field of the 49 /// CompileCommand, and as project SourceRoot. 50 /// 51 /// If \p RelPathPrefix is not empty, use that as a prefix in front of the 52 /// source file name, instead of using an absolute path. 53 MockCompilationDatabase(StringRef Directory = StringRef(), 54 StringRef RelPathPrefix = StringRef()); 55 56 llvm::Optional<tooling::CompileCommand> 57 getCompileCommand(PathRef File) const override; 58 59 llvm::Optional<ProjectInfo> getProjectInfo(PathRef File) const override; 60 61 std::vector<std::string> ExtraClangFlags; 62 63 private: 64 StringRef Directory; 65 StringRef RelPathPrefix; 66 }; 67 68 // Returns an absolute (fake) test directory for this OS. 69 const char *testRoot(); 70 71 // Returns a suitable absolute path for this OS. 72 std::string testPath(PathRef File, 73 llvm::sys::path::Style = llvm::sys::path::Style::native); 74 75 // unittest: is a scheme that refers to files relative to testRoot() 76 // This anchor is used to force the linker to link in the generated object file 77 // and thus register unittest: URI scheme plugin. 78 extern volatile int UnittestSchemeAnchorSource; 79 80 } // namespace clangd 81 } // namespace clang 82 #endif 83