1 //===-- TestFS.cpp ----------------------------------------------*- 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 #include "TestFS.h"
9 #include "GlobalCompilationDatabase.h"
10 #include "URI.h"
11 #include "support/Path.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Errc.h"
16 #include "llvm/Support/Path.h"
17
18 namespace clang {
19 namespace clangd {
20
21 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
buildTestFS(llvm::StringMap<std::string> const & Files,llvm::StringMap<time_t> const & Timestamps)22 buildTestFS(llvm::StringMap<std::string> const &Files,
23 llvm::StringMap<time_t> const &Timestamps) {
24 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
25 new llvm::vfs::InMemoryFileSystem);
26 MemFS->setCurrentWorkingDirectory(testRoot());
27 for (auto &FileAndContents : Files) {
28 llvm::StringRef File = FileAndContents.first();
29 MemFS->addFile(
30 File, Timestamps.lookup(File),
31 llvm::MemoryBuffer::getMemBufferCopy(FileAndContents.second, File));
32 }
33 return MemFS;
34 }
35
MockCompilationDatabase(llvm::StringRef Directory,llvm::StringRef RelPathPrefix)36 MockCompilationDatabase::MockCompilationDatabase(llvm::StringRef Directory,
37 llvm::StringRef RelPathPrefix)
38 : ExtraClangFlags({"-ffreestanding"}), Directory(Directory),
39 RelPathPrefix(RelPathPrefix) {
40 // -ffreestanding avoids implicit stdc-predef.h.
41 }
42
43 llvm::Optional<ProjectInfo>
getProjectInfo(PathRef File) const44 MockCompilationDatabase::getProjectInfo(PathRef File) const {
45 return ProjectInfo{std::string(Directory)};
46 }
47
48 llvm::Optional<tooling::CompileCommand>
getCompileCommand(PathRef File) const49 MockCompilationDatabase::getCompileCommand(PathRef File) const {
50 if (ExtraClangFlags.empty())
51 return None;
52
53 auto FileName = llvm::sys::path::filename(File);
54
55 // Build the compile command.
56 auto CommandLine = ExtraClangFlags;
57 CommandLine.insert(CommandLine.begin(), "clang");
58 if (RelPathPrefix.empty()) {
59 // Use the absolute path in the compile command.
60 CommandLine.push_back(std::string(File));
61 } else {
62 // Build a relative path using RelPathPrefix.
63 llvm::SmallString<32> RelativeFilePath(RelPathPrefix);
64 llvm::sys::path::append(RelativeFilePath, FileName);
65 CommandLine.push_back(std::string(RelativeFilePath.str()));
66 }
67
68 return {tooling::CompileCommand(Directory != llvm::StringRef()
69 ? Directory
70 : llvm::sys::path::parent_path(File),
71 FileName, std::move(CommandLine), "")};
72 }
73
testRoot()74 const char *testRoot() {
75 #ifdef _WIN32
76 return "C:\\clangd-test";
77 #else
78 return "/clangd-test";
79 #endif
80 }
81
testPath(PathRef File,llvm::sys::path::Style Style)82 std::string testPath(PathRef File, llvm::sys::path::Style Style) {
83 assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
84
85 llvm::SmallString<32> NativeFile = File;
86 llvm::sys::path::native(NativeFile, Style);
87 llvm::SmallString<32> Path;
88 llvm::sys::path::append(Path, Style, testRoot(), NativeFile);
89 return std::string(Path.str());
90 }
91
92 /// unittest: is a scheme that refers to files relative to testRoot().
93 /// URI body is a path relative to testRoot() e.g. unittest:///x.h for
94 /// /clangd-test/x.h.
95 class TestScheme : public URIScheme {
96 public:
97 static const char *Scheme;
98
99 llvm::Expected<std::string>
getAbsolutePath(llvm::StringRef,llvm::StringRef Body,llvm::StringRef HintPath) const100 getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
101 llvm::StringRef HintPath) const override {
102 if (!HintPath.startswith(testRoot()))
103 return error("Hint path doesn't start with test root: {0}", HintPath);
104 if (!Body.consume_front("/"))
105 return error("Body of an unittest: URI must start with '/'");
106 llvm::SmallString<16> Path(Body.begin(), Body.end());
107 llvm::sys::path::native(Path);
108 return testPath(Path);
109 }
110
111 llvm::Expected<URI>
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const112 uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
113 llvm::StringRef Body = AbsolutePath;
114 if (!Body.consume_front(testRoot()))
115 return error("{0} does not start with {1}", AbsolutePath, testRoot());
116
117 return URI(Scheme, /*Authority=*/"",
118 llvm::sys::path::convert_to_slash(Body));
119 }
120 };
121
122 const char *TestScheme::Scheme = "unittest";
123
124 static URISchemeRegistry::Add<TestScheme> X(TestScheme::Scheme, "Test schema");
125
126 volatile int UnittestSchemeAnchorSource = 0;
127
128 } // namespace clangd
129 } // namespace clang
130