@@ -189,17 +189,6 @@ printTypeInterface(ModuleDecl *M, StringRef TypeUSR, ASTPrinter &Printer,
189
189
Printer, TypeName, Error);
190
190
}
191
191
192
- void swift::ide::printModuleInterface (ModuleDecl *M, Optional<StringRef> Group,
193
- ModuleTraversalOptions TraversalOptions,
194
- ASTPrinter &Printer,
195
- const PrintOptions &Options,
196
- const bool PrintSynthesizedExtensions) {
197
- printSubmoduleInterface (M, M->getName ().str (),
198
- Group.hasValue () ? Group.getValue () : ArrayRef<StringRef>(),
199
- TraversalOptions, Printer, Options,
200
- PrintSynthesizedExtensions);
201
- }
202
-
203
192
static void adjustPrintOptions (PrintOptions &AdjustedOptions) {
204
193
// Don't print empty curly braces while printing the module interface.
205
194
AdjustedOptions.FunctionDefinitions = false ;
@@ -230,7 +219,7 @@ static bool extensionHasClangNode(ExtensionDecl *ext) {
230
219
231
220
Optional<StringRef>
232
221
swift::ide::findGroupNameForUSR (ModuleDecl *M, StringRef USR) {
233
- for (auto File : M->getFiles ()) {
222
+ for (auto File : M->getTopLevelModule ()-> getFiles ()) {
234
223
if (auto Name = File->getGroupNameByUSR (USR)) {
235
224
return Name;
236
225
}
@@ -539,53 +528,45 @@ static void printCrossImportOverlays(ModuleDecl *Declaring, ASTContext &Ctx,
539
528
}
540
529
}
541
530
542
- void swift::ide::printSubmoduleInterface (
543
- ModuleDecl *M,
544
- ArrayRef<StringRef> FullModuleName,
531
+ void swift::ide::printModuleInterface (
532
+ ModuleDecl *TargetMod,
545
533
ArrayRef<StringRef> GroupNames,
546
534
ModuleTraversalOptions TraversalOptions,
547
535
ASTPrinter &Printer,
548
536
const PrintOptions &Options,
549
537
const bool PrintSynthesizedExtensions) {
550
- auto &SwiftContext = M->getASTContext ();
538
+
539
+ // Clang submodules aren't handled well by `getDisplayDecls()` (no decls are
540
+ // returned), so map them to their top-level module and filter out the extra
541
+ // results below.
542
+ const clang::Module *TargetClangMod = TargetMod->findUnderlyingClangModule ();
543
+ ModuleDecl *TopLevelMod = TargetMod->getTopLevelModule ();
544
+ bool IsSubmodule = TargetMod != TopLevelMod;
545
+
546
+ auto &SwiftContext = TopLevelMod->getASTContext ();
551
547
auto &Importer =
552
548
static_cast <ClangImporter &>(*SwiftContext.getClangModuleLoader ());
553
549
554
550
auto AdjustedOptions = Options;
555
551
adjustPrintOptions (AdjustedOptions);
556
552
557
553
SmallVector<Decl *, 1 > Decls;
558
- M ->getDisplayDecls (Decls);
554
+ TopLevelMod ->getDisplayDecls (Decls);
559
555
560
- const clang::Module *InterestingClangModule = nullptr ;
561
556
SmallVector<ImportDecl *, 1 > ImportDecls;
562
557
llvm::DenseSet<const clang::Module *> ClangModulesForImports;
563
558
SmallVector<Decl *, 1 > SwiftDecls;
564
559
llvm::DenseMap<const clang::Module *,
565
560
SmallVector<std::pair<Decl *, clang::SourceLocation>, 1 >>
566
561
ClangDecls;
567
562
568
- // Drop top-level module name.
569
- FullModuleName = FullModuleName.slice (1 );
570
-
571
- InterestingClangModule = M->findUnderlyingClangModule ();
572
- if (InterestingClangModule) {
573
- for (StringRef Name : FullModuleName) {
574
- InterestingClangModule = InterestingClangModule->findSubmodule (Name);
575
- if (!InterestingClangModule)
576
- return ;
577
- }
578
- } else {
579
- assert (FullModuleName.empty ());
580
- }
581
-
582
563
// If we're printing recursively, find all of the submodules to print.
583
- if (InterestingClangModule ) {
564
+ if (TargetClangMod ) {
584
565
if (TraversalOptions) {
585
566
SmallVector<const clang::Module *, 8 > Worklist;
586
567
SmallPtrSet<const clang::Module *, 8 > Visited;
587
- Worklist.push_back (InterestingClangModule );
588
- Visited.insert (InterestingClangModule );
568
+ Worklist.push_back (TargetClangMod );
569
+ Visited.insert (TargetClangMod );
589
570
while (!Worklist.empty ()) {
590
571
const clang::Module *CM = Worklist.pop_back_val ();
591
572
if (!(TraversalOptions & ModuleTraversal::VisitHidden) &&
@@ -604,17 +585,17 @@ void swift::ide::printSubmoduleInterface(
604
585
}
605
586
}
606
587
} else {
607
- ClangDecls.insert ({ InterestingClangModule , {} });
588
+ ClangDecls.insert ({ TargetClangMod , {} });
608
589
}
609
590
}
610
591
611
- // Collect those submodules that are actually imported but have no import decls
612
- // in the module.
592
+ // Collect those submodules that are actually imported but have no import
593
+ // decls in the module.
613
594
llvm::SmallPtrSet<const clang::Module *, 16 > NoImportSubModules;
614
- if (InterestingClangModule ) {
595
+ if (TargetClangMod ) {
615
596
// Assume all submodules are missing.
616
- for (auto It =InterestingClangModule ->submodule_begin ();
617
- It != InterestingClangModule ->submodule_end (); It++) {
597
+ for (auto It = TargetClangMod ->submodule_begin ();
598
+ It != TargetClangMod ->submodule_end (); It++) {
618
599
NoImportSubModules.insert (*It);
619
600
}
620
601
}
@@ -631,18 +612,18 @@ void swift::ide::printSubmoduleInterface(
631
612
}
632
613
633
614
auto ShouldPrintImport = [&](ImportDecl *ImportD) -> bool {
634
- if (!InterestingClangModule )
615
+ if (!TargetClangMod )
635
616
return true ;
636
- auto ClangMod = ImportD->getClangModule ();
637
- if (!ClangMod )
617
+ auto ImportedMod = ImportD->getClangModule ();
618
+ if (!ImportedMod )
638
619
return true ;
639
- if (!ClangMod ->isSubModule ())
620
+ if (!ImportedMod ->isSubModule ())
640
621
return true ;
641
- if (ClangMod == InterestingClangModule )
622
+ if (ImportedMod == TargetClangMod )
642
623
return false ;
643
624
// FIXME: const-ness on the clang API.
644
- return ClangMod ->isSubModuleOf (
645
- const_cast <clang::Module*>(InterestingClangModule ));
625
+ return ImportedMod ->isSubModuleOf (
626
+ const_cast <clang::Module*>(TargetClangMod ));
646
627
};
647
628
648
629
if (auto ID = dyn_cast<ImportDecl>(D)) {
@@ -690,14 +671,16 @@ void swift::ide::printSubmoduleInterface(
690
671
}
691
672
}
692
673
693
- if (FullModuleName. empty () ) {
674
+ if (!IsSubmodule ) {
694
675
// If group name is given and the decl does not belong to the group, skip it.
695
676
if (!GroupNames.empty ()){
696
- if (auto Target = D->getGroupName ()) {
677
+ if (auto TargetGroup = D->getGroupName ()) {
697
678
if (std::find (GroupNames.begin (), GroupNames.end (),
698
- Target.getValue ()) != GroupNames.end ()) {
699
- FileRangedDecls.insert (std::make_pair (D->getSourceFileName ().getValue (),
700
- std::vector<Decl*>())).first ->getValue ().push_back (D);
679
+ TargetGroup.getValue ()) != GroupNames.end ()) {
680
+ FileRangedDecls.insert ({
681
+ D->getSourceFileName ().getValue (),
682
+ std::vector<Decl*>()
683
+ }).first ->getValue ().push_back (D);
701
684
}
702
685
}
703
686
continue ;
@@ -725,8 +708,9 @@ void swift::ide::printSubmoduleInterface(
725
708
}
726
709
727
710
// Create the missing import decls and add to the collector.
728
- for (auto *SM : NoImportSubModules) {
729
- ImportDecls.push_back (createImportDecl (M->getASTContext (), M, SM, {}));
711
+ for (auto *SubMod : NoImportSubModules) {
712
+ ImportDecls.push_back (createImportDecl (TopLevelMod->getASTContext (),
713
+ TopLevelMod, SubMod, {}));
730
714
}
731
715
732
716
// Sort imported clang declarations in source order *within a submodule*.
@@ -760,7 +744,7 @@ void swift::ide::printSubmoduleInterface(
760
744
};
761
745
762
746
// Imports from the stdlib are internal details that don't need to be exposed.
763
- if (!M ->isStdlibModule ()) {
747
+ if (!TargetMod ->isStdlibModule ()) {
764
748
for (auto *D : ImportDecls)
765
749
PrintDecl (D);
766
750
Printer << " \n " ;
@@ -785,8 +769,7 @@ void swift::ide::printSubmoduleInterface(
785
769
}
786
770
}
787
771
788
- if (!(TraversalOptions & ModuleTraversal::SkipOverlay) ||
789
- !InterestingClangModule) {
772
+ if (!(TraversalOptions & ModuleTraversal::SkipOverlay) || !TargetClangMod) {
790
773
for (auto *D : SwiftDecls) {
791
774
if (PrintDecl (D))
792
775
Printer << " \n " ;
@@ -796,8 +779,8 @@ void swift::ide::printSubmoduleInterface(
796
779
// also print the decls from any underscored Swift cross-import overlays it
797
780
// is the underlying module of, transitively.
798
781
if (GroupNames.empty ()) {
799
- printCrossImportOverlays (M , SwiftContext, *PrinterToUse, AdjustedOptions ,
800
- PrintSynthesizedExtensions);
782
+ printCrossImportOverlays (TargetMod , SwiftContext, *PrinterToUse,
783
+ AdjustedOptions, PrintSynthesizedExtensions);
801
784
}
802
785
}
803
786
}
0 commit comments