From: Douglas Gregor Date: Thu, 25 Aug 2011 21:19:59 +0000 (+0000) Subject: Clean up the reloading of identifier information following the load of X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=46c03c3e7482660fcc0c9f461ca4e40da2cda01b;p=clang Clean up the reloading of identifier information following the load of a top-level module. This code is still horrible and should go away, but we're not there yet. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138586 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 86625b7bdf..540b9ef034 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -2540,6 +2540,43 @@ ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) { return Success; } +namespace { + /// \brief Visitor class used to look up identifirs in an AST file. + class IdentifierLookupVisitor { + StringRef Name; + IdentifierInfo *Found; + public: + explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { } + + static bool visit(Module &M, void *UserData) { + IdentifierLookupVisitor *This + = static_cast(UserData); + + ASTIdentifierLookupTable *IdTable + = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; + if (!IdTable) + return false; + + std::pair Key(This->Name.begin(), + This->Name.size()); + ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); + 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->Found = *Pos; + return true; + } + + // \brief Retrieve the identifier info found within the module + // files. + IdentifierInfo *getIdentifierInfo() const { return Found; } + }; +} + + ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, ModuleKind Type) { switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) { @@ -2577,28 +2614,10 @@ ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, IdEnd = PP->getIdentifierTable().end(); Id != IdEnd; ++Id) Identifiers.push_back(Id->second); - // We need to search the tables in all files. - for (ModuleIterator J = ModuleMgr.begin(), - M = ModuleMgr.end(); J != M; ++J) { - ASTIdentifierLookupTable *IdTable - = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable; - // Not all AST files necessarily have identifier tables, only the useful - // ones. - if (!IdTable) - continue; - for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { - IdentifierInfo *II = Identifiers[I]; - // Look in the on-disk hash tables for an entry for this identifier - ASTIdentifierLookupTrait Info(*this, *(*J), II); - std::pair Key(II->getNameStart(),II->getLength()); - ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); - if (Pos == IdTable->end()) - continue; - - // Dereferencing the iterator has the effect of populating the - // IdentifierInfo node with the various declarations it needs. - (void)*Pos; - } + + for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { + IdentifierLookupVisitor Visitor(Identifiers[I]->getName()); + ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor); } } @@ -4467,42 +4486,6 @@ void ASTReader::InitializeSema(Sema &S) { } } -namespace { - /// \brief Visitor class used to look up identifirs in - class IdentifierLookupVisitor { - StringRef Name; - IdentifierInfo *Found; - public: - explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { } - - static bool visit(Module &M, void *UserData) { - IdentifierLookupVisitor *This - = static_cast(UserData); - - ASTIdentifierLookupTable *IdTable - = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; - if (!IdTable) - return false; - - std::pair Key(This->Name.begin(), - This->Name.size()); - ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); - 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->Found = *Pos; - return true; - } - - // \brief Retrieve the identifier info found within the module - // files. - IdentifierInfo *getIdentifierInfo() const { return Found; } - }; -} - IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart)); ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);