Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
clang/unittests/Basic/FileManagerTest.cpp
Show All 25 Lines | |||||
class FakeStatCache : public FileSystemStatCache { | class FakeStatCache : public FileSystemStatCache { | ||||
private: | private: | ||||
// Maps a file/directory path to its desired stat result. Anything | // Maps a file/directory path to its desired stat result. Anything | ||||
// not in this map is considered to not exist in the file system. | // not in this map is considered to not exist in the file system. | ||||
llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; | llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; | ||||
void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile, | void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile, | ||||
const char *StatPath) { | const char *StatPath) { | ||||
#ifndef _WIN32 | |||||
SmallString<128> NormalizedPath(Path); | SmallString<128> NormalizedPath(Path); | ||||
SmallString<128> NormalizedStatPath; | |||||
if (is_style_posix(llvm::sys::path::Style::native)) { | |||||
llvm::sys::path::native(NormalizedPath); | llvm::sys::path::native(NormalizedPath); | ||||
Path = NormalizedPath.c_str(); | Path = NormalizedPath.c_str(); | ||||
SmallString<128> NormalizedStatPath; | |||||
if (StatPath) { | if (StatPath) { | ||||
NormalizedStatPath = StatPath; | NormalizedStatPath = StatPath; | ||||
llvm::sys::path::native(NormalizedStatPath); | llvm::sys::path::native(NormalizedStatPath); | ||||
StatPath = NormalizedStatPath.c_str(); | StatPath = NormalizedStatPath.c_str(); | ||||
} | } | ||||
#endif | } | ||||
if (!StatPath) | if (!StatPath) | ||||
StatPath = Path; | StatPath = Path; | ||||
auto fileType = IsFile ? | auto fileType = IsFile ? | ||||
llvm::sys::fs::file_type::regular_file : | llvm::sys::fs::file_type::regular_file : | ||||
llvm::sys::fs::file_type::directory_file; | llvm::sys::fs::file_type::directory_file; | ||||
llvm::vfs::Status Status(StatPath, llvm::sys::fs::UniqueID(1, INode), | llvm::vfs::Status Status(StatPath, llvm::sys::fs::UniqueID(1, INode), | ||||
Show All 15 Lines | void InjectDirectory(const char *Path, ino_t INode) { | ||||
InjectFileOrDirectory(Path, INode, /*IsFile=*/false, nullptr); | InjectFileOrDirectory(Path, INode, /*IsFile=*/false, nullptr); | ||||
} | } | ||||
// Implement FileSystemStatCache::getStat(). | // Implement FileSystemStatCache::getStat(). | ||||
std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, | std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, | ||||
bool isFile, | bool isFile, | ||||
std::unique_ptr<llvm::vfs::File> *F, | std::unique_ptr<llvm::vfs::File> *F, | ||||
llvm::vfs::FileSystem &FS) override { | llvm::vfs::FileSystem &FS) override { | ||||
#ifndef _WIN32 | |||||
SmallString<128> NormalizedPath(Path); | SmallString<128> NormalizedPath(Path); | ||||
if (is_style_posix(llvm::sys::path::Style::native)) { | |||||
llvm::sys::path::native(NormalizedPath); | llvm::sys::path::native(NormalizedPath); | ||||
Path = NormalizedPath.c_str(); | Path = NormalizedPath.c_str(); | ||||
#endif | } | ||||
if (StatCalls.count(Path) != 0) { | if (StatCalls.count(Path) != 0) { | ||||
Status = StatCalls[Path]; | Status = StatCalls[Path]; | ||||
return std::error_code(); | return std::error_code(); | ||||
} | } | ||||
return std::make_error_code(std::errc::no_such_file_or_directory); | return std::make_error_code(std::errc::no_such_file_or_directory); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 341 Lines • ▼ Show 20 Lines | TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { | ||||
EXPECT_EQ(43U, (*file2)->getUniqueID().getFile()); | EXPECT_EQ(43U, (*file2)->getUniqueID().getFile()); | ||||
// Check that the contents of the UFE are not overwritten by the entry in the | // Check that the contents of the UFE are not overwritten by the entry in the | ||||
// filesystem: | // filesystem: | ||||
EXPECT_EQ(123, (*file2)->getSize()); | EXPECT_EQ(123, (*file2)->getSize()); | ||||
} | } | ||||
#endif // !_WIN32 | #endif // !_WIN32 | ||||
static StringRef getSystemRoot() { | |||||
return is_style_windows(llvm::sys::path::Style::native) ? "C:/" : "/"; | |||||
} | |||||
TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { | TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { | ||||
SmallString<64> CustomWorkingDir; | // FIXME: Should this be using a root path / call getSystemRoot()? For now, | ||||
#ifdef _WIN32 | // avoiding that and leaving the test as-is. | ||||
CustomWorkingDir = "C:"; | SmallString<64> CustomWorkingDir = | ||||
#else | is_style_windows(llvm::sys::path::Style::native) ? StringRef("C:") | ||||
CustomWorkingDir = "/"; | : StringRef("/"); | ||||
#endif | |||||
llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); | llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); | ||||
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | ||||
new llvm::vfs::InMemoryFileSystem); | new llvm::vfs::InMemoryFileSystem); | ||||
// setCurrentworkingdirectory must finish without error. | // setCurrentworkingdirectory must finish without error. | ||||
ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ||||
FileSystemOptions Opts; | FileSystemOptions Opts; | ||||
FileManager Manager(Opts, FS); | FileManager Manager(Opts, FS); | ||||
SmallString<64> Path("a/foo.cpp"); | SmallString<64> Path("a/foo.cpp"); | ||||
SmallString<64> ExpectedResult(CustomWorkingDir); | SmallString<64> ExpectedResult(CustomWorkingDir); | ||||
llvm::sys::path::append(ExpectedResult, Path); | llvm::sys::path::append(ExpectedResult, Path); | ||||
ASSERT_TRUE(Manager.makeAbsolutePath(Path)); | ASSERT_TRUE(Manager.makeAbsolutePath(Path)); | ||||
EXPECT_EQ(Path, ExpectedResult); | EXPECT_EQ(Path, ExpectedResult); | ||||
} | } | ||||
// getVirtualFile should always fill the real path. | // getVirtualFile should always fill the real path. | ||||
TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { | TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { | ||||
SmallString<64> CustomWorkingDir; | SmallString<64> CustomWorkingDir = getSystemRoot(); | ||||
#ifdef _WIN32 | |||||
CustomWorkingDir = "C:/"; | |||||
#else | |||||
CustomWorkingDir = "/"; | |||||
#endif | |||||
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | ||||
new llvm::vfs::InMemoryFileSystem); | new llvm::vfs::InMemoryFileSystem); | ||||
// setCurrentworkingdirectory must finish without error. | // setCurrentworkingdirectory must finish without error. | ||||
ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ||||
FileSystemOptions Opts; | FileSystemOptions Opts; | ||||
FileManager Manager(Opts, FS); | FileManager Manager(Opts, FS); | ||||
Show All 11 Lines | TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { | ||||
ASSERT_TRUE(file->isValid()); | ASSERT_TRUE(file->isValid()); | ||||
SmallString<64> ExpectedResult = CustomWorkingDir; | SmallString<64> ExpectedResult = CustomWorkingDir; | ||||
llvm::sys::path::append(ExpectedResult, "tmp", "test"); | llvm::sys::path::append(ExpectedResult, "tmp", "test"); | ||||
EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); | EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); | ||||
} | } | ||||
TEST_F(FileManagerTest, getFileDontOpenRealPath) { | TEST_F(FileManagerTest, getFileDontOpenRealPath) { | ||||
SmallString<64> CustomWorkingDir; | SmallString<64> CustomWorkingDir = getSystemRoot(); | ||||
#ifdef _WIN32 | |||||
CustomWorkingDir = "C:/"; | |||||
#else | |||||
CustomWorkingDir = "/"; | |||||
#endif | |||||
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( | ||||
new llvm::vfs::InMemoryFileSystem); | new llvm::vfs::InMemoryFileSystem); | ||||
// setCurrentworkingdirectory must finish without error. | // setCurrentworkingdirectory must finish without error. | ||||
ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); | ||||
FileSystemOptions Opts; | FileSystemOptions Opts; | ||||
FileManager Manager(Opts, FS); | FileManager Manager(Opts, FS); | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines |