From: Benjamin Kramer Date: Sat, 25 Jul 2015 12:14:04 +0000 (+0000) Subject: [Modules] Wrap the main ModuleManager visitor in a function_ref. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6a286985e5f705550d467b5c9904581d367d3b80;p=clang [Modules] Wrap the main ModuleManager visitor in a function_ref. Avoids the awkward passing of an opaque void *UserData argument. No functional change intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243213 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Serialization/ModuleManager.h b/include/clang/Serialization/ModuleManager.h index abd5da5422..bf3525a250 100644 --- a/include/clang/Serialization/ModuleManager.h +++ b/include/clang/Serialization/ModuleManager.h @@ -269,19 +269,15 @@ public: /// operations that can find data in any of the loaded modules. /// /// \param Visitor A visitor function that will be invoked with each - /// module and the given user data pointer. The return value must be - /// convertible to bool; when false, the visitation continues to - /// modules that the current module depends on. When true, the - /// visitation skips any modules that the current module depends on. - /// - /// \param UserData User data associated with the visitor object, which - /// will be passed along to the visitor. + /// module. The return value must be convertible to bool; when false, the + /// visitation continues to modules that the current module depends on. When + /// true, the visitation skips any modules that the current module depends on. /// /// \param ModuleFilesHit If non-NULL, contains the set of module files /// that we know we need to visit because the global module index told us to. /// Any module that is known to both the global module index and the module /// manager that is *not* in this set can be skipped. - void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData, + void visit(llvm::function_ref Visitor, llvm::SmallPtrSetImpl *ModuleFilesHit = nullptr); /// \brief Control DFS behavior during preorder visitation. diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 1bb5c3ff27..76a8cde885 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -2782,39 +2782,29 @@ bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { return true; } -namespace { -struct PCHLocatorInfo { - serialization::ModuleFile *Mod; - PCHLocatorInfo() : Mod(nullptr) {} -}; -} - -static bool PCHLocator(serialization::ModuleFile &M, void *UserData) { - PCHLocatorInfo &Info = *static_cast(UserData); - switch (M.Kind) { - case serialization::MK_ImplicitModule: - case serialization::MK_ExplicitModule: - return true; // skip dependencies. - case serialization::MK_PCH: - Info.Mod = &M; - return true; // found it. - case serialization::MK_Preamble: - return false; // look in dependencies. - case serialization::MK_MainFile: - return false; // look in dependencies. - } - - return true; -} - const FileEntry *ASTUnit::getPCHFile() { if (!Reader) return nullptr; - PCHLocatorInfo Info; - Reader->getModuleManager().visit(PCHLocator, &Info); - if (Info.Mod) - return Info.Mod->File; + serialization::ModuleFile *Mod = nullptr; + Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) { + switch (M.Kind) { + case serialization::MK_ImplicitModule: + case serialization::MK_ExplicitModule: + return true; // skip dependencies. + case serialization::MK_PCH: + Mod = &M; + return true; // found it. + case serialization::MK_Preamble: + return false; // look in dependencies. + case serialization::MK_MainFile: + return false; // look in dependencies. + } + + return true; + }); + if (Mod) + return Mod->File; return nullptr; } diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 73985fe228..520c3418b2 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -1658,33 +1658,30 @@ namespace { Found() { } - - static bool visit(ModuleFile &M, void *UserData) { - IdentifierLookupVisitor *This - = static_cast(UserData); - + + bool operator()(ModuleFile &M) { // If we've already searched this module file, skip it now. - if (M.Generation <= This->PriorGeneration) + if (M.Generation <= this->PriorGeneration) return true; ASTIdentifierLookupTable *IdTable = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; if (!IdTable) return false; - - ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), - M, This->Found); - ++This->NumIdentifierLookups; + + ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, + this->Found); + ++this->NumIdentifierLookups; ASTIdentifierLookupTable::iterator Pos = - IdTable->find_hashed(This->Name, This->NameHash, &Trait); + IdTable->find_hashed(this->Name, this->NameHash, &Trait); if (Pos == IdTable->end()) return false; // Dereferencing the iterator has the effect of building the // IdentifierInfo node and populating it with the various // declarations it needs. - ++This->NumIdentifierLookupHits; - This->Found = *Pos; + ++this->NumIdentifierLookupHits; + this->Found = *Pos; return true; } @@ -1715,7 +1712,7 @@ void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, NumIdentifierLookups, NumIdentifierLookupHits); - ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); + ModuleMgr.visit(Visitor, HitsPtr); markIdentifierUpToDate(&II); } @@ -4866,22 +4863,19 @@ namespace { public: explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) { } - - static bool visit(ModuleFile &M, void *UserData) { - HeaderFileInfoVisitor *This - = static_cast(UserData); - + + bool operator()(ModuleFile &M) { HeaderFileInfoLookupTable *Table = static_cast(M.HeaderFileInfoTable); if (!Table) return false; // Look in the on-disk hash table for an entry for this file name. - HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE); + HeaderFileInfoLookupTable::iterator Pos = Table->find(this->FE); if (Pos == Table->end()) return false; - This->HFI = *Pos; + this->HFI = *Pos; return true; } @@ -4891,7 +4885,7 @@ namespace { HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { HeaderFileInfoVisitor Visitor(FE); - ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor); + ModuleMgr.visit(Visitor); if (Optional HFI = Visitor.getHeaderFileInfo()) return *HFI; @@ -6372,22 +6366,18 @@ namespace { ModuleFile *Definitive; if (Contexts.size() == 1 && (Definitive = getDefinitiveModuleFileFor(Contexts[0], Reader))) { - visit(*Definitive, this); + (*this)(*Definitive); } else { - Reader.getModuleManager().visit(&visit, this); + Reader.getModuleManager().visit(*this); } } - private: - static bool visit(ModuleFile &M, void *UserData) { - DeclContextNameLookupVisitor *This - = static_cast(UserData); - + bool operator()(ModuleFile &M) { // Check whether we have any visible declaration information for // this context in this module. ModuleFile::DeclContextInfosMap::iterator Info; bool FoundInfo = false; - for (auto *DC : This->Contexts) { + for (auto *DC : this->Contexts) { Info = M.DeclContextInfos.find(DC); if (Info != M.DeclContextInfos.end() && Info->second.NameLookupTableData) { @@ -6402,19 +6392,19 @@ namespace { // Look for this name within this module. ASTDeclContextNameLookupTable *LookupTable = Info->second.NameLookupTableData; - ASTDeclContextNameLookupTable::iterator Pos - = LookupTable->find_hashed(This->NameKey, This->NameHash); + ASTDeclContextNameLookupTable::iterator Pos = + LookupTable->find_hashed(this->NameKey, this->NameHash); if (Pos == LookupTable->end()) return false; bool FoundAnything = false; ASTDeclContextNameLookupTrait::data_type Data = *Pos; for (; Data.first != Data.second; ++Data.first) { - NamedDecl *ND = This->Reader.GetLocalDeclAs(M, *Data.first); + NamedDecl *ND = this->Reader.GetLocalDeclAs(M, *Data.first); if (!ND) continue; - if (ND->getDeclName() != This->Name) { + if (ND->getDeclName() != this->Name) { // A name might be null because the decl's redeclarable part is // currently read before reading its name. The lookup is triggered by // building that decl (likely indirectly), and so it is later in the @@ -6426,8 +6416,8 @@ namespace { // Record this declaration. FoundAnything = true; - if (This->DeclSet.insert(ND).second) - This->Decls.push_back(ND); + if (this->DeclSet.insert(ND).second) + this->Decls.push_back(ND); } return FoundAnything; @@ -6504,16 +6494,13 @@ namespace { DeclsMap &Decls, bool VisitAll) : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { } - static bool visit(ModuleFile &M, void *UserData) { - DeclContextAllNamesVisitor *This - = static_cast(UserData); - + bool operator()(ModuleFile &M) { // Check whether we have any visible declaration information for // this context in this module. ModuleFile::DeclContextInfosMap::iterator Info; bool FoundInfo = false; - for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) { - Info = M.DeclContextInfos.find(This->Contexts[I]); + for (unsigned I = 0, N = this->Contexts.size(); I != N; ++I) { + Info = M.DeclContextInfos.find(this->Contexts[I]); if (Info != M.DeclContextInfos.end() && Info->second.NameLookupTableData) { FoundInfo = true; @@ -6533,19 +6520,19 @@ namespace { ++I) { ASTDeclContextNameLookupTrait::data_type Data = *I; for (; Data.first != Data.second; ++Data.first) { - NamedDecl *ND = This->Reader.GetLocalDeclAs(M, - *Data.first); + NamedDecl *ND = + this->Reader.GetLocalDeclAs(M, *Data.first); if (!ND) continue; // Record this declaration. FoundAnything = true; - if (This->DeclSet.insert(ND).second) - This->Decls[ND->getDeclName()].push_back(ND); + if (this->DeclSet.insert(ND).second) + this->Decls[ND->getDeclName()].push_back(ND); } } - return FoundAnything && !This->VisitAll; + return FoundAnything && !this->VisitAll; } }; } @@ -6573,7 +6560,7 @@ void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls, /*VisitAll=*/DC->isFileContext()); - ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor); + ModuleMgr.visit(Visitor); ++NumVisibleDeclContextsRead; for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { @@ -6859,7 +6846,7 @@ IdentifierInfo *ASTReader::get(StringRef Name) { // a complete initial identifier table if we're carrying on from a PCH. if (Context.getLangOpts().CPlusPlus) { for (auto F : ModuleMgr.pch_modules()) - if (Visitor.visit(*F, &Visitor)) + if (Visitor(*F)) break; } else { // If there is a global index, look there first to determine which modules @@ -6872,7 +6859,7 @@ IdentifierInfo *ASTReader::get(StringRef Name) { } } - ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr); + ModuleMgr.visit(Visitor, HitsPtr); } IdentifierInfo *II = Visitor.getIdentifierInfo(); @@ -6961,41 +6948,37 @@ namespace clang { namespace serialization { InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false), FactoryHasMoreThanOneDecl(false) {} - static bool visit(ModuleFile &M, void *UserData) { - ReadMethodPoolVisitor *This - = static_cast(UserData); - + bool operator()(ModuleFile &M) { if (!M.SelectorLookupTable) return false; // If we've already searched this module file, skip it now. - if (M.Generation <= This->PriorGeneration) + if (M.Generation <= this->PriorGeneration) return true; - ++This->Reader.NumMethodPoolTableLookups; + ++this->Reader.NumMethodPoolTableLookups; ASTSelectorLookupTable *PoolTable = (ASTSelectorLookupTable*)M.SelectorLookupTable; - ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel); + ASTSelectorLookupTable::iterator Pos = PoolTable->find(this->Sel); if (Pos == PoolTable->end()) return false; - ++This->Reader.NumMethodPoolTableHits; - ++This->Reader.NumSelectorsRead; + ++this->Reader.NumMethodPoolTableHits; + ++this->Reader.NumSelectorsRead; // FIXME: Not quite happy with the statistics here. We probably should // disable this tracking when called via LoadSelector. // Also, should entries without methods count as misses? - ++This->Reader.NumMethodPoolEntriesRead; + ++this->Reader.NumMethodPoolEntriesRead; ASTSelectorLookupTrait::data_type Data = *Pos; - if (This->Reader.DeserializationListener) - This->Reader.DeserializationListener->SelectorRead(Data.ID, - This->Sel); - - This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); - This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); - This->InstanceBits = Data.InstanceBits; - This->FactoryBits = Data.FactoryBits; - This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; - This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; + if (this->Reader.DeserializationListener) + this->Reader.DeserializationListener->SelectorRead(Data.ID, this->Sel); + + this->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); + this->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); + this->InstanceBits = Data.InstanceBits; + this->FactoryBits = Data.FactoryBits; + this->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; + this->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; return true; } @@ -7035,8 +7018,8 @@ void ASTReader::ReadMethodPool(Selector Sel) { // Search for methods defined with this selector. ++NumMethodPoolLookups; ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); - ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor); - + ModuleMgr.visit(Visitor); + if (Visitor.getInstanceMethods().empty() && Visitor.getFactoryMethods().empty()) return; diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 3dff7029a5..f6858b4a5b 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -3426,10 +3426,6 @@ namespace { assert(std::is_sorted(SearchDecls.begin(), SearchDecls.end())); } - static bool visit(ModuleFile &M, void *UserData) { - return static_cast(UserData)->visit(M); - } - /// Get the chain, in order from newest to oldest. ArrayRef getChain() const { return Chain; @@ -3457,7 +3453,8 @@ namespace { return Result->Offset; } - bool visit(ModuleFile &M) { + public: + bool operator()(ModuleFile &M) { llvm::ArrayRef ToSearch = SearchDecls; GlobalDeclID LocalSearchDeclID = 0; @@ -3542,7 +3539,7 @@ void ASTReader::loadPendingDeclChain(Decl *CanonDecl) { // Build up the list of redeclarations. RedeclChainVisitor Visitor(*this, SearchDecls, RedeclsDeserialized, CanonID); - ModuleMgr.visit(&RedeclChainVisitor::visit, &Visitor); + ModuleMgr.visit(Visitor); // Retrieve the chains. ArrayRef Chain = Visitor.getChain(); @@ -3635,11 +3632,7 @@ namespace { } } - static bool visit(ModuleFile &M, void *UserData) { - return static_cast(UserData)->visit(M); - } - - bool visit(ModuleFile &M) { + bool operator()(ModuleFile &M) { // If we've loaded all of the category information we care about from // this module file, we're done. if (M.Generation <= PreviousGeneration) @@ -3684,7 +3677,7 @@ void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID, unsigned PreviousGeneration) { ObjCCategoriesVisitor Visitor(*this, ID, D, CategoriesDeserialized, PreviousGeneration); - ModuleMgr.visit(ObjCCategoriesVisitor::visit, &Visitor); + ModuleMgr.visit(Visitor); } template diff --git a/lib/Serialization/ModuleManager.cpp b/lib/Serialization/ModuleManager.cpp index dbdd94549e..406c5c0a38 100644 --- a/lib/Serialization/ModuleManager.cpp +++ b/lib/Serialization/ModuleManager.cpp @@ -313,10 +313,8 @@ ModuleManager::~ModuleManager() { delete FirstVisitState; } -void -ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), - void *UserData, - llvm::SmallPtrSetImpl *ModuleFilesHit) { +void ModuleManager::visit(llvm::function_ref Visitor, + llvm::SmallPtrSetImpl *ModuleFilesHit) { // If the visitation order vector is the wrong size, recompute the order. if (VisitOrder.size() != Chain.size()) { unsigned N = size(); @@ -388,7 +386,7 @@ ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), // Visit the module. assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); State->VisitNumber[CurrentModule->Index] = VisitNumber; - if (!Visitor(*CurrentModule, UserData)) + if (!Visitor(*CurrentModule)) continue; // The visitor has requested that cut off visitation of any