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
This commit is contained in:
Duncan P. N. Exon Smith 2020-10-20 18:11:52 -04:00
parent c29513f7e0
commit 9f151df178
9 changed files with 31 additions and 28 deletions

View File

@ -64,6 +64,7 @@ public:
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

View File

@ -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 @@ private:
/// 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 @@ public:
}
/// 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;
}

View File

@ -159,7 +159,7 @@ public:
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.

View File

@ -307,10 +307,8 @@ public:
/// \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();

View File

@ -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();
}

View File

@ -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;

View File

@ -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 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 @@ 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;

View File

@ -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) {

View File

@ -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;
}