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