[vfs] Normalize working directory if requested.

FixedCompilationDatabase sets the working dir to "." by default. For
chdir(".") this is a noop but this lead to InMemoryFileSystem to create
bogus paths. Fixes PR25327.

llvm-svn: 257260
This commit is contained in:
Benjamin Kramer 2016-01-09 16:33:16 +00:00
parent 88c163460c
commit e9e7607974
3 changed files with 24 additions and 4 deletions

View File

@ -299,10 +299,7 @@ public:
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
return WorkingDirectory; return WorkingDirectory;
} }
std::error_code setCurrentWorkingDirectory(const Twine &Path) override { std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
WorkingDirectory = Path.str();
return std::error_code();
}
}; };
/// \brief Get a globally unique ID for a virtual file or directory. /// \brief Get a globally unique ID for a virtual file or directory.

View File

@ -658,6 +658,23 @@ directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
EC = make_error_code(llvm::errc::not_a_directory); EC = make_error_code(llvm::errc::not_a_directory);
return directory_iterator(std::make_shared<InMemoryDirIterator>()); return directory_iterator(std::make_shared<InMemoryDirIterator>());
} }
std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
SmallString<128> Path;
P.toVector(Path);
// Fix up relative paths. This just prepends the current working directory.
std::error_code EC = makeAbsolute(Path);
assert(!EC);
(void)EC;
if (useNormalizedPaths())
llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
if (!Path.empty())
WorkingDirectory = Path.str();
return std::error_code();
}
} }
} }

View File

@ -657,6 +657,12 @@ TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
Stat = FS.status("c"); Stat = FS.status("c");
ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
NormalizedFS.setCurrentWorkingDirectory("/b/c");
NormalizedFS.setCurrentWorkingDirectory(".");
ASSERT_EQ("/b/c", NormalizedFS.getCurrentWorkingDirectory().get());
NormalizedFS.setCurrentWorkingDirectory("..");
ASSERT_EQ("/b", NormalizedFS.getCurrentWorkingDirectory().get());
} }
// NOTE: in the tests below, we use '//root/' as our root directory, since it is // NOTE: in the tests below, we use '//root/' as our root directory, since it is