Author: Duncan P. N. Exon Smith Date: 2020-11-02T15:11:51-05:00 New Revision: 9f151df17800e1668c32e5314a290ae94c8f2dd3
URL: https://github.com/llvm/llvm-project/commit/9f151df17800e1668c32e5314a290ae94c8f2dd3 DIFF: https://github.com/llvm/llvm-project/commit/9f151df17800e1668c32e5314a290ae94c8f2dd3.diff LOG: Change Module::ASTFile and ModuleFile::File => Optional<FileEntryRef>, NFC Change `Module::ASTFile` and `ModuleFile::File` to use `Optional<FileEntryRef>` instead of `const FileEntry *`. One of many steps toward removing `FileEntry::getName`. Differential Revision: https://reviews.llvm.org/D89836 Added: Modified: clang/include/clang/Basic/FileEntry.h clang/include/clang/Basic/Module.h clang/include/clang/Serialization/ModuleFile.h clang/include/clang/Serialization/ModuleManager.h clang/lib/Basic/Module.cpp clang/lib/Sema/SemaModule.cpp clang/lib/Serialization/ModuleManager.cpp clang/tools/libclang/CIndex.cpp clang/tools/libclang/CXIndexDataConsumer.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/FileEntry.h b/clang/include/clang/Basic/FileEntry.h index 7f5efb1c48f3..e4c2e7e34db9 100644 --- a/clang/include/clang/Basic/FileEntry.h +++ b/clang/include/clang/Basic/FileEntry.h @@ -64,6 +64,7 @@ class FileEntryRef { 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 @@ time_t FileEntryRef::getModificationTime() const { 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 index ac33c7573f35..5a4975f25b72 100644 --- 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 @@ class Module { /// 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<FileEntryRef> ASTFile; /// The top-level headers associated with this module. llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; @@ -529,14 +530,14 @@ class Module { } /// 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<FileEntryRef> 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 index 598e61210702..a309c1143350 100644 --- a/clang/include/clang/Serialization/ModuleFile.h +++ b/clang/include/clang/Serialization/ModuleFile.h @@ -159,7 +159,7 @@ class ModuleFile { 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 index 15ddb9875ff1..7081eedad4b4 100644 --- a/clang/include/clang/Serialization/ModuleManager.h +++ b/clang/include/clang/Serialization/ModuleManager.h @@ -307,10 +307,8 @@ class ModuleManager { /// \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<FileEntryRef> &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 index b25248de6832..68c1d0f77f22 100644 --- a/clang/lib/Basic/Module.cpp +++ b/clang/lib/Basic/Module.cpp @@ -671,7 +671,7 @@ ASTSourceDescriptor::ASTSourceDescriptor(Module &M) : 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 index 10de0ca91221..af95b1a93cc4 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -187,7 +187,7 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, 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<FileEntryRef> 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 index 2c65c0be4074..40ffa6cfee8f 100644 --- a/clang/lib/Serialization/ModuleManager.cpp +++ b/clang/lib/Serialization/ModuleManager.cpp @@ -112,7 +112,7 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type, // Look for the file entry. This only fails if the expected size or // modification time diff er. - 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 diff erent mtime due to being moved across filesystems in @@ -288,7 +288,7 @@ void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) { if (modMap) { StringRef ModuleName = victim->ModuleName; if (Module *mod = modMap->findModule(ModuleName)) { - mod->setASTFile(nullptr); + mod->setASTFile(None); } } } @@ -458,18 +458,18 @@ void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 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<FileEntryRef> &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<FileEntryRef> 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 index cbfaf173b2c6..273b579754ae 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -8400,7 +8400,9 @@ CXFile clang_Module_getASTFile(CXModule CXMod) { if (!CXMod) return nullptr; Module *Mod = static_cast<Module *>(CXMod); - return const_cast<FileEntry *>(Mod->getASTFile()); + if (auto File = Mod->getASTFile()) + return const_cast<FileEntry *>(&File->getFileEntry()); + return nullptr; } CXModule clang_Module_getParent(CXModule CXMod) { diff --git a/clang/tools/libclang/CXIndexDataConsumer.cpp b/clang/tools/libclang/CXIndexDataConsumer.cpp index fb04a06f8ae7..2f892fd1a43f 100644 --- a/clang/tools/libclang/CXIndexDataConsumer.cpp +++ b/clang/tools/libclang/CXIndexDataConsumer.cpp @@ -491,13 +491,12 @@ void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) { if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule()) return; - CXIdxImportedASTFileInfo Info = { - static_cast<CXFile>( - const_cast<FileEntry *>(Mod->getASTFile())), - Mod, - getIndexLoc(ImportD->getLocation()), - ImportD->isImplicit() - }; + FileEntry *FE = nullptr; + if (auto File = Mod->getASTFile()) + FE = const_cast<FileEntry *>(&File->getFileEntry()); + CXIdxImportedASTFileInfo Info = {static_cast<CXFile>(FE), Mod, + getIndexLoc(ImportD->getLocation()), + ImportD->isImplicit()}; CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info); (void)astFile; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits