[CodeCompletion] Suggest synthesized declarations from macros

* Don't invalidate the lookup cache in 'getOrCreateSynthesizedFile()'
  Adding a synthesized file itself doesn't introduce any decls. Instead,
  we should invalidate the right after the actual declrations are added
  in the file
* Remove 'SourceLookupCache::invalidate()' method. It was just used
  right before the destruction. It was just not necessary
* Include auxiliary decls in 'SourceLookupCache::lookupVisibleDecls()'
  Previously, global symbol completion didn't include decls synthesized
  by peer macros or freestanding decl macros
* Include "auxiliary decls" in visible member lookup, and visible local
  decl lookup
* Hide macro unique names

rdar://110535113
This commit is contained in:
Rintaro Ishizaki
2023-06-21 11:33:02 -07:00
parent 262e23858f
commit 096d8ea142
7 changed files with 188 additions and 39 deletions

View File

@@ -188,9 +188,6 @@ public:
SourceLookupCache(const SourceFile &SF);
SourceLookupCache(const ModuleDecl &Mod);
/// Throw away as much memory as possible.
void invalidate();
void lookupValue(DeclName Name, NLKind LookupKind,
OptionSet<ModuleLookupFlags> Flags,
SmallVectorImpl<ValueDecl*> &Result);
@@ -552,6 +549,29 @@ void SourceLookupCache::lookupVisibleDecls(ImportPath::Access AccessPath,
Consumer.foundDecl(vd, DeclVisibilityKind::VisibleAtTopLevel);
}
}
populateAuxiliaryDeclCache();
SmallVector<MissingDecl *, 4> unexpandedDecls;
for (auto &entry : TopLevelAuxiliaryDecls) {
for (auto &decl : entry.second) {
unexpandedDecls.append(entry.second.begin(), entry.second.end());
}
}
// Store macro expanded decls in a 'SmallSetVector' because different
// MissingDecls might be created by a single macro expansion. (e.g. multiple
// 'names' in macro role attributes). Since expansions are cached, it doesn't
// cause duplicated expansions, but different 'unexpandedDecl' may report the
// same 'ValueDecl'.
SmallSetVector<ValueDecl *, 4> macroExpandedDecls;
for (MissingDecl *unexpandedDecl : unexpandedDecls) {
unexpandedDecl->forEachMacroExpandedDecl([&](ValueDecl *vd) {
macroExpandedDecls.insert(vd);
});
}
for (auto *vd : macroExpandedDecls) {
Consumer.foundDecl(vd, DeclVisibilityKind::VisibleAtTopLevel);
}
}
void SourceLookupCache::lookupClassMembers(ImportPath::Access accessPath,
@@ -608,16 +628,6 @@ void SourceLookupCache::lookupClassMember(ImportPath::Access accessPath,
results.append(iter->second.begin(), iter->second.end());
}
void SourceLookupCache::invalidate() {
TopLevelValues.clear();
ClassMembers.clear();
MemberCachePopulated = false;
// std::move AllVisibleValues into a temporary to destroy its contents.
using SameSizeSmallVector = decltype(AllVisibleValues);
(void)SameSizeSmallVector{std::move(AllVisibleValues)};
}
//===----------------------------------------------------------------------===//
// Module Implementation
//===----------------------------------------------------------------------===//
@@ -1067,9 +1077,12 @@ void SourceFile::lookupValue(DeclName name, NLKind lookupKind,
void ModuleDecl::lookupVisibleDecls(ImportPath::Access AccessPath,
VisibleDeclConsumer &Consumer,
NLKind LookupKind) const {
if (isParsedModule(this))
return getSourceLookupCache().lookupVisibleDecls(
AccessPath, Consumer, LookupKind);
if (isParsedModule(this)) {
auto &cache = getSourceLookupCache();
cache.lookupVisibleDecls(AccessPath, Consumer, LookupKind);
assert(Cache.get() == &cache && "cache invalidated during lookup");
return;
}
FORWARD(lookupVisibleDecls, (AccessPath, Consumer, LookupKind));
}
@@ -3524,7 +3537,6 @@ void ModuleDecl::clearLookupCache() {
return;
// Abandon any current cache. We'll rebuild it on demand.
Cache->invalidate();
Cache.reset();
}
@@ -3944,9 +3956,6 @@ SynthesizedFileUnit &FileUnit::getOrCreateSynthesizedFile() {
return *thisSynth;
SynthesizedFile = new (getASTContext()) SynthesizedFileUnit(*this);
SynthesizedFileAndKind.setPointer(SynthesizedFile);
// Rebuild the source lookup caches now that we have a synthesized file
// full of declarations to look into.
getParentModule()->clearLookupCache();
}
return *SynthesizedFile;
}