Skip to content

Commit e1d3183

Browse files
committed
[swift-ide-test] Introduce '-print-decl' option.
This is helpful for understanding how specific APIs are imported.
1 parent 9756755 commit e1d3183

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum class ActionType {
8181
PrintModule,
8282
PrintHeader,
8383
PrintSwiftFileInterface,
84+
PrintDecl,
8485
PrintTypes,
8586
PrintComments,
8687
PrintModuleComments,
@@ -169,6 +170,8 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
169170
"print-header", "Print visible declarations in a header file"),
170171
clEnumValN(ActionType::PrintSwiftFileInterface,
171172
"print-swift-file-interface", "Print interface of a swift file"),
173+
clEnumValN(ActionType::PrintDecl,
174+
"print-decl", "Print interface of a decl"),
172175
clEnumValN(ActionType::PrintTypes,
173176
"print-types", "Print types of all subexpressions and declarations in the AST"),
174177
clEnumValN(ActionType::PrintComments,
@@ -522,6 +525,10 @@ static llvm::cl::list<std::string>
522525
HeaderToPrint("header-to-print",
523526
llvm::cl::desc("Header filename to print swift interface for"));
524527

528+
static llvm::cl::list<std::string>
529+
DeclToPrint("decl-to-print",
530+
llvm::cl::desc("Decl name to print swift interface for"));
531+
525532
static llvm::cl::opt<std::string>
526533
LineColumnPair("pos", llvm::cl::desc("Line:Column pair"));
527534

@@ -1889,6 +1896,59 @@ static int doPrintSwiftFileInterface(const CompilerInvocation &InitInvok,
18891896
return 0;
18901897
}
18911898

1899+
static int doPrintDecls(const CompilerInvocation &InitInvok,
1900+
StringRef SourceFilename,
1901+
ArrayRef<std::string> DeclsToPrint,
1902+
const PrintOptions &Options,
1903+
bool AnnotatePrint) {
1904+
CompilerInvocation Invocation(InitInvok);
1905+
Invocation.addInputFilename(SourceFilename);
1906+
Invocation.getFrontendOptions().PrimaryInput = 0;
1907+
Invocation.getLangOptions().AttachCommentsToDecls = true;
1908+
CompilerInstance CI;
1909+
// Display diagnostics to stderr.
1910+
PrintingDiagnosticConsumer PrintDiags;
1911+
CI.addDiagnosticConsumer(&PrintDiags);
1912+
if (CI.setup(Invocation))
1913+
return 1;
1914+
CI.performSema();
1915+
1916+
std::unique_ptr<ASTPrinter> Printer;
1917+
if (AnnotatePrint)
1918+
Printer.reset(new AnnotatingPrinter(llvm::outs()));
1919+
else
1920+
Printer.reset(new StreamPrinter(llvm::outs()));
1921+
1922+
for (const auto &name : DeclsToPrint) {
1923+
ASTContext &ctx = CI.getASTContext();
1924+
UnqualifiedLookup lookup(ctx.getIdentifier(name),
1925+
CI.getPrimarySourceFile(), nullptr);
1926+
for (auto result : lookup.Results) {
1927+
result.getValueDecl()->print(*Printer, Options);
1928+
1929+
if (auto typeDecl = dyn_cast<TypeDecl>(result.getValueDecl())) {
1930+
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
1931+
TypeDecl *origTypeDecl = typeAliasDecl->getUnderlyingType()
1932+
->getNominalOrBoundGenericNominal();
1933+
if (origTypeDecl) {
1934+
origTypeDecl->print(*Printer, Options);
1935+
typeDecl = origTypeDecl;
1936+
}
1937+
}
1938+
1939+
// Print extensions.
1940+
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
1941+
for (auto extension : nominal->getExtensions()) {
1942+
extension->print(*Printer, Options);
1943+
}
1944+
}
1945+
}
1946+
}
1947+
}
1948+
1949+
return 0;
1950+
}
1951+
18921952
namespace {
18931953
class ASTTypePrinter : public ASTWalker {
18941954
raw_ostream &OS;
@@ -2901,6 +2961,13 @@ int main(int argc, char *argv[]) {
29012961
break;
29022962
}
29032963

2964+
case ActionType::PrintDecl: {
2965+
ExitCode = doPrintDecls(
2966+
InitInvok, options::SourceFilename,
2967+
options::DeclToPrint, PrintOpts, options::AnnotatePrint);
2968+
break;
2969+
}
2970+
29042971
case ActionType::PrintTypes:
29052972
ExitCode = doPrintTypes(InitInvok, options::SourceFilename,
29062973
options::FullyQualifiedTypes);

0 commit comments

Comments
 (0)