[clang][Frontend] Add overload to ASTPrinter that doesn't own output stream (#142163)

We're planning on using the ASTPrinter in LLDB for AST dumping. But it
currently takes the output stream via `unique_ptr`. In LLDB we don't
have the output stream available in this form and instead it would be
convenient if we could just pass a reference to the stream.

This patch adds that overload.
This commit is contained in:
Michael Buch 2025-06-02 10:19:24 +01:00 committed by GitHub
parent 8f3ccd1674
commit 41d6343978
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -35,6 +35,11 @@ CreateASTDumper(std::unique_ptr<raw_ostream> OS, StringRef FilterString,
bool DumpDecls, bool Deserialize, bool DumpLookups,
bool DumpDeclTypes, ASTDumpOutputFormat Format);
std::unique_ptr<ASTConsumer>
CreateASTDumper(raw_ostream &OS, StringRef FilterString, bool DumpDecls,
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
ASTDumpOutputFormat Format);
// AST Decl node lister: prints qualified names of all filterable AST Decl
// nodes.
std::unique_ptr<ASTConsumer> CreateASTDeclNodeLister();

View File

@ -38,6 +38,13 @@ namespace {
OutputKind(K), OutputFormat(Format), FilterString(FilterString),
DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
ASTPrinter(raw_ostream &Out, Kind K, ASTDumpOutputFormat Format,
StringRef FilterString, bool DumpLookups = false,
bool DumpDeclTypes = false)
: Out(Out), OwnedOut(nullptr), OutputKind(K), OutputFormat(Format),
FilterString(FilterString), DumpLookups(DumpLookups),
DumpDeclTypes(DumpDeclTypes) {}
void HandleTranslationUnit(ASTContext &Context) override {
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
@ -173,6 +180,19 @@ clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out, StringRef FilterString,
Format, FilterString, DumpLookups, DumpDeclTypes);
}
std::unique_ptr<ASTConsumer>
clang::CreateASTDumper(raw_ostream &Out, StringRef FilterString, bool DumpDecls,
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
ASTDumpOutputFormat Format) {
assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump");
return std::make_unique<ASTPrinter>(Out,
Deserialize ? ASTPrinter::DumpFull
: DumpDecls ? ASTPrinter::Dump
: ASTPrinter::None,
Format, FilterString, DumpLookups,
DumpDeclTypes);
}
std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
return std::make_unique<ASTDeclNodeLister>(nullptr);
}