summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/clangd/TestFS.cpp
blob: 38cef89407448eb68abe42459575843bb56b5bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//===-- TestFS.cpp ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TestFS.h"
#include "llvm/Support/Errc.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
IntrusiveRefCntPtr<vfs::FileSystem>
buildTestFS(llvm::StringMap<std::string> const &Files) {
  IntrusiveRefCntPtr<vfs::InMemoryFileSystem> MemFS(
      new vfs::InMemoryFileSystem);
  for (auto &FileAndContents : Files)
    MemFS->addFile(FileAndContents.first(), time_t(),
                   llvm::MemoryBuffer::getMemBuffer(FileAndContents.second,
                                                    FileAndContents.first()));
  return MemFS;
}

Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
MockFSProvider::getTaggedFileSystem(PathRef File) {
  if (ExpectedFile) {
    EXPECT_EQ(*ExpectedFile, File);
  }

  auto FS = buildTestFS(Files);
  return make_tagged(FS, Tag);
}

MockCompilationDatabase::MockCompilationDatabase(bool UseRelPaths)
    : ExtraClangFlags({"-ffreestanding"}), UseRelPaths(UseRelPaths) {
  // -ffreestanding avoids implicit stdc-predef.h.
}

llvm::Optional<tooling::CompileCommand>
MockCompilationDatabase::getCompileCommand(PathRef File) const {
  if (ExtraClangFlags.empty())
    return llvm::None;

  auto CommandLine = ExtraClangFlags;
  auto FileName = llvm::sys::path::filename(File);
  CommandLine.insert(CommandLine.begin(), "clang");
  CommandLine.insert(CommandLine.end(), UseRelPaths ? FileName : File);
  return {tooling::CompileCommand(llvm::sys::path::parent_path(File), FileName,
                                  std::move(CommandLine), "")};
}

const char *getVirtualTestRoot() {
#ifdef LLVM_ON_WIN32
  return "C:\\clangd-test";
#else
  return "/clangd-test";
#endif
}

llvm::SmallString<32> getVirtualTestFilePath(PathRef File) {
  assert(llvm::sys::path::is_relative(File) && "FileName should be relative");

  llvm::SmallString<32> Path;
  llvm::sys::path::append(Path, getVirtualTestRoot(), File);
  return Path;
}

} // namespace clangd
} // namespace clang
OpenPOWER on IntegriCloud