1 //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AST dump methods, which dump out the
11 // AST in a form that exposes type details and other fields.
13 //===----------------------------------------------------------------------===//
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;
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
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.
41 struct TerminalColor {
42 raw_ostream::Colors Color;
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
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 };
65 // Type names (int, float, etc, plus user defined types)
66 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
69 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
71 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
74 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
75 // bitfield/objcproperty/objcsubscript/vectorcomponent
76 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
79 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
81 // Undeserialized entities
82 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
84 // CastKind from CastExpr's
85 static const TerminalColor CastColor = { raw_ostream::RED, false };
87 // Value of the statement
88 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
90 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
92 // Indents ( `, -. | )
93 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
96 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
97 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
99 const CommandTraits *Traits;
100 const SourceManager *SM;
102 /// Pending[i] is an action to dump an entity at level i.
103 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
105 /// Indicates whether we're at the top level.
108 /// Indicates if we're handling the first child after entering a new depth.
111 /// Prefix for currently-being-dumped entity.
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;
119 /// The \c FullComment parent of the comment being dumped.
120 const FullComment *FC;
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
131 while (!Pending.empty()) {
132 Pending.back()(true);
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:
148 // | `-C Prefix = "| "
150 // |-E Prefix = " | "
154 // Note that the first level gets no prefix.
157 ColorScope Color(*this, IndentColor);
158 OS << Prefix << (isLastChild ? '`' : '|') << '-';
159 this->Prefix.push_back(isLastChild ? ' ' : '|');
160 this->Prefix.push_back(' ');
164 unsigned Depth = Pending.size();
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();
176 // Restore the old prefix.
177 this->Prefix.resize(Prefix.size() - 2);
181 Pending.push_back(std::move(dumpWithIndent));
183 Pending.back()(false);
184 Pending.back() = std::move(dumpWithIndent);
192 ColorScope(ASTDumper &Dumper, TerminalColor Color)
194 if (Dumper.ShowColors)
195 Dumper.OS.changeColor(Color.Color, Color.Bold);
198 if (Dumper.ShowColors)
199 Dumper.OS.resetColor();
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()) { }
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) { }
216 void dumpDecl(const Decl *D);
217 void dumpStmt(const Stmt *S);
218 void dumpFullComment(const FullComment *C);
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);
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());
246 // Objective-C utilities.
247 void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
250 void VisitComplexType(const ComplexType *T) {
251 dumpTypeAsChild(T->getElementType());
253 void VisitPointerType(const PointerType *T) {
254 dumpTypeAsChild(T->getPointeeType());
256 void VisitBlockPointerType(const BlockPointerType *T) {
257 dumpTypeAsChild(T->getPointeeType());
259 void VisitReferenceType(const ReferenceType *T) {
260 dumpTypeAsChild(T->getPointeeType());
262 void VisitRValueReferenceType(const ReferenceType *T) {
263 if (T->isSpelledAsLValue())
264 OS << " written as lvalue reference";
265 VisitReferenceType(T);
267 void VisitMemberPointerType(const MemberPointerType *T) {
268 dumpTypeAsChild(T->getClass());
269 dumpTypeAsChild(T->getPointeeType());
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;
277 OS << " " << T->getIndexTypeQualifiers().getAsString();
278 dumpTypeAsChild(T->getElementType());
280 void VisitConstantArrayType(const ConstantArrayType *T) {
281 OS << " " << T->getSize();
284 void VisitVariableArrayType(const VariableArrayType *T) {
286 dumpSourceRange(T->getBracketsRange());
288 dumpStmt(T->getSizeExpr());
290 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
293 dumpSourceRange(T->getBracketsRange());
294 dumpStmt(T->getSizeExpr());
296 void VisitDependentSizedExtVectorType(
297 const DependentSizedExtVectorType *T) {
299 dumpLocation(T->getAttributeLoc());
300 dumpTypeAsChild(T->getElementType());
301 dumpStmt(T->getSizeExpr());
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;
312 OS << " " << T->getNumElements();
313 dumpTypeAsChild(T->getElementType());
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());
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) {
331 case RQ_LValue: OS << " &"; break;
332 case RQ_RValue: OS << " &&"; break;
334 // FIXME: Exception specification.
335 // FIXME: Consumed parameters.
336 VisitFunctionType(T);
337 for (QualType PT : T->getParamTypes())
340 dumpChild([=] { OS << "..."; });
342 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
343 dumpDeclRef(T->getDecl());
345 void VisitTypedefType(const TypedefType *T) {
346 dumpDeclRef(T->getDecl());
348 void VisitTypeOfExprType(const TypeOfExprType *T) {
349 dumpStmt(T->getUnderlyingExpr());
351 void VisitDecltypeType(const DecltypeType *T) {
352 dumpStmt(T->getUnderlyingExpr());
354 void VisitUnaryTransformType(const UnaryTransformType *T) {
355 switch (T->getUTTKind()) {
356 case UnaryTransformType::EnumUnderlyingType:
357 OS << " underlying_type";
360 dumpTypeAsChild(T->getBaseType());
362 void VisitTagType(const TagType *T) {
363 dumpDeclRef(T->getDecl());
365 void VisitAttributedType(const AttributedType *T) {
367 dumpTypeAsChild(T->getModifiedType());
369 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
370 OS << " depth " << T->getDepth() << " index " << T->getIndex();
371 if (T->isParameterPack()) OS << " pack";
372 dumpDeclRef(T->getDecl());
374 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
375 dumpTypeAsChild(T->getReplacedParameter());
377 void VisitSubstTemplateTypeParmPackType(
378 const SubstTemplateTypeParmPackType *T) {
379 dumpTypeAsChild(T->getReplacedParameter());
380 dumpTemplateArgument(T->getArgumentPack());
382 void VisitAutoType(const AutoType *T) {
383 if (T->isDecltypeAuto()) OS << " decltype(auto)";
387 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
388 if (T->isTypeAlias()) OS << " alias";
389 OS << " "; T->getTemplateName().dump(OS);
391 dumpTemplateArgument(Arg);
392 if (T->isTypeAlias())
393 dumpTypeAsChild(T->getAliasedType());
395 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
396 dumpDeclRef(T->getDecl());
398 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
399 dumpDeclRef(T->getDecl());
401 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
402 dumpTypeAsChild(T->getPointeeType());
404 void VisitAtomicType(const AtomicType *T) {
405 dumpTypeAsChild(T->getValueType());
407 void VisitPipeType(const PipeType *T) {
408 dumpTypeAsChild(T->getElementType());
410 void VisitAdjustedType(const AdjustedType *T) {
411 dumpTypeAsChild(T->getOriginalType());
413 void VisitPackExpansionType(const PackExpansionType *T) {
414 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
416 dumpTypeAsChild(T->getPattern());
418 // FIXME: ElaboratedType, DependentNameType,
419 // DependentTemplateSpecializationType, ObjCObjectType
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);
440 void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
441 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
442 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
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,
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);
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);
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);
508 void VisitOMPExecutableDirective(const OMPExecutableDirective *Node);
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 VisitArrayInitLoopExpr(const ArrayInitLoopExpr *ILE);
521 void VisitArrayInitIndexExpr(const ArrayInitIndexExpr *ILE);
522 void VisitUnaryOperator(const UnaryOperator *Node);
523 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
524 void VisitMemberExpr(const MemberExpr *Node);
525 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
526 void VisitBinaryOperator(const BinaryOperator *Node);
527 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
528 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
529 void VisitBlockExpr(const BlockExpr *Node);
530 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
533 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
534 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
535 void VisitCXXThisExpr(const CXXThisExpr *Node);
536 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
537 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
538 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
539 void VisitCXXNewExpr(const CXXNewExpr *Node);
540 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
541 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
542 void VisitExprWithCleanups(const ExprWithCleanups *Node);
543 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
544 void dumpCXXTemporary(const CXXTemporary *Temporary);
545 void VisitLambdaExpr(const LambdaExpr *Node) {
547 dumpDecl(Node->getLambdaClass());
549 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
551 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
554 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
555 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
556 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
557 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
558 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
559 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
560 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
561 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
562 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
563 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
566 const char *getCommandName(unsigned CommandID);
567 void dumpComment(const Comment *C);
570 void visitTextComment(const TextComment *C);
571 void visitInlineCommandComment(const InlineCommandComment *C);
572 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
573 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
576 void visitBlockCommandComment(const BlockCommandComment *C);
577 void visitParamCommandComment(const ParamCommandComment *C);
578 void visitTParamCommandComment(const TParamCommandComment *C);
579 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
580 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
581 void visitVerbatimLineComment(const VerbatimLineComment *C);
585 //===----------------------------------------------------------------------===//
587 //===----------------------------------------------------------------------===//
589 void ASTDumper::dumpPointer(const void *Ptr) {
590 ColorScope Color(*this, AddressColor);
594 void ASTDumper::dumpLocation(SourceLocation Loc) {
598 ColorScope Color(*this, LocationColor);
599 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
601 // The general format we print out is filename:line:col, but we drop pieces
602 // that haven't changed since the last loc printed.
603 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
605 if (PLoc.isInvalid()) {
606 OS << "<invalid sloc>";
610 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
611 OS << PLoc.getFilename() << ':' << PLoc.getLine()
612 << ':' << PLoc.getColumn();
613 LastLocFilename = PLoc.getFilename();
614 LastLocLine = PLoc.getLine();
615 } else if (PLoc.getLine() != LastLocLine) {
616 OS << "line" << ':' << PLoc.getLine()
617 << ':' << PLoc.getColumn();
618 LastLocLine = PLoc.getLine();
620 OS << "col" << ':' << PLoc.getColumn();
624 void ASTDumper::dumpSourceRange(SourceRange R) {
625 // Can't translate locations if a SourceManager isn't available.
630 dumpLocation(R.getBegin());
631 if (R.getBegin() != R.getEnd()) {
633 dumpLocation(R.getEnd());
637 // <t2.c:123:421[blah], t2.c:412:321>
641 void ASTDumper::dumpBareType(QualType T, bool Desugar) {
642 ColorScope Color(*this, TypeColor);
644 SplitQualType T_split = T.split();
645 OS << "'" << QualType::getAsString(T_split) << "'";
647 if (Desugar && !T.isNull()) {
648 // If the type is sugared, also dump a (shallow) desugared type.
649 SplitQualType D_split = T.getSplitDesugaredType();
650 if (T_split != D_split)
651 OS << ":'" << QualType::getAsString(D_split) << "'";
655 void ASTDumper::dumpType(QualType T) {
660 void ASTDumper::dumpTypeAsChild(QualType T) {
661 SplitQualType SQT = T.split();
662 if (!SQT.Quals.hasQualifiers())
663 return dumpTypeAsChild(SQT.Ty);
667 dumpPointer(T.getAsOpaquePtr());
669 dumpBareType(T, false);
670 OS << " " << T.split().Quals.getAsString();
671 dumpTypeAsChild(T.split().Ty);
675 void ASTDumper::dumpTypeAsChild(const Type *T) {
678 ColorScope Color(*this, NullColor);
682 if (const LocInfoType *LIT = llvm::dyn_cast<LocInfoType>(T)) {
684 ColorScope Color(*this, TypeColor);
685 OS << "LocInfo Type";
688 dumpTypeAsChild(LIT->getTypeSourceInfo()->getType());
693 ColorScope Color(*this, TypeColor);
694 OS << T->getTypeClassName() << "Type";
698 dumpBareType(QualType(T, 0), false);
700 QualType SingleStepDesugar =
701 T->getLocallyUnqualifiedSingleStepDesugaredType();
702 if (SingleStepDesugar != QualType(T, 0))
704 if (T->isDependentType())
706 else if (T->isInstantiationDependentType())
707 OS << " instantiation_dependent";
708 if (T->isVariablyModifiedType())
709 OS << " variably_modified";
710 if (T->containsUnexpandedParameterPack())
711 OS << " contains_unexpanded_pack";
715 TypeVisitor<ASTDumper>::Visit(T);
717 if (SingleStepDesugar != QualType(T, 0))
718 dumpTypeAsChild(SingleStepDesugar);
722 void ASTDumper::dumpBareDeclRef(const Decl *D) {
724 ColorScope Color(*this, NullColor);
730 ColorScope Color(*this, DeclKindNameColor);
731 OS << D->getDeclKindName();
735 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
736 ColorScope Color(*this, DeclNameColor);
737 OS << " '" << ND->getDeclName() << '\'';
740 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
741 dumpType(VD->getType());
744 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
755 void ASTDumper::dumpName(const NamedDecl *ND) {
756 if (ND->getDeclName()) {
757 ColorScope Color(*this, DeclNameColor);
758 OS << ' ' << ND->getNameAsString();
762 bool ASTDumper::hasNodes(const DeclContext *DC) {
766 return DC->hasExternalLexicalStorage() ||
767 DC->noload_decls_begin() != DC->noload_decls_end();
770 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
774 for (auto *D : DC->noload_decls())
777 if (DC->hasExternalLexicalStorage()) {
779 ColorScope Color(*this, UndeserializedColor);
780 OS << "<undeserialized declarations>";
785 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
787 OS << "StoredDeclsMap ";
788 dumpBareDeclRef(cast<Decl>(DC));
790 const DeclContext *Primary = DC->getPrimaryContext();
793 dumpPointer(cast<Decl>(Primary));
796 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
798 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
799 E = Primary->noload_lookups_end();
801 DeclarationName Name = I.getLookupName();
802 DeclContextLookupResult R = *I++;
805 OS << "DeclarationName ";
807 ColorScope Color(*this, DeclNameColor);
808 OS << '\'' << Name << '\'';
811 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
814 dumpBareDeclRef(*RI);
816 if ((*RI)->isHidden())
819 // If requested, dump the redecl chain for this lookup.
821 // Dump earliest decl first.
822 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
823 if (Decl *Prev = D->getPreviousDecl())
834 if (HasUndeserializedLookups) {
836 ColorScope Color(*this, UndeserializedColor);
837 OS << "<undeserialized lookups>";
843 void ASTDumper::dumpAttr(const Attr *A) {
846 ColorScope Color(*this, AttrColor);
848 switch (A->getKind()) {
849 #define ATTR(X) case attr::X: OS << #X; break;
850 #include "clang/Basic/AttrList.inc"
855 dumpSourceRange(A->getRange());
856 if (A->isInherited())
860 #include "clang/AST/AttrDump.inc"
864 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
867 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
868 const T *First = D->getFirstDecl();
870 OS << " first " << First;
874 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
875 const T *Prev = D->getPreviousDecl();
877 OS << " prev " << Prev;
880 /// Dump the previous declaration in the redeclaration chain for a declaration,
882 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
883 switch (D->getKind()) {
884 #define DECL(DERIVED, BASE) \
885 case Decl::DERIVED: \
886 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
887 #define ABSTRACT_DECL(DECL)
888 #include "clang/AST/DeclNodes.inc"
890 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
893 //===----------------------------------------------------------------------===//
895 //===----------------------------------------------------------------------===//
897 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
913 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
915 OS << "CXXCtorInitializer";
916 if (Init->isAnyMemberInitializer()) {
918 dumpBareDeclRef(Init->getAnyMember());
919 } else if (Init->isBaseInitializer()) {
920 dumpType(QualType(Init->getBaseClass(), 0));
921 } else if (Init->isDelegatingInitializer()) {
922 dumpType(Init->getTypeSourceInfo()->getType());
924 llvm_unreachable("Unknown initializer type");
926 dumpStmt(Init->getInit());
930 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
934 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
939 void ASTDumper::dumpTemplateArgumentListInfo(
940 const TemplateArgumentListInfo &TALI) {
941 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
942 dumpTemplateArgumentLoc(TALI[i]);
945 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
946 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
949 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
950 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
951 dumpTemplateArgument(TAL[i]);
954 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
956 OS << "TemplateArgument";
960 switch (A.getKind()) {
961 case TemplateArgument::Null:
964 case TemplateArgument::Type:
966 dumpType(A.getAsType());
968 case TemplateArgument::Declaration:
970 dumpDeclRef(A.getAsDecl());
972 case TemplateArgument::NullPtr:
975 case TemplateArgument::Integral:
976 OS << " integral " << A.getAsIntegral();
978 case TemplateArgument::Template:
980 A.getAsTemplate().dump(OS);
982 case TemplateArgument::TemplateExpansion:
983 OS << " template expansion";
984 A.getAsTemplateOrTemplatePattern().dump(OS);
986 case TemplateArgument::Expression:
988 dumpStmt(A.getAsExpr());
990 case TemplateArgument::Pack:
992 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
994 dumpTemplateArgument(*I);
1000 //===----------------------------------------------------------------------===//
1001 // Objective-C Utilities
1002 //===----------------------------------------------------------------------===//
1003 void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
1007 for (auto typeParam : *typeParams) {
1008 dumpDecl(typeParam);
1012 //===----------------------------------------------------------------------===//
1013 // Decl dumping methods.
1014 //===----------------------------------------------------------------------===//
1016 void ASTDumper::dumpDecl(const Decl *D) {
1019 ColorScope Color(*this, NullColor);
1025 ColorScope Color(*this, DeclKindNameColor);
1026 OS << D->getDeclKindName() << "Decl";
1029 if (D->getLexicalDeclContext() != D->getDeclContext())
1030 OS << " parent " << cast<Decl>(D->getDeclContext());
1031 dumpPreviousDecl(OS, D);
1032 dumpSourceRange(D->getSourceRange());
1034 dumpLocation(D->getLocation());
1035 if (Module *M = D->getImportedOwningModule())
1036 OS << " in " << M->getFullModuleName();
1037 else if (Module *M = D->getLocalOwningModule())
1038 OS << " in (local) " << M->getFullModuleName();
1039 if (auto *ND = dyn_cast<NamedDecl>(D))
1040 for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
1041 const_cast<NamedDecl *>(ND)))
1042 dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
1043 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
1046 if (D->isImplicit())
1050 else if (D->isThisDeclarationReferenced())
1051 OS << " referenced";
1052 if (D->isInvalidDecl())
1054 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1055 if (FD->isConstexpr())
1059 ConstDeclVisitor<ASTDumper>::Visit(D);
1061 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1065 if (const FullComment *Comment =
1066 D->getASTContext().getLocalCommentForDeclUncached(D))
1067 dumpFullComment(Comment);
1069 // Decls within functions are visited by the body.
1070 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1071 hasNodes(dyn_cast<DeclContext>(D)))
1072 dumpDeclContext(cast<DeclContext>(D));
1076 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
1080 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
1082 dumpType(D->getUnderlyingType());
1083 if (D->isModulePrivate())
1084 OS << " __module_private__";
1085 dumpTypeAsChild(D->getUnderlyingType());
1088 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
1089 if (D->isScoped()) {
1090 if (D->isScopedUsingClassTag())
1096 if (D->isModulePrivate())
1097 OS << " __module_private__";
1099 dumpType(D->getIntegerType());
1102 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
1103 OS << ' ' << D->getKindName();
1105 if (D->isModulePrivate())
1106 OS << " __module_private__";
1107 if (D->isCompleteDefinition())
1108 OS << " definition";
1111 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1113 dumpType(D->getType());
1114 if (const Expr *Init = D->getInitExpr())
1118 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1120 dumpType(D->getType());
1122 for (auto *Child : D->chain())
1126 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
1128 dumpType(D->getType());
1130 StorageClass SC = D->getStorageClass();
1132 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1133 if (D->isInlineSpecified())
1135 if (D->isVirtualAsWritten())
1137 if (D->isModulePrivate())
1138 OS << " __module_private__";
1142 if (D->isDefaulted()) {
1147 if (D->isDeletedAsWritten())
1152 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1153 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1154 switch (EPI.ExceptionSpec.Type) {
1156 case EST_Unevaluated:
1157 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1159 case EST_Uninstantiated:
1160 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1165 if (const FunctionTemplateSpecializationInfo *FTSI =
1166 D->getTemplateSpecializationInfo())
1167 dumpTemplateArgumentList(*FTSI->TemplateArguments);
1169 if (!D->param_begin() && D->getNumParams())
1170 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1172 for (const ParmVarDecl *Parameter : D->parameters())
1173 dumpDecl(Parameter);
1175 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
1176 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
1179 dumpCXXCtorInitializer(*I);
1181 if (D->doesThisDeclarationHaveABody())
1182 dumpStmt(D->getBody());
1185 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
1187 dumpType(D->getType());
1190 if (D->isModulePrivate())
1191 OS << " __module_private__";
1193 if (D->isBitField())
1194 dumpStmt(D->getBitWidth());
1195 if (Expr *Init = D->getInClassInitializer())
1199 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1201 dumpType(D->getType());
1202 StorageClass SC = D->getStorageClass();
1204 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1205 switch (D->getTLSKind()) {
1206 case VarDecl::TLS_None: break;
1207 case VarDecl::TLS_Static: OS << " tls"; break;
1208 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1210 if (D->isModulePrivate())
1211 OS << " __module_private__";
1212 if (D->isNRVOVariable())
1216 if (D->isConstexpr())
1219 switch (D->getInitStyle()) {
1220 case VarDecl::CInit: OS << " cinit"; break;
1221 case VarDecl::CallInit: OS << " callinit"; break;
1222 case VarDecl::ListInit: OS << " listinit"; break;
1224 dumpStmt(D->getInit());
1228 void ASTDumper::VisitDecompositionDecl(const DecompositionDecl *D) {
1230 for (auto *B : D->bindings())
1234 void ASTDumper::VisitBindingDecl(const BindingDecl *D) {
1236 dumpType(D->getType());
1237 if (auto *E = D->getBinding())
1241 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1242 dumpStmt(D->getAsmString());
1245 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1246 OS << ' ' << D->getImportedModule()->getFullModuleName();
1249 void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1251 switch (D->getCommentKind()) {
1252 case PCK_Unknown: llvm_unreachable("unexpected pragma comment kind");
1253 case PCK_Compiler: OS << "compiler"; break;
1254 case PCK_ExeStr: OS << "exestr"; break;
1255 case PCK_Lib: OS << "lib"; break;
1256 case PCK_Linker: OS << "linker"; break;
1257 case PCK_User: OS << "user"; break;
1259 StringRef Arg = D->getArg();
1261 OS << " \"" << Arg << "\"";
1264 void ASTDumper::VisitPragmaDetectMismatchDecl(
1265 const PragmaDetectMismatchDecl *D) {
1266 OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1269 void ASTDumper::VisitCapturedDecl(const CapturedDecl *D) {
1270 dumpStmt(D->getBody());
1273 //===----------------------------------------------------------------------===//
1274 // OpenMP Declarations
1275 //===----------------------------------------------------------------------===//
1277 void ASTDumper::VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
1278 for (auto *E : D->varlists())
1282 void ASTDumper::VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
1284 dumpType(D->getType());
1286 dumpStmt(D->getCombiner());
1287 if (auto *Initializer = D->getInitializer()) {
1288 OS << " initializer";
1289 dumpStmt(Initializer);
1293 void ASTDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1295 dumpType(D->getType());
1296 dumpStmt(D->getInit());
1299 //===----------------------------------------------------------------------===//
1301 //===----------------------------------------------------------------------===//
1303 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1307 if (!D->isOriginalNamespace())
1308 dumpDeclRef(D->getOriginalNamespace(), "original");
1311 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1313 dumpBareDeclRef(D->getNominatedNamespace());
1316 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1318 dumpDeclRef(D->getAliasedNamespace());
1321 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1323 dumpType(D->getUnderlyingType());
1324 dumpTypeAsChild(D->getUnderlyingType());
1327 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1329 dumpTemplateParameters(D->getTemplateParameters());
1330 dumpDecl(D->getTemplatedDecl());
1333 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1335 if (!D->isCompleteDefinition())
1338 for (const auto &I : D->bases()) {
1342 dumpAccessSpecifier(I.getAccessSpecifier());
1343 dumpType(I.getType());
1344 if (I.isPackExpansion())
1350 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1351 dumpStmt(D->getAssertExpr());
1352 dumpStmt(D->getMessage());
1355 template<typename SpecializationDecl>
1356 void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
1357 bool DumpExplicitInst,
1359 bool DumpedAny = false;
1360 for (auto *RedeclWithBadType : D->redecls()) {
1361 // FIXME: The redecls() range sometimes has elements of a less-specific
1362 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1363 // us TagDecls, and should give CXXRecordDecls).
1364 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1366 // Found the injected-class-name for a class template. This will be dumped
1367 // as part of its surrounding class so we don't need to dump it here.
1368 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1369 "expected an injected-class-name");
1373 switch (Redecl->getTemplateSpecializationKind()) {
1374 case TSK_ExplicitInstantiationDeclaration:
1375 case TSK_ExplicitInstantiationDefinition:
1376 if (!DumpExplicitInst)
1379 case TSK_Undeclared:
1380 case TSK_ImplicitInstantiation:
1382 dumpDeclRef(Redecl);
1387 case TSK_ExplicitSpecialization:
1392 // Ensure we dump at least one decl for each specialization.
1397 template<typename TemplateDecl>
1398 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1399 bool DumpExplicitInst) {
1401 dumpTemplateParameters(D->getTemplateParameters());
1403 dumpDecl(D->getTemplatedDecl());
1405 for (auto *Child : D->specializations())
1406 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
1407 !D->isCanonicalDecl());
1410 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1411 // FIXME: We don't add a declaration of a function template specialization
1412 // to its context when it's explicitly instantiated, so dump explicit
1413 // instantiations when we dump the template itself.
1414 VisitTemplateDecl(D, true);
1417 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1418 VisitTemplateDecl(D, false);
1421 void ASTDumper::VisitClassTemplateSpecializationDecl(
1422 const ClassTemplateSpecializationDecl *D) {
1423 VisitCXXRecordDecl(D);
1424 dumpTemplateArgumentList(D->getTemplateArgs());
1427 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1428 const ClassTemplatePartialSpecializationDecl *D) {
1429 VisitClassTemplateSpecializationDecl(D);
1430 dumpTemplateParameters(D->getTemplateParameters());
1433 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1434 const ClassScopeFunctionSpecializationDecl *D) {
1435 dumpDeclRef(D->getSpecialization());
1436 if (D->hasExplicitTemplateArgs())
1437 dumpTemplateArgumentListInfo(D->templateArgs());
1440 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1441 VisitTemplateDecl(D, false);
1444 void ASTDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
1446 dumpTemplateParameters(D->getTemplateParameters());
1449 void ASTDumper::VisitVarTemplateSpecializationDecl(
1450 const VarTemplateSpecializationDecl *D) {
1451 dumpTemplateArgumentList(D->getTemplateArgs());
1455 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1456 const VarTemplatePartialSpecializationDecl *D) {
1457 dumpTemplateParameters(D->getTemplateParameters());
1458 VisitVarTemplateSpecializationDecl(D);
1461 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1462 if (D->wasDeclaredWithTypename())
1466 if (D->isParameterPack())
1469 if (D->hasDefaultArgument())
1470 dumpTemplateArgument(D->getDefaultArgument());
1473 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1474 dumpType(D->getType());
1475 if (D->isParameterPack())
1478 if (D->hasDefaultArgument())
1479 dumpTemplateArgument(D->getDefaultArgument());
1482 void ASTDumper::VisitTemplateTemplateParmDecl(
1483 const TemplateTemplateParmDecl *D) {
1484 if (D->isParameterPack())
1487 dumpTemplateParameters(D->getTemplateParameters());
1488 if (D->hasDefaultArgument())
1489 dumpTemplateArgumentLoc(D->getDefaultArgument());
1492 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1494 if (D->getQualifier())
1495 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1496 OS << D->getNameAsString();
1499 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1500 const UnresolvedUsingTypenameDecl *D) {
1502 if (D->getQualifier())
1503 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1504 OS << D->getNameAsString();
1507 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1509 if (D->getQualifier())
1510 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1511 OS << D->getNameAsString();
1512 dumpType(D->getType());
1515 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1517 dumpBareDeclRef(D->getTargetDecl());
1518 if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
1519 dumpTypeAsChild(TD->getTypeForDecl());
1522 void ASTDumper::VisitConstructorUsingShadowDecl(
1523 const ConstructorUsingShadowDecl *D) {
1524 if (D->constructsVirtualBase())
1529 dumpBareDeclRef(D->getTargetDecl());
1534 dumpBareDeclRef(D->getNominatedBaseClass());
1536 dumpBareDeclRef(D->getNominatedBaseClassShadowDecl());
1540 OS << "constructed ";
1541 dumpBareDeclRef(D->getConstructedBaseClass());
1543 dumpBareDeclRef(D->getConstructedBaseClassShadowDecl());
1547 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1548 switch (D->getLanguage()) {
1549 case LinkageSpecDecl::lang_c: OS << " C"; break;
1550 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1554 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1556 dumpAccessSpecifier(D->getAccess());
1559 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1560 if (TypeSourceInfo *T = D->getFriendType())
1561 dumpType(T->getType());
1563 dumpDecl(D->getFriendDecl());
1566 //===----------------------------------------------------------------------===//
1567 // Obj-C Declarations
1568 //===----------------------------------------------------------------------===//
1570 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1572 dumpType(D->getType());
1573 if (D->getSynthesize())
1574 OS << " synthesize";
1576 switch (D->getAccessControl()) {
1577 case ObjCIvarDecl::None:
1580 case ObjCIvarDecl::Private:
1583 case ObjCIvarDecl::Protected:
1586 case ObjCIvarDecl::Public:
1589 case ObjCIvarDecl::Package:
1595 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1596 if (D->isInstanceMethod())
1601 dumpType(D->getReturnType());
1603 if (D->isThisDeclarationADefinition()) {
1606 for (const ParmVarDecl *Parameter : D->parameters())
1607 dumpDecl(Parameter);
1610 if (D->isVariadic())
1611 dumpChild([=] { OS << "..."; });
1614 dumpStmt(D->getBody());
1617 void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
1619 switch (D->getVariance()) {
1620 case ObjCTypeParamVariance::Invariant:
1623 case ObjCTypeParamVariance::Covariant:
1627 case ObjCTypeParamVariance::Contravariant:
1628 OS << " contravariant";
1632 if (D->hasExplicitBound())
1634 dumpType(D->getUnderlyingType());
1637 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1639 dumpDeclRef(D->getClassInterface());
1640 dumpObjCTypeParamList(D->getTypeParamList());
1641 dumpDeclRef(D->getImplementation());
1642 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1643 E = D->protocol_end();
1648 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1650 dumpDeclRef(D->getClassInterface());
1651 dumpDeclRef(D->getCategoryDecl());
1654 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1657 for (auto *Child : D->protocols())
1661 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1663 dumpObjCTypeParamList(D->getTypeParamListAsWritten());
1664 dumpDeclRef(D->getSuperClass(), "super");
1666 dumpDeclRef(D->getImplementation());
1667 for (auto *Child : D->protocols())
1671 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1673 dumpDeclRef(D->getSuperClass(), "super");
1674 dumpDeclRef(D->getClassInterface());
1675 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1678 dumpCXXCtorInitializer(*I);
1681 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1683 dumpDeclRef(D->getClassInterface());
1686 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1688 dumpType(D->getType());
1690 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1692 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1695 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1696 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1697 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1699 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1701 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1703 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1705 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1707 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1709 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1711 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1713 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1715 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1716 OS << " unsafe_unretained";
1717 if (Attrs & ObjCPropertyDecl::OBJC_PR_class)
1719 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
1720 dumpDeclRef(D->getGetterMethodDecl(), "getter");
1721 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
1722 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1726 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1727 dumpName(D->getPropertyDecl());
1728 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1729 OS << " synthesize";
1732 dumpDeclRef(D->getPropertyDecl());
1733 dumpDeclRef(D->getPropertyIvarDecl());
1736 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1737 for (auto I : D->parameters())
1740 if (D->isVariadic())
1741 dumpChild([=]{ OS << "..."; });
1743 if (D->capturesCXXThis())
1744 dumpChild([=]{ OS << "capture this"; });
1746 for (const auto &I : D->captures()) {
1753 if (I.getVariable()) {
1755 dumpBareDeclRef(I.getVariable());
1757 if (I.hasCopyExpr())
1758 dumpStmt(I.getCopyExpr());
1761 dumpStmt(D->getBody());
1764 //===----------------------------------------------------------------------===//
1765 // Stmt dumping methods.
1766 //===----------------------------------------------------------------------===//
1768 void ASTDumper::dumpStmt(const Stmt *S) {
1771 ColorScope Color(*this, NullColor);
1776 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1781 ConstStmtVisitor<ASTDumper>::Visit(S);
1783 for (const Stmt *SubStmt : S->children())
1788 void ASTDumper::VisitStmt(const Stmt *Node) {
1790 ColorScope Color(*this, StmtColor);
1791 OS << Node->getStmtClassName();
1794 dumpSourceRange(Node->getSourceRange());
1797 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1799 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1800 E = Node->decl_end();
1805 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1807 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1808 E = Node->getAttrs().end();
1813 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1815 OS << " '" << Node->getName() << "'";
1818 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1820 OS << " '" << Node->getLabel()->getName() << "'";
1821 dumpPointer(Node->getLabel());
1824 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1826 dumpDecl(Node->getExceptionDecl());
1829 void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
1831 dumpDecl(Node->getCapturedDecl());
1834 //===----------------------------------------------------------------------===//
1835 // OpenMP dumping methods.
1836 //===----------------------------------------------------------------------===//
1838 void ASTDumper::VisitOMPExecutableDirective(
1839 const OMPExecutableDirective *Node) {
1841 for (auto *C : Node->clauses()) {
1844 ColorScope Color(*this, NullColor);
1845 OS << "<<<NULL>>> OMPClause";
1849 ColorScope Color(*this, AttrColor);
1850 StringRef ClauseName(getOpenMPClauseName(C->getClauseKind()));
1851 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
1852 << ClauseName.drop_front() << "Clause";
1855 dumpSourceRange(SourceRange(C->getLocStart(), C->getLocEnd()));
1856 if (C->isImplicit())
1857 OS << " <implicit>";
1858 for (auto *S : C->children())
1864 //===----------------------------------------------------------------------===//
1865 // Expr dumping methods.
1866 //===----------------------------------------------------------------------===//
1868 void ASTDumper::VisitExpr(const Expr *Node) {
1870 dumpType(Node->getType());
1873 ColorScope Color(*this, ValueKindColor);
1874 switch (Node->getValueKind()) {
1887 ColorScope Color(*this, ObjectKindColor);
1888 switch (Node->getObjectKind()) {
1894 case OK_ObjCProperty:
1895 OS << " objcproperty";
1897 case OK_ObjCSubscript:
1898 OS << " objcsubscript";
1900 case OK_VectorComponent:
1901 OS << " vectorcomponent";
1907 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1908 if (Node->path_empty())
1913 for (CastExpr::path_const_iterator I = Node->path_begin(),
1914 E = Node->path_end();
1916 const CXXBaseSpecifier *Base = *I;
1920 const CXXRecordDecl *RD =
1921 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1923 if (Base->isVirtual())
1925 OS << RD->getName();
1932 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1936 ColorScope Color(*this, CastColor);
1937 OS << Node->getCastKindName();
1939 dumpBasePath(OS, Node);
1943 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1947 dumpBareDeclRef(Node->getDecl());
1948 if (Node->getDecl() != Node->getFoundDecl()) {
1950 dumpBareDeclRef(Node->getFoundDecl());
1955 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1958 if (!Node->requiresADL())
1960 OS << "ADL) = '" << Node->getName() << '\'';
1962 UnresolvedLookupExpr::decls_iterator
1963 I = Node->decls_begin(), E = Node->decls_end();
1970 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1974 ColorScope Color(*this, DeclKindNameColor);
1975 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1977 OS << "='" << *Node->getDecl() << "'";
1978 dumpPointer(Node->getDecl());
1979 if (Node->isFreeIvar())
1980 OS << " isFreeIvar";
1983 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1985 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1988 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1990 ColorScope Color(*this, ValueColor);
1991 OS << " " << Node->getValue();
1994 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1997 bool isSigned = Node->getType()->isSignedIntegerType();
1998 ColorScope Color(*this, ValueColor);
1999 OS << " " << Node->getValue().toString(10, isSigned);
2002 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
2004 ColorScope Color(*this, ValueColor);
2005 OS << " " << Node->getValueAsApproximateDouble();
2008 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
2010 ColorScope Color(*this, ValueColor);
2012 Str->outputString(OS);
2015 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
2017 if (auto *Filler = ILE->getArrayFiller()) {
2019 OS << "array filler";
2023 if (auto *Field = ILE->getInitializedFieldInUnion()) {
2025 dumpBareDeclRef(Field);
2029 void ASTDumper::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2033 void ASTDumper::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2037 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
2039 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
2040 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2043 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
2044 const UnaryExprOrTypeTraitExpr *Node) {
2046 switch(Node->getKind()) {
2056 case UETT_OpenMPRequiredSimdAlign:
2057 OS << " __builtin_omp_required_simd_align";
2060 if (Node->isArgumentType())
2061 dumpType(Node->getArgumentType());
2064 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
2066 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
2067 dumpPointer(Node->getMemberDecl());
2070 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
2072 OS << " " << Node->getAccessor().getNameStart();
2075 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
2077 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2080 void ASTDumper::VisitCompoundAssignOperator(
2081 const CompoundAssignOperator *Node) {
2083 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
2084 << "' ComputeLHSTy=";
2085 dumpBareType(Node->getComputationLHSType());
2086 OS << " ComputeResultTy=";
2087 dumpBareType(Node->getComputationResultType());
2090 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
2092 dumpDecl(Node->getBlockDecl());
2095 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
2098 if (Expr *Source = Node->getSourceExpr())
2104 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
2106 OS << " " << Node->getLabel()->getName();
2107 dumpPointer(Node->getLabel());
2110 //===----------------------------------------------------------------------===//
2112 //===----------------------------------------------------------------------===//
2114 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
2116 OS << " " << Node->getCastName()
2117 << "<" << Node->getTypeAsWritten().getAsString() << ">"
2118 << " <" << Node->getCastKindName();
2119 dumpBasePath(OS, Node);
2123 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
2125 OS << " " << (Node->getValue() ? "true" : "false");
2128 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
2133 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
2135 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
2136 << " <" << Node->getCastKindName() << ">";
2139 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
2141 CXXConstructorDecl *Ctor = Node->getConstructor();
2142 dumpType(Ctor->getType());
2143 if (Node->isElidable())
2145 if (Node->requiresZeroInitialization())
2149 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
2152 dumpCXXTemporary(Node->getTemporary());
2155 void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
2157 if (Node->isGlobalNew())
2159 if (Node->isArray())
2161 if (Node->getOperatorNew()) {
2163 dumpBareDeclRef(Node->getOperatorNew());
2165 // We could dump the deallocation function used in case of error, but it's
2166 // usually not that interesting.
2169 void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
2171 if (Node->isGlobalDelete())
2173 if (Node->isArrayForm())
2175 if (Node->getOperatorDelete()) {
2177 dumpBareDeclRef(Node->getOperatorDelete());
2182 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
2184 if (const ValueDecl *VD = Node->getExtendingDecl()) {
2185 OS << " extended by ";
2186 dumpBareDeclRef(VD);
2190 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
2192 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
2193 dumpDeclRef(Node->getObject(i), "cleanup");
2196 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
2197 OS << "(CXXTemporary";
2198 dumpPointer(Temporary);
2202 void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
2204 dumpPointer(Node->getPack());
2205 dumpName(Node->getPack());
2206 if (Node->isPartiallySubstituted())
2207 for (const auto &A : Node->getPartialArguments())
2208 dumpTemplateArgument(A);
2211 void ASTDumper::VisitCXXDependentScopeMemberExpr(
2212 const CXXDependentScopeMemberExpr *Node) {
2214 OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember();
2217 //===----------------------------------------------------------------------===//
2218 // Obj-C Expressions
2219 //===----------------------------------------------------------------------===//
2221 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
2224 Node->getSelector().print(OS);
2225 switch (Node->getReceiverKind()) {
2226 case ObjCMessageExpr::Instance:
2229 case ObjCMessageExpr::Class:
2231 dumpBareType(Node->getClassReceiver());
2234 case ObjCMessageExpr::SuperInstance:
2235 OS << " super (instance)";
2238 case ObjCMessageExpr::SuperClass:
2239 OS << " super (class)";
2244 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
2246 if (auto *BoxingMethod = Node->getBoxingMethod()) {
2248 BoxingMethod->getSelector().print(OS);
2252 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
2254 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
2255 dumpDecl(CatchParam);
2260 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
2262 dumpType(Node->getEncodedType());
2265 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
2269 Node->getSelector().print(OS);
2272 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
2275 OS << ' ' << *Node->getProtocol();
2278 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
2280 if (Node->isImplicitProperty()) {
2281 OS << " Kind=MethodRef Getter=\"";
2282 if (Node->getImplicitPropertyGetter())
2283 Node->getImplicitPropertyGetter()->getSelector().print(OS);
2287 OS << "\" Setter=\"";
2288 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
2289 Setter->getSelector().print(OS);
2294 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
2297 if (Node->isSuperReceiver())
2300 OS << " Messaging=";
2301 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2302 OS << "Getter&Setter";
2303 else if (Node->isMessagingGetter())
2305 else if (Node->isMessagingSetter())
2309 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
2311 if (Node->isArraySubscriptRefExpr())
2312 OS << " Kind=ArraySubscript GetterForArray=\"";
2314 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2315 if (Node->getAtIndexMethodDecl())
2316 Node->getAtIndexMethodDecl()->getSelector().print(OS);
2320 if (Node->isArraySubscriptRefExpr())
2321 OS << "\" SetterForArray=\"";
2323 OS << "\" SetterForDictionary=\"";
2324 if (Node->setAtIndexMethodDecl())
2325 Node->setAtIndexMethodDecl()->getSelector().print(OS);
2330 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2332 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2335 //===----------------------------------------------------------------------===//
2337 //===----------------------------------------------------------------------===//
2339 const char *ASTDumper::getCommandName(unsigned CommandID) {
2341 return Traits->getCommandInfo(CommandID)->Name;
2342 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2345 return "<not a builtin command>";
2348 void ASTDumper::dumpFullComment(const FullComment *C) {
2357 void ASTDumper::dumpComment(const Comment *C) {
2360 ColorScope Color(*this, NullColor);
2366 ColorScope Color(*this, CommentColor);
2367 OS << C->getCommentKindName();
2370 dumpSourceRange(C->getSourceRange());
2371 ConstCommentVisitor<ASTDumper>::visit(C);
2372 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2378 void ASTDumper::visitTextComment(const TextComment *C) {
2379 OS << " Text=\"" << C->getText() << "\"";
2382 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2383 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2384 switch (C->getRenderKind()) {
2385 case InlineCommandComment::RenderNormal:
2386 OS << " RenderNormal";
2388 case InlineCommandComment::RenderBold:
2389 OS << " RenderBold";
2391 case InlineCommandComment::RenderMonospaced:
2392 OS << " RenderMonospaced";
2394 case InlineCommandComment::RenderEmphasized:
2395 OS << " RenderEmphasized";
2399 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2400 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2403 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2404 OS << " Name=\"" << C->getTagName() << "\"";
2405 if (C->getNumAttrs() != 0) {
2407 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2408 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2409 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2412 if (C->isSelfClosing())
2413 OS << " SelfClosing";
2416 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2417 OS << " Name=\"" << C->getTagName() << "\"";
2420 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2421 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2422 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2423 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2426 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2427 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2429 if (C->isDirectionExplicit())
2430 OS << " explicitly";
2432 OS << " implicitly";
2434 if (C->hasParamName()) {
2435 if (C->isParamIndexValid())
2436 OS << " Param=\"" << C->getParamName(FC) << "\"";
2438 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2441 if (C->isParamIndexValid() && !C->isVarArgParam())
2442 OS << " ParamIndex=" << C->getParamIndex();
2445 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2446 if (C->hasParamName()) {
2447 if (C->isPositionValid())
2448 OS << " Param=\"" << C->getParamName(FC) << "\"";
2450 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2453 if (C->isPositionValid()) {
2454 OS << " Position=<";
2455 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2456 OS << C->getIndex(i);
2464 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2465 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2466 " CloseName=\"" << C->getCloseName() << "\"";
2469 void ASTDumper::visitVerbatimBlockLineComment(
2470 const VerbatimBlockLineComment *C) {
2471 OS << " Text=\"" << C->getText() << "\"";
2474 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2475 OS << " Text=\"" << C->getText() << "\"";
2478 //===----------------------------------------------------------------------===//
2479 // Type method implementations
2480 //===----------------------------------------------------------------------===//
2482 void QualType::dump(const char *msg) const {
2484 llvm::errs() << msg << ": ";
2488 LLVM_DUMP_METHOD void QualType::dump() const { dump(llvm::errs()); }
2490 LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS) const {
2491 ASTDumper Dumper(OS, nullptr, nullptr);
2492 Dumper.dumpTypeAsChild(*this);
2495 LLVM_DUMP_METHOD void Type::dump() const { dump(llvm::errs()); }
2497 LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {
2498 QualType(this, 0).dump(OS);
2501 //===----------------------------------------------------------------------===//
2502 // Decl method implementations
2503 //===----------------------------------------------------------------------===//
2505 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2507 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
2508 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2509 &getASTContext().getSourceManager());
2513 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2514 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2515 &getASTContext().getSourceManager(), /*ShowColors*/true);
2519 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2520 dumpLookups(llvm::errs());
2523 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2524 bool DumpDecls) const {
2525 const DeclContext *DC = this;
2526 while (!DC->isTranslationUnit())
2527 DC = DC->getParent();
2528 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2529 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2530 P.dumpLookups(this, DumpDecls);
2533 //===----------------------------------------------------------------------===//
2534 // Stmt method implementations
2535 //===----------------------------------------------------------------------===//
2537 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2538 dump(llvm::errs(), SM);
2541 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2542 ASTDumper P(OS, nullptr, &SM);
2546 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2547 ASTDumper P(OS, nullptr, nullptr);
2551 LLVM_DUMP_METHOD void Stmt::dump() const {
2552 ASTDumper P(llvm::errs(), nullptr, nullptr);
2556 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2557 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2561 //===----------------------------------------------------------------------===//
2562 // Comment method implementations
2563 //===----------------------------------------------------------------------===//
2565 LLVM_DUMP_METHOD void Comment::dump() const {
2566 dump(llvm::errs(), nullptr, nullptr);
2569 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2570 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2571 &Context.getSourceManager());
2574 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2575 const SourceManager *SM) const {
2576 const FullComment *FC = dyn_cast<FullComment>(this);
2577 ASTDumper D(OS, Traits, SM);
2578 D.dumpFullComment(FC);
2581 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2582 const FullComment *FC = dyn_cast<FullComment>(this);
2583 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2584 D.dumpFullComment(FC);