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