Skip to content

[IDE][DocSupport] Fix DocInfo missing decls when generated for clang submodules #31589

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
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
10 changes: 1 addition & 9 deletions include/swift/IDE/ModuleInterfacePrinting.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,11 @@ bool printTypeInterface(ModuleDecl *M, Type Ty, ASTPrinter &Printer,
bool printTypeInterface(ModuleDecl *M, StringRef TypeUSR, ASTPrinter &Printer,
std::string &TypeName, std::string &Error);

void printModuleInterface(ModuleDecl *M, Optional<StringRef> Group,
void printModuleInterface(ModuleDecl *M, ArrayRef<StringRef> GroupNames,
ModuleTraversalOptions TraversalOptions,
ASTPrinter &Printer, const PrintOptions &Options,
const bool PrintSynthesizedExtensions);

// FIXME: this API should go away when Swift can represent Clang submodules as
// 'swift::ModuleDecl *' objects.
void printSubmoduleInterface(ModuleDecl *M, ArrayRef<StringRef> FullModuleName,
ArrayRef<StringRef> GroupNames,
ModuleTraversalOptions TraversalOptions,
ASTPrinter &Printer, const PrintOptions &Options,
const bool PrintSynthesizedExtensions);

/// Print the interface for a header that has been imported via the implicit
/// objc header importing feature.
void printHeaderInterface(StringRef Filename, ASTContext &Ctx,
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ class FilteringDeclaredDeclConsumer : public swift::VisibleDeclConsumer {
FilteringDeclaredDeclConsumer(swift::VisibleDeclConsumer &consumer,
const ClangModuleUnit *CMU)
: NextConsumer(consumer), ModuleFilter(CMU) {
assert(CMU);
assert(CMU && CMU->isTopLevel() && "Only top-level modules supported");
}

void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
Expand Down
103 changes: 43 additions & 60 deletions lib/IDE/ModuleInterfacePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,6 @@ printTypeInterface(ModuleDecl *M, StringRef TypeUSR, ASTPrinter &Printer,
Printer, TypeName, Error);
}

void swift::ide::printModuleInterface(ModuleDecl *M, Optional<StringRef> Group,
ModuleTraversalOptions TraversalOptions,
ASTPrinter &Printer,
const PrintOptions &Options,
const bool PrintSynthesizedExtensions) {
printSubmoduleInterface(M, M->getName().str(),
Group.hasValue() ? Group.getValue() : ArrayRef<StringRef>(),
TraversalOptions, Printer, Options,
PrintSynthesizedExtensions);
}

static void adjustPrintOptions(PrintOptions &AdjustedOptions) {
// Don't print empty curly braces while printing the module interface.
AdjustedOptions.FunctionDefinitions = false;
Expand Down Expand Up @@ -230,7 +219,7 @@ static bool extensionHasClangNode(ExtensionDecl *ext) {

Optional<StringRef>
swift::ide::findGroupNameForUSR(ModuleDecl *M, StringRef USR) {
for (auto File : M->getFiles()) {
for (auto File : M->getTopLevelModule()->getFiles()) {
if (auto Name = File->getGroupNameByUSR(USR)) {
return Name;
}
Expand Down Expand Up @@ -539,53 +528,45 @@ static void printCrossImportOverlays(ModuleDecl *Declaring, ASTContext &Ctx,
}
}

void swift::ide::printSubmoduleInterface(
ModuleDecl *M,
ArrayRef<StringRef> FullModuleName,
void swift::ide::printModuleInterface(
ModuleDecl *TargetMod,
ArrayRef<StringRef> GroupNames,
ModuleTraversalOptions TraversalOptions,
ASTPrinter &Printer,
const PrintOptions &Options,
const bool PrintSynthesizedExtensions) {
auto &SwiftContext = M->getASTContext();

// 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;

auto &SwiftContext = TopLevelMod->getASTContext();
auto &Importer =
static_cast<ClangImporter &>(*SwiftContext.getClangModuleLoader());

auto AdjustedOptions = Options;
adjustPrintOptions(AdjustedOptions);

SmallVector<Decl *, 1> Decls;
M->getDisplayDecls(Decls);
TopLevelMod->getDisplayDecls(Decls);

const clang::Module *InterestingClangModule = nullptr;
SmallVector<ImportDecl *, 1> ImportDecls;
llvm::DenseSet<const clang::Module *> ClangModulesForImports;
SmallVector<Decl *, 1> SwiftDecls;
llvm::DenseMap<const clang::Module *,
SmallVector<std::pair<Decl *, clang::SourceLocation>, 1>>
ClangDecls;

// Drop top-level module name.
FullModuleName = FullModuleName.slice(1);

InterestingClangModule = M->findUnderlyingClangModule();
if (InterestingClangModule) {
for (StringRef Name : FullModuleName) {
InterestingClangModule = InterestingClangModule->findSubmodule(Name);
if (!InterestingClangModule)
return;
}
} else {
assert(FullModuleName.empty());
}

// If we're printing recursively, find all of the submodules to print.
if (InterestingClangModule) {
if (TargetClangMod) {
if (TraversalOptions) {
SmallVector<const clang::Module *, 8> Worklist;
SmallPtrSet<const clang::Module *, 8> Visited;
Worklist.push_back(InterestingClangModule);
Visited.insert(InterestingClangModule);
Worklist.push_back(TargetClangMod);
Visited.insert(TargetClangMod);
while (!Worklist.empty()) {
const clang::Module *CM = Worklist.pop_back_val();
if (!(TraversalOptions & ModuleTraversal::VisitHidden) &&
Expand All @@ -604,17 +585,17 @@ void swift::ide::printSubmoduleInterface(
}
}
} else {
ClangDecls.insert({ InterestingClangModule, {} });
ClangDecls.insert({ TargetClangMod, {} });
}
}

// Collect those submodules that are actually imported but have no import decls
// in the module.
// Collect those submodules that are actually imported but have no import
// decls in the module.
llvm::SmallPtrSet<const clang::Module *, 16> NoImportSubModules;
if (InterestingClangModule) {
if (TargetClangMod) {
// Assume all submodules are missing.
for (auto It =InterestingClangModule->submodule_begin();
It != InterestingClangModule->submodule_end(); It++) {
for (auto It = TargetClangMod->submodule_begin();
It != TargetClangMod->submodule_end(); It++) {
NoImportSubModules.insert(*It);
}
}
Expand All @@ -631,18 +612,18 @@ void swift::ide::printSubmoduleInterface(
}

auto ShouldPrintImport = [&](ImportDecl *ImportD) -> bool {
if (!InterestingClangModule)
if (!TargetClangMod)
return true;
auto ClangMod = ImportD->getClangModule();
if (!ClangMod)
auto ImportedMod = ImportD->getClangModule();
if (!ImportedMod)
return true;
if (!ClangMod->isSubModule())
if (!ImportedMod->isSubModule())
return true;
if (ClangMod == InterestingClangModule)
if (ImportedMod == TargetClangMod)
return false;
// FIXME: const-ness on the clang API.
return ClangMod->isSubModuleOf(
const_cast<clang::Module*>(InterestingClangModule));
return ImportedMod->isSubModuleOf(
const_cast<clang::Module*>(TargetClangMod));
};

if (auto ID = dyn_cast<ImportDecl>(D)) {
Expand Down Expand Up @@ -690,14 +671,16 @@ void swift::ide::printSubmoduleInterface(
}
}

if (FullModuleName.empty()) {
if (!IsSubmodule) {
// If group name is given and the decl does not belong to the group, skip it.
if (!GroupNames.empty()){
if (auto Target = D->getGroupName()) {
if (auto TargetGroup = D->getGroupName()) {
if (std::find(GroupNames.begin(), GroupNames.end(),
Target.getValue()) != GroupNames.end()) {
FileRangedDecls.insert(std::make_pair(D->getSourceFileName().getValue(),
std::vector<Decl*>())).first->getValue().push_back(D);
TargetGroup.getValue()) != GroupNames.end()) {
FileRangedDecls.insert({
D->getSourceFileName().getValue(),
std::vector<Decl*>()
}).first->getValue().push_back(D);
}
}
continue;
Expand Down Expand Up @@ -725,8 +708,9 @@ void swift::ide::printSubmoduleInterface(
}

// Create the missing import decls and add to the collector.
for (auto *SM : NoImportSubModules) {
ImportDecls.push_back(createImportDecl(M->getASTContext(), M, SM, {}));
for (auto *SubMod : NoImportSubModules) {
ImportDecls.push_back(createImportDecl(TopLevelMod->getASTContext(),
TopLevelMod, SubMod, {}));
}

// Sort imported clang declarations in source order *within a submodule*.
Expand Down Expand Up @@ -760,7 +744,7 @@ void swift::ide::printSubmoduleInterface(
};

// Imports from the stdlib are internal details that don't need to be exposed.
if (!M->isStdlibModule()) {
if (!TargetMod->isStdlibModule()) {
for (auto *D : ImportDecls)
PrintDecl(D);
Printer << "\n";
Expand All @@ -785,8 +769,7 @@ void swift::ide::printSubmoduleInterface(
}
}

if (!(TraversalOptions & ModuleTraversal::SkipOverlay) ||
!InterestingClangModule) {
if (!(TraversalOptions & ModuleTraversal::SkipOverlay) || !TargetClangMod) {
for (auto *D : SwiftDecls) {
if (PrintDecl(D))
Printer << "\n";
Expand All @@ -796,8 +779,8 @@ void swift::ide::printSubmoduleInterface(
// also print the decls from any underscored Swift cross-import overlays it
// is the underlying module of, transitively.
if (GroupNames.empty()) {
printCrossImportOverlays(M, SwiftContext, *PrinterToUse, AdjustedOptions,
PrintSynthesizedExtensions);
printCrossImportOverlays(TargetMod, SwiftContext, *PrinterToUse,
AdjustedOptions, PrintSynthesizedExtensions);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/SourceKit/DocSupport/doc_clang_module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
// RUN: %sourcekitd-test -req=doc-info -module Foo -- -F %S/../Inputs/libIDE-mock-sdk \
// RUN: -target %target-triple %clang-importer-sdk-nosource -I %t | %sed_clean > %t.response
// RUN: %diff -u %s.response %t.response

// RUN: %sourcekitd-test -req=doc-info -module Foo.FooSub -- -F %S/../Inputs/libIDE-mock-sdk \
// RUN: -target %target-triple %clang-importer-sdk-nosource -I %t | %sed_clean > %t.response
// RUN: %diff -u %s.sub.response %t.response
Loading