Index: clang/include/clang/Basic/FileEntry.h =================================================================== --- clang/include/clang/Basic/FileEntry.h +++ clang/include/clang/Basic/FileEntry.h @@ -122,6 +122,7 @@ inline unsigned getUID() const; inline const llvm::sys::fs::UniqueID &getUniqueID() const; inline time_t getModificationTime() const; + inline void closeFile() const; explicit FileEntryRef(const MapEntry &ME) : FileEntryRefBase(&ME) {} }; @@ -241,6 +242,8 @@ return getFileEntry().getModificationTime(); } +void FileEntryRef::closeFile() const { getFileEntry().closeFile(); } + } // end namespace clang #endif // LLVM_CLANG_BASIC_FILEENTRY_H Index: clang/include/clang/Basic/Module.h =================================================================== --- clang/include/clang/Basic/Module.h +++ clang/include/clang/Basic/Module.h @@ -15,6 +15,7 @@ #ifndef LLVM_CLANG_BASIC_MODULE_H #define LLVM_CLANG_BASIC_MODULE_H +#include "clang/Basic/FileEntry.h" #include "clang/Basic/SourceLocation.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" @@ -160,7 +161,7 @@ /// The AST file if this is a top-level module which has a /// corresponding serialized AST file, or null otherwise. - const FileEntry *ASTFile = nullptr; + MaybeFileEntryRef ASTFile; /// The top-level headers associated with this module. llvm::SmallSetVector TopHeaders; @@ -529,14 +530,14 @@ } /// The serialized AST file for this module, if one was created. - const FileEntry *getASTFile() const { + MaybeFileEntryRef getASTFile() const { return getTopLevelModule()->ASTFile; } /// Set the serialized AST file for the top-level module of this module. - void setASTFile(const FileEntry *File) { - assert((File == nullptr || getASTFile() == nullptr || - getASTFile() == File) && "file path changed"); + void setASTFile(MaybeFileEntryRef File) { + assert((!File || !getASTFile() || getASTFile() == File) && + "file path changed"); getTopLevelModule()->ASTFile = File; } Index: clang/include/clang/Serialization/ModuleFile.h =================================================================== --- clang/include/clang/Serialization/ModuleFile.h +++ clang/include/clang/Serialization/ModuleFile.h @@ -159,7 +159,7 @@ bool DidReadTopLevelSubmodule = false; /// The file entry for the module file. - const FileEntry *File = nullptr; + MaybeFileEntryRef File; /// The signature of the module file, which may be used instead of the size /// and modification time to identify this particular file. Index: clang/include/clang/Serialization/ModuleManager.h =================================================================== --- clang/include/clang/Serialization/ModuleManager.h +++ clang/include/clang/Serialization/ModuleManager.h @@ -307,10 +307,8 @@ /// \returns True if a file exists but does not meet the size/ /// modification time criteria, false if the file is either available and /// suitable, or is missing. - bool lookupModuleFile(StringRef FileName, - off_t ExpectedSize, - time_t ExpectedModTime, - const FileEntry *&File); + bool lookupModuleFile(StringRef FileName, off_t ExpectedSize, + time_t ExpectedModTime, MaybeFileEntryRef &File); /// View the graphviz representation of the module graph. void viewGraph(); Index: clang/lib/Basic/Module.cpp =================================================================== --- clang/lib/Basic/Module.cpp +++ clang/lib/Basic/Module.cpp @@ -671,7 +671,7 @@ : Signature(M.Signature), ClangModule(&M) { if (M.Directory) Path = M.Directory->getName(); - if (auto *File = M.getASTFile()) + if (auto File = M.getASTFile()) ASTFile = File->getName(); } Index: clang/lib/Sema/SemaModule.cpp =================================================================== --- clang/lib/Sema/SemaModule.cpp +++ clang/lib/Sema/SemaModule.cpp @@ -187,7 +187,7 @@ Diag(Path[0].second, diag::err_module_redefinition) << ModuleName; if (M->DefinitionLoc.isValid()) Diag(M->DefinitionLoc, diag::note_prev_module_definition); - else if (const auto *FE = M->getASTFile()) + else if (MaybeFileEntryRef FE = M->getASTFile()) Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file) << FE->getName(); Mod = M; Index: clang/lib/Serialization/ModuleManager.cpp =================================================================== --- clang/lib/Serialization/ModuleManager.cpp +++ clang/lib/Serialization/ModuleManager.cpp @@ -112,7 +112,7 @@ // Look for the file entry. This only fails if the expected size or // modification time differ. - const FileEntry *Entry; + MaybeFileEntryRef Entry; if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { // If we're not expecting to pull this file out of the module cache, it // might have a different mtime due to being moved across filesystems in @@ -288,7 +288,7 @@ if (modMap) { StringRef ModuleName = victim->ModuleName; if (Module *mod = modMap->findModule(ModuleName)) { - mod->setASTFile(nullptr); + mod->setASTFile(None); } } } @@ -458,18 +458,18 @@ returnVisitState(State); } -bool ModuleManager::lookupModuleFile(StringRef FileName, - off_t ExpectedSize, +bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize, time_t ExpectedModTime, - const FileEntry *&File) { - File = nullptr; + MaybeFileEntryRef &File) { + File = None; if (FileName == "-") return false; // Open the file immediately to ensure there is no race between stat'ing and // opening the file. - auto FileOrErr = FileMgr.getFile(FileName, /*OpenFile=*/true, - /*CacheFailure=*/false); + MaybeFileEntryRef FileOrErr = + expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true, + /*CacheFailure=*/false)); if (!FileOrErr) return false; Index: clang/tools/libclang/CIndex.cpp =================================================================== --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -8397,7 +8397,7 @@ if (!CXMod) return nullptr; Module *Mod = static_cast(CXMod); - return const_cast(Mod->getASTFile()); + return const_cast(Mod->getASTFile().getFileEntry()); } CXModule clang_Module_getParent(CXModule CXMod) { Index: clang/tools/libclang/CXIndexDataConsumer.cpp =================================================================== --- clang/tools/libclang/CXIndexDataConsumer.cpp +++ clang/tools/libclang/CXIndexDataConsumer.cpp @@ -491,13 +491,10 @@ if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule()) return; - CXIdxImportedASTFileInfo Info = { - static_cast( - const_cast(Mod->getASTFile())), - Mod, - getIndexLoc(ImportD->getLocation()), - ImportD->isImplicit() - }; + CXIdxImportedASTFileInfo Info = {static_cast(const_cast( + Mod->getASTFile().getFileEntry())), + Mod, getIndexLoc(ImportD->getLocation()), + ImportD->isImplicit()}; CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info); (void)astFile; }