From e84385fef88d1e48bc63d31a9cf03c1dab2eb420 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Mon, 19 Nov 2018 13:37:46 +0000 Subject: [FileManager] getFile(open=true) after getFile(open=false) should open the file. Summary: Old behavior is to just return the cached entry regardless of opened-ness. That feels buggy (though I guess nobody ever actually needed this). This came up in the context of clangd+clang-tidy integration: we're going to getFile(open=false) to replay preprocessor actions obscured by the preamble, but the compilation may subsequently getFile(open=true) for non-preamble includes. Reviewers: ilya-biryukov Subscribers: ioeric, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D54691 llvm-svn: 347205 --- clang/unittests/Basic/FileManagerTest.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'clang/unittests/Basic/FileManagerTest.cpp') diff --git a/clang/unittests/Basic/FileManagerTest.cpp b/clang/unittests/Basic/FileManagerTest.cpp index 309de479499..b5855eeff7e 100644 --- a/clang/unittests/Basic/FileManagerTest.cpp +++ b/clang/unittests/Basic/FileManagerTest.cpp @@ -222,6 +222,33 @@ TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { EXPECT_EQ(nullptr, file); } +// When calling getFile(OpenFile=false); getFile(OpenFile=true) the file is +// opened for the second call. +TEST_F(FileManagerTest, getFileDefersOpen) { + llvm::IntrusiveRefCntPtr FS( + new llvm::vfs::InMemoryFileSystem()); + FS->addFile("/tmp/test", 0, llvm::MemoryBuffer::getMemBufferCopy("test")); + FS->addFile("/tmp/testv", 0, llvm::MemoryBuffer::getMemBufferCopy("testv")); + FileManager manager(options, FS); + + const FileEntry *file = manager.getFile("/tmp/test", /*OpenFile=*/false); + ASSERT_TRUE(file != nullptr); + ASSERT_TRUE(file->isValid()); + // "real path name" reveals whether the file was actually opened. + EXPECT_EQ("", file->tryGetRealPathName()); + + file = manager.getFile("/tmp/test", /*OpenFile=*/true); + ASSERT_TRUE(file != nullptr); + ASSERT_TRUE(file->isValid()); + EXPECT_EQ("/tmp/test", file->tryGetRealPathName()); + + // However we should never try to open a file previously opened as virtual. + ASSERT_TRUE(manager.getVirtualFile("/tmp/testv", 5, 0)); + ASSERT_TRUE(manager.getFile("/tmp/testv", /*OpenFile=*/false)); + file = manager.getFile("/tmp/testv", /*OpenFile=*/true); + EXPECT_EQ("", file->tryGetRealPathName()); +} + // The following tests apply to Unix-like system only. #ifndef _WIN32 -- cgit v1.2.3