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