Skip to content

Commit 6c2414b

Browse files
committed
[clang][Frontend] Add overload to ASTPrinter that doesn't own output stream (llvm#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. (cherry picked from commit 41d6343)
1 parent 0d47fa0 commit 6c2414b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

clang/include/clang/Frontend/ASTConsumers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ CreateASTDumper(std::unique_ptr<raw_ostream> OS, StringRef FilterString,
3535
bool DumpDecls, bool Deserialize, bool DumpLookups,
3636
bool DumpDeclTypes, ASTDumpOutputFormat Format);
3737

38+
std::unique_ptr<ASTConsumer>
39+
CreateASTDumper(raw_ostream &OS, StringRef FilterString, bool DumpDecls,
40+
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
41+
ASTDumpOutputFormat Format);
42+
3843
// AST Decl node lister: prints qualified names of all filterable AST Decl
3944
// nodes.
4045
std::unique_ptr<ASTConsumer> CreateASTDeclNodeLister();

clang/lib/Frontend/ASTConsumers.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ namespace {
4141
OutputKind(K), OutputFormat(Format), FilterString(FilterString),
4242
DumpLookups(DumpLookups), DumpDeclTypes(DumpDeclTypes) {}
4343

44+
ASTPrinter(raw_ostream &Out, Kind K, ASTDumpOutputFormat Format,
45+
StringRef FilterString, bool DumpLookups = false,
46+
bool DumpDeclTypes = false)
47+
: Out(Out), OwnedOut(nullptr), OutputKind(K), OutputFormat(Format),
48+
FilterString(FilterString), DumpLookups(DumpLookups),
49+
DumpDeclTypes(DumpDeclTypes) {}
50+
4451
void HandleTranslationUnit(ASTContext &Context) override {
4552
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
4653

@@ -175,6 +182,19 @@ clang::CreateASTDumper(std::unique_ptr<raw_ostream> Out, StringRef FilterString,
175182
Format, FilterString, DumpLookups, DumpDeclTypes);
176183
}
177184

185+
std::unique_ptr<ASTConsumer>
186+
clang::CreateASTDumper(raw_ostream &Out, StringRef FilterString, bool DumpDecls,
187+
bool Deserialize, bool DumpLookups, bool DumpDeclTypes,
188+
ASTDumpOutputFormat Format) {
189+
assert((DumpDecls || Deserialize || DumpLookups) && "nothing to dump");
190+
return std::make_unique<ASTPrinter>(Out,
191+
Deserialize ? ASTPrinter::DumpFull
192+
: DumpDecls ? ASTPrinter::Dump
193+
: ASTPrinter::None,
194+
Format, FilterString, DumpLookups,
195+
DumpDeclTypes);
196+
}
197+
178198
std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
179199
return std::make_unique<ASTDeclNodeLister>(nullptr);
180200
}

0 commit comments

Comments
 (0)