clang-tools  14.0.0git
TestFS.cpp
Go to the documentation of this file.
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"
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>
22 buildTestFS(llvm::StringMap<std::string> const &Files,
23  llvm::StringMap<time_t> const &Timestamps) {
24  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
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 
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>
45  return ProjectInfo{std::string(Directory)};
46 }
47 
48 llvm::Optional<tooling::CompileCommand>
50  if (ExtraClangFlags.empty())
51  return None;
52 
53  auto FileName = llvm::sys::path::filename(File);
54 
55  // Build the compile command.
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 
74 const char *testRoot() {
75 #ifdef _WIN32
76  return "C:\\clangd-test";
77 #else
78  return "/clangd-test";
79 #endif
80 }
81 
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>
100  getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
101  llvm::StringRef HintPath) const override {
102  if (!HintPath.empty() && !HintPath.startswith(testRoot()))
103  return error("Hint path is not empty and doesn't start with {0}: {1}",
104  testRoot(), HintPath);
105  if (!Body.consume_front("/"))
106  return error("Body of an unittest: URI must start with '/'");
107  llvm::SmallString<16> Path(Body.begin(), Body.end());
108  llvm::sys::path::native(Path);
109  return testPath(Path);
110  }
111 
112  llvm::Expected<URI>
113  uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
114  llvm::StringRef Body = AbsolutePath;
115  if (!Body.consume_front(testRoot()))
116  return error("{0} does not start with {1}", AbsolutePath, testRoot());
117 
118  return URI(Scheme, /*Authority=*/"",
119  llvm::sys::path::convert_to_slash(Body));
120  }
121 };
122 
123 const char *TestScheme::Scheme = "unittest";
124 
125 static URISchemeRegistry::Add<TestScheme> X(TestScheme::Scheme, "Test schema");
126 
127 volatile int UnittestSchemeAnchorSource = 0;
128 
129 } // namespace clangd
130 } // namespace clang
clang::clangd::testPath
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:82
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
Path.h
clang::clangd::X
static URISchemeRegistry::Add< TestScheme > X(TestScheme::Scheme, "Test schema")
clang::clangd::URIScheme
URIScheme is an extension point for teaching clangd to recognize a custom URI scheme.
Definition: URI.h:108
clang::clangd::TestScheme::getAbsolutePath
llvm::Expected< std::string > getAbsolutePath(llvm::StringRef, llvm::StringRef Body, llvm::StringRef HintPath) const override
Returns the absolute path of the file corresponding to the URI authority+body in the file system.
Definition: TestFS.cpp:100
clang::clangd::TestScheme::Scheme
static const char * Scheme
Definition: TestFS.cpp:97
clang::clangd::ProjectInfo
Definition: GlobalCompilationDatabase.h:29
clang::clangd::UnittestSchemeAnchorSource
volatile int UnittestSchemeAnchorSource
Definition: TestFS.cpp:127
clang::clangd::MockCompilationDatabase::getCompileCommand
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File) const override
If there are any known-good commands for building this file, returns one.
Definition: TestFS.cpp:49
clang::clangd::MockCompilationDatabase::MockCompilationDatabase
MockCompilationDatabase(StringRef Directory=StringRef(), StringRef RelPathPrefix=StringRef())
If Directory is not empty, use that as the Directory field of the CompileCommand, and as project Sour...
Definition: TestFS.cpp:36
clang::clangd::testRoot
const char * testRoot()
Definition: TestFS.cpp:74
GlobalCompilationDatabase.h
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::MockCompilationDatabase::getProjectInfo
llvm::Optional< ProjectInfo > getProjectInfo(PathRef File) const override
Finds the closest project to File.
Definition: TestFS.cpp:44
CommandLine
std::vector< llvm::StringRef > CommandLine
Definition: Serialization.cpp:420
TestFS.h
Directory
llvm::StringRef Directory
Definition: Serialization.cpp:419
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
InMemoryFileSystem
llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > InMemoryFileSystem
Definition: IndexActionTests.cpp:109
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::TestScheme::uriFromAbsolutePath
llvm::Expected< URI > uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override
Definition: TestFS.cpp:113
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
URI.h
clang::clangd::MockCompilationDatabase::ExtraClangFlags
std::vector< std::string > ExtraClangFlags
Definition: TestFS.h:70
clang::clangd::URI
A URI describes the location of a source file.
Definition: URI.h:28