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