Modules: Return ModuleFile& from ModuleManager::begin, etc.; NFC

Hide the pointer indirection in ModuleManager::begin, ModuleManager::end,
ModuleManager::rbegin, and ModuleManager::rend.  Besides tidying up the call
sites, this is preparation for making ownership of ModuleFile explicit.

llvm-svn: 293394
This commit is contained in:
Duncan P. N. Exon Smith 2017-01-28 22:15:22 +00:00
parent 14afc8e7b8
commit 96a06e0ec0
5 changed files with 60 additions and 56 deletions

View File

@ -1633,7 +1633,7 @@ public:
unsigned Result = 0; unsigned Result = 0;
for (ModuleConstIterator I = ModuleMgr.begin(), for (ModuleConstIterator I = ModuleMgr.begin(),
E = ModuleMgr.end(); I != E; ++I) { E = ModuleMgr.end(); I != E; ++I) {
Result += (*I)->NumPreprocessedEntities; Result += I->NumPreprocessedEntities;
} }
return Result; return Result;

View File

@ -19,6 +19,7 @@
#include "clang/Serialization/Module.h" #include "clang/Serialization/Module.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/iterator.h"
namespace clang { namespace clang {
@ -111,9 +112,13 @@ class ModuleManager {
void returnVisitState(VisitState *State); void returnVisitState(VisitState *State);
public: public:
typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator; typedef llvm::pointee_iterator<SmallVectorImpl<ModuleFile *>::iterator>
typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator; ModuleIterator;
typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator; typedef llvm::pointee_iterator<SmallVectorImpl<ModuleFile *>::const_iterator>
ModuleConstIterator;
typedef llvm::pointee_iterator<
SmallVectorImpl<ModuleFile *>::reverse_iterator>
ModuleReverseIterator;
typedef std::pair<uint32_t, StringRef> ModuleOffset; typedef std::pair<uint32_t, StringRef> ModuleOffset;
explicit ModuleManager(FileManager &FileMgr, explicit ModuleManager(FileManager &FileMgr,
@ -136,7 +141,8 @@ public:
ModuleReverseIterator rend() { return Chain.rend(); } ModuleReverseIterator rend() { return Chain.rend(); }
/// \brief A range covering the PCH and preamble module files loaded. /// \brief A range covering the PCH and preamble module files loaded.
llvm::iterator_range<ModuleConstIterator> pch_modules() const { llvm::iterator_range<SmallVectorImpl<ModuleFile *>::const_iterator>
pch_modules() const {
return llvm::make_range(PCHChain.begin(), PCHChain.end()); return llvm::make_range(PCHChain.begin(), PCHChain.end());
} }

View File

@ -482,7 +482,7 @@ bool PCHValidator::ReadDiagnosticOptions(
// Note: ModuleMgr.rbegin() may not be the current module, but it must be in // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
// the transitive closure of its imports, since unrelated modules cannot be // the transitive closure of its imports, since unrelated modules cannot be
// imported until after this module finishes validation. // imported until after this module finishes validation.
ModuleFile *TopImport = *ModuleMgr.rbegin(); ModuleFile *TopImport = &*ModuleMgr.rbegin();
while (!TopImport->ImportedBy.empty()) while (!TopImport->ImportedBy.empty())
TopImport = TopImport->ImportedBy[0]; TopImport = TopImport->ImportedBy[0];
if (TopImport->Kind != MK_ImplicitModule) if (TopImport->Kind != MK_ImplicitModule)
@ -1713,15 +1713,15 @@ void ASTReader::ReadDefinedMacros() {
// Note that we are loading defined macros. // Note that we are loading defined macros.
Deserializing Macros(this); Deserializing Macros(this);
for (auto &I : llvm::reverse(ModuleMgr)) { for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
BitstreamCursor &MacroCursor = I->MacroCursor; BitstreamCursor &MacroCursor = I.MacroCursor;
// If there was no preprocessor block, skip this file. // If there was no preprocessor block, skip this file.
if (MacroCursor.getBitcodeBytes().empty()) if (MacroCursor.getBitcodeBytes().empty())
continue; continue;
BitstreamCursor Cursor = MacroCursor; BitstreamCursor Cursor = MacroCursor;
Cursor.JumpToBit(I->MacroStartOffset); Cursor.JumpToBit(I.MacroStartOffset);
RecordData Record; RecordData Record;
while (true) { while (true) {
@ -1743,7 +1743,7 @@ void ASTReader::ReadDefinedMacros() {
case PP_MACRO_OBJECT_LIKE: case PP_MACRO_OBJECT_LIKE:
case PP_MACRO_FUNCTION_LIKE: { case PP_MACRO_FUNCTION_LIKE: {
IdentifierInfo *II = getLocalIdentifier(*I, Record[0]); IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
if (II->isOutOfDate()) if (II->isOutOfDate())
updateOutOfDateIdentifier(*II); updateOutOfDateIdentifier(*II);
break; break;
@ -3351,8 +3351,7 @@ ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
// usable header search context. // usable header search context.
assert(!F.ModuleName.empty() && assert(!F.ModuleName.empty() &&
"MODULE_NAME should come before MODULE_MAP_FILE"); "MODULE_NAME should come before MODULE_MAP_FILE");
if (F.Kind == MK_ImplicitModule && if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
(*ModuleMgr.begin())->Kind != MK_MainFile) {
// An implicitly-loaded module file should have its module listed in some // An implicitly-loaded module file should have its module listed in some
// module map file that we've already loaded. // module map file that we've already loaded.
Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
@ -5300,8 +5299,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
using DiagState = DiagnosticsEngine::DiagState; using DiagState = DiagnosticsEngine::DiagState;
SmallVector<DiagState *, 32> DiagStates; SmallVector<DiagState *, 32> DiagStates;
for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { for (ModuleFile &F : ModuleMgr) {
ModuleFile &F = *(*I);
unsigned Idx = 0; unsigned Idx = 0;
auto &Record = F.PragmaDiagMappings; auto &Record = F.PragmaDiagMappings;
if (Record.empty()) if (Record.empty())
@ -7150,18 +7148,15 @@ LLVM_DUMP_METHOD void ASTReader::dump() {
GlobalPreprocessedEntityMap); GlobalPreprocessedEntityMap);
llvm::errs() << "\n*** PCH/Modules Loaded:"; llvm::errs() << "\n*** PCH/Modules Loaded:";
for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), for (ModuleFile &M : ModuleMgr)
MEnd = ModuleMgr.end(); M.dump();
M != MEnd; ++M)
(*M)->dump();
} }
/// Return the amount of memory used by memory buffers, breaking down /// Return the amount of memory used by memory buffers, breaking down
/// by heap-backed versus mmap'ed memory. /// by heap-backed versus mmap'ed memory.
void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
for (ModuleConstIterator I = ModuleMgr.begin(), for (ModuleFile &I : ModuleMgr) {
E = ModuleMgr.end(); I != E; ++I) { if (llvm::MemoryBuffer *buf = I.Buffer.get()) {
if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
size_t bytes = buf->getBufferSize(); size_t bytes = buf->getBufferSize();
switch (buf->getBufferKind()) { switch (buf->getBufferKind()) {
case llvm::MemoryBuffer::MemoryBuffer_Malloc: case llvm::MemoryBuffer::MemoryBuffer_Malloc:

View File

@ -1435,17 +1435,17 @@ uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
serialization::ModuleManager &Mgr = Chain->getModuleManager(); serialization::ModuleManager &Mgr = Chain->getModuleManager();
Record.clear(); Record.clear();
for (auto *M : Mgr) { for (ModuleFile &M : Mgr) {
// Skip modules that weren't directly imported. // Skip modules that weren't directly imported.
if (!M->isDirectlyImported()) if (!M.isDirectlyImported())
continue; continue;
Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
AddSourceLocation(M->ImportLoc, Record); AddSourceLocation(M.ImportLoc, Record);
Record.push_back(M->File->getSize()); Record.push_back(M.File->getSize());
Record.push_back(getTimestampForOutput(M->File)); Record.push_back(getTimestampForOutput(M.File));
Record.push_back(M->Signature); Record.push_back(M.Signature);
AddPath(M->FileName, Record); AddPath(M.FileName, Record);
} }
Stream.EmitRecord(IMPORTS, Record); Stream.EmitRecord(IMPORTS, Record);
} }
@ -4605,10 +4605,10 @@ uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
SmallString<2048> Buffer; SmallString<2048> Buffer;
{ {
llvm::raw_svector_ostream Out(Buffer); llvm::raw_svector_ostream Out(Buffer);
for (ModuleFile *M : Chain->ModuleMgr) { for (ModuleFile &M : Chain->ModuleMgr) {
using namespace llvm::support; using namespace llvm::support;
endian::Writer<little> LE(Out); endian::Writer<little> LE(Out);
StringRef FileName = M->FileName; StringRef FileName = M.FileName;
LE.write<uint16_t>(FileName.size()); LE.write<uint16_t>(FileName.size());
Out.write(FileName.data(), FileName.size()); Out.write(FileName.data(), FileName.size());
@ -4626,15 +4626,15 @@ uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
// These values should be unique within a chain, since they will be read // These values should be unique within a chain, since they will be read
// as keys into ContinuousRangeMaps. // as keys into ContinuousRangeMaps.
writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries); writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers); writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros); writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
writeBaseIDOrNone(M->BasePreprocessedEntityID, writeBaseIDOrNone(M.BasePreprocessedEntityID,
M->NumPreprocessedEntities); M.NumPreprocessedEntities);
writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules); writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors); writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls); writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes); writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
} }
} }
RecordData::value_type Record[] = {MODULE_OFFSET_MAP}; RecordData::value_type Record[] = {MODULE_OFFSET_MAP};

View File

@ -195,7 +195,9 @@ void ModuleManager::removeModules(
VisitOrder.clear(); VisitOrder.clear();
// Collect the set of module file pointers that we'll be removing. // Collect the set of module file pointers that we'll be removing.
llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
(llvm::pointer_iterator<ModuleIterator>(first)),
(llvm::pointer_iterator<ModuleIterator>(last)));
auto IsVictim = [&](ModuleFile *MF) { auto IsVictim = [&](ModuleFile *MF) {
return victimSet.count(MF); return victimSet.count(MF);
@ -209,8 +211,8 @@ void ModuleManager::removeModules(
// Remove the modules from the PCH chain. // Remove the modules from the PCH chain.
for (auto I = first; I != last; ++I) { for (auto I = first; I != last; ++I) {
if (!(*I)->isModule()) { if (!I->isModule()) {
PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I), PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I),
PCHChain.end()); PCHChain.end());
break; break;
} }
@ -218,10 +220,10 @@ void ModuleManager::removeModules(
// Delete the modules and erase them from the various structures. // Delete the modules and erase them from the various structures.
for (ModuleIterator victim = first; victim != last; ++victim) { for (ModuleIterator victim = first; victim != last; ++victim) {
Modules.erase((*victim)->File); Modules.erase(victim->File);
if (modMap) { if (modMap) {
StringRef ModuleName = (*victim)->ModuleName; StringRef ModuleName = victim->ModuleName;
if (Module *mod = modMap->findModule(ModuleName)) { if (Module *mod = modMap->findModule(ModuleName)) {
mod->setASTFile(nullptr); mod->setASTFile(nullptr);
} }
@ -230,14 +232,15 @@ void ModuleManager::removeModules(
// Files that didn't make it through ReadASTCore successfully will be // Files that didn't make it through ReadASTCore successfully will be
// rebuilt (or there was an error). Invalidate them so that we can load the // rebuilt (or there was an error). Invalidate them so that we can load the
// new files that will be renamed over the old ones. // new files that will be renamed over the old ones.
if (LoadedSuccessfully.count(*victim) == 0) if (LoadedSuccessfully.count(&*victim) == 0)
FileMgr.invalidateCache((*victim)->File); FileMgr.invalidateCache(victim->File);
delete *victim; delete &*victim;
} }
// Remove the modules from the chain. // Remove the modules from the chain.
Chain.erase(first, last); Chain.erase(Chain.begin() + (first - begin()),
Chain.begin() + (last - begin()));
} }
void void
@ -317,11 +320,11 @@ void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
Queue.reserve(N); Queue.reserve(N);
llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
UnusedIncomingEdges.resize(size()); UnusedIncomingEdges.resize(size());
for (ModuleFile *M : llvm::reverse(*this)) { for (ModuleFile &M : llvm::reverse(*this)) {
unsigned Size = M->ImportedBy.size(); unsigned Size = M.ImportedBy.size();
UnusedIncomingEdges[M->Index] = Size; UnusedIncomingEdges[M.Index] = Size;
if (!Size) if (!Size)
Queue.push_back(M); Queue.push_back(&M);
} }
// Traverse the graph, making sure to visit a module before visiting any // Traverse the graph, making sure to visit a module before visiting any
@ -436,7 +439,7 @@ namespace llvm {
struct GraphTraits<ModuleManager> { struct GraphTraits<ModuleManager> {
typedef ModuleFile *NodeRef; typedef ModuleFile *NodeRef;
typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
typedef ModuleManager::ModuleConstIterator nodes_iterator; typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator;
static ChildIteratorType child_begin(NodeRef Node) { static ChildIteratorType child_begin(NodeRef Node) {
return Node->Imports.begin(); return Node->Imports.begin();
@ -447,11 +450,11 @@ namespace llvm {
} }
static nodes_iterator nodes_begin(const ModuleManager &Manager) { static nodes_iterator nodes_begin(const ModuleManager &Manager) {
return Manager.begin(); return nodes_iterator(Manager.begin());
} }
static nodes_iterator nodes_end(const ModuleManager &Manager) { static nodes_iterator nodes_end(const ModuleManager &Manager) {
return Manager.end(); return nodes_iterator(Manager.end());
} }
}; };