From: Douglas Gregor Date: Wed, 23 Jan 2013 18:53:14 +0000 (+0000) Subject: Factor the trait for lookup into the on-based hash table of X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=479633ced74c887ee9ec5905b3cb0bb1a37349b0;p=clang Factor the trait for lookup into the on-based hash table of identifiers into two parts: the part that involves dealing with the key (which can be re-used) and the ASTReader-specific part that creates the IdentifierInfos. While I'm at it, StringRef'ify this code, which was using pair. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173283 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 3acf5b5751..83d894c1d6 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -440,22 +440,22 @@ ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, return Result; } -unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) { - return llvm::HashString(StringRef(a.first, a.second)); +unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { + return llvm::HashString(a); } std::pair -ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) { +ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { using namespace clang::io; unsigned DataLen = ReadUnalignedLE16(d); unsigned KeyLen = ReadUnalignedLE16(d); return std::make_pair(KeyLen, DataLen); } -std::pair -ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) { +ASTIdentifierLookupTraitBase::internal_key_type +ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { assert(n >= 2 && d[n-1] == '\0'); - return std::make_pair((const char*) d, n-1); + return StringRef((const char*) d, n-1); } IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, @@ -474,7 +474,7 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, // and associate it with the persistent ID. IdentifierInfo *II = KnownII; if (!II) { - II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); + II = &Reader.getIdentifierTable().getOwn(k); KnownII = II; } Reader.SetIdentifierInfo(ID, II); @@ -503,7 +503,7 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, // the new IdentifierInfo. IdentifierInfo *II = KnownII; if (!II) { - II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second)); + II = &Reader.getIdentifierTable().getOwn(StringRef(k)); KnownII = II; } Reader.markIdentifierUpToDate(II); @@ -1398,9 +1398,7 @@ namespace { ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, This->Found); - std::pair Key(This->Name.begin(), - This->Name.size()); - ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait); + ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name, &Trait); if (Pos == IdTable->end()) return false; @@ -5745,9 +5743,9 @@ StringRef ASTIdentifierIterator::Next() { // We have any identifiers remaining in the current AST file; return // the next one. - std::pair Key = *Current; + StringRef Result = *Current; ++Current; - return StringRef(Key.first, Key.second); + return Result; } IdentifierIterator *ASTReader::getIdentifiers() const { diff --git a/lib/Serialization/ASTReaderInternals.h b/lib/Serialization/ASTReaderInternals.h index 8e07284d28..dc6ccb72d2 100644 --- a/lib/Serialization/ASTReaderInternals.h +++ b/lib/Serialization/ASTReaderInternals.h @@ -13,9 +13,9 @@ #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H #define LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H -#include "ASTReaderInternals.h" #include "clang/AST/DeclarationName.h" #include "clang/Basic/OnDiskHashTable.h" +#include "clang/Serialization/ASTBitCodes.h" #include "llvm/Support/Endian.h" #include #include @@ -78,8 +78,43 @@ public: unsigned DataLen); }; +/// \brief Base class for the trait describing the on-disk hash table for the +/// identifiers in an AST file. +/// +/// This class is not useful by itself; rather, it provides common +/// functionality for accessing the on-disk hash table of identifiers +/// in an AST file. Different subclasses customize that functionality +/// based on what information they are interested in. Those subclasses +/// must provide the \c data_type typedef and the ReadData operation, +/// only. +class ASTIdentifierLookupTraitBase { +public: + typedef StringRef external_key_type; + typedef StringRef internal_key_type; + + + static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { + return a == b; + } + + static unsigned ComputeHash(const internal_key_type& a); + + static std::pair + ReadKeyDataLength(const unsigned char*& d); + + // This hopefully will just get inlined and removed by the optimizer. + static const internal_key_type& + GetInternalKey(const external_key_type& x) { return x; } + + // This hopefully will just get inlined and removed by the optimizer. + static const external_key_type& + GetExternalKey(const internal_key_type& x) { return x; } + + static internal_key_type ReadKey(const unsigned char* d, unsigned n); +}; + /// \brief Class that performs lookup for an identifier stored in an AST file. -class ASTIdentifierLookupTrait { +class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase { ASTReader &Reader; ModuleFile &F; @@ -91,42 +126,15 @@ class ASTIdentifierLookupTrait { public: typedef IdentifierInfo * data_type; - typedef const std::pair external_key_type; - - typedef external_key_type internal_key_type; - ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F, IdentifierInfo *II = 0) : Reader(Reader), F(F), KnownII(II) { } - - static bool EqualKey(const internal_key_type& a, - const internal_key_type& b) { - return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 - : false; - } - - static unsigned ComputeHash(const internal_key_type& a); - - // This hopefully will just get inlined and removed by the optimizer. - static const internal_key_type& - GetInternalKey(const external_key_type& x) { return x; } - - // This hopefully will just get inlined and removed by the optimizer. - static const external_key_type& - GetExternalKey(const internal_key_type& x) { return x; } - - static std::pair - ReadKeyDataLength(const unsigned char*& d); - - static std::pair - ReadKey(const unsigned char* d, unsigned n); - - IdentifierInfo *ReadData(const internal_key_type& k, - const unsigned char* d, - unsigned DataLen); + + data_type ReadData(const internal_key_type& k, + const unsigned char* d, + unsigned DataLen); ASTReader &getReader() const { return Reader; } - }; /// \brief The on-disk hash table used to contain information about