summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2018-05-24 11:17:00 +0000
committerEric Liu <ioeric@google.com>2018-05-24 11:17:00 +0000
commit33dd619c804d65c40a07f769da06b7462bdb073a (patch)
tree6e60a95ec779a5516cae5f2f4d11f3adf46e3225 /clang
parent34391f097d9350d59d3050bf149c0f536dd460da (diff)
downloadbcm5719-llvm-33dd619c804d65c40a07f769da06b7462bdb073a.tar.gz
bcm5719-llvm-33dd619c804d65c40a07f769da06b7462bdb073a.zip
[VFS] Implement getRealPath in InMemoryFileSystem.
Reviewers: bkramer Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D47262 llvm-svn: 333172
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/Basic/VirtualFileSystem.h8
-rw-r--r--clang/lib/Basic/VirtualFileSystem.cpp13
-rw-r--r--clang/unittests/Basic/VirtualFileSystemTest.cpp22
3 files changed, 43 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/VirtualFileSystem.h b/clang/include/clang/Basic/VirtualFileSystem.h
index 21daeb2afcf..2480b91123c 100644
--- a/clang/include/clang/Basic/VirtualFileSystem.h
+++ b/clang/include/clang/Basic/VirtualFileSystem.h
@@ -374,6 +374,14 @@ public:
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
return WorkingDirectory;
}
+ /// Canonicalizes \p Path by combining with the current working
+ /// directory and normalizing the path (e.g. remove dots). If the current
+ /// working directory is not set, this returns errc::operation_not_permitted.
+ ///
+ /// This doesn't resolve symlinks as they are not supported in in-memory file
+ /// system.
+ std::error_code getRealPath(const Twine &Path,
+ SmallVectorImpl<char> &Output) const override;
std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
};
diff --git a/clang/lib/Basic/VirtualFileSystem.cpp b/clang/lib/Basic/VirtualFileSystem.cpp
index b2eb5b9abfa..234adcd9aa0 100644
--- a/clang/lib/Basic/VirtualFileSystem.cpp
+++ b/clang/lib/Basic/VirtualFileSystem.cpp
@@ -788,6 +788,19 @@ std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
return {};
}
+std::error_code
+InMemoryFileSystem::getRealPath(const Twine &Path,
+ SmallVectorImpl<char> &Output) const {
+ auto CWD = getCurrentWorkingDirectory();
+ if (!CWD || CWD->empty())
+ return errc::operation_not_permitted;
+ Path.toVector(Output);
+ if (auto EC = makeAbsolute(Output))
+ return EC;
+ llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
+ return {};
+}
+
} // namespace vfs
} // namespace clang
diff --git a/clang/unittests/Basic/VirtualFileSystemTest.cpp b/clang/unittests/Basic/VirtualFileSystemTest.cpp
index 673eca88d6c..e510c3e2e35 100644
--- a/clang/unittests/Basic/VirtualFileSystemTest.cpp
+++ b/clang/unittests/Basic/VirtualFileSystemTest.cpp
@@ -813,6 +813,28 @@ TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
NormalizedFS.getCurrentWorkingDirectory().get()));
}
+TEST_F(InMemoryFileSystemTest, GetRealPath) {
+ SmallString<16> Path;
+ EXPECT_EQ(FS.getRealPath("b", Path), errc::operation_not_permitted);
+
+ auto GetRealPath = [this](StringRef P) {
+ SmallString<16> Output;
+ auto EC = FS.getRealPath(P, Output);
+ EXPECT_FALSE(EC);
+ return Output.str().str();
+ };
+
+ FS.setCurrentWorkingDirectory("a");
+ EXPECT_EQ(GetRealPath("b"), "a/b");
+ EXPECT_EQ(GetRealPath("../b"), "b");
+ EXPECT_EQ(GetRealPath("b/./c"), "a/b/c");
+
+ FS.setCurrentWorkingDirectory("/a");
+ EXPECT_EQ(GetRealPath("b"), "/a/b");
+ EXPECT_EQ(GetRealPath("../b"), "/b");
+ EXPECT_EQ(GetRealPath("b/./c"), "/a/b/c");
+}
+
TEST_F(InMemoryFileSystemTest, AddFileWithUser) {
FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), 0xFEEDFACE);
auto Stat = FS.status("/a");
OpenPOWER on IntegriCloud