Skip to content

Commit c81482b

Browse files
committed
[ASTDumper][NFC] Prevent direct printing
Only PrintBase can access the raw_ostream and indentation directly, so all direct I/O is now isolated to that class. I think this will help us to eventually support multiple output formats (e.g. JSON).
1 parent a96995c commit c81482b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ static Type defaultGetTypeOfKeyPathComponent(KeyPathExpr *E, unsigned index) {
371371

372372
namespace {
373373
class PrintBase {
374-
public:
375374
raw_ostream &OS;
376375
unsigned Indent;
376+
public:
377377
llvm::function_ref<Type(Expr *)> GetTypeOfExpr;
378378
llvm::function_ref<Type(TypeRepr *)> GetTypeOfTypeRepr;
379379
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
@@ -388,6 +388,10 @@ class PrintBase {
388388
GetTypeOfTypeRepr(getTypeOfTypeRepr),
389389
GetTypeOfKeyPathComponent(getTypeOfKeyPathComponent) { }
390390

391+
bool hasNonStandardOutput() {
392+
return &OS != &llvm::errs() && &OS != &llvm::dbgs();
393+
}
394+
391395
template <typename Fn>
392396
void printRecRaw(Fn Body, StringRef label = "") {
393397
Indent += 2;
@@ -869,7 +873,7 @@ class PrintBase {
869873
void visitExtensionDecl(ExtensionDecl *ED, StringRef label) {
870874
printCommon(ED, "extension_decl", label, ExtensionColor);
871875
printFlag(!ED->hasBeenBound(), "unbound");
872-
printNameRaw([&](raw_ostream &os) {
876+
printNameRaw([&](raw_ostream &OS) {
873877
if (ED->hasBeenBound())
874878
ED->getExtendedType().print(OS);
875879
else
@@ -2031,7 +2035,6 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
20312035

20322036
void visitOtherConstructorDeclRefExpr(OtherConstructorDeclRefExpr *E, StringRef label) {
20332037
printCommon(E, "other_constructor_ref_expr", label);
2034-
PrintWithColorRAII(OS, DeclColor);
20352038
printDeclRefField("decl", E->getDeclRef());
20362039
printFoot();
20372040
}
@@ -2496,7 +2499,7 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
24962499

24972500
// If we aren't printing to standard error or the debugger output stream,
24982501
// this client expects to see the computed discriminator. Compute it now.
2499-
if (&OS != &llvm::errs() && &OS != &llvm::dbgs())
2502+
if (hasNonStandardOutput())
25002503
(void)E->getDiscriminator();
25012504

25022505
printField("discriminator", E->getRawDiscriminator(), DiscriminatorColor);
@@ -2518,7 +2521,6 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
25182521
}
25192522

25202523
if (!E->getCaptureInfo().isTrivial()) {
2521-
OS << " ";
25222524
printFieldRaw("", [&](raw_ostream &OS) {
25232525
E->getCaptureInfo().print(OS);
25242526
}, CapturesColor);

0 commit comments

Comments
 (0)