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/DeclVisitor.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/AST/TypeVisitor.h"
24 #include "clang/Basic/Module.h"
25 #include "clang/Basic/SourceManager.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
28 using namespace clang::comments;
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
35 // Colors used for various parts of the AST dump
36 // Do not use bold yellow for any text. It is hard to read on white screens.
38 struct TerminalColor {
39 raw_ostream::Colors Color;
45 // Bold Green - DeclKindNameColor, UndeserializedColor
46 // Yellow - AddressColor, LocationColor
47 // Blue - CommentColor, NullColor, IndentColor
48 // Bold Blue - AttrColor
49 // Bold Magenta - StmtColor
50 // Cyan - ValueKindColor, ObjectKindColor
51 // Bold Cyan - ValueColor, DeclNameColor
53 // Decl kind names (VarDecl, FunctionDecl, etc)
54 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
55 // Attr names (CleanupAttr, GuardedByAttr, etc)
56 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
57 // Statement names (DeclStmt, ImplicitCastExpr, etc)
58 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
59 // Comment names (FullComment, ParagraphComment, TextComment, etc)
60 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
62 // Type names (int, float, etc, plus user defined types)
63 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
66 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
68 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
71 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
72 // bitfield/objcproperty/objcsubscript/vectorcomponent
73 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
76 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
78 // Undeserialized entities
79 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
81 // CastKind from CastExpr's
82 static const TerminalColor CastColor = { raw_ostream::RED, false };
84 // Value of the statement
85 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
87 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
89 // Indents ( `, -. | )
90 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
93 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
94 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
96 const CommandTraits *Traits;
97 const SourceManager *SM;
99 /// Pending[i] is an action to dump an entity at level i.
100 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
102 /// Indicates whether we're at the top level.
105 /// Indicates if we're handling the first child after entering a new depth.
108 /// Prefix for currently-being-dumped entity.
111 /// Keep track of the last location we print out so that we can
112 /// print out deltas from then on out.
113 const char *LastLocFilename;
114 unsigned LastLocLine;
116 /// The \c FullComment parent of the comment being dumped.
117 const FullComment *FC;
121 /// Dump a child of the current node.
122 template<typename Fn> void dumpChild(Fn doDumpChild) {
123 // If we're at the top level, there's nothing interesting to do; just
128 while (!Pending.empty()) {
129 Pending.back()(true);
138 const FullComment *OrigFC = FC;
139 auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
140 // Print out the appropriate tree structure and work out the prefix for
141 // children of this node. For instance:
145 // | `-C Prefix = "| "
147 // |-E Prefix = " | "
151 // Note that the first level gets no prefix.
154 ColorScope Color(*this, IndentColor);
155 OS << Prefix << (isLastChild ? '`' : '|') << '-';
156 this->Prefix.push_back(isLastChild ? ' ' : '|');
157 this->Prefix.push_back(' ');
161 unsigned Depth = Pending.size();
166 // If any children are left, they're the last at their nesting level.
167 // Dump those ones out now.
168 while (Depth < Pending.size()) {
169 Pending.back()(true);
170 this->Pending.pop_back();
173 // Restore the old prefix.
174 this->Prefix.resize(Prefix.size() - 2);
178 Pending.push_back(std::move(dumpWithIndent));
180 Pending.back()(false);
181 Pending.back() = std::move(dumpWithIndent);
189 ColorScope(ASTDumper &Dumper, TerminalColor Color)
191 if (Dumper.ShowColors)
192 Dumper.OS.changeColor(Color.Color, Color.Bold);
195 if (Dumper.ShowColors)
196 Dumper.OS.resetColor();
201 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
202 const SourceManager *SM)
203 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
204 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
205 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
207 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
208 const SourceManager *SM, bool ShowColors)
209 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
210 LastLocFilename(""), LastLocLine(~0U),
211 ShowColors(ShowColors) { }
213 void dumpDecl(const Decl *D);
214 void dumpStmt(const Stmt *S);
215 void dumpFullComment(const FullComment *C);
218 void dumpPointer(const void *Ptr);
219 void dumpSourceRange(SourceRange R);
220 void dumpLocation(SourceLocation Loc);
221 void dumpBareType(QualType T, bool Desugar = true);
222 void dumpType(QualType T);
223 void dumpTypeAsChild(QualType T);
224 void dumpTypeAsChild(const Type *T);
225 void dumpBareDeclRef(const Decl *Node);
226 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
227 void dumpName(const NamedDecl *D);
228 bool hasNodes(const DeclContext *DC);
229 void dumpDeclContext(const DeclContext *DC);
230 void dumpLookups(const DeclContext *DC, bool DumpDecls);
231 void dumpAttr(const Attr *A);
234 void dumpAccessSpecifier(AccessSpecifier AS);
235 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
236 void dumpTemplateParameters(const TemplateParameterList *TPL);
237 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
238 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
239 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
240 void dumpTemplateArgument(const TemplateArgument &A,
241 SourceRange R = SourceRange());
244 void VisitComplexType(const ComplexType *T) {
245 dumpTypeAsChild(T->getElementType());
247 void VisitPointerType(const PointerType *T) {
248 dumpTypeAsChild(T->getPointeeType());
250 void VisitBlockPointerType(const BlockPointerType *T) {
251 dumpTypeAsChild(T->getPointeeType());
253 void VisitReferenceType(const ReferenceType *T) {
254 dumpTypeAsChild(T->getPointeeType());
256 void VisitRValueReferenceType(const ReferenceType *T) {
257 if (T->isSpelledAsLValue())
258 OS << " written as lvalue reference";
259 VisitReferenceType(T);
261 void VisitMemberPointerType(const MemberPointerType *T) {
262 dumpTypeAsChild(T->getClass());
263 dumpTypeAsChild(T->getPointeeType());
265 void VisitArrayType(const ArrayType *T) {
266 switch (T->getSizeModifier()) {
267 case ArrayType::Normal: break;
268 case ArrayType::Static: OS << " static"; break;
269 case ArrayType::Star: OS << " *"; break;
271 OS << " " << T->getIndexTypeQualifiers().getAsString();
272 dumpTypeAsChild(T->getElementType());
274 void VisitConstantArrayType(const ConstantArrayType *T) {
275 OS << " " << T->getSize();
278 void VisitVariableArrayType(const VariableArrayType *T) {
280 dumpSourceRange(T->getBracketsRange());
282 dumpStmt(T->getSizeExpr());
284 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
287 dumpSourceRange(T->getBracketsRange());
288 dumpStmt(T->getSizeExpr());
290 void VisitDependentSizedExtVectorType(
291 const DependentSizedExtVectorType *T) {
293 dumpLocation(T->getAttributeLoc());
294 dumpTypeAsChild(T->getElementType());
295 dumpStmt(T->getSizeExpr());
297 void VisitVectorType(const VectorType *T) {
298 switch (T->getVectorKind()) {
299 case VectorType::GenericVector: break;
300 case VectorType::AltiVecVector: OS << " altivec"; break;
301 case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
302 case VectorType::AltiVecBool: OS << " altivec bool"; break;
303 case VectorType::NeonVector: OS << " neon"; break;
304 case VectorType::NeonPolyVector: OS << " neon poly"; break;
306 OS << " " << T->getNumElements();
307 dumpTypeAsChild(T->getElementType());
309 void VisitFunctionType(const FunctionType *T) {
310 auto EI = T->getExtInfo();
311 if (EI.getNoReturn()) OS << " noreturn";
312 if (EI.getProducesResult()) OS << " produces_result";
313 if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
314 OS << " " << FunctionType::getNameForCallConv(EI.getCC());
315 dumpTypeAsChild(T->getReturnType());
317 void VisitFunctionProtoType(const FunctionProtoType *T) {
318 auto EPI = T->getExtProtoInfo();
319 if (EPI.HasTrailingReturn) OS << " trailing_return";
320 if (T->isConst()) OS << " const";
321 if (T->isVolatile()) OS << " volatile";
322 if (T->isRestrict()) OS << " restrict";
323 switch (EPI.RefQualifier) {
325 case RQ_LValue: OS << " &"; break;
326 case RQ_RValue: OS << " &&"; break;
328 // FIXME: Exception specification.
329 // FIXME: Consumed parameters.
330 VisitFunctionType(T);
331 for (QualType PT : T->getParamTypes())
334 dumpChild([=] { OS << "..."; });
336 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
337 dumpDeclRef(T->getDecl());
339 void VisitTypedefType(const TypedefType *T) {
340 dumpDeclRef(T->getDecl());
342 void VisitTypeOfExprType(const TypeOfExprType *T) {
343 dumpStmt(T->getUnderlyingExpr());
345 void VisitDecltypeType(const DecltypeType *T) {
346 dumpStmt(T->getUnderlyingExpr());
348 void VisitUnaryTransformType(const UnaryTransformType *T) {
349 switch (T->getUTTKind()) {
350 case UnaryTransformType::EnumUnderlyingType:
351 OS << " underlying_type";
354 dumpTypeAsChild(T->getBaseType());
356 void VisitTagType(const TagType *T) {
357 dumpDeclRef(T->getDecl());
359 void VisitAttributedType(const AttributedType *T) {
361 dumpTypeAsChild(T->getModifiedType());
363 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
364 OS << " depth " << T->getDepth() << " index " << T->getIndex();
365 if (T->isParameterPack()) OS << " pack";
366 dumpDeclRef(T->getDecl());
368 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
369 dumpTypeAsChild(T->getReplacedParameter());
371 void VisitSubstTemplateTypeParmPackType(
372 const SubstTemplateTypeParmPackType *T) {
373 dumpTypeAsChild(T->getReplacedParameter());
374 dumpTemplateArgument(T->getArgumentPack());
376 void VisitAutoType(const AutoType *T) {
377 if (T->isDecltypeAuto()) OS << " decltype(auto)";
381 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
382 if (T->isTypeAlias()) OS << " alias";
383 OS << " "; T->getTemplateName().dump(OS);
385 dumpTemplateArgument(Arg);
386 if (T->isTypeAlias())
387 dumpTypeAsChild(T->getAliasedType());
389 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
390 dumpDeclRef(T->getDecl());
392 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
393 dumpDeclRef(T->getDecl());
395 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
396 dumpTypeAsChild(T->getPointeeType());
398 void VisitAtomicType(const AtomicType *T) {
399 dumpTypeAsChild(T->getValueType());
401 void VisitAdjustedType(const AdjustedType *T) {
402 dumpTypeAsChild(T->getOriginalType());
404 void VisitPackExpansionType(const PackExpansionType *T) {
405 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
407 dumpTypeAsChild(T->getPattern());
409 // FIXME: ElaboratedType, DependentNameType,
410 // DependentTemplateSpecializationType, ObjCObjectType
413 void VisitLabelDecl(const LabelDecl *D);
414 void VisitTypedefDecl(const TypedefDecl *D);
415 void VisitEnumDecl(const EnumDecl *D);
416 void VisitRecordDecl(const RecordDecl *D);
417 void VisitEnumConstantDecl(const EnumConstantDecl *D);
418 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
419 void VisitFunctionDecl(const FunctionDecl *D);
420 void VisitFieldDecl(const FieldDecl *D);
421 void VisitVarDecl(const VarDecl *D);
422 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
423 void VisitImportDecl(const ImportDecl *D);
426 void VisitNamespaceDecl(const NamespaceDecl *D);
427 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
428 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
429 void VisitTypeAliasDecl(const TypeAliasDecl *D);
430 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
431 void VisitCXXRecordDecl(const CXXRecordDecl *D);
432 void VisitStaticAssertDecl(const StaticAssertDecl *D);
433 template<typename SpecializationDecl>
434 void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
435 bool DumpExplicitInst,
437 template<typename TemplateDecl>
438 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
439 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
440 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
441 void VisitClassTemplateSpecializationDecl(
442 const ClassTemplateSpecializationDecl *D);
443 void VisitClassTemplatePartialSpecializationDecl(
444 const ClassTemplatePartialSpecializationDecl *D);
445 void VisitClassScopeFunctionSpecializationDecl(
446 const ClassScopeFunctionSpecializationDecl *D);
447 void VisitVarTemplateDecl(const VarTemplateDecl *D);
448 void VisitVarTemplateSpecializationDecl(
449 const VarTemplateSpecializationDecl *D);
450 void VisitVarTemplatePartialSpecializationDecl(
451 const VarTemplatePartialSpecializationDecl *D);
452 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
453 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
454 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
455 void VisitUsingDecl(const UsingDecl *D);
456 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
457 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
458 void VisitUsingShadowDecl(const UsingShadowDecl *D);
459 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
460 void VisitAccessSpecDecl(const AccessSpecDecl *D);
461 void VisitFriendDecl(const FriendDecl *D);
464 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
465 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
466 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
467 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
468 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
469 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
470 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
471 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
472 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
473 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
474 void VisitBlockDecl(const BlockDecl *D);
477 void VisitStmt(const Stmt *Node);
478 void VisitDeclStmt(const DeclStmt *Node);
479 void VisitAttributedStmt(const AttributedStmt *Node);
480 void VisitLabelStmt(const LabelStmt *Node);
481 void VisitGotoStmt(const GotoStmt *Node);
482 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
485 void VisitExpr(const Expr *Node);
486 void VisitCastExpr(const CastExpr *Node);
487 void VisitDeclRefExpr(const DeclRefExpr *Node);
488 void VisitPredefinedExpr(const PredefinedExpr *Node);
489 void VisitCharacterLiteral(const CharacterLiteral *Node);
490 void VisitIntegerLiteral(const IntegerLiteral *Node);
491 void VisitFloatingLiteral(const FloatingLiteral *Node);
492 void VisitStringLiteral(const StringLiteral *Str);
493 void VisitInitListExpr(const InitListExpr *ILE);
494 void VisitUnaryOperator(const UnaryOperator *Node);
495 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
496 void VisitMemberExpr(const MemberExpr *Node);
497 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
498 void VisitBinaryOperator(const BinaryOperator *Node);
499 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
500 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
501 void VisitBlockExpr(const BlockExpr *Node);
502 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
505 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
506 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
507 void VisitCXXThisExpr(const CXXThisExpr *Node);
508 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
509 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
510 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
511 void VisitCXXNewExpr(const CXXNewExpr *Node);
512 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
513 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
514 void VisitExprWithCleanups(const ExprWithCleanups *Node);
515 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
516 void dumpCXXTemporary(const CXXTemporary *Temporary);
517 void VisitLambdaExpr(const LambdaExpr *Node) {
519 dumpDecl(Node->getLambdaClass());
521 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
524 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
525 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
526 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
527 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
528 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
529 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
530 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
531 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
532 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
533 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
536 const char *getCommandName(unsigned CommandID);
537 void dumpComment(const Comment *C);
540 void visitTextComment(const TextComment *C);
541 void visitInlineCommandComment(const InlineCommandComment *C);
542 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
543 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
546 void visitBlockCommandComment(const BlockCommandComment *C);
547 void visitParamCommandComment(const ParamCommandComment *C);
548 void visitTParamCommandComment(const TParamCommandComment *C);
549 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
550 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
551 void visitVerbatimLineComment(const VerbatimLineComment *C);
555 //===----------------------------------------------------------------------===//
557 //===----------------------------------------------------------------------===//
559 void ASTDumper::dumpPointer(const void *Ptr) {
560 ColorScope Color(*this, AddressColor);
564 void ASTDumper::dumpLocation(SourceLocation Loc) {
568 ColorScope Color(*this, LocationColor);
569 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
571 // The general format we print out is filename:line:col, but we drop pieces
572 // that haven't changed since the last loc printed.
573 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
575 if (PLoc.isInvalid()) {
576 OS << "<invalid sloc>";
580 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
581 OS << PLoc.getFilename() << ':' << PLoc.getLine()
582 << ':' << PLoc.getColumn();
583 LastLocFilename = PLoc.getFilename();
584 LastLocLine = PLoc.getLine();
585 } else if (PLoc.getLine() != LastLocLine) {
586 OS << "line" << ':' << PLoc.getLine()
587 << ':' << PLoc.getColumn();
588 LastLocLine = PLoc.getLine();
590 OS << "col" << ':' << PLoc.getColumn();
594 void ASTDumper::dumpSourceRange(SourceRange R) {
595 // Can't translate locations if a SourceManager isn't available.
600 dumpLocation(R.getBegin());
601 if (R.getBegin() != R.getEnd()) {
603 dumpLocation(R.getEnd());
607 // <t2.c:123:421[blah], t2.c:412:321>
611 void ASTDumper::dumpBareType(QualType T, bool Desugar) {
612 ColorScope Color(*this, TypeColor);
614 SplitQualType T_split = T.split();
615 OS << "'" << QualType::getAsString(T_split) << "'";
617 if (Desugar && !T.isNull()) {
618 // If the type is sugared, also dump a (shallow) desugared type.
619 SplitQualType D_split = T.getSplitDesugaredType();
620 if (T_split != D_split)
621 OS << ":'" << QualType::getAsString(D_split) << "'";
625 void ASTDumper::dumpType(QualType T) {
630 void ASTDumper::dumpTypeAsChild(QualType T) {
631 SplitQualType SQT = T.split();
632 if (!SQT.Quals.hasQualifiers())
633 return dumpTypeAsChild(SQT.Ty);
637 dumpPointer(T.getAsOpaquePtr());
639 dumpBareType(T, false);
640 OS << " " << T.split().Quals.getAsString();
641 dumpTypeAsChild(T.split().Ty);
645 void ASTDumper::dumpTypeAsChild(const Type *T) {
648 ColorScope Color(*this, NullColor);
654 ColorScope Color(*this, TypeColor);
655 OS << T->getTypeClassName() << "Type";
659 dumpBareType(QualType(T, 0), false);
661 QualType SingleStepDesugar =
662 T->getLocallyUnqualifiedSingleStepDesugaredType();
663 if (SingleStepDesugar != QualType(T, 0))
665 if (T->isDependentType())
667 else if (T->isInstantiationDependentType())
668 OS << " instantiation_dependent";
669 if (T->isVariablyModifiedType())
670 OS << " variably_modified";
671 if (T->containsUnexpandedParameterPack())
672 OS << " contains_unexpanded_pack";
676 TypeVisitor<ASTDumper>::Visit(T);
678 if (SingleStepDesugar != QualType(T, 0))
679 dumpTypeAsChild(SingleStepDesugar);
683 void ASTDumper::dumpBareDeclRef(const Decl *D) {
685 ColorScope Color(*this, DeclKindNameColor);
686 OS << D->getDeclKindName();
690 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
691 ColorScope Color(*this, DeclNameColor);
692 OS << " '" << ND->getDeclName() << '\'';
695 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
696 dumpType(VD->getType());
699 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
710 void ASTDumper::dumpName(const NamedDecl *ND) {
711 if (ND->getDeclName()) {
712 ColorScope Color(*this, DeclNameColor);
713 OS << ' ' << ND->getNameAsString();
717 bool ASTDumper::hasNodes(const DeclContext *DC) {
721 return DC->hasExternalLexicalStorage() ||
722 DC->noload_decls_begin() != DC->noload_decls_end();
725 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
729 for (auto *D : DC->noload_decls())
732 if (DC->hasExternalLexicalStorage()) {
734 ColorScope Color(*this, UndeserializedColor);
735 OS << "<undeserialized declarations>";
740 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
742 OS << "StoredDeclsMap ";
743 dumpBareDeclRef(cast<Decl>(DC));
745 const DeclContext *Primary = DC->getPrimaryContext();
748 dumpPointer(cast<Decl>(Primary));
751 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
753 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
754 E = Primary->noload_lookups_end();
756 DeclarationName Name = I.getLookupName();
757 DeclContextLookupResult R = *I++;
760 OS << "DeclarationName ";
762 ColorScope Color(*this, DeclNameColor);
763 OS << '\'' << Name << '\'';
766 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
769 dumpBareDeclRef(*RI);
771 if ((*RI)->isHidden())
774 // If requested, dump the redecl chain for this lookup.
776 // Dump earliest decl first.
777 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
778 if (Decl *Prev = D->getPreviousDecl())
789 if (HasUndeserializedLookups) {
791 ColorScope Color(*this, UndeserializedColor);
792 OS << "<undeserialized lookups>";
798 void ASTDumper::dumpAttr(const Attr *A) {
801 ColorScope Color(*this, AttrColor);
803 switch (A->getKind()) {
804 #define ATTR(X) case attr::X: OS << #X; break;
805 #include "clang/Basic/AttrList.inc"
807 llvm_unreachable("unexpected attribute kind");
812 dumpSourceRange(A->getRange());
813 if (A->isInherited())
817 #include "clang/AST/AttrDump.inc"
821 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
824 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
825 const T *First = D->getFirstDecl();
827 OS << " first " << First;
831 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
832 const T *Prev = D->getPreviousDecl();
834 OS << " prev " << Prev;
837 /// Dump the previous declaration in the redeclaration chain for a declaration,
839 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
840 switch (D->getKind()) {
841 #define DECL(DERIVED, BASE) \
842 case Decl::DERIVED: \
843 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
844 #define ABSTRACT_DECL(DECL)
845 #include "clang/AST/DeclNodes.inc"
847 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
850 //===----------------------------------------------------------------------===//
852 //===----------------------------------------------------------------------===//
854 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
870 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
872 OS << "CXXCtorInitializer";
873 if (Init->isAnyMemberInitializer()) {
875 dumpBareDeclRef(Init->getAnyMember());
876 } else if (Init->isBaseInitializer()) {
877 dumpType(QualType(Init->getBaseClass(), 0));
878 } else if (Init->isDelegatingInitializer()) {
879 dumpType(Init->getTypeSourceInfo()->getType());
881 llvm_unreachable("Unknown initializer type");
883 dumpStmt(Init->getInit());
887 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
891 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
896 void ASTDumper::dumpTemplateArgumentListInfo(
897 const TemplateArgumentListInfo &TALI) {
898 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
899 dumpTemplateArgumentLoc(TALI[i]);
902 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
903 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
906 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
907 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
908 dumpTemplateArgument(TAL[i]);
911 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
913 OS << "TemplateArgument";
917 switch (A.getKind()) {
918 case TemplateArgument::Null:
921 case TemplateArgument::Type:
923 dumpType(A.getAsType());
925 case TemplateArgument::Declaration:
927 dumpDeclRef(A.getAsDecl());
929 case TemplateArgument::NullPtr:
932 case TemplateArgument::Integral:
933 OS << " integral " << A.getAsIntegral();
935 case TemplateArgument::Template:
937 A.getAsTemplate().dump(OS);
939 case TemplateArgument::TemplateExpansion:
940 OS << " template expansion";
941 A.getAsTemplateOrTemplatePattern().dump(OS);
943 case TemplateArgument::Expression:
945 dumpStmt(A.getAsExpr());
947 case TemplateArgument::Pack:
949 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
951 dumpTemplateArgument(*I);
957 //===----------------------------------------------------------------------===//
958 // Decl dumping methods.
959 //===----------------------------------------------------------------------===//
961 void ASTDumper::dumpDecl(const Decl *D) {
964 ColorScope Color(*this, NullColor);
970 ColorScope Color(*this, DeclKindNameColor);
971 OS << D->getDeclKindName() << "Decl";
974 if (D->getLexicalDeclContext() != D->getDeclContext())
975 OS << " parent " << cast<Decl>(D->getDeclContext());
976 dumpPreviousDecl(OS, D);
977 dumpSourceRange(D->getSourceRange());
979 dumpLocation(D->getLocation());
980 if (Module *M = D->getImportedOwningModule())
981 OS << " in " << M->getFullModuleName();
982 else if (Module *M = D->getLocalOwningModule())
983 OS << " in (local) " << M->getFullModuleName();
984 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
991 else if (D->isThisDeclarationReferenced())
993 if (D->isInvalidDecl())
995 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
996 if (FD->isConstexpr())
1000 ConstDeclVisitor<ASTDumper>::Visit(D);
1002 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1006 if (const FullComment *Comment =
1007 D->getASTContext().getLocalCommentForDeclUncached(D))
1008 dumpFullComment(Comment);
1010 // Decls within functions are visited by the body.
1011 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1012 hasNodes(dyn_cast<DeclContext>(D)))
1013 dumpDeclContext(cast<DeclContext>(D));
1017 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
1021 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
1023 dumpType(D->getUnderlyingType());
1024 if (D->isModulePrivate())
1025 OS << " __module_private__";
1028 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
1029 if (D->isScoped()) {
1030 if (D->isScopedUsingClassTag())
1036 if (D->isModulePrivate())
1037 OS << " __module_private__";
1039 dumpType(D->getIntegerType());
1042 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
1043 OS << ' ' << D->getKindName();
1045 if (D->isModulePrivate())
1046 OS << " __module_private__";
1047 if (D->isCompleteDefinition())
1048 OS << " definition";
1051 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1053 dumpType(D->getType());
1054 if (const Expr *Init = D->getInitExpr())
1058 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1060 dumpType(D->getType());
1062 for (auto *Child : D->chain())
1066 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
1068 dumpType(D->getType());
1070 StorageClass SC = D->getStorageClass();
1072 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1073 if (D->isInlineSpecified())
1075 if (D->isVirtualAsWritten())
1077 if (D->isModulePrivate())
1078 OS << " __module_private__";
1082 else if (D->isDeletedAsWritten())
1085 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1086 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1087 switch (EPI.ExceptionSpec.Type) {
1089 case EST_Unevaluated:
1090 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1092 case EST_Uninstantiated:
1093 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1098 if (const FunctionTemplateSpecializationInfo *FTSI =
1099 D->getTemplateSpecializationInfo())
1100 dumpTemplateArgumentList(*FTSI->TemplateArguments);
1102 for (ArrayRef<NamedDecl *>::iterator
1103 I = D->getDeclsInPrototypeScope().begin(),
1104 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
1107 if (!D->param_begin() && D->getNumParams())
1108 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1110 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1115 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
1116 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
1119 dumpCXXCtorInitializer(*I);
1121 if (D->doesThisDeclarationHaveABody())
1122 dumpStmt(D->getBody());
1125 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
1127 dumpType(D->getType());
1130 if (D->isModulePrivate())
1131 OS << " __module_private__";
1133 if (D->isBitField())
1134 dumpStmt(D->getBitWidth());
1135 if (Expr *Init = D->getInClassInitializer())
1139 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1141 dumpType(D->getType());
1142 StorageClass SC = D->getStorageClass();
1144 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1145 switch (D->getTLSKind()) {
1146 case VarDecl::TLS_None: break;
1147 case VarDecl::TLS_Static: OS << " tls"; break;
1148 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1150 if (D->isModulePrivate())
1151 OS << " __module_private__";
1152 if (D->isNRVOVariable())
1155 switch (D->getInitStyle()) {
1156 case VarDecl::CInit: OS << " cinit"; break;
1157 case VarDecl::CallInit: OS << " callinit"; break;
1158 case VarDecl::ListInit: OS << " listinit"; break;
1160 dumpStmt(D->getInit());
1164 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1165 dumpStmt(D->getAsmString());
1168 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1169 OS << ' ' << D->getImportedModule()->getFullModuleName();
1172 //===----------------------------------------------------------------------===//
1174 //===----------------------------------------------------------------------===//
1176 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1180 if (!D->isOriginalNamespace())
1181 dumpDeclRef(D->getOriginalNamespace(), "original");
1184 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1186 dumpBareDeclRef(D->getNominatedNamespace());
1189 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1191 dumpDeclRef(D->getAliasedNamespace());
1194 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1196 dumpType(D->getUnderlyingType());
1199 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1201 dumpTemplateParameters(D->getTemplateParameters());
1202 dumpDecl(D->getTemplatedDecl());
1205 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1207 if (!D->isCompleteDefinition())
1210 for (const auto &I : D->bases()) {
1214 dumpAccessSpecifier(I.getAccessSpecifier());
1215 dumpType(I.getType());
1216 if (I.isPackExpansion())
1222 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1223 dumpStmt(D->getAssertExpr());
1224 dumpStmt(D->getMessage());
1227 template<typename SpecializationDecl>
1228 void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
1229 bool DumpExplicitInst,
1231 bool DumpedAny = false;
1232 for (auto *RedeclWithBadType : D->redecls()) {
1233 // FIXME: The redecls() range sometimes has elements of a less-specific
1234 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1235 // us TagDecls, and should give CXXRecordDecls).
1236 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1238 // Found the injected-class-name for a class template. This will be dumped
1239 // as part of its surrounding class so we don't need to dump it here.
1240 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1241 "expected an injected-class-name");
1245 switch (Redecl->getTemplateSpecializationKind()) {
1246 case TSK_ExplicitInstantiationDeclaration:
1247 case TSK_ExplicitInstantiationDefinition:
1248 if (!DumpExplicitInst)
1251 case TSK_Undeclared:
1252 case TSK_ImplicitInstantiation:
1254 dumpDeclRef(Redecl);
1259 case TSK_ExplicitSpecialization:
1264 // Ensure we dump at least one decl for each specialization.
1269 template<typename TemplateDecl>
1270 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1271 bool DumpExplicitInst) {
1273 dumpTemplateParameters(D->getTemplateParameters());
1275 dumpDecl(D->getTemplatedDecl());
1277 for (auto *Child : D->specializations())
1278 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
1279 !D->isCanonicalDecl());
1282 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1283 // FIXME: We don't add a declaration of a function template specialization
1284 // to its context when it's explicitly instantiated, so dump explicit
1285 // instantiations when we dump the template itself.
1286 VisitTemplateDecl(D, true);
1289 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1290 VisitTemplateDecl(D, false);
1293 void ASTDumper::VisitClassTemplateSpecializationDecl(
1294 const ClassTemplateSpecializationDecl *D) {
1295 VisitCXXRecordDecl(D);
1296 dumpTemplateArgumentList(D->getTemplateArgs());
1299 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1300 const ClassTemplatePartialSpecializationDecl *D) {
1301 VisitClassTemplateSpecializationDecl(D);
1302 dumpTemplateParameters(D->getTemplateParameters());
1305 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1306 const ClassScopeFunctionSpecializationDecl *D) {
1307 dumpDeclRef(D->getSpecialization());
1308 if (D->hasExplicitTemplateArgs())
1309 dumpTemplateArgumentListInfo(D->templateArgs());
1312 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1313 VisitTemplateDecl(D, false);
1316 void ASTDumper::VisitVarTemplateSpecializationDecl(
1317 const VarTemplateSpecializationDecl *D) {
1318 dumpTemplateArgumentList(D->getTemplateArgs());
1322 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1323 const VarTemplatePartialSpecializationDecl *D) {
1324 dumpTemplateParameters(D->getTemplateParameters());
1325 VisitVarTemplateSpecializationDecl(D);
1328 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1329 if (D->wasDeclaredWithTypename())
1333 if (D->isParameterPack())
1336 if (D->hasDefaultArgument())
1337 dumpTemplateArgument(D->getDefaultArgument());
1340 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1341 dumpType(D->getType());
1342 if (D->isParameterPack())
1345 if (D->hasDefaultArgument())
1346 dumpTemplateArgument(D->getDefaultArgument());
1349 void ASTDumper::VisitTemplateTemplateParmDecl(
1350 const TemplateTemplateParmDecl *D) {
1351 if (D->isParameterPack())
1354 dumpTemplateParameters(D->getTemplateParameters());
1355 if (D->hasDefaultArgument())
1356 dumpTemplateArgumentLoc(D->getDefaultArgument());
1359 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1361 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1362 OS << D->getNameAsString();
1365 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1366 const UnresolvedUsingTypenameDecl *D) {
1368 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1369 OS << D->getNameAsString();
1372 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1374 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1375 OS << D->getNameAsString();
1376 dumpType(D->getType());
1379 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1381 dumpBareDeclRef(D->getTargetDecl());
1384 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1385 switch (D->getLanguage()) {
1386 case LinkageSpecDecl::lang_c: OS << " C"; break;
1387 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1391 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1393 dumpAccessSpecifier(D->getAccess());
1396 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1397 if (TypeSourceInfo *T = D->getFriendType())
1398 dumpType(T->getType());
1400 dumpDecl(D->getFriendDecl());
1403 //===----------------------------------------------------------------------===//
1404 // Obj-C Declarations
1405 //===----------------------------------------------------------------------===//
1407 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1409 dumpType(D->getType());
1410 if (D->getSynthesize())
1411 OS << " synthesize";
1413 switch (D->getAccessControl()) {
1414 case ObjCIvarDecl::None:
1417 case ObjCIvarDecl::Private:
1420 case ObjCIvarDecl::Protected:
1423 case ObjCIvarDecl::Public:
1426 case ObjCIvarDecl::Package:
1432 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1433 if (D->isInstanceMethod())
1438 dumpType(D->getReturnType());
1440 if (D->isThisDeclarationADefinition()) {
1443 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1449 if (D->isVariadic())
1450 dumpChild([=] { OS << "..."; });
1453 dumpStmt(D->getBody());
1456 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1458 dumpDeclRef(D->getClassInterface());
1459 dumpDeclRef(D->getImplementation());
1460 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1461 E = D->protocol_end();
1466 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1468 dumpDeclRef(D->getClassInterface());
1469 dumpDeclRef(D->getCategoryDecl());
1472 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1475 for (auto *Child : D->protocols())
1479 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1481 dumpDeclRef(D->getSuperClass(), "super");
1483 dumpDeclRef(D->getImplementation());
1484 for (auto *Child : D->protocols())
1488 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1490 dumpDeclRef(D->getSuperClass(), "super");
1491 dumpDeclRef(D->getClassInterface());
1492 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1495 dumpCXXCtorInitializer(*I);
1498 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1500 dumpDeclRef(D->getClassInterface());
1503 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1505 dumpType(D->getType());
1507 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1509 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1512 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1513 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1514 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1516 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1518 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1520 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1522 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1524 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1526 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1528 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1530 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1532 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1533 OS << " unsafe_unretained";
1534 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
1535 dumpDeclRef(D->getGetterMethodDecl(), "getter");
1536 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
1537 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1541 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1542 dumpName(D->getPropertyDecl());
1543 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1544 OS << " synthesize";
1547 dumpDeclRef(D->getPropertyDecl());
1548 dumpDeclRef(D->getPropertyIvarDecl());
1551 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1552 for (auto I : D->params())
1555 if (D->isVariadic())
1556 dumpChild([=]{ OS << "..."; });
1558 if (D->capturesCXXThis())
1559 dumpChild([=]{ OS << "capture this"; });
1561 for (const auto &I : D->captures()) {
1568 if (I.getVariable()) {
1570 dumpBareDeclRef(I.getVariable());
1572 if (I.hasCopyExpr())
1573 dumpStmt(I.getCopyExpr());
1576 dumpStmt(D->getBody());
1579 //===----------------------------------------------------------------------===//
1580 // Stmt dumping methods.
1581 //===----------------------------------------------------------------------===//
1583 void ASTDumper::dumpStmt(const Stmt *S) {
1586 ColorScope Color(*this, NullColor);
1591 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1596 ConstStmtVisitor<ASTDumper>::Visit(S);
1598 for (Stmt::const_child_range CI = S->children(); CI; ++CI)
1603 void ASTDumper::VisitStmt(const Stmt *Node) {
1605 ColorScope Color(*this, StmtColor);
1606 OS << Node->getStmtClassName();
1609 dumpSourceRange(Node->getSourceRange());
1612 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1614 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1615 E = Node->decl_end();
1620 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1622 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1623 E = Node->getAttrs().end();
1628 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1630 OS << " '" << Node->getName() << "'";
1633 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1635 OS << " '" << Node->getLabel()->getName() << "'";
1636 dumpPointer(Node->getLabel());
1639 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1641 dumpDecl(Node->getExceptionDecl());
1644 //===----------------------------------------------------------------------===//
1645 // Expr dumping methods.
1646 //===----------------------------------------------------------------------===//
1648 void ASTDumper::VisitExpr(const Expr *Node) {
1650 dumpType(Node->getType());
1653 ColorScope Color(*this, ValueKindColor);
1654 switch (Node->getValueKind()) {
1667 ColorScope Color(*this, ObjectKindColor);
1668 switch (Node->getObjectKind()) {
1674 case OK_ObjCProperty:
1675 OS << " objcproperty";
1677 case OK_ObjCSubscript:
1678 OS << " objcsubscript";
1680 case OK_VectorComponent:
1681 OS << " vectorcomponent";
1687 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
1688 if (Node->path_empty())
1693 for (CastExpr::path_const_iterator I = Node->path_begin(),
1694 E = Node->path_end();
1696 const CXXBaseSpecifier *Base = *I;
1700 const CXXRecordDecl *RD =
1701 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1703 if (Base->isVirtual())
1705 OS << RD->getName();
1712 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
1716 ColorScope Color(*this, CastColor);
1717 OS << Node->getCastKindName();
1719 dumpBasePath(OS, Node);
1723 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1727 dumpBareDeclRef(Node->getDecl());
1728 if (Node->getDecl() != Node->getFoundDecl()) {
1730 dumpBareDeclRef(Node->getFoundDecl());
1735 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
1738 if (!Node->requiresADL())
1740 OS << "ADL) = '" << Node->getName() << '\'';
1742 UnresolvedLookupExpr::decls_iterator
1743 I = Node->decls_begin(), E = Node->decls_end();
1750 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1754 ColorScope Color(*this, DeclKindNameColor);
1755 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1757 OS << "='" << *Node->getDecl() << "'";
1758 dumpPointer(Node->getDecl());
1759 if (Node->isFreeIvar())
1760 OS << " isFreeIvar";
1763 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1765 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
1768 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1770 ColorScope Color(*this, ValueColor);
1771 OS << " " << Node->getValue();
1774 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1777 bool isSigned = Node->getType()->isSignedIntegerType();
1778 ColorScope Color(*this, ValueColor);
1779 OS << " " << Node->getValue().toString(10, isSigned);
1782 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1784 ColorScope Color(*this, ValueColor);
1785 OS << " " << Node->getValueAsApproximateDouble();
1788 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
1790 ColorScope Color(*this, ValueColor);
1792 Str->outputString(OS);
1795 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1797 if (auto *Filler = ILE->getArrayFiller()) {
1799 OS << "array filler";
1803 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1805 dumpBareDeclRef(Field);
1809 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
1811 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1812 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1815 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1816 const UnaryExprOrTypeTraitExpr *Node) {
1818 switch(Node->getKind()) {
1829 if (Node->isArgumentType())
1830 dumpType(Node->getArgumentType());
1833 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
1835 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1836 dumpPointer(Node->getMemberDecl());
1839 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
1841 OS << " " << Node->getAccessor().getNameStart();
1844 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
1846 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1849 void ASTDumper::VisitCompoundAssignOperator(
1850 const CompoundAssignOperator *Node) {
1852 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1853 << "' ComputeLHSTy=";
1854 dumpBareType(Node->getComputationLHSType());
1855 OS << " ComputeResultTy=";
1856 dumpBareType(Node->getComputationResultType());
1859 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
1861 dumpDecl(Node->getBlockDecl());
1864 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
1867 if (Expr *Source = Node->getSourceExpr())
1873 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
1875 OS << " " << Node->getLabel()->getName();
1876 dumpPointer(Node->getLabel());
1879 //===----------------------------------------------------------------------===//
1881 //===----------------------------------------------------------------------===//
1883 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
1885 OS << " " << Node->getCastName()
1886 << "<" << Node->getTypeAsWritten().getAsString() << ">"
1887 << " <" << Node->getCastKindName();
1888 dumpBasePath(OS, Node);
1892 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
1894 OS << " " << (Node->getValue() ? "true" : "false");
1897 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
1902 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
1904 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1905 << " <" << Node->getCastKindName() << ">";
1908 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
1910 CXXConstructorDecl *Ctor = Node->getConstructor();
1911 dumpType(Ctor->getType());
1912 if (Node->isElidable())
1914 if (Node->requiresZeroInitialization())
1918 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
1921 dumpCXXTemporary(Node->getTemporary());
1924 void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
1926 if (Node->isGlobalNew())
1928 if (Node->isArray())
1930 if (Node->getOperatorNew()) {
1932 dumpBareDeclRef(Node->getOperatorNew());
1934 // We could dump the deallocation function used in case of error, but it's
1935 // usually not that interesting.
1938 void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
1940 if (Node->isGlobalDelete())
1942 if (Node->isArrayForm())
1944 if (Node->getOperatorDelete()) {
1946 dumpBareDeclRef(Node->getOperatorDelete());
1951 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1953 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1954 OS << " extended by ";
1955 dumpBareDeclRef(VD);
1959 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
1961 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1962 dumpDeclRef(Node->getObject(i), "cleanup");
1965 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
1966 OS << "(CXXTemporary";
1967 dumpPointer(Temporary);
1971 void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
1973 dumpPointer(Node->getPack());
1974 dumpName(Node->getPack());
1978 //===----------------------------------------------------------------------===//
1979 // Obj-C Expressions
1980 //===----------------------------------------------------------------------===//
1982 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
1985 Node->getSelector().print(OS);
1986 switch (Node->getReceiverKind()) {
1987 case ObjCMessageExpr::Instance:
1990 case ObjCMessageExpr::Class:
1992 dumpBareType(Node->getClassReceiver());
1995 case ObjCMessageExpr::SuperInstance:
1996 OS << " super (instance)";
1999 case ObjCMessageExpr::SuperClass:
2000 OS << " super (class)";
2005 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
2008 Node->getBoxingMethod()->getSelector().print(OS);
2011 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
2013 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
2014 dumpDecl(CatchParam);
2019 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
2021 dumpType(Node->getEncodedType());
2024 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
2028 Node->getSelector().print(OS);
2031 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
2034 OS << ' ' << *Node->getProtocol();
2037 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
2039 if (Node->isImplicitProperty()) {
2040 OS << " Kind=MethodRef Getter=\"";
2041 if (Node->getImplicitPropertyGetter())
2042 Node->getImplicitPropertyGetter()->getSelector().print(OS);
2046 OS << "\" Setter=\"";
2047 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
2048 Setter->getSelector().print(OS);
2053 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
2056 if (Node->isSuperReceiver())
2059 OS << " Messaging=";
2060 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2061 OS << "Getter&Setter";
2062 else if (Node->isMessagingGetter())
2064 else if (Node->isMessagingSetter())
2068 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
2070 if (Node->isArraySubscriptRefExpr())
2071 OS << " Kind=ArraySubscript GetterForArray=\"";
2073 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2074 if (Node->getAtIndexMethodDecl())
2075 Node->getAtIndexMethodDecl()->getSelector().print(OS);
2079 if (Node->isArraySubscriptRefExpr())
2080 OS << "\" SetterForArray=\"";
2082 OS << "\" SetterForDictionary=\"";
2083 if (Node->setAtIndexMethodDecl())
2084 Node->setAtIndexMethodDecl()->getSelector().print(OS);
2089 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2091 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2094 //===----------------------------------------------------------------------===//
2096 //===----------------------------------------------------------------------===//
2098 const char *ASTDumper::getCommandName(unsigned CommandID) {
2100 return Traits->getCommandInfo(CommandID)->Name;
2101 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2104 return "<not a builtin command>";
2107 void ASTDumper::dumpFullComment(const FullComment *C) {
2116 void ASTDumper::dumpComment(const Comment *C) {
2119 ColorScope Color(*this, NullColor);
2125 ColorScope Color(*this, CommentColor);
2126 OS << C->getCommentKindName();
2129 dumpSourceRange(C->getSourceRange());
2130 ConstCommentVisitor<ASTDumper>::visit(C);
2131 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2137 void ASTDumper::visitTextComment(const TextComment *C) {
2138 OS << " Text=\"" << C->getText() << "\"";
2141 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2142 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2143 switch (C->getRenderKind()) {
2144 case InlineCommandComment::RenderNormal:
2145 OS << " RenderNormal";
2147 case InlineCommandComment::RenderBold:
2148 OS << " RenderBold";
2150 case InlineCommandComment::RenderMonospaced:
2151 OS << " RenderMonospaced";
2153 case InlineCommandComment::RenderEmphasized:
2154 OS << " RenderEmphasized";
2158 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2159 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2162 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2163 OS << " Name=\"" << C->getTagName() << "\"";
2164 if (C->getNumAttrs() != 0) {
2166 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2167 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2168 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2171 if (C->isSelfClosing())
2172 OS << " SelfClosing";
2175 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2176 OS << " Name=\"" << C->getTagName() << "\"";
2179 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2180 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2181 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2182 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2185 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2186 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2188 if (C->isDirectionExplicit())
2189 OS << " explicitly";
2191 OS << " implicitly";
2193 if (C->hasParamName()) {
2194 if (C->isParamIndexValid())
2195 OS << " Param=\"" << C->getParamName(FC) << "\"";
2197 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2200 if (C->isParamIndexValid() && !C->isVarArgParam())
2201 OS << " ParamIndex=" << C->getParamIndex();
2204 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2205 if (C->hasParamName()) {
2206 if (C->isPositionValid())
2207 OS << " Param=\"" << C->getParamName(FC) << "\"";
2209 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2212 if (C->isPositionValid()) {
2213 OS << " Position=<";
2214 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2215 OS << C->getIndex(i);
2223 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2224 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2225 " CloseName=\"" << C->getCloseName() << "\"";
2228 void ASTDumper::visitVerbatimBlockLineComment(
2229 const VerbatimBlockLineComment *C) {
2230 OS << " Text=\"" << C->getText() << "\"";
2233 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2234 OS << " Text=\"" << C->getText() << "\"";
2237 //===----------------------------------------------------------------------===//
2238 // Type method implementations
2239 //===----------------------------------------------------------------------===//
2241 void QualType::dump(const char *msg) const {
2243 llvm::errs() << msg << ": ";
2247 LLVM_DUMP_METHOD void QualType::dump() const {
2248 ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2249 Dumper.dumpTypeAsChild(*this);
2252 LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2254 //===----------------------------------------------------------------------===//
2255 // Decl method implementations
2256 //===----------------------------------------------------------------------===//
2258 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2260 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
2261 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2262 &getASTContext().getSourceManager());
2266 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2267 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2268 &getASTContext().getSourceManager(), /*ShowColors*/true);
2272 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2273 dumpLookups(llvm::errs());
2276 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2277 bool DumpDecls) const {
2278 const DeclContext *DC = this;
2279 while (!DC->isTranslationUnit())
2280 DC = DC->getParent();
2281 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2282 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
2283 P.dumpLookups(this, DumpDecls);
2286 //===----------------------------------------------------------------------===//
2287 // Stmt method implementations
2288 //===----------------------------------------------------------------------===//
2290 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2291 dump(llvm::errs(), SM);
2294 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2295 ASTDumper P(OS, nullptr, &SM);
2299 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2300 ASTDumper P(OS, nullptr, nullptr);
2304 LLVM_DUMP_METHOD void Stmt::dump() const {
2305 ASTDumper P(llvm::errs(), nullptr, nullptr);
2309 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2310 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2314 //===----------------------------------------------------------------------===//
2315 // Comment method implementations
2316 //===----------------------------------------------------------------------===//
2318 LLVM_DUMP_METHOD void Comment::dump() const {
2319 dump(llvm::errs(), nullptr, nullptr);
2322 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2323 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2324 &Context.getSourceManager());
2327 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2328 const SourceManager *SM) const {
2329 const FullComment *FC = dyn_cast<FullComment>(this);
2330 ASTDumper D(OS, Traits, SM);
2331 D.dumpFullComment(FC);
2334 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2335 const FullComment *FC = dyn_cast<FullComment>(this);
2336 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2337 D.dumpFullComment(FC);