Skip to content

Commit ed0d6d4

Browse files
[AST] check modules before recursing for display decls
1 parent a2e7347 commit ed0d6d4

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

include/swift/AST/FileUnit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
189189
virtual Identifier
190190
getDiscriminatorForPrivateValue(const ValueDecl *D) const = 0;
191191

192+
virtual bool shouldCollectDisplayDecls() const { return true; }
193+
192194
/// Finds all top-level decls in this file.
193195
///
194196
/// This does a simple local lookup, not recursively looking through imports.

include/swift/AST/Module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,15 @@ class ModuleDecl
760760
/// The order of the results is not guaranteed to be meaningful.
761761
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const;
762762

763+
/// Determines whether this module should be recursed into when calling
764+
/// \c getDisplayDecls.
765+
///
766+
/// Some modules should not call \c getDisplayDecls, due to assertions
767+
/// in their implementation. These are usually implicit imports that would be
768+
/// recursed into for parsed modules. This function provides a guard against
769+
/// recusing into modules that should not have decls collected.
770+
bool shouldCollectDisplayDecls() const;
771+
763772
/// Finds all top-level decls that should be displayed to a client of this
764773
/// module.
765774
///

include/swift/ClangImporter/ClangModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class ClangModuleUnit final : public LoadedFile {
8787
ObjCSelector selector,
8888
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
8989

90+
virtual bool shouldCollectDisplayDecls() const override;
91+
9092
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;
9193

9294
virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results) const override;

lib/AST/Module.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,15 +780,23 @@ void SourceFile::lookupObjCMethods(
780780
results.append(known->second.begin(), known->second.end());
781781
}
782782

783+
bool ModuleDecl::shouldCollectDisplayDecls() const {
784+
for (const FileUnit *file : Files) {
785+
if (!file->shouldCollectDisplayDecls())
786+
return false;
787+
}
788+
return true;
789+
}
790+
783791
static void collectParsedExportedImports(const ModuleDecl *M, SmallPtrSetImpl<ModuleDecl *> &Imports) {
784792
for (const FileUnit *file : M->getFiles()) {
785793
if (const SourceFile *source = dyn_cast<SourceFile>(file)) {
786794
if (source->hasImports()) {
787795
for (auto import : source->getImports()) {
788-
if (import.options.contains(ImportFlags::Exported)) {
789-
if (!Imports.contains(import.module.importedModule)) {
790-
Imports.insert(import.module.importedModule);
791-
}
796+
if (import.options.contains(ImportFlags::Exported) &&
797+
!Imports.contains(import.module.importedModule) &&
798+
import.module.importedModule->shouldCollectDisplayDecls()) {
799+
Imports.insert(import.module.importedModule);
792800
}
793801
}
794802
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,6 +3120,8 @@ class VectorDeclPtrConsumer : public swift::VisibleDeclConsumer {
31203120
};
31213121
} // unnamed namespace
31223122

3123+
bool ClangModuleUnit::shouldCollectDisplayDecls() const { return isTopLevel(); }
3124+
31233125
void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
31243126
VectorDeclPtrConsumer consumer(results);
31253127
FilteringDeclaredDeclConsumer filterConsumer(consumer, this);

0 commit comments

Comments
 (0)