aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/VirtualFileSystemTest.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-11-11 23:03:38 -0500
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-12-08 17:53:30 -0800
commit75cd8d756d6e88b075fae8fe631fdbb17802bdb8 (patch)
treedf4bde4ec81beb710efb58420cc24f4111f649b7 /llvm/unittests/Support/VirtualFileSystemTest.cpp
parenta22eda548b8e1f9362018dec194af0ca91b35da0 (diff)
downloadllvm-75cd8d756d6e88b075fae8fe631fdbb17802bdb8.zip
llvm-75cd8d756d6e88b075fae8fe631fdbb17802bdb8.tar.gz
llvm-75cd8d756d6e88b075fae8fe631fdbb17802bdb8.tar.bz2
Support: Add RedirectingFileSystem::create from simple list of redirections
Add an overload of `RedirectingFileSystem::create` that builds a redirecting filesystem off of a simple vector of string pairs. This is intended to be used to support `clang::arcmt::FileRemapper` and `clang::PreprocessorOptions::RemappedFiles`. Differential Revision: https://reviews.llvm.org/D91317
Diffstat (limited to 'llvm/unittests/Support/VirtualFileSystemTest.cpp')
-rw-r--r--llvm/unittests/Support/VirtualFileSystemTest.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index 1b574b36..6aff06b 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -2287,3 +2287,89 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) {
EXPECT_FALSE(FS->exists(_b.path("b")));
EXPECT_FALSE(FS->exists(_c.path("c")));
}
+
+TEST(VFSFromRemappedFilesTest, Basic) {
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> BaseFS =
+ new vfs::InMemoryFileSystem;
+ BaseFS->addFile("//root/b", 0, MemoryBuffer::getMemBuffer("contents of b"));
+ BaseFS->addFile("//root/c", 0, MemoryBuffer::getMemBuffer("contents of c"));
+
+ std::vector<std::pair<std::string, std::string>> RemappedFiles = {
+ {"//root/a/a", "//root/b"},
+ {"//root/a/b/c", "//root/c"},
+ };
+ auto RemappedFS = vfs::RedirectingFileSystem::create(
+ RemappedFiles, /*UseExternalNames=*/false, *BaseFS);
+
+ auto StatA = RemappedFS->status("//root/a/a");
+ auto StatB = RemappedFS->status("//root/a/b/c");
+ ASSERT_TRUE(StatA);
+ ASSERT_TRUE(StatB);
+ EXPECT_EQ("//root/a/a", StatA->getName());
+ EXPECT_EQ("//root/a/b/c", StatB->getName());
+
+ auto BufferA = RemappedFS->getBufferForFile("//root/a/a");
+ auto BufferB = RemappedFS->getBufferForFile("//root/a/b/c");
+ ASSERT_TRUE(BufferA);
+ ASSERT_TRUE(BufferB);
+ EXPECT_EQ("contents of b", (*BufferA)->getBuffer());
+ EXPECT_EQ("contents of c", (*BufferB)->getBuffer());
+}
+
+TEST(VFSFromRemappedFilesTest, UseExternalNames) {
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> BaseFS =
+ new vfs::InMemoryFileSystem;
+ BaseFS->addFile("//root/b", 0, MemoryBuffer::getMemBuffer("contents of b"));
+ BaseFS->addFile("//root/c", 0, MemoryBuffer::getMemBuffer("contents of c"));
+
+ std::vector<std::pair<std::string, std::string>> RemappedFiles = {
+ {"//root/a/a", "//root/b"},
+ {"//root/a/b/c", "//root/c"},
+ };
+ auto RemappedFS = vfs::RedirectingFileSystem::create(
+ RemappedFiles, /*UseExternalNames=*/true, *BaseFS);
+
+ auto StatA = RemappedFS->status("//root/a/a");
+ auto StatB = RemappedFS->status("//root/a/b/c");
+ ASSERT_TRUE(StatA);
+ ASSERT_TRUE(StatB);
+ EXPECT_EQ("//root/b", StatA->getName());
+ EXPECT_EQ("//root/c", StatB->getName());
+
+ auto BufferA = RemappedFS->getBufferForFile("//root/a/a");
+ auto BufferB = RemappedFS->getBufferForFile("//root/a/b/c");
+ ASSERT_TRUE(BufferA);
+ ASSERT_TRUE(BufferB);
+ EXPECT_EQ("contents of b", (*BufferA)->getBuffer());
+ EXPECT_EQ("contents of c", (*BufferB)->getBuffer());
+}
+
+TEST(VFSFromRemappedFilesTest, LastMappingWins) {
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> BaseFS =
+ new vfs::InMemoryFileSystem;
+ BaseFS->addFile("//root/b", 0, MemoryBuffer::getMemBuffer("contents of b"));
+ BaseFS->addFile("//root/c", 0, MemoryBuffer::getMemBuffer("contents of c"));
+
+ std::vector<std::pair<std::string, std::string>> RemappedFiles = {
+ {"//root/a", "//root/b"},
+ {"//root/a", "//root/c"},
+ };
+ auto RemappedFSKeepName = vfs::RedirectingFileSystem::create(
+ RemappedFiles, /*UseExternalNames=*/false, *BaseFS);
+ auto RemappedFSExternalName = vfs::RedirectingFileSystem::create(
+ RemappedFiles, /*UseExternalNames=*/true, *BaseFS);
+
+ auto StatKeepA = RemappedFSKeepName->status("//root/a");
+ auto StatExternalA = RemappedFSExternalName->status("//root/a");
+ ASSERT_TRUE(StatKeepA);
+ ASSERT_TRUE(StatExternalA);
+ EXPECT_EQ("//root/a", StatKeepA->getName());
+ EXPECT_EQ("//root/c", StatExternalA->getName());
+
+ auto BufferKeepA = RemappedFSKeepName->getBufferForFile("//root/a");
+ auto BufferExternalA = RemappedFSExternalName->getBufferForFile("//root/a");
+ ASSERT_TRUE(BufferKeepA);
+ ASSERT_TRUE(BufferExternalA);
+ EXPECT_EQ("contents of c", (*BufferKeepA)->getBuffer());
+ EXPECT_EQ("contents of c", (*BufferExternalA)->getBuffer());
+}