Skip to content

add support to getTopLevelDecls for clang submodules #76401

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 13 commits into from
Jan 30, 2025
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
3 changes: 3 additions & 0 deletions include/swift/AST/ClangNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class ClangNode {
/// clang::ImportDecl or null if it's neither.
const clang::Module *getClangModule() const;

/// Returns the owning clang module of this node, if it exists.
const clang::Module *getOwningClangModule() const;

clang::SourceLocation getLocation() const;
clang::SourceRange getSourceRange() const;

Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
virtual Identifier
getDiscriminatorForPrivateDecl(const Decl *D) const = 0;

virtual bool shouldCollectDisplayDecls() const { return true; }

/// Finds all top-level decls in this file.
///
/// This does a simple local lookup, not recursively looking through imports.
Expand Down
15 changes: 6 additions & 9 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,12 @@ class ModuleDecl
/// returns the module \c Foo.
ModuleDecl *getTopLevelModule(bool overlay = false);

/// Returns whether or not this module is a submodule of the given module.
/// If `this == M`, this returns false. If this is a submodule such as
/// `Foo.Bar.Baz`, and the given module is either `Foo` or `Foo.Bar`, this
/// returns true.
bool isSubmoduleOf(const ModuleDecl *M) const;

bool isResilient() const {
return getResilienceStrategy() != ResilienceStrategy::Default;
}
Expand Down Expand Up @@ -1086,15 +1092,6 @@ class ModuleDecl
/// The order of the results is not guaranteed to be meaningful.
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const;

/// Determines whether this module should be recursed into when calling
/// \c getDisplayDecls.
///
/// Some modules should not call \c getDisplayDecls, due to assertions
/// in their implementation. These are usually implicit imports that would be
/// recursed into for parsed modules. This function provides a guard against
/// recusing into modules that should not have decls collected.
bool shouldCollectDisplayDecls() const;

/// Finds all top-level decls that should be displayed to a client of this
/// module.
///
Expand Down
2 changes: 0 additions & 2 deletions include/swift/ClangImporter/ClangModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class ClangModuleUnit final : public LoadedFile {
ObjCSelector selector,
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;

virtual bool shouldCollectDisplayDecls() const override;

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

virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive = false) const override;
Expand Down
6 changes: 5 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2647,7 +2647,11 @@ static void addNamespaceMembers(Decl *decl,
const auto *declOwner = namespaceDecl->getOwningModule();
if (declOwner)
declOwner = declOwner->getTopLevelModule();
for (auto redecl : namespaceDecl->redecls()) {
auto Redecls = llvm::SmallVector<clang::NamespaceDecl *, 2>(namespaceDecl->redecls());
std::stable_sort(Redecls.begin(), Redecls.end(), [&](clang::NamespaceDecl *LHS, clang::NamespaceDecl *RHS) {
return LHS->getOwningModule()->Name < RHS->getOwningModule()->Name;
});
for (auto redecl : Redecls) {
// Skip namespace declarations that come from other top-level modules.
if (const auto *redeclOwner = redecl->getOwningModule()) {
if (declOwner && declOwner != redeclOwner->getTopLevelModule())
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ const clang::Module *ClangNode::getClangModule() const {
return nullptr;
}

const clang::Module *ClangNode::getOwningClangModule() const {
if (auto *M = getAsModule())
return M;
if (auto D = getAsDecl())
return D->getOwningModule();
if (auto MI = getAsModuleMacro())
return MI->getOwningModule();
return nullptr;
}

void ClangNode::dump() const {
if (auto D = getAsDecl())
D->dump();
Expand Down
45 changes: 29 additions & 16 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,22 @@ ModuleDecl *ModuleDecl::getTopLevelModule(bool overlay) {
return this;
}

bool ModuleDecl::isSubmoduleOf(const ModuleDecl *M) const {
// Swift modules don't currently support submodules.
if (!isNonSwiftModule())
return false;

auto *ClangParent = M->findUnderlyingClangModule();
if (!ClangParent)
return false;

auto *ClangModule = findUnderlyingClangModule();
if (!ClangModule)
return false;

return ClangModule->isSubModuleOf(ClangParent);
}

static bool isParsedModule(const ModuleDecl *mod) {
// FIXME: If we ever get mixed modules that contain both SourceFiles and other
// kinds of file units, this will break; there all callers of this function should
Expand Down Expand Up @@ -1213,14 +1229,6 @@ void SourceFile::lookupObjCMethods(
results.append(known->second.begin(), known->second.end());
}

bool ModuleDecl::shouldCollectDisplayDecls() const {
for (const FileUnit *file : getFiles()) {
if (!file->shouldCollectDisplayDecls())
return false;
}
return true;
}

void ModuleDecl::getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const {
FORWARD(getLocalTypeDecls, (Results));
}
Expand Down Expand Up @@ -1445,9 +1453,6 @@ void ModuleDecl::ImportCollector::collect(
const ImportedModule &importedModule) {
auto *module = importedModule.importedModule;

if (!module->shouldCollectDisplayDecls())
return;

if (importFilter && !importFilter(module))
return;

Expand All @@ -1468,11 +1473,17 @@ static void
collectExportedImports(const ModuleDecl *topLevelModule,
ModuleDecl::ImportCollector &importCollector) {
SmallVector<const ModuleDecl *> stack;
SmallPtrSet<const ModuleDecl *, 4> visited;
visited.insert(topLevelModule);
stack.push_back(topLevelModule);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the import graph is not a tree, you will push the same node multiple times on the stack and repeat the work of visiting its successors. Eg, suppose we're asked to collectExportedImports() of A with the import graph below, where A re-exports B and C, B and C both re-export D, etc:

    B       E       H   
   + \     + \     + \    
  /   \   /   \   /   \   
 /     + /     + /     + 
A       D       G       J
 \     + \     + \     + 
  \   /   \   /   \   /  
   + /     + /     + /   
    C       F       I

We're going to visit D twice, G four times, J eight times, etc, so this algorithm has a worst case running time of O(2^n). You can fix this by adding a visited set alongside stack. Before pushing something on the stack, check if it's already been visited, and skip it if so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I've just pushed a commit that adds a visited set to counteract this.

while (!stack.empty()) {
const ModuleDecl *module = stack.pop_back_val();
if (module->isNonSwiftModule())
if (module->isNonSwiftModule() && module != topLevelModule &&
!module->isSubmoduleOf(topLevelModule)) {
// Recurse into submodules of the top-level module so that we can
// re-export them if necessary.
continue;
}

for (const FileUnit *file : module->getFiles()) {
if (const SourceFile *source = dyn_cast<SourceFile>(file)) {
Expand All @@ -1492,10 +1503,12 @@ collectExportedImports(const ModuleDecl *topLevelModule,
ModuleDecl::ImportFilterKind::Exported);
for (const auto &im : exportedImports) {
// Skip collecting the underlying clang module as we already have the relevant import.
if (module->isClangOverlayOf(im.importedModule))
continue;
importCollector.collect(im);
stack.push_back(im.importedModule);
if (!module->isClangOverlayOf(im.importedModule))
importCollector.collect(im);
if (!visited.contains(im.importedModule)) {
visited.insert(im.importedModule);
stack.push_back(im.importedModule);
}
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,15 @@ static bool isDeclaredInModule(const ClangModuleUnit *ModuleFilter,
if (VD->getModuleContext()->getName().str() == CLANG_HEADER_MODULE_NAME) {
return true;
}
// Because the ClangModuleUnit saved as a decl context will be saved as the top-level module, but
// the ModuleFilter we're given might be a submodule (if a submodule was passed to
// getTopLevelDecls, for example), we should compare the underlying Clang modules to determine
// module membership.
if (auto ClangNode = VD->getClangNode()) {
if (auto *ClangModule = ClangNode.getOwningClangModule()) {
return ModuleFilter->getClangModule() == ClangModule;
}
}
auto ContainingUnit = VD->getDeclContext()->getModuleScopeContext();
return ModuleFilter == ContainingUnit;
}
Expand Down Expand Up @@ -3259,7 +3268,7 @@ class FilteringDeclaredDeclConsumer : public swift::VisibleDeclConsumer {
FilteringDeclaredDeclConsumer(swift::VisibleDeclConsumer &consumer,
const ClangModuleUnit *CMU)
: NextConsumer(consumer), ModuleFilter(CMU) {
assert(CMU && CMU->isTopLevel() && "Only top-level modules supported");
assert(CMU);
}

void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
Expand Down Expand Up @@ -3610,9 +3619,6 @@ class VectorDeclPtrConsumer : public swift::VisibleDeclConsumer {
};
} // unnamed namespace

// FIXME(https://github.com/apple/swift-docc/issues/190): Should submodules still be crawled for the symbol graph?
bool ClangModuleUnit::shouldCollectDisplayDecls() const { return isTopLevel(); }

void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
VectorDeclPtrConsumer consumer(results);
FilteringDeclaredDeclConsumer filterConsumer(consumer, this);
Expand All @@ -3633,10 +3639,12 @@ void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {

// Add the extensions produced by importing categories.
for (auto category : lookupTable->categories()) {
if (auto extension = cast_or_null<ExtensionDecl>(
owner.importDecl(category, owner.CurrentVersion,
/*UseCanonical*/false))) {
results.push_back(extension);
if (category->getOwningModule() == clangModule) {
if (auto extension = cast_or_null<ExtensionDecl>(
owner.importDecl(category, owner.CurrentVersion,
/*UseCanonical*/false))) {
results.push_back(extension);
}
}
}

Expand All @@ -3651,11 +3659,11 @@ void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
};
// Retrieve all of the globals that will be mapped to members.

// FIXME: Since we don't represent Clang submodules as Swift
// modules, we're getting everything.
llvm::SmallPtrSet<ExtensionDecl *, 8> knownExtensions;
for (auto entry : lookupTable->allGlobalsAsMembers()) {
auto decl = entry.get<clang::NamedDecl *>();
if (decl->getOwningModule() != clangModule) continue;

Decl *importedDecl = owner.importDecl(decl, owner.CurrentVersion);
if (!importedDecl) continue;

Expand Down
17 changes: 12 additions & 5 deletions lib/IDE/ModuleInterfacePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,6 @@ void swift::ide::printModuleInterface(
const PrintOptions &Options,
const bool PrintSynthesizedExtensions) {

// Clang submodules aren't handled well by `getDisplayDecls()` (no decls are
// returned), so map them to their top-level module and filter out the extra
// results below.
const clang::Module *TargetClangMod = TargetMod->findUnderlyingClangModule();
ModuleDecl *TopLevelMod = TargetMod->getTopLevelModule();
bool IsSubmodule = TargetMod != TopLevelMod;
Expand All @@ -460,8 +457,8 @@ void swift::ide::printModuleInterface(
auto AdjustedOptions = Options;
adjustPrintOptions(AdjustedOptions);

SmallVector<Decl *, 1> Decls;
swift::getTopLevelDeclsForDisplay(TopLevelMod, Decls);
SmallVector<ModuleDecl *, 1> ModuleList;
ModuleList.push_back(TargetMod);

SmallVector<ImportDecl *, 1> ImportDecls;
llvm::DenseSet<const clang::Module *> ClangModulesForImports;
Expand All @@ -485,6 +482,10 @@ void swift::ide::printModuleInterface(

ClangDecls.insert({ CM, {} });

if (CM != TargetClangMod)
if (auto *OwningModule = Importer.getWrapperForModule(CM))
ModuleList.push_back(OwningModule);

// If we're supposed to visit submodules, add them now.
if (TraversalOptions & ModuleTraversal::VisitSubmodules) {
for (clang::Module * submodule: CM->submodules()) {
Expand All @@ -499,6 +500,12 @@ void swift::ide::printModuleInterface(
}
}

SmallVector<Decl *, 1> Decls;

for (ModuleDecl *M : ModuleList) {
swift::getTopLevelDeclsForDisplay(M, Decls);
}

// Collect those submodules that are actually imported but have no import
// decls in the module.
llvm::SmallPtrSet<const clang::Module *, 16> NoImportSubModules;
Expand Down
15 changes: 12 additions & 3 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "clang/AST/DeclObjC.h"
#include "clang/Basic/Module.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Comment.h"
#include "swift/AST/Decl.h"
Expand Down Expand Up @@ -584,14 +585,14 @@ void SymbolGraph::serialize(llvm::json::OStream &OS) {
OS.attributeObject("module", [&](){
if (DeclaringModule) {
// A cross-import overlay can be considered part of its declaring module
OS.attribute("name", (*DeclaringModule)->getNameStr());
OS.attribute("name", getFullModuleName(*DeclaringModule));
std::vector<StringRef> B;
for (auto BModule : BystanderModules) {
B.push_back(BModule.str());
}
OS.attribute("bystanders", B);
} else {
OS.attribute("name", M.getNameStr());
OS.attribute("name", getFullModuleName(&M));
}
AttributeRAII Platform("platform", OS);

Expand Down Expand Up @@ -855,7 +856,15 @@ bool SymbolGraph::isUnconditionallyUnavailableOnAllPlatforms(const Decl *D) cons
bool SymbolGraph::canIncludeDeclAsNode(const Decl *D) const {
// If this decl isn't in this module or module that this module imported with `@_exported`, don't record it,
// as it will appear elsewhere in its module's symbol graph.
if (D->getModuleContext()->getName() != M.getName() && !Walker.isConsideredExportedImported(D)) {

// If a Clang decl was declared in a submodule, the Swift decl's context will still point to the
// top-level module. Instead, we need to probe the owning module on the Clang side, which will
// correctly point to the submodule.
auto RealModuleName = (std::string)D->getModuleContext()->getName();
if (auto *ClangDecl = D->getClangDecl())
if (auto *ClangModule = ClangDecl->getOwningModule())
RealModuleName = ClangModule->Name;
if (RealModuleName != (std::string)M.getName() && !Walker.isConsideredExportedImported(D)) {
return false;
}

Expand Down
Loading