Index: include/clang/Basic/FileManager.h =================================================================== --- include/clang/Basic/FileManager.h +++ include/clang/Basic/FileManager.h @@ -58,6 +58,7 @@ /// descriptor for the file. class FileEntry { friend class FileManager; + friend class FileEntryCloser; StringRef Name; // Name of the file. std::string RealPathName; // Real path to the file; could be empty. @@ -104,6 +105,27 @@ } }; +class FileEntryCloser +{ +public: + FileEntryCloser(const FileEntry *File) : m_File(File) {} + ~FileEntryCloser() + { + if(m_File) + m_File->closeFile(); + } + + const FileEntry* release() + { + const FileEntry* fileEntry = m_File; + m_File = nullptr; + return fileEntry; + } + +private: + const FileEntry *m_File; +}; + struct FileData; /// \brief Implements support for file system lookup, file system caching, Index: lib/Lex/PPDirectives.cpp =================================================================== --- lib/Lex/PPDirectives.cpp +++ lib/Lex/PPDirectives.cpp @@ -1850,6 +1850,8 @@ } } + FileEntryCloser fileEntryCloser(File); + // Should we enter the source file? Set to false if either the source file is // known to have no effect beyond its effect on module visibility -- that is, // if it's got an include guard that is already defined or is a modular header @@ -2027,8 +2029,10 @@ assert(FID.isValid() && "Expected valid file ID"); // If all is good, enter the new file! - if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation())) + if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation())) { + fileEntryCloser.release(); return; + } // Determine if we're switching to building a new submodule, and which one. if (auto *M = SuggestedModule.getModule()) {