diff --git a/clang/include/clang/Basic/FileEntry.h b/clang/include/clang/Basic/FileEntry.h --- a/clang/include/clang/Basic/FileEntry.h +++ b/clang/include/clang/Basic/FileEntry.h @@ -64,6 +64,7 @@ inline unsigned getUID() const; inline const llvm::sys::fs::UniqueID &getUniqueID() const; inline time_t getModificationTime() const; + inline void closeFile() const; /// Check if the underlying FileEntry is the same, intentially ignoring /// whether the file was referenced with the same spelling of the filename. @@ -360,6 +361,8 @@ return getFileEntry().getModificationTime(); } +void FileEntryRef::closeFile() const { getFileEntry().closeFile(); } + } // end namespace clang #endif // LLVM_CLANG_BASIC_FILEENTRY_H diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h --- a/clang/include/clang/Basic/Module.h +++ b/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; + Optional 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 { + OptionalFileEntryRefDegradesToFileEntryPtr 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(Optional File) { + assert((!File || !getASTFile() || getASTFile() == File) && + "file path changed"); getTopLevelModule()->ASTFile = File; } diff --git a/clang/include/clang/Serialization/ModuleFile.h b/clang/include/clang/Serialization/ModuleFile.h --- a/clang/include/clang/Serialization/ModuleFile.h +++ b/clang/include/clang/Serialization/ModuleFile.h @@ -159,7 +159,7 @@ bool DidReadTopLevelSubmodule = false; /// The file entry for the module file. - const FileEntry *File = nullptr; + OptionalFileEntryRefDegradesToFileEntryPtr File; /// The signature of the module file, which may be used instead of the size /// and modification time to identify this particular file. diff --git a/clang/include/clang/Serialization/ModuleManager.h b/clang/include/clang/Serialization/ModuleManager.h --- a/clang/include/clang/Serialization/ModuleManager.h +++ b/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, Optional &File); /// View the graphviz representation of the module graph. void viewGraph(); diff --git a/clang/lib/Basic/Module.cpp b/clang/lib/Basic/Module.cpp --- a/clang/lib/Basic/Module.cpp +++ b/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(); } diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp --- a/clang/lib/Sema/SemaModule.cpp +++ b/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 (Optional FE = M->getASTFile()) Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file) << FE->getName(); Mod = M; diff --git a/clang/lib/Serialization/ModuleManager.cpp b/clang/lib/Serialization/ModuleManager.cpp --- a/clang/lib/Serialization/ModuleManager.cpp +++ b/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; + OptionalFileEntryRefDegradesToFileEntryPtr 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; + Optional &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); + Optional FileOrErr = + expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true, + /*CacheFailure=*/false)); if (!FileOrErr) return false; diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -8400,7 +8400,9 @@ if (!CXMod) return nullptr; Module *Mod = static_cast(CXMod); - return const_cast(Mod->getASTFile()); + if (auto File = Mod->getASTFile()) + return const_cast(&File->getFileEntry()); + return nullptr; } CXModule clang_Module_getParent(CXModule CXMod) { diff --git a/clang/tools/libclang/CXIndexDataConsumer.cpp b/clang/tools/libclang/CXIndexDataConsumer.cpp --- a/clang/tools/libclang/CXIndexDataConsumer.cpp +++ b/clang/tools/libclang/CXIndexDataConsumer.cpp @@ -491,13 +491,12 @@ if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule()) return; - CXIdxImportedASTFileInfo Info = { - static_cast( - const_cast(Mod->getASTFile())), - Mod, - getIndexLoc(ImportD->getLocation()), - ImportD->isImplicit() - }; + FileEntry *FE = nullptr; + if (auto File = Mod->getASTFile()) + FE = const_cast(&File->getFileEntry()); + CXIdxImportedASTFileInfo Info = {static_cast(FE), Mod, + getIndexLoc(ImportD->getLocation()), + ImportD->isImplicit()}; CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info); (void)astFile; }