Skip to content

[AST] only recurse getDisplayDecls in SymbolGraphGen #41204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
///
/// This can differ from \c getTopLevelDecls, e.g. it returns decls from a
/// shadowed clang module.
virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results) const {
virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false) const {
getTopLevelDecls(results);
}

Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ class ModuleDecl
/// shadowed clang module. It does not force synthesized top-level decls that
/// should be printed to be added; use \c swift::getTopLevelDeclsForDisplay()
/// for that.
void getDisplayDecls(SmallVectorImpl<Decl*> &results) const;
void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false) const;

using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/ClangImporter/ClangModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ClangModuleUnit final : public LoadedFile {

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

virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results) const override;
virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false) const override;

virtual void
getImportedModules(SmallVectorImpl<ImportedModule> &imports,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Sema/IDETypeChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace swift {
/// \c ModuleDecl::getDisplayDecls() would only return if previous
/// work happened to have synthesized them.
void
getTopLevelDeclsForDisplay(ModuleDecl *M, SmallVectorImpl<Decl*> &Results);
getTopLevelDeclsForDisplay(ModuleDecl *M, SmallVectorImpl<Decl*> &Results, bool Recursive = false);

struct ExtensionInfo {
// The extension with the declarations to apply.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class SerializedASTFile final : public LoadedFile {
virtual void
getOpaqueReturnTypeDecls(SmallVectorImpl<OpaqueTypeDecl*> &results) const override;

virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results) const override;
virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false) const override;

virtual void
getImportedModules(SmallVectorImpl<ImportedModule> &imports,
Expand Down
30 changes: 16 additions & 14 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,30 +949,32 @@ SourceFile::getExternalRawLocsForDecl(const Decl *D) const {
return Result;
}

void ModuleDecl::getDisplayDecls(SmallVectorImpl<Decl*> &Results) const {
if (isParsedModule(this)) {
void ModuleDecl::getDisplayDecls(SmallVectorImpl<Decl*> &Results, bool Recursive) const {
if (Recursive && isParsedModule(this)) {
SmallPtrSet<ModuleDecl *, 4> Modules;
collectParsedExportedImports(this, Modules);
for (const ModuleDecl *import : Modules) {
import->getDisplayDecls(Results);
import->getDisplayDecls(Results, Recursive);
}
}
// FIXME: Should this do extra access control filtering?
FORWARD(getDisplayDecls, (Results));

#ifndef NDEBUG
llvm::DenseSet<Decl *> visited;
for (auto *D : Results) {
// decls synthesized from implicit clang decls may appear multiple times;
// e.g. if multiple modules with underlying clang modules are re-exported.
// including duplicates of these is harmless, so skip them when counting
// this assertion
if (const auto *CD = D->getClangDecl()) {
if (CD->isImplicit()) continue;
}
if (Recursive) {
llvm::DenseSet<Decl *> visited;
for (auto *D : Results) {
// decls synthesized from implicit clang decls may appear multiple times;
// e.g. if multiple modules with underlying clang modules are re-exported.
// including duplicates of these is harmless, so skip them when counting
// this assertion
if (const auto *CD = D->getClangDecl()) {
if (CD->isImplicit()) continue;
}

auto inserted = visited.insert(D).second;
assert(inserted && "there should be no duplicate decls");
auto inserted = visited.insert(D).second;
assert(inserted && "there should be no duplicate decls");
}
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,7 @@ static void getImportDecls(ClangModuleUnit *ClangUnit, const clang::Module *M,
}
}

void ClangModuleUnit::getDisplayDecls(SmallVectorImpl<Decl*> &results) const {
void ClangModuleUnit::getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive) const {
if (clangModule)
getImportDecls(const_cast<ClangModuleUnit *>(this), clangModule, results);
getTopLevelDecls(results);
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/DWARFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DWARFModuleUnit final : public LoadedFile {
getTopLevelDecls(SmallVectorImpl<Decl *> &results) const override {}

virtual void
getDisplayDecls(SmallVectorImpl<Decl *> &results) const override {}
getDisplayDecls(SmallVectorImpl<Decl *> &results, bool recursive = false) const override {}

virtual void
getImportedModules(SmallVectorImpl<ImportedModule> &imports,
Expand Down
7 changes: 4 additions & 3 deletions lib/IDE/IDETypeChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ using namespace swift;

void
swift::getTopLevelDeclsForDisplay(ModuleDecl *M,
SmallVectorImpl<Decl*> &Results) {
SmallVectorImpl<Decl*> &Results,
bool Recursive) {
auto startingSize = Results.size();
M->getDisplayDecls(Results);
M->getDisplayDecls(Results, Recursive);

// Force Sendable on all types, which might synthesize some extensions.
// FIXME: We can remove this if @_nonSendable stops creating extensions.
Expand All @@ -49,7 +50,7 @@ swift::getTopLevelDeclsForDisplay(ModuleDecl *M,
// Remove what we fetched and fetch again, possibly now with additional
// extensions.
Results.resize(startingSize);
M->getDisplayDecls(Results);
M->getDisplayDecls(Results, Recursive);
}

static bool shouldPrintAsFavorable(const Decl *D, const PrintOptions &Options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,9 @@ ModuleFile::getOpaqueReturnTypeDecls(SmallVectorImpl<OpaqueTypeDecl *> &results)
}
}

void ModuleFile::getDisplayDecls(SmallVectorImpl<Decl *> &results) {
void ModuleFile::getDisplayDecls(SmallVectorImpl<Decl *> &results, bool recursive) {
if (UnderlyingModule)
UnderlyingModule->getDisplayDecls(results);
UnderlyingModule->getDisplayDecls(results, recursive);

PrettyStackTraceModuleFile stackEntry(*this);
getImportDecls(results);
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ class ModuleFile
/// This includes all decls that should be displayed to clients of the module.
/// This can differ from \c getTopLevelDecls, e.g. it returns decls from a
/// shadowed clang module.
void getDisplayDecls(SmallVectorImpl<Decl*> &results);
void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false);

StringRef getModuleFilename() const {
if (!Core->ModuleInterfacePath.empty())
Expand Down
4 changes: 2 additions & 2 deletions lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1561,8 +1561,8 @@ SerializedASTFile::getOpaqueReturnTypeDecls(
}

void
SerializedASTFile::getDisplayDecls(SmallVectorImpl<Decl*> &results) const {
File.getDisplayDecls(results);
SerializedASTFile::getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive) const {
File.getDisplayDecls(results, recursive);
}

StringRef SerializedASTFile::getFilename() const {
Expand Down
2 changes: 1 addition & 1 deletion lib/SymbolGraphGen/SymbolGraphGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ symbolgraphgen::emitSymbolGraphForModule(ModuleDecl *M,
const SymbolGraphOptions &Options) {
SymbolGraphASTWalker Walker(*M, Options);
SmallVector<Decl *, 64> ModuleDecls;
swift::getTopLevelDeclsForDisplay(M, ModuleDecls);
swift::getTopLevelDeclsForDisplay(M, ModuleDecls, /*recursive*/true);

if (Options.PrintMessages)
llvm::errs() << ModuleDecls.size()
Expand Down
12 changes: 12 additions & 0 deletions test/ModuleInterface/exported-import.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -emit-module-path %t/Other.swiftmodule -module-name Other %S/Inputs/other.swift
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -emit-module-path /dev/null -emit-module-interface-path %t/ExportedImport.swiftmodule -module-name ExportedImport %s -I %t

// RUN: %FileCheck --input-file %t/ExportedImport.swiftmodule %s

// CHECK-NOT: otherFileFunction

@_exported import Other

// CHECK: public struct SomeStruct
public struct SomeStruct {}