@@ -1612,36 +1612,80 @@ class AnnotatingPrinter : public StreamPrinter {
1612
1612
};
1613
1613
}
1614
1614
1615
- struct GroupNamesPrinter : public StreamPrinter {
1615
+ struct GroupNamesPrinter {
1616
1616
llvm::StringSet<> Groups;
1617
- GroupNamesPrinter (raw_ostream &OS) : StreamPrinter(OS) {}
1617
+ raw_ostream &OS;
1618
+ GroupNamesPrinter (raw_ostream &OS) : OS(OS) {}
1618
1619
~GroupNamesPrinter () {
1619
1620
OS << " Module groups begin:\n " ;
1620
1621
for (auto &Entry : Groups) {
1621
1622
OS << Entry.getKey () << " \n " ;
1622
1623
}
1623
1624
OS << " Module groups end.\n " ;
1624
1625
}
1625
- void printText (StringRef Text) override { } // Drop Declarations.
1626
1626
1627
- void printDeclPre (const Decl *D) override {
1628
- StreamPrinter::printDeclPre (D);
1629
- // If we have raw comment, we should have group name.
1630
- if (!D-> getRawComment ().Comments . empty ()) {
1631
- StringRef Name = D-> getGroupName (). getValue ( );
1632
- Groups. insert (Name. empty () ? " <NULL> " : Name);
1627
+ void addDecl (const Decl *D) {
1628
+ if ( auto VD = dyn_cast<ValueDecl>(D)) {
1629
+ if (!VD-> isImplicit () && !VD-> isPrivateStdlibDecl ()) {
1630
+ StringRef Name = VD-> getGroupName ().getValue ();
1631
+ Groups. insert (Name. empty () ? " <NULL> " : Name );
1632
+ }
1633
1633
}
1634
1634
}
1635
1635
};
1636
1636
1637
+ static int doPrintModuleGroups (const CompilerInvocation &InitInvok,
1638
+ const std::vector<std::string> ModulesToPrint) {
1639
+ CompilerInvocation Invocation (InitInvok);
1640
+
1641
+ CompilerInstance CI;
1642
+ // Display diagnostics to stderr.
1643
+ PrintingDiagnosticConsumer PrintDiags;
1644
+ CI.addDiagnosticConsumer (&PrintDiags);
1645
+ if (CI.setup (Invocation))
1646
+ return 1 ;
1647
+
1648
+ auto &Context = CI.getASTContext ();
1649
+
1650
+ // Load standard library so that Clang importer can use it.
1651
+ auto *Stdlib = getModuleByFullName (Context, Context.StdlibModuleName );
1652
+ if (!Stdlib) {
1653
+ llvm::errs () << " Failed loading stdlib\n " ;
1654
+ return 1 ;
1655
+ }
1656
+
1657
+ int ExitCode = 0 ;
1658
+ for (StringRef ModuleToPrint : ModulesToPrint) {
1659
+ if (ModuleToPrint.empty ()) {
1660
+ ExitCode = 1 ;
1661
+ continue ;
1662
+ }
1663
+
1664
+ // Get the (sub)module to print.
1665
+ auto *M = getModuleByFullName (Context, ModuleToPrint);
1666
+ if (!M) {
1667
+ ExitCode = 1 ;
1668
+ continue ;
1669
+ }
1670
+ {
1671
+ GroupNamesPrinter Printer (llvm::outs ());
1672
+ llvm::SmallVector<Decl*, 256 > Results;
1673
+ M->getDisplayDecls (Results);
1674
+ for (auto R : Results) {
1675
+ Printer.addDecl (R);
1676
+ }
1677
+ }
1678
+ }
1679
+ return ExitCode;
1680
+ }
1681
+
1637
1682
static int doPrintModules (const CompilerInvocation &InitInvok,
1638
1683
const std::vector<std::string> ModulesToPrint,
1639
1684
const std::vector<std::string> GroupsToPrint,
1640
1685
ide::ModuleTraversalOptions TraversalOptions,
1641
1686
const PrintOptions &Options,
1642
1687
bool AnnotatePrint,
1643
- bool SynthesizeExtensions,
1644
- bool PrintGroupNames) {
1688
+ bool SynthesizeExtensions) {
1645
1689
CompilerInvocation Invocation (InitInvok);
1646
1690
1647
1691
CompilerInstance CI;
@@ -1663,9 +1707,7 @@ static int doPrintModules(const CompilerInvocation &InitInvok,
1663
1707
int ExitCode = 0 ;
1664
1708
1665
1709
std::unique_ptr<ASTPrinter> Printer;
1666
- if (PrintGroupNames)
1667
- Printer.reset (new GroupNamesPrinter (llvm::outs ()));
1668
- else if (AnnotatePrint)
1710
+ if (AnnotatePrint)
1669
1711
Printer.reset (new AnnotatingPrinter (llvm::outs ()));
1670
1712
else
1671
1713
Printer.reset (new StreamPrinter (llvm::outs ()));
@@ -2706,11 +2748,13 @@ int main(int argc, char *argv[]) {
2706
2748
if (options::ModulePrintSkipOverlay)
2707
2749
TraversalOptions |= ide::ModuleTraversal::SkipOverlay;
2708
2750
2709
- ExitCode = doPrintModules (
2751
+ if (options::Action == ActionType::PrintModuleGroups)
2752
+ ExitCode = doPrintModuleGroups (InitInvok, options::ModuleToPrint);
2753
+ else
2754
+ ExitCode = doPrintModules (
2710
2755
InitInvok, options::ModuleToPrint, options::ModuleGroupToPrint,
2711
2756
TraversalOptions, PrintOpts, options::AnnotatePrint,
2712
- options::SynthesizeExtension,
2713
- options::Action == ActionType::PrintModuleGroups);
2757
+ options::SynthesizeExtension);
2714
2758
break ;
2715
2759
}
2716
2760
0 commit comments