@@ -81,6 +81,7 @@ enum class ActionType {
81
81
PrintModule,
82
82
PrintHeader,
83
83
PrintSwiftFileInterface,
84
+ PrintDecl,
84
85
PrintTypes,
85
86
PrintComments,
86
87
PrintModuleComments,
@@ -169,6 +170,8 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
169
170
" print-header" , " Print visible declarations in a header file" ),
170
171
clEnumValN(ActionType::PrintSwiftFileInterface,
171
172
" print-swift-file-interface" , " Print interface of a swift file" ),
173
+ clEnumValN(ActionType::PrintDecl,
174
+ " print-decl" , " Print interface of a decl" ),
172
175
clEnumValN(ActionType::PrintTypes,
173
176
" print-types" , " Print types of all subexpressions and declarations in the AST" ),
174
177
clEnumValN(ActionType::PrintComments,
@@ -522,6 +525,10 @@ static llvm::cl::list<std::string>
522
525
HeaderToPrint (" header-to-print" ,
523
526
llvm::cl::desc (" Header filename to print swift interface for" ));
524
527
528
+ static llvm::cl::list<std::string>
529
+ DeclToPrint (" decl-to-print" ,
530
+ llvm::cl::desc (" Decl name to print swift interface for" ));
531
+
525
532
static llvm::cl::opt<std::string>
526
533
LineColumnPair (" pos" , llvm::cl::desc(" Line:Column pair" ));
527
534
@@ -1889,6 +1896,59 @@ static int doPrintSwiftFileInterface(const CompilerInvocation &InitInvok,
1889
1896
return 0 ;
1890
1897
}
1891
1898
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
+
1892
1952
namespace {
1893
1953
class ASTTypePrinter : public ASTWalker {
1894
1954
raw_ostream &OS;
@@ -2901,6 +2961,13 @@ int main(int argc, char *argv[]) {
2901
2961
break ;
2902
2962
}
2903
2963
2964
+ case ActionType::PrintDecl: {
2965
+ ExitCode = doPrintDecls (
2966
+ InitInvok, options::SourceFilename,
2967
+ options::DeclToPrint, PrintOpts, options::AnnotatePrint);
2968
+ break ;
2969
+ }
2970
+
2904
2971
case ActionType::PrintTypes:
2905
2972
ExitCode = doPrintTypes (InitInvok, options::SourceFilename,
2906
2973
options::FullyQualifiedTypes);
0 commit comments