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