summaryrefslogtreecommitdiffstats
path: root/clang/lib/Basic/FileManager.cpp
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2018-11-19 13:37:46 +0000
committerSam McCall <sam.mccall@gmail.com>2018-11-19 13:37:46 +0000
commite84385fef88d1e48bc63d31a9cf03c1dab2eb420 (patch)
treebfee58427b285adb75784569a4c79f4fc15059f9 /clang/lib/Basic/FileManager.cpp
parent71fdb5764086bf8cbcb84666002090032d152caa (diff)
downloadbcm5719-llvm-e84385fef88d1e48bc63d31a9cf03c1dab2eb420.tar.gz
bcm5719-llvm-e84385fef88d1e48bc63d31a9cf03c1dab2eb420.zip
[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
Diffstat (limited to 'clang/lib/Basic/FileManager.cpp')
-rw-r--r--clang/lib/Basic/FileManager.cpp51
1 files changed, 32 insertions, 19 deletions
diff --git a/clang/lib/Basic/FileManager.cpp b/clang/lib/Basic/FileManager.cpp
index c4b15b30552..21cb92d51c2 100644
--- a/clang/lib/Basic/FileManager.cpp
+++ b/clang/lib/Basic/FileManager.cpp
@@ -221,15 +221,21 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
*SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
// See if there is already an entry in the map.
- if (NamedFileEnt.second)
- return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr
- : NamedFileEnt.second;
+ if (NamedFileEnt.second) {
+ if (NamedFileEnt.second == NON_EXISTENT_FILE)
+ return nullptr;
+ // Entry exists: return it *unless* it wasn't opened and open is requested.
+ if (!(NamedFileEnt.second->DeferredOpen && openFile))
+ return NamedFileEnt.second;
+ // We previously stat()ed the file, but didn't open it: do that below.
+ // FIXME: the below does other redundant work too (stats the dir and file).
+ } else {
+ // By default, initialize it to invalid.
+ NamedFileEnt.second = NON_EXISTENT_FILE;
+ }
++NumFileCacheMisses;
- // By default, initialize it to invalid.
- NamedFileEnt.second = NON_EXISTENT_FILE;
-
// Get the null-terminated file name as stored as the key of the
// SeenFileEntries map.
StringRef InterndFileName = NamedFileEnt.first();
@@ -267,6 +273,7 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
// It exists. See if we have already opened a file with the same inode.
// This occurs when one dir is symlinked to another, for example.
FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
+ UFE.DeferredOpen = !openFile;
NamedFileEnt.second = &UFE;
@@ -283,6 +290,23 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
InterndFileName = NamedFileEnt.first().data();
}
+ // If we opened the file for the first time, record the resulting info.
+ // Do this even if the cache entry was valid, maybe we didn't previously open.
+ if (F && !UFE.File) {
+ if (auto PathName = F->getName()) {
+ llvm::SmallString<128> AbsPath(*PathName);
+ // This is not the same as `VFS::getRealPath()`, which resolves symlinks
+ // but can be very expensive on real file systems.
+ // FIXME: the semantic of RealPathName is unclear, and the name might be
+ // misleading. We need to clean up the interface here.
+ makeAbsolutePath(AbsPath);
+ llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
+ UFE.RealPathName = AbsPath.str();
+ }
+ UFE.File = std::move(F);
+ assert(!UFE.DeferredOpen && "we just opened it!");
+ }
+
if (UFE.isValid()) { // Already have an entry with this inode, return it.
// FIXME: this hack ensures that if we look up a file by a virtual path in
@@ -313,21 +337,9 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
UFE.UniqueID = Data.UniqueID;
UFE.IsNamedPipe = Data.IsNamedPipe;
UFE.InPCH = Data.InPCH;
- UFE.File = std::move(F);
UFE.IsValid = true;
+ // Note File and DeferredOpen were initialized above.
- if (UFE.File) {
- if (auto PathName = UFE.File->getName()) {
- llvm::SmallString<128> AbsPath(*PathName);
- // This is not the same as `VFS::getRealPath()`, which resolves symlinks
- // but can be very expensive on real file systems.
- // FIXME: the semantic of RealPathName is unclear, and the name might be
- // misleading. We need to clean up the interface here.
- makeAbsolutePath(AbsPath);
- llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
- UFE.RealPathName = AbsPath.str();
- }
- }
return &UFE;
}
@@ -398,6 +410,7 @@ FileManager::getVirtualFile(StringRef Filename, off_t Size,
UFE->UID = NextFileUID++;
UFE->IsValid = true;
UFE->File.reset();
+ UFE->DeferredOpen = false;
return UFE;
}
OpenPOWER on IntegriCloud