Fix stack overflow and improve performance when a module contains many
overloads of a name by claiming that there are no lookup results for that name
in modules while loading the names from the module. Lookups in deserialization
really don't want to find names which they themselves are in the process of
introducing. This also has the pleasant side-effect of automatically caching
PCH lookups which found no names.
The runtime here is still quadratic in the number of overloads, but the
constant is lower.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174685 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 3039c95..e1202c2 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -1203,14 +1203,16 @@
if (LookupPtr.getInt())
Map = buildLookup();
+ if (!Map)
+ Map = CreateStoredDeclsMap(getParentASTContext());
+
// If a PCH/module has a result for this name, and we have a local
// declaration, we will have imported the PCH/module result when adding the
// local declaration or when reconciling the module.
- if (Map) {
- StoredDeclsMap::iterator I = Map->find(Name);
- if (I != Map->end())
- return I->second.getLookupResult();
- }
+ std::pair<StoredDeclsMap::iterator, bool> R =
+ Map->insert(std::make_pair(Name, StoredDeclsList()));
+ if (!R.second)
+ return R.first->second.getLookupResult();
ExternalASTSource *Source = getParentASTContext().getExternalSource();
if (Source->FindExternalVisibleDeclsByName(this, Name)) {