1 //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AST dump methods, which dump out the
11 // AST in a form that exposes type details and other fields.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CommentVisitor.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclLookups.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/DeclVisitor.h"
23 #include "clang/AST/LocInfoType.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TypeVisitor.h"
26 #include "clang/Basic/Builtins.h"
27 #include "clang/Basic/Module.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "llvm/Support/raw_ostream.h"
30 using namespace clang;
31 using namespace clang::comments;
32 
33 //===----------------------------------------------------------------------===//
34 // ASTDumper Visitor
35 //===----------------------------------------------------------------------===//
36 
37 namespace  {
38   // Colors used for various parts of the AST dump
39   // Do not use bold yellow for any text.  It is hard to read on white screens.
40 
41   struct TerminalColor {
42     raw_ostream::Colors Color;
43     bool Bold;
44   };
45 
46   // Red           - CastColor
47   // Green         - TypeColor
48   // Bold Green    - DeclKindNameColor, UndeserializedColor
49   // Yellow        - AddressColor, LocationColor
50   // Blue          - CommentColor, NullColor, IndentColor
51   // Bold Blue     - AttrColor
52   // Bold Magenta  - StmtColor
53   // Cyan          - ValueKindColor, ObjectKindColor
54   // Bold Cyan     - ValueColor, DeclNameColor
55 
56   // Decl kind names (VarDecl, FunctionDecl, etc)
57   static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
58   // Attr names (CleanupAttr, GuardedByAttr, etc)
59   static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
60   // Statement names (DeclStmt, ImplicitCastExpr, etc)
61   static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
62   // Comment names (FullComment, ParagraphComment, TextComment, etc)
63   static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
64 
65   // Type names (int, float, etc, plus user defined types)
66   static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
67 
68   // Pointer address
69   static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
70   // Source locations
71   static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
72 
73   // lvalue/xvalue
74   static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
75   // bitfield/objcproperty/objcsubscript/vectorcomponent
76   static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
77 
78   // Null statements
79   static const TerminalColor NullColor = { raw_ostream::BLUE, false };
80 
81   // Undeserialized entities
82   static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
83 
84   // CastKind from CastExpr's
85   static const TerminalColor CastColor = { raw_ostream::RED, false };
86 
87   // Value of the statement
88   static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
89   // Decl names
90   static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
91 
92   // Indents ( `, -. | )
93   static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
94 
95   class ASTDumper
96       : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
97         public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
98     raw_ostream &OS;
99     const CommandTraits *Traits;
100     const SourceManager *SM;
101 
102     /// Pending[i] is an action to dump an entity at level i.
103     llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
104 
105     /// Indicates whether we're at the top level.
106     bool TopLevel;
107 
108     /// Indicates if we're handling the first child after entering a new depth.
109     bool FirstChild;
110 
111     /// Prefix for currently-being-dumped entity.
112     std::string Prefix;
113 
114     /// Keep track of the last location we print out so that we can
115     /// print out deltas from then on out.
116     const char *LastLocFilename;
117     unsigned LastLocLine;
118 
119     /// The \c FullComment parent of the comment being dumped.
120     const FullComment *FC;
121 
122     bool ShowColors;
123 
124     /// Dump a child of the current node.
125     template<typename Fn> void dumpChild(Fn doDumpChild) {
126       // If we're at the top level, there's nothing interesting to do; just
127       // run the dumper.
128       if (TopLevel) {
129         TopLevel = false;
130         doDumpChild();
131         while (!Pending.empty()) {
132           Pending.back()(true);
133           Pending.pop_back();
134         }
135         Prefix.clear();
136         OS << "\n";
137         TopLevel = true;
138         return;
139       }
140 
141       const FullComment *OrigFC = FC;
142       auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
143         // Print out the appropriate tree structure and work out the prefix for
144         // children of this node. For instance:
145         //
146         //   A        Prefix = ""
147         //   |-B      Prefix = "| "
148         //   | `-C    Prefix = "|   "
149         //   `-D      Prefix = "  "
150         //     |-E    Prefix = "  | "
151         //     `-F    Prefix = "    "
152         //   G        Prefix = ""
153         //
154         // Note that the first level gets no prefix.
155         {
156           OS << '\n';
157           ColorScope Color(*this, IndentColor);
158           OS << Prefix << (isLastChild ? '`' : '|') << '-';
159           this->Prefix.push_back(isLastChild ? ' ' : '|');
160           this->Prefix.push_back(' ');
161         }
162 
163         FirstChild = true;
164         unsigned Depth = Pending.size();
165 
166         FC = OrigFC;
167         doDumpChild();
168 
169         // If any children are left, they're the last at their nesting level.
170         // Dump those ones out now.
171         while (Depth < Pending.size()) {
172           Pending.back()(true);
173           this->Pending.pop_back();
174         }
175 
176         // Restore the old prefix.
177         this->Prefix.resize(Prefix.size() - 2);
178       };
179 
180       if (FirstChild) {
181         Pending.push_back(std::move(dumpWithIndent));
182       } else {
183         Pending.back()(false);
184         Pending.back() = std::move(dumpWithIndent);
185       }
186       FirstChild = false;
187     }
188 
189     class ColorScope {
190       ASTDumper &Dumper;
191     public:
192       ColorScope(ASTDumper &Dumper, TerminalColor Color)
193         : Dumper(Dumper) {
194         if (Dumper.ShowColors)
195           Dumper.OS.changeColor(Color.Color, Color.Bold);
196       }
197       ~ColorScope() {
198         if (Dumper.ShowColors)
199           Dumper.OS.resetColor();
200       }
201     };
202 
203   public:
204     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
205               const SourceManager *SM)
206       : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
207         LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
208         ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
209 
210     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
211               const SourceManager *SM, bool ShowColors)
212       : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
213         LastLocFilename(""), LastLocLine(~0U),
214         ShowColors(ShowColors) { }
215 
216     void dumpDecl(const Decl *D);
217     void dumpStmt(const Stmt *S);
218     void dumpFullComment(const FullComment *C);
219 
220     // Utilities
221     void dumpPointer(const void *Ptr);
222     void dumpSourceRange(SourceRange R);
223     void dumpLocation(SourceLocation Loc);
224     void dumpBareType(QualType T, bool Desugar = true);
225     void dumpType(QualType T);
226     void dumpTypeAsChild(QualType T);
227     void dumpTypeAsChild(const Type *T);
228     void dumpBareDeclRef(const Decl *Node);
229     void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
230     void dumpName(const NamedDecl *D);
231     bool hasNodes(const DeclContext *DC);
232     void dumpDeclContext(const DeclContext *DC);
233     void dumpLookups(const DeclContext *DC, bool DumpDecls);
234     void dumpAttr(const Attr *A);
235 
236     // C++ Utilities
237     void dumpAccessSpecifier(AccessSpecifier AS);
238     void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
239     void dumpTemplateParameters(const TemplateParameterList *TPL);
240     void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
241     void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
242     void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
243     void dumpTemplateArgument(const TemplateArgument &A,
244                               SourceRange R = SourceRange());
245 
246     // Objective-C utilities.
247     void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
248 
249     // Types
250     void VisitComplexType(const ComplexType *T) {
251       dumpTypeAsChild(T->getElementType());
252     }
253     void VisitPointerType(const PointerType *T) {
254       dumpTypeAsChild(T->getPointeeType());
255     }
256     void VisitBlockPointerType(const BlockPointerType *T) {
257       dumpTypeAsChild(T->getPointeeType());
258     }
259     void VisitReferenceType(const ReferenceType *T) {
260       dumpTypeAsChild(T->getPointeeType());
261     }
262     void VisitRValueReferenceType(const ReferenceType *T) {
263       if (T->isSpelledAsLValue())
264         OS << " written as lvalue reference";
265       VisitReferenceType(T);
266     }
267     void VisitMemberPointerType(const MemberPointerType *T) {
268       dumpTypeAsChild(T->getClass());
269       dumpTypeAsChild(T->getPointeeType());
270     }
271     void VisitArrayType(const ArrayType *T) {
272       switch (T->getSizeModifier()) {
273         case ArrayType::Normal: break;
274         case ArrayType::Static: OS << " static"; break;
275         case ArrayType::Star: OS << " *"; break;
276       }
277       OS << " " << T->getIndexTypeQualifiers().getAsString();
278       dumpTypeAsChild(T->getElementType());
279     }
280     void VisitConstantArrayType(const ConstantArrayType *T) {
281       OS << " " << T->getSize();
282       VisitArrayType(T);
283     }
284     void VisitVariableArrayType(const VariableArrayType *T) {
285       OS << " ";
286       dumpSourceRange(T->getBracketsRange());
287       VisitArrayType(T);
288       dumpStmt(T->getSizeExpr());
289     }
290     void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
291       VisitArrayType(T);
292       OS << " ";
293       dumpSourceRange(T->getBracketsRange());
294       dumpStmt(T->getSizeExpr());
295     }
296     void VisitDependentSizedExtVectorType(
297         const DependentSizedExtVectorType *T) {
298       OS << " ";
299       dumpLocation(T->getAttributeLoc());
300       dumpTypeAsChild(T->getElementType());
301       dumpStmt(T->getSizeExpr());
302     }
303     void VisitVectorType(const VectorType *T) {
304       switch (T->getVectorKind()) {
305         case VectorType::GenericVector: break;
306         case VectorType::AltiVecVector: OS << " altivec"; break;
307         case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
308         case VectorType::AltiVecBool: OS << " altivec bool"; break;
309         case VectorType::NeonVector: OS << " neon"; break;
310         case VectorType::NeonPolyVector: OS << " neon poly"; break;
311       }
312       OS << " " << T->getNumElements();
313       dumpTypeAsChild(T->getElementType());
314     }
315     void VisitFunctionType(const FunctionType *T) {
316       auto EI = T->getExtInfo();
317       if (EI.getNoReturn()) OS << " noreturn";
318       if (EI.getProducesResult()) OS << " produces_result";
319       if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
320       OS << " " << FunctionType::getNameForCallConv(EI.getCC());
321       dumpTypeAsChild(T->getReturnType());
322     }
323     void VisitFunctionProtoType(const FunctionProtoType *T) {
324       auto EPI = T->getExtProtoInfo();
325       if (EPI.HasTrailingReturn) OS << " trailing_return";
326       if (T->isConst()) OS << " const";
327       if (T->isVolatile()) OS << " volatile";
328       if (T->isRestrict()) OS << " restrict";
329       switch (EPI.RefQualifier) {
330         case RQ_None: break;
331         case RQ_LValue: OS << " &"; break;
332         case RQ_RValue: OS << " &&"; break;
333       }
334       // FIXME: Exception specification.
335       // FIXME: Consumed parameters.
336       VisitFunctionType(T);
337       for (QualType PT : T->getParamTypes())
338         dumpTypeAsChild(PT);
339       if (EPI.Variadic)
340         dumpChild([=] { OS << "..."; });
341     }
342     void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
343       dumpDeclRef(T->getDecl());
344     }
345     void VisitTypedefType(const TypedefType *T) {
346       dumpDeclRef(T->getDecl());
347     }
348     void VisitTypeOfExprType(const TypeOfExprType *T) {
349       dumpStmt(T->getUnderlyingExpr());
350     }
351     void VisitDecltypeType(const DecltypeType *T) {
352       dumpStmt(T->getUnderlyingExpr());
353     }
354     void VisitUnaryTransformType(const UnaryTransformType *T) {
355       switch (T->getUTTKind()) {
356       case UnaryTransformType::EnumUnderlyingType:
357         OS << " underlying_type";
358         break;
359       }
360       dumpTypeAsChild(T->getBaseType());
361     }
362     void VisitTagType(const TagType *T) {
363       dumpDeclRef(T->getDecl());
364     }
365     void VisitAttributedType(const AttributedType *T) {
366       // FIXME: AttrKind
367       dumpTypeAsChild(T->getModifiedType());
368     }
369     void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
370       OS << " depth " << T->getDepth() << " index " << T->getIndex();
371       if (T->isParameterPack()) OS << " pack";
372       dumpDeclRef(T->getDecl());
373     }
374     void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
375       dumpTypeAsChild(T->getReplacedParameter());
376     }
377     void VisitSubstTemplateTypeParmPackType(
378         const SubstTemplateTypeParmPackType *T) {
379       dumpTypeAsChild(T->getReplacedParameter());
380       dumpTemplateArgument(T->getArgumentPack());
381     }
382     void VisitAutoType(const AutoType *T) {
383       if (T->isDecltypeAuto()) OS << " decltype(auto)";
384       if (!T->isDeduced())
385         OS << " undeduced";
386     }
387     void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
388       if (T->isTypeAlias()) OS << " alias";
389       OS << " "; T->getTemplateName().dump(OS);
390       for (auto &Arg : *T)
391         dumpTemplateArgument(Arg);
392       if (T->isTypeAlias())
393         dumpTypeAsChild(T->getAliasedType());
394     }
395     void VisitInjectedClassNameType(const InjectedClassNameType *T) {
396       dumpDeclRef(T->getDecl());
397     }
398     void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
399       dumpDeclRef(T->getDecl());
400     }
401     void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
402       dumpTypeAsChild(T->getPointeeType());
403     }
404     void VisitAtomicType(const AtomicType *T) {
405       dumpTypeAsChild(T->getValueType());
406     }
407     void VisitPipeType(const PipeType *T) {
408       dumpTypeAsChild(T->getElementType());
409     }
410     void VisitAdjustedType(const AdjustedType *T) {
411       dumpTypeAsChild(T->getOriginalType());
412     }
413     void VisitPackExpansionType(const PackExpansionType *T) {
414       if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
415       if (!T->isSugared())
416         dumpTypeAsChild(T->getPattern());
417     }
418     // FIXME: ElaboratedType, DependentNameType,
419     // DependentTemplateSpecializationType, ObjCObjectType
420 
421     // Decls
422     void VisitLabelDecl(const LabelDecl *D);
423     void VisitTypedefDecl(const TypedefDecl *D);
424     void VisitEnumDecl(const EnumDecl *D);
425     void VisitRecordDecl(const RecordDecl *D);
426     void VisitEnumConstantDecl(const EnumConstantDecl *D);
427     void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
428     void VisitFunctionDecl(const FunctionDecl *D);
429     void VisitFieldDecl(const FieldDecl *D);
430     void VisitVarDecl(const VarDecl *D);
431     void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
432     void VisitImportDecl(const ImportDecl *D);
433     void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
434     void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
435     void VisitCapturedDecl(const CapturedDecl *D);
436 
437     // OpenMP decls
438     void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
439     void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
440     void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
441 
442     // C++ Decls
443     void VisitNamespaceDecl(const NamespaceDecl *D);
444     void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
445     void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
446     void VisitTypeAliasDecl(const TypeAliasDecl *D);
447     void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
448     void VisitCXXRecordDecl(const CXXRecordDecl *D);
449     void VisitStaticAssertDecl(const StaticAssertDecl *D);
450     template<typename SpecializationDecl>
451     void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
452                                          bool DumpExplicitInst,
453                                          bool DumpRefOnly);
454     template<typename TemplateDecl>
455     void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
456     void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
457     void VisitClassTemplateDecl(const ClassTemplateDecl *D);
458     void VisitClassTemplateSpecializationDecl(
459         const ClassTemplateSpecializationDecl *D);
460     void VisitClassTemplatePartialSpecializationDecl(
461         const ClassTemplatePartialSpecializationDecl *D);
462     void VisitClassScopeFunctionSpecializationDecl(
463         const ClassScopeFunctionSpecializationDecl *D);
464     void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
465     void VisitVarTemplateDecl(const VarTemplateDecl *D);
466     void VisitVarTemplateSpecializationDecl(
467         const VarTemplateSpecializationDecl *D);
468     void VisitVarTemplatePartialSpecializationDecl(
469         const VarTemplatePartialSpecializationDecl *D);
470     void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
471     void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
472     void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
473     void VisitUsingDecl(const UsingDecl *D);
474     void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
475     void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
476     void VisitUsingShadowDecl(const UsingShadowDecl *D);
477     void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
478     void VisitAccessSpecDecl(const AccessSpecDecl *D);
479     void VisitFriendDecl(const FriendDecl *D);
480 
481     // ObjC Decls
482     void VisitObjCIvarDecl(const ObjCIvarDecl *D);
483     void VisitObjCMethodDecl(const ObjCMethodDecl *D);
484     void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
485     void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
486     void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
487     void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
488     void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
489     void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
490     void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
491     void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
492     void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
493     void VisitBlockDecl(const BlockDecl *D);
494 
495     // Stmts.
496     void VisitStmt(const Stmt *Node);
497     void VisitDeclStmt(const DeclStmt *Node);
498     void VisitAttributedStmt(const AttributedStmt *Node);
499     void VisitLabelStmt(const LabelStmt *Node);
500     void VisitGotoStmt(const GotoStmt *Node);
501     void VisitCXXCatchStmt(const CXXCatchStmt *Node);
502     void VisitCapturedStmt(const CapturedStmt *Node);
503 
504     // OpenMP
505     void VisitOMPExecutableDirective(const OMPExecutableDirective *Node);
506 
507     // Exprs
508     void VisitExpr(const Expr *Node);
509     void VisitCastExpr(const CastExpr *Node);
510     void VisitDeclRefExpr(const DeclRefExpr *Node);
511     void VisitPredefinedExpr(const PredefinedExpr *Node);
512     void VisitCharacterLiteral(const CharacterLiteral *Node);
513     void VisitIntegerLiteral(const IntegerLiteral *Node);
514     void VisitFloatingLiteral(const FloatingLiteral *Node);
515     void VisitStringLiteral(const StringLiteral *Str);
516     void VisitInitListExpr(const InitListExpr *ILE);
517     void VisitUnaryOperator(const UnaryOperator *Node);
518     void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
519     void VisitMemberExpr(const MemberExpr *Node);
520     void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
521     void VisitBinaryOperator(const BinaryOperator *Node);
522     void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
523     void VisitAddrLabelExpr(const AddrLabelExpr *Node);
524     void VisitBlockExpr(const BlockExpr *Node);
525     void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
526 
527     // C++
528     void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
529     void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
530     void VisitCXXThisExpr(const CXXThisExpr *Node);
531     void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
532     void VisitCXXConstructExpr(const CXXConstructExpr *Node);
533     void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
534     void VisitCXXNewExpr(const CXXNewExpr *Node);
535     void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
536     void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
537     void VisitExprWithCleanups(const ExprWithCleanups *Node);
538     void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
539     void dumpCXXTemporary(const CXXTemporary *Temporary);
540     void VisitLambdaExpr(const LambdaExpr *Node) {
541       VisitExpr(Node);
542       dumpDecl(Node->getLambdaClass());
543     }
544     void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
545 
546     // ObjC
547     void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
548     void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
549     void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
550     void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
551     void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
552     void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
553     void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
554     void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
555     void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
556     void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
557 
558     // Comments.
559     const char *getCommandName(unsigned CommandID);
560     void dumpComment(const Comment *C);
561 
562     // Inline comments.
563     void visitTextComment(const TextComment *C);
564     void visitInlineCommandComment(const InlineCommandComment *C);
565     void visitHTMLStartTagComment(const HTMLStartTagComment *C);
566     void visitHTMLEndTagComment(const HTMLEndTagComment *C);
567 
568     // Block comments.
569     void visitBlockCommandComment(const BlockCommandComment *C);
570     void visitParamCommandComment(const ParamCommandComment *C);
571     void visitTParamCommandComment(const TParamCommandComment *C);
572     void visitVerbatimBlockComment(const VerbatimBlockComment *C);
573     void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
574     void visitVerbatimLineComment(const VerbatimLineComment *C);
575   };
576 }
577 
578 //===----------------------------------------------------------------------===//
579 //  Utilities
580 //===----------------------------------------------------------------------===//
581 
582 void ASTDumper::dumpPointer(const void *Ptr) {
583   ColorScope Color(*this, AddressColor);
584   OS << ' ' << Ptr;
585 }
586 
587 void ASTDumper::dumpLocation(SourceLocation Loc) {
588   if (!SM)
589     return;
590 
591   ColorScope Color(*this, LocationColor);
592   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
593 
594   // The general format we print out is filename:line:col, but we drop pieces
595   // that haven't changed since the last loc printed.
596   PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
597 
598   if (PLoc.isInvalid()) {
599     OS << "<invalid sloc>";
600     return;
601   }
602 
603   if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
604     OS << PLoc.getFilename() << ':' << PLoc.getLine()
605        << ':' << PLoc.getColumn();
606     LastLocFilename = PLoc.getFilename();
607     LastLocLine = PLoc.getLine();
608   } else if (PLoc.getLine() != LastLocLine) {
609     OS << "line" << ':' << PLoc.getLine()
610        << ':' << PLoc.getColumn();
611     LastLocLine = PLoc.getLine();
612   } else {
613     OS << "col" << ':' << PLoc.getColumn();
614   }
615 }
616 
617 void ASTDumper::dumpSourceRange(SourceRange R) {
618   // Can't translate locations if a SourceManager isn't available.
619   if (!SM)
620     return;
621 
622   OS << " <";
623   dumpLocation(R.getBegin());
624   if (R.getBegin() != R.getEnd()) {
625     OS << ", ";
626     dumpLocation(R.getEnd());
627   }
628   OS << ">";
629 
630   // <t2.c:123:421[blah], t2.c:412:321>
631 
632 }
633 
634 void ASTDumper::dumpBareType(QualType T, bool Desugar) {
635   ColorScope Color(*this, TypeColor);
636 
637   SplitQualType T_split = T.split();
638   OS << "'" << QualType::getAsString(T_split) << "'";
639 
640   if (Desugar && !T.isNull()) {
641     // If the type is sugared, also dump a (shallow) desugared type.
642     SplitQualType D_split = T.getSplitDesugaredType();
643     if (T_split != D_split)
644       OS << ":'" << QualType::getAsString(D_split) << "'";
645   }
646 }
647 
648 void ASTDumper::dumpType(QualType T) {
649   OS << ' ';
650   dumpBareType(T);
651 }
652 
653 void ASTDumper::dumpTypeAsChild(QualType T) {
654   SplitQualType SQT = T.split();
655   if (!SQT.Quals.hasQualifiers())
656     return dumpTypeAsChild(SQT.Ty);
657 
658   dumpChild([=] {
659     OS << "QualType";
660     dumpPointer(T.getAsOpaquePtr());
661     OS << " ";
662     dumpBareType(T, false);
663     OS << " " << T.split().Quals.getAsString();
664     dumpTypeAsChild(T.split().Ty);
665   });
666 }
667 
668 void ASTDumper::dumpTypeAsChild(const Type *T) {
669   dumpChild([=] {
670     if (!T) {
671       ColorScope Color(*this, NullColor);
672       OS << "<<<NULL>>>";
673       return;
674     }
675     if (const LocInfoType *LIT = llvm::dyn_cast<LocInfoType>(T)) {
676       {
677         ColorScope Color(*this, TypeColor);
678         OS << "LocInfo Type";
679       }
680       dumpPointer(T);
681       dumpTypeAsChild(LIT->getTypeSourceInfo()->getType());
682       return;
683     }
684 
685     {
686       ColorScope Color(*this, TypeColor);
687       OS << T->getTypeClassName() << "Type";
688     }
689     dumpPointer(T);
690     OS << " ";
691     dumpBareType(QualType(T, 0), false);
692 
693     QualType SingleStepDesugar =
694         T->getLocallyUnqualifiedSingleStepDesugaredType();
695     if (SingleStepDesugar != QualType(T, 0))
696       OS << " sugar";
697     if (T->isDependentType())
698       OS << " dependent";
699     else if (T->isInstantiationDependentType())
700       OS << " instantiation_dependent";
701     if (T->isVariablyModifiedType())
702       OS << " variably_modified";
703     if (T->containsUnexpandedParameterPack())
704       OS << " contains_unexpanded_pack";
705     if (T->isFromAST())
706       OS << " imported";
707 
708     TypeVisitor<ASTDumper>::Visit(T);
709 
710     if (SingleStepDesugar != QualType(T, 0))
711       dumpTypeAsChild(SingleStepDesugar);
712   });
713 }
714 
715 void ASTDumper::dumpBareDeclRef(const Decl *D) {
716   {
717     ColorScope Color(*this, DeclKindNameColor);
718     OS << D->getDeclKindName();
719   }
720   dumpPointer(D);
721 
722   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
723     ColorScope Color(*this, DeclNameColor);
724     OS << " '" << ND->getDeclName() << '\'';
725   }
726 
727   if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
728     dumpType(VD->getType());
729 }
730 
731 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
732   if (!D)
733     return;
734 
735   dumpChild([=]{
736     if (Label)
737       OS << Label << ' ';
738     dumpBareDeclRef(D);
739   });
740 }
741 
742 void ASTDumper::dumpName(const NamedDecl *ND) {
743   if (ND->getDeclName()) {
744     ColorScope Color(*this, DeclNameColor);
745     OS << ' ' << ND->getNameAsString();
746   }
747 }
748 
749 bool ASTDumper::hasNodes(const DeclContext *DC) {
750   if (!DC)
751     return false;
752 
753   return DC->hasExternalLexicalStorage() ||
754          DC->noload_decls_begin() != DC->noload_decls_end();
755 }
756 
757 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
758   if (!DC)
759     return;
760 
761   for (auto *D : DC->noload_decls())
762     dumpDecl(D);
763 
764   if (DC->hasExternalLexicalStorage()) {
765     dumpChild([=]{
766       ColorScope Color(*this, UndeserializedColor);
767       OS << "<undeserialized declarations>";
768     });
769   }
770 }
771 
772 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
773   dumpChild([=] {
774     OS << "StoredDeclsMap ";
775     dumpBareDeclRef(cast<Decl>(DC));
776 
777     const DeclContext *Primary = DC->getPrimaryContext();
778     if (Primary != DC) {
779       OS << " primary";
780       dumpPointer(cast<Decl>(Primary));
781     }
782 
783     bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
784 
785     DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
786                                       E = Primary->noload_lookups_end();
787     while (I != E) {
788       DeclarationName Name = I.getLookupName();
789       DeclContextLookupResult R = *I++;
790 
791       dumpChild([=] {
792         OS << "DeclarationName ";
793         {
794           ColorScope Color(*this, DeclNameColor);
795           OS << '\'' << Name << '\'';
796         }
797 
798         for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
799              RI != RE; ++RI) {
800           dumpChild([=] {
801             dumpBareDeclRef(*RI);
802 
803             if ((*RI)->isHidden())
804               OS << " hidden";
805 
806             // If requested, dump the redecl chain for this lookup.
807             if (DumpDecls) {
808               // Dump earliest decl first.
809               std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
810                 if (Decl *Prev = D->getPreviousDecl())
811                   DumpWithPrev(Prev);
812                 dumpDecl(D);
813               };
814               DumpWithPrev(*RI);
815             }
816           });
817         }
818       });
819     }
820 
821     if (HasUndeserializedLookups) {
822       dumpChild([=] {
823         ColorScope Color(*this, UndeserializedColor);
824         OS << "<undeserialized lookups>";
825       });
826     }
827   });
828 }
829 
830 void ASTDumper::dumpAttr(const Attr *A) {
831   dumpChild([=] {
832     {
833       ColorScope Color(*this, AttrColor);
834 
835       switch (A->getKind()) {
836 #define ATTR(X) case attr::X: OS << #X; break;
837 #include "clang/Basic/AttrList.inc"
838       }
839       OS << "Attr";
840     }
841     dumpPointer(A);
842     dumpSourceRange(A->getRange());
843     if (A->isInherited())
844       OS << " Inherited";
845     if (A->isImplicit())
846       OS << " Implicit";
847 #include "clang/AST/AttrDump.inc"
848   });
849 }
850 
851 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
852 
853 template<typename T>
854 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
855   const T *First = D->getFirstDecl();
856   if (First != D)
857     OS << " first " << First;
858 }
859 
860 template<typename T>
861 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
862   const T *Prev = D->getPreviousDecl();
863   if (Prev)
864     OS << " prev " << Prev;
865 }
866 
867 /// Dump the previous declaration in the redeclaration chain for a declaration,
868 /// if any.
869 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
870   switch (D->getKind()) {
871 #define DECL(DERIVED, BASE) \
872   case Decl::DERIVED: \
873     return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
874 #define ABSTRACT_DECL(DECL)
875 #include "clang/AST/DeclNodes.inc"
876   }
877   llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
878 }
879 
880 //===----------------------------------------------------------------------===//
881 //  C++ Utilities
882 //===----------------------------------------------------------------------===//
883 
884 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
885   switch (AS) {
886   case AS_none:
887     break;
888   case AS_public:
889     OS << "public";
890     break;
891   case AS_protected:
892     OS << "protected";
893     break;
894   case AS_private:
895     OS << "private";
896     break;
897   }
898 }
899 
900 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
901   dumpChild([=] {
902     OS << "CXXCtorInitializer";
903     if (Init->isAnyMemberInitializer()) {
904       OS << ' ';
905       dumpBareDeclRef(Init->getAnyMember());
906     } else if (Init->isBaseInitializer()) {
907       dumpType(QualType(Init->getBaseClass(), 0));
908     } else if (Init->isDelegatingInitializer()) {
909       dumpType(Init->getTypeSourceInfo()->getType());
910     } else {
911       llvm_unreachable("Unknown initializer type");
912     }
913     dumpStmt(Init->getInit());
914   });
915 }
916 
917 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
918   if (!TPL)
919     return;
920 
921   for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
922        I != E; ++I)
923     dumpDecl(*I);
924 }
925 
926 void ASTDumper::dumpTemplateArgumentListInfo(
927     const TemplateArgumentListInfo &TALI) {
928   for (unsigned i = 0, e = TALI.size(); i < e; ++i)
929     dumpTemplateArgumentLoc(TALI[i]);
930 }
931 
932 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
933   dumpTemplateArgument(A.getArgument(), A.getSourceRange());
934 }
935 
936 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
937   for (unsigned i = 0, e = TAL.size(); i < e; ++i)
938     dumpTemplateArgument(TAL[i]);
939 }
940 
941 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
942   dumpChild([=] {
943     OS << "TemplateArgument";
944     if (R.isValid())
945       dumpSourceRange(R);
946 
947     switch (A.getKind()) {
948     case TemplateArgument::Null:
949       OS << " null";
950       break;
951     case TemplateArgument::Type:
952       OS << " type";
953       dumpType(A.getAsType());
954       break;
955     case TemplateArgument::Declaration:
956       OS << " decl";
957       dumpDeclRef(A.getAsDecl());
958       break;
959     case TemplateArgument::NullPtr:
960       OS << " nullptr";
961       break;
962     case TemplateArgument::Integral:
963       OS << " integral " << A.getAsIntegral();
964       break;
965     case TemplateArgument::Template:
966       OS << " template ";
967       A.getAsTemplate().dump(OS);
968       break;
969     case TemplateArgument::TemplateExpansion:
970       OS << " template expansion";
971       A.getAsTemplateOrTemplatePattern().dump(OS);
972       break;
973     case TemplateArgument::Expression:
974       OS << " expr";
975       dumpStmt(A.getAsExpr());
976       break;
977     case TemplateArgument::Pack:
978       OS << " pack";
979       for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
980            I != E; ++I)
981         dumpTemplateArgument(*I);
982       break;
983     }
984   });
985 }
986 
987 //===----------------------------------------------------------------------===//
988 //  Objective-C Utilities
989 //===----------------------------------------------------------------------===//
990 void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
991   if (!typeParams)
992     return;
993 
994   for (auto typeParam : *typeParams) {
995     dumpDecl(typeParam);
996   }
997 }
998 
999 //===----------------------------------------------------------------------===//
1000 //  Decl dumping methods.
1001 //===----------------------------------------------------------------------===//
1002 
1003 void ASTDumper::dumpDecl(const Decl *D) {
1004   dumpChild([=] {
1005     if (!D) {
1006       ColorScope Color(*this, NullColor);
1007       OS << "<<<NULL>>>";
1008       return;
1009     }
1010 
1011     {
1012       ColorScope Color(*this, DeclKindNameColor);
1013       OS << D->getDeclKindName() << "Decl";
1014     }
1015     dumpPointer(D);
1016     if (D->getLexicalDeclContext() != D->getDeclContext())
1017       OS << " parent " << cast<Decl>(D->getDeclContext());
1018     dumpPreviousDecl(OS, D);
1019     dumpSourceRange(D->getSourceRange());
1020     OS << ' ';
1021     dumpLocation(D->getLocation());
1022     if (Module *M = D->getImportedOwningModule())
1023       OS << " in " << M->getFullModuleName();
1024     else if (Module *M = D->getLocalOwningModule())
1025       OS << " in (local) " << M->getFullModuleName();
1026     if (auto *ND = dyn_cast<NamedDecl>(D))
1027       for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
1028                const_cast<NamedDecl *>(ND)))
1029         dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
1030     if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
1031       if (ND->isHidden())
1032         OS << " hidden";
1033     if (D->isImplicit())
1034       OS << " implicit";
1035     if (D->isUsed())
1036       OS << " used";
1037     else if (D->isThisDeclarationReferenced())
1038       OS << " referenced";
1039     if (D->isInvalidDecl())
1040       OS << " invalid";
1041     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1042       if (FD->isConstexpr())
1043         OS << " constexpr";
1044 
1045 
1046     ConstDeclVisitor<ASTDumper>::Visit(D);
1047 
1048     for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1049          ++I)
1050       dumpAttr(*I);
1051 
1052     if (const FullComment *Comment =
1053             D->getASTContext().getLocalCommentForDeclUncached(D))
1054       dumpFullComment(Comment);
1055 
1056     // Decls within functions are visited by the body.
1057     if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1058         hasNodes(dyn_cast<DeclContext>(D)))
1059       dumpDeclContext(cast<DeclContext>(D));
1060   });
1061 }
1062 
1063 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
1064   dumpName(D);
1065 }
1066 
1067 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
1068   dumpName(D);
1069   dumpType(D->getUnderlyingType());
1070   if (D->isModulePrivate())
1071     OS << " __module_private__";
1072   dumpTypeAsChild(D->getUnderlyingType());
1073 }
1074 
1075 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
1076   if (D->isScoped()) {
1077     if (D->isScopedUsingClassTag())
1078       OS << " class";
1079     else
1080       OS << " struct";
1081   }
1082   dumpName(D);
1083   if (D->isModulePrivate())
1084     OS << " __module_private__";
1085   if (D->isFixed())
1086     dumpType(D->getIntegerType());
1087 }
1088 
1089 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
1090   OS << ' ' << D->getKindName();
1091   dumpName(D);
1092   if (D->isModulePrivate())
1093     OS << " __module_private__";
1094   if (D->isCompleteDefinition())
1095     OS << " definition";
1096 }
1097 
1098 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1099   dumpName(D);
1100   dumpType(D->getType());
1101   if (const Expr *Init = D->getInitExpr())
1102     dumpStmt(Init);
1103 }
1104 
1105 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1106   dumpName(D);
1107   dumpType(D->getType());
1108 
1109   for (auto *Child : D->chain())
1110     dumpDeclRef(Child);
1111 }
1112 
1113 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
1114   dumpName(D);
1115   dumpType(D->getType());
1116 
1117   StorageClass SC = D->getStorageClass();
1118   if (SC != SC_None)
1119     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1120   if (D->isInlineSpecified())
1121     OS << " inline";
1122   if (D->isVirtualAsWritten())
1123     OS << " virtual";
1124   if (D->isModulePrivate())
1125     OS << " __module_private__";
1126 
1127   if (D->isPure())
1128     OS << " pure";
1129   else if (D->isDeletedAsWritten())
1130     OS << " delete";
1131 
1132   if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1133     FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1134     switch (EPI.ExceptionSpec.Type) {
1135     default: break;
1136     case EST_Unevaluated:
1137       OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1138       break;
1139     case EST_Uninstantiated:
1140       OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1141       break;
1142     }
1143   }
1144 
1145   if (const FunctionTemplateSpecializationInfo *FTSI =
1146           D->getTemplateSpecializationInfo())
1147     dumpTemplateArgumentList(*FTSI->TemplateArguments);
1148 
1149   for (ArrayRef<NamedDecl *>::iterator
1150        I = D->getDeclsInPrototypeScope().begin(),
1151        E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
1152     dumpDecl(*I);
1153 
1154   if (!D->param_begin() && D->getNumParams())
1155     dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1156   else
1157     for (FunctionDecl::param_const_iterator I = D->param_begin(),
1158                                             E = D->param_end();
1159          I != E; ++I)
1160       dumpDecl(*I);
1161 
1162   if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
1163     for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
1164                                                  E = C->init_end();
1165          I != E; ++I)
1166       dumpCXXCtorInitializer(*I);
1167 
1168   if (D->doesThisDeclarationHaveABody())
1169     dumpStmt(D->getBody());
1170 }
1171 
1172 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
1173   dumpName(D);
1174   dumpType(D->getType());
1175   if (D->isMutable())
1176     OS << " mutable";
1177   if (D->isModulePrivate())
1178     OS << " __module_private__";
1179 
1180   if (D->isBitField())
1181     dumpStmt(D->getBitWidth());
1182   if (Expr *Init = D->getInClassInitializer())
1183     dumpStmt(Init);
1184 }
1185 
1186 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1187   dumpName(D);
1188   dumpType(D->getType());
1189   StorageClass SC = D->getStorageClass();
1190   if (SC != SC_None)
1191     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1192   switch (D->getTLSKind()) {
1193   case VarDecl::TLS_None: break;
1194   case VarDecl::TLS_Static: OS << " tls"; break;
1195   case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1196   }
1197   if (D->isModulePrivate())
1198     OS << " __module_private__";
1199   if (D->isNRVOVariable())
1200     OS << " nrvo";
1201   if (D->hasInit()) {
1202     switch (D->getInitStyle()) {
1203     case VarDecl::CInit: OS << " cinit"; break;
1204     case VarDecl::CallInit: OS << " callinit"; break;
1205     case VarDecl::ListInit: OS << " listinit"; break;
1206     }
1207     dumpStmt(D->getInit());
1208   }
1209 }
1210 
1211 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1212   dumpStmt(D->getAsmString());
1213 }
1214 
1215 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1216   OS << ' ' << D->getImportedModule()->getFullModuleName();
1217 }
1218 
1219 void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1220   OS << ' ';
1221   switch (D->getCommentKind()) {
1222   case PCK_Unknown:  llvm_unreachable("unexpected pragma comment kind");
1223   case PCK_Compiler: OS << "compiler"; break;
1224   case PCK_ExeStr:   OS << "exestr"; break;
1225   case PCK_Lib:      OS << "lib"; break;
1226   case PCK_Linker:   OS << "linker"; break;
1227   case PCK_User:     OS << "user"; break;
1228   }
1229   StringRef Arg = D->getArg();
1230   if (!Arg.empty())
1231     OS << " \"" << Arg << "\"";
1232 }
1233 
1234 void ASTDumper::VisitPragmaDetectMismatchDecl(
1235     const PragmaDetectMismatchDecl *D) {
1236   OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1237 }
1238 
1239 void ASTDumper::VisitCapturedDecl(const CapturedDecl *D) {
1240   dumpStmt(D->getBody());
1241 }
1242 
1243 //===----------------------------------------------------------------------===//
1244 // OpenMP Declarations
1245 //===----------------------------------------------------------------------===//
1246 
1247 void ASTDumper::VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
1248   for (auto *E : D->varlists())
1249     dumpStmt(E);
1250 }
1251 
1252 void ASTDumper::VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
1253   dumpName(D);
1254   dumpType(D->getType());
1255   OS << " combiner";
1256   dumpStmt(D->getCombiner());
1257   if (auto *Initializer = D->getInitializer()) {
1258     OS << " initializer";
1259     dumpStmt(Initializer);
1260   }
1261 }
1262 
1263 void ASTDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1264   dumpName(D);
1265   dumpType(D->getType());
1266   dumpStmt(D->getInit());
1267 }
1268 
1269 //===----------------------------------------------------------------------===//
1270 // C++ Declarations
1271 //===----------------------------------------------------------------------===//
1272 
1273 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1274   dumpName(D);
1275   if (D->isInline())
1276     OS << " inline";
1277   if (!D->isOriginalNamespace())
1278     dumpDeclRef(D->getOriginalNamespace(), "original");
1279 }
1280 
1281 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1282   OS << ' ';
1283   dumpBareDeclRef(D->getNominatedNamespace());
1284 }
1285 
1286 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1287   dumpName(D);
1288   dumpDeclRef(D->getAliasedNamespace());
1289 }
1290 
1291 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1292   dumpName(D);
1293   dumpType(D->getUnderlyingType());
1294   dumpTypeAsChild(D->getUnderlyingType());
1295 }
1296 
1297 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1298   dumpName(D);
1299   dumpTemplateParameters(D->getTemplateParameters());
1300   dumpDecl(D->getTemplatedDecl());
1301 }
1302 
1303 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1304   VisitRecordDecl(D);
1305   if (!D->isCompleteDefinition())
1306     return;
1307 
1308   for (const auto &I : D->bases()) {
1309     dumpChild([=] {
1310       if (I.isVirtual())
1311         OS << "virtual ";
1312       dumpAccessSpecifier(I.getAccessSpecifier());
1313       dumpType(I.getType());
1314       if (I.isPackExpansion())
1315         OS << "...";
1316     });
1317   }
1318 }
1319 
1320 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1321   dumpStmt(D->getAssertExpr());
1322   dumpStmt(D->getMessage());
1323 }
1324 
1325 template<typename SpecializationDecl>
1326 void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
1327                                                 bool DumpExplicitInst,
1328                                                 bool DumpRefOnly) {
1329   bool DumpedAny = false;
1330   for (auto *RedeclWithBadType : D->redecls()) {
1331     // FIXME: The redecls() range sometimes has elements of a less-specific
1332     // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1333     // us TagDecls, and should give CXXRecordDecls).
1334     auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1335     if (!Redecl) {
1336       // Found the injected-class-name for a class template. This will be dumped
1337       // as part of its surrounding class so we don't need to dump it here.
1338       assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1339              "expected an injected-class-name");
1340       continue;
1341     }
1342 
1343     switch (Redecl->getTemplateSpecializationKind()) {
1344     case TSK_ExplicitInstantiationDeclaration:
1345     case TSK_ExplicitInstantiationDefinition:
1346       if (!DumpExplicitInst)
1347         break;
1348       // Fall through.
1349     case TSK_Undeclared:
1350     case TSK_ImplicitInstantiation:
1351       if (DumpRefOnly)
1352         dumpDeclRef(Redecl);
1353       else
1354         dumpDecl(Redecl);
1355       DumpedAny = true;
1356       break;
1357     case TSK_ExplicitSpecialization:
1358       break;
1359     }
1360   }
1361 
1362   // Ensure we dump at least one decl for each specialization.
1363   if (!DumpedAny)
1364     dumpDeclRef(D);
1365 }
1366 
1367 template<typename TemplateDecl>
1368 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1369                                   bool DumpExplicitInst) {
1370   dumpName(D);
1371   dumpTemplateParameters(D->getTemplateParameters());
1372 
1373   dumpDecl(D->getTemplatedDecl());
1374 
1375   for (auto *Child : D->specializations())
1376     VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
1377                                     !D->isCanonicalDecl());
1378 }
1379 
1380 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1381   // FIXME: We don't add a declaration of a function template specialization
1382   // to its context when it's explicitly instantiated, so dump explicit
1383   // instantiations when we dump the template itself.
1384   VisitTemplateDecl(D, true);
1385 }
1386 
1387 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1388   VisitTemplateDecl(D, false);
1389 }
1390 
1391 void ASTDumper::VisitClassTemplateSpecializationDecl(
1392     const ClassTemplateSpecializationDecl *D) {
1393   VisitCXXRecordDecl(D);
1394   dumpTemplateArgumentList(D->getTemplateArgs());
1395 }
1396 
1397 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1398     const ClassTemplatePartialSpecializationDecl *D) {
1399   VisitClassTemplateSpecializationDecl(D);
1400   dumpTemplateParameters(D->getTemplateParameters());
1401 }
1402 
1403 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1404     const ClassScopeFunctionSpecializationDecl *D) {
1405   dumpDeclRef(D->getSpecialization());
1406   if (D->hasExplicitTemplateArgs())
1407     dumpTemplateArgumentListInfo(D->templateArgs());
1408 }
1409 
1410 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1411   VisitTemplateDecl(D, false);
1412 }
1413 
1414 void ASTDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
1415   dumpName(D);
1416   dumpTemplateParameters(D->getTemplateParameters());
1417 }
1418 
1419 void ASTDumper::VisitVarTemplateSpecializationDecl(
1420     const VarTemplateSpecializationDecl *D) {
1421   dumpTemplateArgumentList(D->getTemplateArgs());
1422   VisitVarDecl(D);
1423 }
1424 
1425 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1426     const VarTemplatePartialSpecializationDecl *D) {
1427   dumpTemplateParameters(D->getTemplateParameters());
1428   VisitVarTemplateSpecializationDecl(D);
1429 }
1430 
1431 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1432   if (D->wasDeclaredWithTypename())
1433     OS << " typename";
1434   else
1435     OS << " class";
1436   if (D->isParameterPack())
1437     OS << " ...";
1438   dumpName(D);
1439   if (D->hasDefaultArgument())
1440     dumpTemplateArgument(D->getDefaultArgument());
1441 }
1442 
1443 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1444   dumpType(D->getType());
1445   if (D->isParameterPack())
1446     OS << " ...";
1447   dumpName(D);
1448   if (D->hasDefaultArgument())
1449     dumpTemplateArgument(D->getDefaultArgument());
1450 }
1451 
1452 void ASTDumper::VisitTemplateTemplateParmDecl(
1453     const TemplateTemplateParmDecl *D) {
1454   if (D->isParameterPack())
1455     OS << " ...";
1456   dumpName(D);
1457   dumpTemplateParameters(D->getTemplateParameters());
1458   if (D->hasDefaultArgument())
1459     dumpTemplateArgumentLoc(D->getDefaultArgument());
1460 }
1461 
1462 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1463   OS << ' ';
1464   if (D->getQualifier())
1465     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1466   OS << D->getNameAsString();
1467 }
1468 
1469 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1470     const UnresolvedUsingTypenameDecl *D) {
1471   OS << ' ';
1472   if (D->getQualifier())
1473     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1474   OS << D->getNameAsString();
1475 }
1476 
1477 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1478   OS << ' ';
1479   if (D->getQualifier())
1480     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1481   OS << D->getNameAsString();
1482   dumpType(D->getType());
1483 }
1484 
1485 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1486   OS << ' ';
1487   dumpBareDeclRef(D->getTargetDecl());
1488   if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
1489     dumpTypeAsChild(TD->getTypeForDecl());
1490 }
1491 
1492 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1493   switch (D->getLanguage()) {
1494   case LinkageSpecDecl::lang_c: OS << " C"; break;
1495   case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1496   }
1497 }
1498 
1499 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1500   OS << ' ';
1501   dumpAccessSpecifier(D->getAccess());
1502 }
1503 
1504 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1505   if (TypeSourceInfo *T = D->getFriendType())
1506     dumpType(T->getType());
1507   else
1508     dumpDecl(D->getFriendDecl());
1509 }
1510 
1511 //===----------------------------------------------------------------------===//
1512 // Obj-C Declarations
1513 //===----------------------------------------------------------------------===//
1514 
1515 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1516   dumpName(D);
1517   dumpType(D->getType());
1518   if (D->getSynthesize())
1519     OS << " synthesize";
1520 
1521   switch (D->getAccessControl()) {
1522   case ObjCIvarDecl::None:
1523     OS << " none";
1524     break;
1525   case ObjCIvarDecl::Private:
1526     OS << " private";
1527     break;
1528   case ObjCIvarDecl::Protected:
1529     OS << " protected";
1530     break;
1531   case ObjCIvarDecl::Public:
1532     OS << " public";
1533     break;
1534   case ObjCIvarDecl::Package:
1535     OS << " package";
1536     break;
1537   }
1538 }
1539 
1540 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1541   if (D->isInstanceMethod())
1542     OS << " -";
1543   else
1544     OS << " +";
1545   dumpName(D);
1546   dumpType(D->getReturnType());
1547 
1548   if (D->isThisDeclarationADefinition()) {
1549     dumpDeclContext(D);
1550   } else {
1551     for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1552                                               E = D->param_end();
1553          I != E; ++I)
1554       dumpDecl(*I);
1555   }
1556 
1557   if (D->isVariadic())
1558     dumpChild([=] { OS << "..."; });
1559 
1560   if (D->hasBody())
1561     dumpStmt(D->getBody());
1562 }
1563 
1564 void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
1565   dumpName(D);
1566   switch (D->getVariance()) {
1567   case ObjCTypeParamVariance::Invariant:
1568     break;
1569 
1570   case ObjCTypeParamVariance::Covariant:
1571     OS << " covariant";
1572     break;
1573 
1574   case ObjCTypeParamVariance::Contravariant:
1575     OS << " contravariant";
1576     break;
1577   }
1578 
1579   if (D->hasExplicitBound())
1580     OS << " bounded";
1581   dumpType(D->getUnderlyingType());
1582 }
1583 
1584 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1585   dumpName(D);
1586   dumpDeclRef(D->getClassInterface());
1587   dumpObjCTypeParamList(D->getTypeParamList());
1588   dumpDeclRef(D->getImplementation());
1589   for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1590                                            E = D->protocol_end();
1591        I != E; ++I)
1592     dumpDeclRef(*I);
1593 }
1594 
1595 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1596   dumpName(D);
1597   dumpDeclRef(D->getClassInterface());
1598   dumpDeclRef(D->getCategoryDecl());
1599 }
1600 
1601 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1602   dumpName(D);
1603 
1604   for (auto *Child : D->protocols())
1605     dumpDeclRef(Child);
1606 }
1607 
1608 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1609   dumpName(D);
1610   dumpObjCTypeParamList(D->getTypeParamListAsWritten());
1611   dumpDeclRef(D->getSuperClass(), "super");
1612 
1613   dumpDeclRef(D->getImplementation());
1614   for (auto *Child : D->protocols())
1615     dumpDeclRef(Child);
1616 }
1617 
1618 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1619   dumpName(D);
1620   dumpDeclRef(D->getSuperClass(), "super");
1621   dumpDeclRef(D->getClassInterface());
1622   for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1623                                                    E = D->init_end();
1624        I != E; ++I)
1625     dumpCXXCtorInitializer(*I);
1626 }
1627 
1628 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1629   dumpName(D);
1630   dumpDeclRef(D->getClassInterface());
1631 }
1632 
1633 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1634   dumpName(D);
1635   dumpType(D->getType());
1636 
1637   if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1638     OS << " required";
1639   else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1640     OS << " optional";
1641 
1642   ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1643   if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1644     if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1645       OS << " readonly";
1646     if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1647       OS << " assign";
1648     if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1649       OS << " readwrite";
1650     if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1651       OS << " retain";
1652     if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1653       OS << " copy";
1654     if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1655       OS << " nonatomic";
1656     if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1657       OS << " atomic";
1658     if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1659       OS << " weak";
1660     if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1661       OS << " strong";
1662     if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1663       OS << " unsafe_unretained";
1664     if (Attrs & ObjCPropertyDecl::OBJC_PR_class)
1665       OS << " class";
1666     if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
1667       dumpDeclRef(D->getGetterMethodDecl(), "getter");
1668     if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
1669       dumpDeclRef(D->getSetterMethodDecl(), "setter");
1670   }
1671 }
1672 
1673 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1674   dumpName(D->getPropertyDecl());
1675   if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1676     OS << " synthesize";
1677   else
1678     OS << " dynamic";
1679   dumpDeclRef(D->getPropertyDecl());
1680   dumpDeclRef(D->getPropertyIvarDecl());
1681 }
1682 
1683 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1684   for (auto I : D->params())
1685     dumpDecl(I);
1686 
1687   if (D->isVariadic())
1688     dumpChild([=]{ OS << "..."; });
1689 
1690   if (D->capturesCXXThis())
1691     dumpChild([=]{ OS << "capture this"; });
1692 
1693   for (const auto &I : D->captures()) {
1694     dumpChild([=] {
1695       OS << "capture";
1696       if (I.isByRef())
1697         OS << " byref";
1698       if (I.isNested())
1699         OS << " nested";
1700       if (I.getVariable()) {
1701         OS << ' ';
1702         dumpBareDeclRef(I.getVariable());
1703       }
1704       if (I.hasCopyExpr())
1705         dumpStmt(I.getCopyExpr());
1706     });
1707   }
1708   dumpStmt(D->getBody());
1709 }
1710 
1711 //===----------------------------------------------------------------------===//
1712 //  Stmt dumping methods.
1713 //===----------------------------------------------------------------------===//
1714 
1715 void ASTDumper::dumpStmt(const Stmt *S) {
1716   dumpChild([=] {
1717     if (!S) {
1718       ColorScope Color(*this, NullColor);
1719       OS << "<<<NULL>>>";
1720       return;
1721     }
1722 
1723     if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1724       VisitDeclStmt(DS);
1725       return;
1726     }
1727 
1728     ConstStmtVisitor<ASTDumper>::Visit(S);
1729 
1730     for (const Stmt *SubStmt : S->children())
1731       dumpStmt(SubStmt);
1732   });
1733 }
1734 
1735 void ASTDumper::VisitStmt(const Stmt *Node) {
1736   {
1737     ColorScope Color(*this, StmtColor);
1738     OS << Node->getStmtClassName();
1739   }
1740   dumpPointer(Node);
1741   dumpSourceRange(Node->getSourceRange());
1742 }
1743 
1744 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1745   VisitStmt(Node);
1746   for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1747                                      E = Node->decl_end();
1748        I != E; ++I)
1749     dumpDecl(*I);
1750 }
1751 
1752 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1753   VisitStmt(Node);
1754   for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1755                                         E = Node->getAttrs().end();
1756        I != E; ++I)
1757     dumpAttr(*I);
1758 }
1759 
1760 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1761   VisitStmt(Node);
1762   OS << " '" << Node->getName() << "'";
1763 }
1764 
1765 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1766   VisitStmt(Node);
1767   OS << " '" << Node->getLabel()->getName() << "'";
1768   dumpPointer(Node->getLabel());
1769 }
1770 
1771 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1772   VisitStmt(Node);
1773   dumpDecl(Node->getExceptionDecl());
1774 }
1775 
1776 void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
1777   VisitStmt(Node);
1778   dumpDecl(Node->getCapturedDecl());
1779 }
1780 
1781 //===----------------------------------------------------------------------===//
1782 //  OpenMP dumping methods.
1783 //===----------------------------------------------------------------------===//
1784 
1785 void ASTDumper::VisitOMPExecutableDirective(
1786     const OMPExecutableDirective *Node) {
1787   VisitStmt(Node);
1788   for (auto *C : Node->clauses()) {
1789     dumpChild([=] {
1790       if (!C) {
1791         ColorScope Color(*this, NullColor);
1792         OS << "<<<NULL>>> OMPClause";
1793         return;
1794       }
1795       {
1796         ColorScope Color(*this, AttrColor);
1797         StringRef ClauseName(getOpenMPClauseName(C->getClauseKind()));
1798         OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
1799            << ClauseName.drop_front() << "Clause";
1800       }
1801       dumpPointer(C);
1802       dumpSourceRange(SourceRange(C->getLocStart(), C->getLocEnd()));
1803       if (C->isImplicit())
1804         OS << " <implicit>";
1805       for (auto *S : C->children())
1806         dumpStmt(S);
1807     });
1808   }
1809 }
1810 
1811 //===----------------------------------------------------------------------===//
1812 //  Expr dumping methods.
1813 //===----------------------------------------------------------------------===//
1814 
1815 void ASTDumper::VisitExpr(const Expr *Node) {
1816   VisitStmt(Node);
1817   dumpType(Node->getType());
1818 
1819   {
1820     ColorScope Color(*this, ValueKindColor);
1821     switch (Node->getValueKind()) {
1822     case VK_RValue:
1823       break;
1824     case VK_LValue:
1825       OS << " lvalue";
1826       break;
1827     case VK_XValue:
1828       OS << " xvalue";
1829       break;
1830     }
1831   }
1832 
1833   {
1834     ColorScope Color(*this, ObjectKindColor);
1835     switch (Node->getObjectKind()) {
1836     case OK_Ordinary:
1837       break;
1838     case OK_BitField:
1839       OS << " bitfield";
1840       break;
1841     case OK_ObjCProperty:
1842       OS << " objcproperty";
1843       break;
1844     case OK_ObjCSubscript:
1845       OS << " objcsubscript";
1846       break;
1847     case OK_VectorComponent:
1848       OS << " vectorcomponent";
1849       break;
1850     }
1851   }
1852 }
1853 
1854 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1855   if (Node->path_empty())
1856     return;
1857 
1858   OS << " (";
1859   bool First = true;
1860   for (CastExpr::path_const_iterator I = Node->path_begin(),
1861                                      E = Node->path_end();
1862        I != E; ++I) {
1863     const CXXBaseSpecifier *Base = *I;
1864     if (!First)
1865       OS << " -> ";
1866 
1867     const CXXRecordDecl *RD =
1868     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1869 
1870     if (Base->isVirtual())
1871       OS << "virtual ";
1872     OS << RD->getName();
1873     First = false;
1874   }
1875 
1876   OS << ')';
1877 }
1878 
1879 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1880   VisitExpr(Node);
1881   OS << " <";
1882   {
1883     ColorScope Color(*this, CastColor);
1884     OS << Node->getCastKindName();
1885   }
1886   dumpBasePath(OS, Node);
1887   OS << ">";
1888 }
1889 
1890 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1891   VisitExpr(Node);
1892 
1893   OS << " ";
1894   dumpBareDeclRef(Node->getDecl());
1895   if (Node->getDecl() != Node->getFoundDecl()) {
1896     OS << " (";
1897     dumpBareDeclRef(Node->getFoundDecl());
1898     OS << ")";
1899   }
1900 }
1901 
1902 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1903   VisitExpr(Node);
1904   OS << " (";
1905   if (!Node->requiresADL())
1906     OS << "no ";
1907   OS << "ADL) = '" << Node->getName() << '\'';
1908 
1909   UnresolvedLookupExpr::decls_iterator
1910     I = Node->decls_begin(), E = Node->decls_end();
1911   if (I == E)
1912     OS << " empty";
1913   for (; I != E; ++I)
1914     dumpPointer(*I);
1915 }
1916 
1917 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1918   VisitExpr(Node);
1919 
1920   {
1921     ColorScope Color(*this, DeclKindNameColor);
1922     OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1923   }
1924   OS << "='" << *Node->getDecl() << "'";
1925   dumpPointer(Node->getDecl());
1926   if (Node->isFreeIvar())
1927     OS << " isFreeIvar";
1928 }
1929 
1930 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1931   VisitExpr(Node);
1932   OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1933 }
1934 
1935 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1936   VisitExpr(Node);
1937   ColorScope Color(*this, ValueColor);
1938   OS << " " << Node->getValue();
1939 }
1940 
1941 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1942   VisitExpr(Node);
1943 
1944   bool isSigned = Node->getType()->isSignedIntegerType();
1945   ColorScope Color(*this, ValueColor);
1946   OS << " " << Node->getValue().toString(10, isSigned);
1947 }
1948 
1949 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1950   VisitExpr(Node);
1951   ColorScope Color(*this, ValueColor);
1952   OS << " " << Node->getValueAsApproximateDouble();
1953 }
1954 
1955 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
1956   VisitExpr(Str);
1957   ColorScope Color(*this, ValueColor);
1958   OS << " ";
1959   Str->outputString(OS);
1960 }
1961 
1962 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1963   VisitExpr(ILE);
1964   if (auto *Filler = ILE->getArrayFiller()) {
1965     dumpChild([=] {
1966       OS << "array filler";
1967       dumpStmt(Filler);
1968     });
1969   }
1970   if (auto *Field = ILE->getInitializedFieldInUnion()) {
1971     OS << " field ";
1972     dumpBareDeclRef(Field);
1973   }
1974 }
1975 
1976 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
1977   VisitExpr(Node);
1978   OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1979      << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1980 }
1981 
1982 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1983     const UnaryExprOrTypeTraitExpr *Node) {
1984   VisitExpr(Node);
1985   switch(Node->getKind()) {
1986   case UETT_SizeOf:
1987     OS << " sizeof";
1988     break;
1989   case UETT_AlignOf:
1990     OS << " alignof";
1991     break;
1992   case UETT_VecStep:
1993     OS << " vec_step";
1994     break;
1995   case UETT_OpenMPRequiredSimdAlign:
1996     OS << " __builtin_omp_required_simd_align";
1997     break;
1998   }
1999   if (Node->isArgumentType())
2000     dumpType(Node->getArgumentType());
2001 }
2002 
2003 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
2004   VisitExpr(Node);
2005   OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
2006   dumpPointer(Node->getMemberDecl());
2007 }
2008 
2009 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
2010   VisitExpr(Node);
2011   OS << " " << Node->getAccessor().getNameStart();
2012 }
2013 
2014 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
2015   VisitExpr(Node);
2016   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2017 }
2018 
2019 void ASTDumper::VisitCompoundAssignOperator(
2020     const CompoundAssignOperator *Node) {
2021   VisitExpr(Node);
2022   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
2023      << "' ComputeLHSTy=";
2024   dumpBareType(Node->getComputationLHSType());
2025   OS << " ComputeResultTy=";
2026   dumpBareType(Node->getComputationResultType());
2027 }
2028 
2029 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
2030   VisitExpr(Node);
2031   dumpDecl(Node->getBlockDecl());
2032 }
2033 
2034 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
2035   VisitExpr(Node);
2036 
2037   if (Expr *Source = Node->getSourceExpr())
2038     dumpStmt(Source);
2039 }
2040 
2041 // GNU extensions.
2042 
2043 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
2044   VisitExpr(Node);
2045   OS << " " << Node->getLabel()->getName();
2046   dumpPointer(Node->getLabel());
2047 }
2048 
2049 //===----------------------------------------------------------------------===//
2050 // C++ Expressions
2051 //===----------------------------------------------------------------------===//
2052 
2053 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
2054   VisitExpr(Node);
2055   OS << " " << Node->getCastName()
2056      << "<" << Node->getTypeAsWritten().getAsString() << ">"
2057      << " <" << Node->getCastKindName();
2058   dumpBasePath(OS, Node);
2059   OS << ">";
2060 }
2061 
2062 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
2063   VisitExpr(Node);
2064   OS << " " << (Node->getValue() ? "true" : "false");
2065 }
2066 
2067 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
2068   VisitExpr(Node);
2069   OS << " this";
2070 }
2071 
2072 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
2073   VisitExpr(Node);
2074   OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
2075      << " <" << Node->getCastKindName() << ">";
2076 }
2077 
2078 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
2079   VisitExpr(Node);
2080   CXXConstructorDecl *Ctor = Node->getConstructor();
2081   dumpType(Ctor->getType());
2082   if (Node->isElidable())
2083     OS << " elidable";
2084   if (Node->requiresZeroInitialization())
2085     OS << " zeroing";
2086 }
2087 
2088 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
2089   VisitExpr(Node);
2090   OS << " ";
2091   dumpCXXTemporary(Node->getTemporary());
2092 }
2093 
2094 void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
2095   VisitExpr(Node);
2096   if (Node->isGlobalNew())
2097     OS << " global";
2098   if (Node->isArray())
2099     OS << " array";
2100   if (Node->getOperatorNew()) {
2101     OS << ' ';
2102     dumpBareDeclRef(Node->getOperatorNew());
2103   }
2104   // We could dump the deallocation function used in case of error, but it's
2105   // usually not that interesting.
2106 }
2107 
2108 void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
2109   VisitExpr(Node);
2110   if (Node->isGlobalDelete())
2111     OS << " global";
2112   if (Node->isArrayForm())
2113     OS << " array";
2114   if (Node->getOperatorDelete()) {
2115     OS << ' ';
2116     dumpBareDeclRef(Node->getOperatorDelete());
2117   }
2118 }
2119 
2120 void
2121 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
2122   VisitExpr(Node);
2123   if (const ValueDecl *VD = Node->getExtendingDecl()) {
2124     OS << " extended by ";
2125     dumpBareDeclRef(VD);
2126   }
2127 }
2128 
2129 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
2130   VisitExpr(Node);
2131   for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
2132     dumpDeclRef(Node->getObject(i), "cleanup");
2133 }
2134 
2135 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
2136   OS << "(CXXTemporary";
2137   dumpPointer(Temporary);
2138   OS << ")";
2139 }
2140 
2141 void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
2142   VisitExpr(Node);
2143   dumpPointer(Node->getPack());
2144   dumpName(Node->getPack());
2145   if (Node->isPartiallySubstituted())
2146     for (const auto &A : Node->getPartialArguments())
2147       dumpTemplateArgument(A);
2148 }
2149 
2150 
2151 //===----------------------------------------------------------------------===//
2152 // Obj-C Expressions
2153 //===----------------------------------------------------------------------===//
2154 
2155 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
2156   VisitExpr(Node);
2157   OS << " selector=";
2158   Node->getSelector().print(OS);
2159   switch (Node->getReceiverKind()) {
2160   case ObjCMessageExpr::Instance:
2161     break;
2162 
2163   case ObjCMessageExpr::Class:
2164     OS << " class=";
2165     dumpBareType(Node->getClassReceiver());
2166     break;
2167 
2168   case ObjCMessageExpr::SuperInstance:
2169     OS << " super (instance)";
2170     break;
2171 
2172   case ObjCMessageExpr::SuperClass:
2173     OS << " super (class)";
2174     break;
2175   }
2176 }
2177 
2178 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
2179   VisitExpr(Node);
2180   OS << " selector=";
2181   Node->getBoxingMethod()->getSelector().print(OS);
2182 }
2183 
2184 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
2185   VisitStmt(Node);
2186   if (const VarDecl *CatchParam = Node->getCatchParamDecl())
2187     dumpDecl(CatchParam);
2188   else
2189     OS << " catch all";
2190 }
2191 
2192 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
2193   VisitExpr(Node);
2194   dumpType(Node->getEncodedType());
2195 }
2196 
2197 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
2198   VisitExpr(Node);
2199 
2200   OS << " ";
2201   Node->getSelector().print(OS);
2202 }
2203 
2204 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
2205   VisitExpr(Node);
2206 
2207   OS << ' ' << *Node->getProtocol();
2208 }
2209 
2210 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
2211   VisitExpr(Node);
2212   if (Node->isImplicitProperty()) {
2213     OS << " Kind=MethodRef Getter=\"";
2214     if (Node->getImplicitPropertyGetter())
2215       Node->getImplicitPropertyGetter()->getSelector().print(OS);
2216     else
2217       OS << "(null)";
2218 
2219     OS << "\" Setter=\"";
2220     if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
2221       Setter->getSelector().print(OS);
2222     else
2223       OS << "(null)";
2224     OS << "\"";
2225   } else {
2226     OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
2227   }
2228 
2229   if (Node->isSuperReceiver())
2230     OS << " super";
2231 
2232   OS << " Messaging=";
2233   if (Node->isMessagingGetter() && Node->isMessagingSetter())
2234     OS << "Getter&Setter";
2235   else if (Node->isMessagingGetter())
2236     OS << "Getter";
2237   else if (Node->isMessagingSetter())
2238     OS << "Setter";
2239 }
2240 
2241 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
2242   VisitExpr(Node);
2243   if (Node->isArraySubscriptRefExpr())
2244     OS << " Kind=ArraySubscript GetterForArray=\"";
2245   else
2246     OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2247   if (Node->getAtIndexMethodDecl())
2248     Node->getAtIndexMethodDecl()->getSelector().print(OS);
2249   else
2250     OS << "(null)";
2251 
2252   if (Node->isArraySubscriptRefExpr())
2253     OS << "\" SetterForArray=\"";
2254   else
2255     OS << "\" SetterForDictionary=\"";
2256   if (Node->setAtIndexMethodDecl())
2257     Node->setAtIndexMethodDecl()->getSelector().print(OS);
2258   else
2259     OS << "(null)";
2260 }
2261 
2262 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2263   VisitExpr(Node);
2264   OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2265 }
2266 
2267 //===----------------------------------------------------------------------===//
2268 // Comments
2269 //===----------------------------------------------------------------------===//
2270 
2271 const char *ASTDumper::getCommandName(unsigned CommandID) {
2272   if (Traits)
2273     return Traits->getCommandInfo(CommandID)->Name;
2274   const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2275   if (Info)
2276     return Info->Name;
2277   return "<not a builtin command>";
2278 }
2279 
2280 void ASTDumper::dumpFullComment(const FullComment *C) {
2281   if (!C)
2282     return;
2283 
2284   FC = C;
2285   dumpComment(C);
2286   FC = nullptr;
2287 }
2288 
2289 void ASTDumper::dumpComment(const Comment *C) {
2290   dumpChild([=] {
2291     if (!C) {
2292       ColorScope Color(*this, NullColor);
2293       OS << "<<<NULL>>>";
2294       return;
2295     }
2296 
2297     {
2298       ColorScope Color(*this, CommentColor);
2299       OS << C->getCommentKindName();
2300     }
2301     dumpPointer(C);
2302     dumpSourceRange(C->getSourceRange());
2303     ConstCommentVisitor<ASTDumper>::visit(C);
2304     for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2305          I != E; ++I)
2306       dumpComment(*I);
2307   });
2308 }
2309 
2310 void ASTDumper::visitTextComment(const TextComment *C) {
2311   OS << " Text=\"" << C->getText() << "\"";
2312 }
2313 
2314 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2315   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2316   switch (C->getRenderKind()) {
2317   case InlineCommandComment::RenderNormal:
2318     OS << " RenderNormal";
2319     break;
2320   case InlineCommandComment::RenderBold:
2321     OS << " RenderBold";
2322     break;
2323   case InlineCommandComment::RenderMonospaced:
2324     OS << " RenderMonospaced";
2325     break;
2326   case InlineCommandComment::RenderEmphasized:
2327     OS << " RenderEmphasized";
2328     break;
2329   }
2330 
2331   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2332     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2333 }
2334 
2335 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2336   OS << " Name=\"" << C->getTagName() << "\"";
2337   if (C->getNumAttrs() != 0) {
2338     OS << " Attrs: ";
2339     for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2340       const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2341       OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2342     }
2343   }
2344   if (C->isSelfClosing())
2345     OS << " SelfClosing";
2346 }
2347 
2348 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2349   OS << " Name=\"" << C->getTagName() << "\"";
2350 }
2351 
2352 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2353   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2354   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2355     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2356 }
2357 
2358 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2359   OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2360 
2361   if (C->isDirectionExplicit())
2362     OS << " explicitly";
2363   else
2364     OS << " implicitly";
2365 
2366   if (C->hasParamName()) {
2367     if (C->isParamIndexValid())
2368       OS << " Param=\"" << C->getParamName(FC) << "\"";
2369     else
2370       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2371   }
2372 
2373   if (C->isParamIndexValid() && !C->isVarArgParam())
2374     OS << " ParamIndex=" << C->getParamIndex();
2375 }
2376 
2377 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2378   if (C->hasParamName()) {
2379     if (C->isPositionValid())
2380       OS << " Param=\"" << C->getParamName(FC) << "\"";
2381     else
2382       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2383   }
2384 
2385   if (C->isPositionValid()) {
2386     OS << " Position=<";
2387     for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2388       OS << C->getIndex(i);
2389       if (i != e - 1)
2390         OS << ", ";
2391     }
2392     OS << ">";
2393   }
2394 }
2395 
2396 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2397   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2398         " CloseName=\"" << C->getCloseName() << "\"";
2399 }
2400 
2401 void ASTDumper::visitVerbatimBlockLineComment(
2402     const VerbatimBlockLineComment *C) {
2403   OS << " Text=\"" << C->getText() << "\"";
2404 }
2405 
2406 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2407   OS << " Text=\"" << C->getText() << "\"";
2408 }
2409 
2410 //===----------------------------------------------------------------------===//
2411 // Type method implementations
2412 //===----------------------------------------------------------------------===//
2413 
2414 void QualType::dump(const char *msg) const {
2415   if (msg)
2416     llvm::errs() << msg << ": ";
2417   dump();
2418 }
2419 
2420 LLVM_DUMP_METHOD void QualType::dump() const {
2421   ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2422   Dumper.dumpTypeAsChild(*this);
2423 }
2424 
2425 LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2426 
2427 //===----------------------------------------------------------------------===//
2428 // Decl method implementations
2429 //===----------------------------------------------------------------------===//
2430 
2431 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2432 
2433 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
2434   ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2435               &getASTContext().getSourceManager());
2436   P.dumpDecl(this);
2437 }
2438 
2439 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2440   ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2441               &getASTContext().getSourceManager(), /*ShowColors*/true);
2442   P.dumpDecl(this);
2443 }
2444 
2445 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2446   dumpLookups(llvm::errs());
2447 }
2448 
2449 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2450                                                bool DumpDecls) const {
2451   const DeclContext *DC = this;
2452   while (!DC->isTranslationUnit())
2453     DC = DC->getParent();
2454   ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2455   ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2456   P.dumpLookups(this, DumpDecls);
2457 }
2458 
2459 //===----------------------------------------------------------------------===//
2460 // Stmt method implementations
2461 //===----------------------------------------------------------------------===//
2462 
2463 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2464   dump(llvm::errs(), SM);
2465 }
2466 
2467 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2468   ASTDumper P(OS, nullptr, &SM);
2469   P.dumpStmt(this);
2470 }
2471 
2472 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2473   ASTDumper P(OS, nullptr, nullptr);
2474   P.dumpStmt(this);
2475 }
2476 
2477 LLVM_DUMP_METHOD void Stmt::dump() const {
2478   ASTDumper P(llvm::errs(), nullptr, nullptr);
2479   P.dumpStmt(this);
2480 }
2481 
2482 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2483   ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2484   P.dumpStmt(this);
2485 }
2486 
2487 //===----------------------------------------------------------------------===//
2488 // Comment method implementations
2489 //===----------------------------------------------------------------------===//
2490 
2491 LLVM_DUMP_METHOD void Comment::dump() const {
2492   dump(llvm::errs(), nullptr, nullptr);
2493 }
2494 
2495 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2496   dump(llvm::errs(), &Context.getCommentCommandTraits(),
2497        &Context.getSourceManager());
2498 }
2499 
2500 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2501                    const SourceManager *SM) const {
2502   const FullComment *FC = dyn_cast<FullComment>(this);
2503   ASTDumper D(OS, Traits, SM);
2504   D.dumpFullComment(FC);
2505 }
2506 
2507 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2508   const FullComment *FC = dyn_cast<FullComment>(this);
2509   ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2510   D.dumpFullComment(FC);
2511 }
2512