From 87f9d81d0ab806dcf6ca50a0c068dcb2ba7f51b3 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Wed, 17 Apr 2013 22:10:55 +0000 Subject: [PATCH] [Modules] Use global index to improve typo correction performance Typo correction for an unqualified name needs to walk through all of the identifier tables of all modules. When we have a global index, just walk its identifier table only. rdar://13425732 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179730 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/IdentifierTable.h | 2 +- include/clang/Serialization/ASTReader.h | 2 +- .../clang/Serialization/GlobalModuleIndex.h | 5 +++ lib/Basic/IdentifierTable.cpp | 2 +- lib/Serialization/ASTReader.cpp | 5 ++- lib/Serialization/GlobalModuleIndex.cpp | 31 +++++++++++++++++++ 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/clang/Basic/IdentifierTable.h b/include/clang/Basic/IdentifierTable.h index c04a893c6f..d4d53390bd 100644 --- a/include/clang/Basic/IdentifierTable.h +++ b/include/clang/Basic/IdentifierTable.h @@ -394,7 +394,7 @@ public: /// /// \returns A new iterator into the set of known identifiers. The /// caller is responsible for deleting this iterator. - virtual IdentifierIterator *getIdentifiers() const; + virtual IdentifierIterator *getIdentifiers(); }; /// \brief An abstract class used to resolve numerical identifier diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index 925533678c..a773e41667 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -1558,7 +1558,7 @@ public: /// \brief Retrieve an iterator into the set of all identifiers /// in all loaded AST files. - virtual IdentifierIterator *getIdentifiers() const; + virtual IdentifierIterator *getIdentifiers(); /// \brief Load the contents of the global method pool for a given /// selector. diff --git a/include/clang/Serialization/GlobalModuleIndex.h b/include/clang/Serialization/GlobalModuleIndex.h index eaf26d1df1..ab91f4009c 100644 --- a/include/clang/Serialization/GlobalModuleIndex.h +++ b/include/clang/Serialization/GlobalModuleIndex.h @@ -146,6 +146,11 @@ public: static std::pair readIndex(StringRef Path); + /// \brief Returns an iterator for identifiers stored in the index table. + /// + /// The caller accepts ownership of the returned object. + IdentifierIterator *createIdentifierIterator() const; + /// \brief Retrieve the set of modules that have up-to-date indexes. /// /// \param ModuleFiles Will be populated with the set of module files that diff --git a/lib/Basic/IdentifierTable.cpp b/lib/Basic/IdentifierTable.cpp index 429d9d8cb2..951c718d18 100644 --- a/lib/Basic/IdentifierTable.cpp +++ b/lib/Basic/IdentifierTable.cpp @@ -65,7 +65,7 @@ namespace { }; } -IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const { +IdentifierIterator *IdentifierInfoLookup::getIdentifiers() { return new EmptyLookupIterator(); } diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 9893823a12..5123b79d2e 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -6109,7 +6109,10 @@ StringRef ASTIdentifierIterator::Next() { return Result; } -IdentifierIterator *ASTReader::getIdentifiers() const { +IdentifierIterator *ASTReader::getIdentifiers() { + if (!loadGlobalIndex()) + return GlobalIndex->createIdentifierIterator(); + return new ASTIdentifierIterator(*this); } diff --git a/lib/Serialization/GlobalModuleIndex.cpp b/lib/Serialization/GlobalModuleIndex.cpp index f9acb84728..b6693e40e5 100644 --- a/lib/Serialization/GlobalModuleIndex.cpp +++ b/lib/Serialization/GlobalModuleIndex.cpp @@ -818,3 +818,34 @@ GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) { // We're done. return EC_None; } + +namespace { + class GlobalIndexIdentifierIterator : public IdentifierIterator { + /// \brief The current position within the identifier lookup table. + IdentifierIndexTable::key_iterator Current; + + /// \brief The end position within the identifier lookup table. + IdentifierIndexTable::key_iterator End; + + public: + explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) { + Current = Idx.key_begin(); + End = Idx.key_end(); + } + + virtual StringRef Next() { + if (Current == End) + return StringRef(); + + StringRef Result = *Current; + ++Current; + return Result; + } + }; +} + +IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const { + IdentifierIndexTable &Table = + *static_cast(IdentifierIndex); + return new GlobalIndexIdentifierIterator(Table); +} -- 2.40.0