1 //===--- TextNodeDumper.cpp - Printing of AST nodes -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements AST dumping of components of individual AST nodes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/TextNodeDumper.h" 14 #include "clang/AST/APValue.h" 15 #include "clang/AST/DeclFriend.h" 16 #include "clang/AST/DeclOpenMP.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/LocInfoType.h" 19 #include "clang/AST/Type.h" 20 #include "clang/Basic/Module.h" 21 #include "clang/Basic/SourceManager.h" 22 #include "clang/Basic/Specifiers.h" 23 #include "clang/Basic/TypeTraits.h" 24 #include "llvm/ADT/StringExtras.h" 25 26 #include <algorithm> 27 #include <utility> 28 29 using namespace clang; 30 31 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {} 32 33 template <typename T> 34 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) { 35 const T *First = D->getFirstDecl(); 36 if (First != D) 37 OS << " first " << First; 38 } 39 40 template <typename T> 41 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) { 42 const T *Prev = D->getPreviousDecl(); 43 if (Prev) 44 OS << " prev " << Prev; 45 } 46 47 /// Dump the previous declaration in the redeclaration chain for a declaration, 48 /// if any. 49 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) { 50 switch (D->getKind()) { 51 #define DECL(DERIVED, BASE) \ 52 case Decl::DERIVED: \ 53 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D)); 54 #define ABSTRACT_DECL(DECL) 55 #include "clang/AST/DeclNodes.inc" 56 } 57 llvm_unreachable("Decl that isn't part of DeclNodes.inc!"); 58 } 59 60 TextNodeDumper::TextNodeDumper(raw_ostream &OS, const ASTContext &Context, 61 bool ShowColors) 62 : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors), 63 Context(&Context), SM(&Context.getSourceManager()), 64 PrintPolicy(Context.getPrintingPolicy()), 65 Traits(&Context.getCommentCommandTraits()) {} 66 67 TextNodeDumper::TextNodeDumper(raw_ostream &OS, bool ShowColors) 68 : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors) {} 69 70 void TextNodeDumper::Visit(const comments::Comment *C, 71 const comments::FullComment *FC) { 72 if (!C) { 73 ColorScope Color(OS, ShowColors, NullColor); 74 OS << "<<<NULL>>>"; 75 return; 76 } 77 78 { 79 ColorScope Color(OS, ShowColors, CommentColor); 80 OS << C->getCommentKindName(); 81 } 82 dumpPointer(C); 83 dumpSourceRange(C->getSourceRange()); 84 85 ConstCommentVisitor<TextNodeDumper, void, 86 const comments::FullComment *>::visit(C, FC); 87 } 88 89 void TextNodeDumper::Visit(const Attr *A) { 90 { 91 ColorScope Color(OS, ShowColors, AttrColor); 92 93 switch (A->getKind()) { 94 #define ATTR(X) \ 95 case attr::X: \ 96 OS << #X; \ 97 break; 98 #include "clang/Basic/AttrList.inc" 99 } 100 OS << "Attr"; 101 } 102 dumpPointer(A); 103 dumpSourceRange(A->getRange()); 104 if (A->isInherited()) 105 OS << " Inherited"; 106 if (A->isImplicit()) 107 OS << " Implicit"; 108 109 ConstAttrVisitor<TextNodeDumper>::Visit(A); 110 } 111 112 void TextNodeDumper::Visit(const TemplateArgument &TA, SourceRange R, 113 const Decl *From, StringRef Label) { 114 OS << "TemplateArgument"; 115 if (R.isValid()) 116 dumpSourceRange(R); 117 118 if (From) 119 dumpDeclRef(From, Label); 120 121 ConstTemplateArgumentVisitor<TextNodeDumper>::Visit(TA); 122 } 123 124 void TextNodeDumper::Visit(const Stmt *Node) { 125 if (!Node) { 126 ColorScope Color(OS, ShowColors, NullColor); 127 OS << "<<<NULL>>>"; 128 return; 129 } 130 { 131 ColorScope Color(OS, ShowColors, StmtColor); 132 OS << Node->getStmtClassName(); 133 } 134 dumpPointer(Node); 135 dumpSourceRange(Node->getSourceRange()); 136 137 if (const auto *E = dyn_cast<Expr>(Node)) { 138 dumpType(E->getType()); 139 140 if (E->containsErrors()) { 141 ColorScope Color(OS, ShowColors, ErrorsColor); 142 OS << " contains-errors"; 143 } 144 145 { 146 ColorScope Color(OS, ShowColors, ValueKindColor); 147 switch (E->getValueKind()) { 148 case VK_PRValue: 149 break; 150 case VK_LValue: 151 OS << " lvalue"; 152 break; 153 case VK_XValue: 154 OS << " xvalue"; 155 break; 156 } 157 } 158 159 { 160 ColorScope Color(OS, ShowColors, ObjectKindColor); 161 switch (E->getObjectKind()) { 162 case OK_Ordinary: 163 break; 164 case OK_BitField: 165 OS << " bitfield"; 166 break; 167 case OK_ObjCProperty: 168 OS << " objcproperty"; 169 break; 170 case OK_ObjCSubscript: 171 OS << " objcsubscript"; 172 break; 173 case OK_VectorComponent: 174 OS << " vectorcomponent"; 175 break; 176 case OK_MatrixComponent: 177 OS << " matrixcomponent"; 178 break; 179 } 180 } 181 } 182 183 ConstStmtVisitor<TextNodeDumper>::Visit(Node); 184 } 185 186 void TextNodeDumper::Visit(const Type *T) { 187 if (!T) { 188 ColorScope Color(OS, ShowColors, NullColor); 189 OS << "<<<NULL>>>"; 190 return; 191 } 192 if (isa<LocInfoType>(T)) { 193 { 194 ColorScope Color(OS, ShowColors, TypeColor); 195 OS << "LocInfo Type"; 196 } 197 dumpPointer(T); 198 return; 199 } 200 201 { 202 ColorScope Color(OS, ShowColors, TypeColor); 203 OS << T->getTypeClassName() << "Type"; 204 } 205 dumpPointer(T); 206 OS << " "; 207 dumpBareType(QualType(T, 0), false); 208 209 QualType SingleStepDesugar = 210 T->getLocallyUnqualifiedSingleStepDesugaredType(); 211 if (SingleStepDesugar != QualType(T, 0)) 212 OS << " sugar"; 213 214 if (T->containsErrors()) { 215 ColorScope Color(OS, ShowColors, ErrorsColor); 216 OS << " contains-errors"; 217 } 218 219 if (T->isDependentType()) 220 OS << " dependent"; 221 else if (T->isInstantiationDependentType()) 222 OS << " instantiation_dependent"; 223 224 if (T->isVariablyModifiedType()) 225 OS << " variably_modified"; 226 if (T->containsUnexpandedParameterPack()) 227 OS << " contains_unexpanded_pack"; 228 if (T->isFromAST()) 229 OS << " imported"; 230 231 TypeVisitor<TextNodeDumper>::Visit(T); 232 } 233 234 void TextNodeDumper::Visit(QualType T) { 235 OS << "QualType"; 236 dumpPointer(T.getAsOpaquePtr()); 237 OS << " "; 238 dumpBareType(T, false); 239 OS << " " << T.split().Quals.getAsString(); 240 } 241 242 void TextNodeDumper::Visit(const Decl *D) { 243 if (!D) { 244 ColorScope Color(OS, ShowColors, NullColor); 245 OS << "<<<NULL>>>"; 246 return; 247 } 248 249 { 250 ColorScope Color(OS, ShowColors, DeclKindNameColor); 251 OS << D->getDeclKindName() << "Decl"; 252 } 253 dumpPointer(D); 254 if (D->getLexicalDeclContext() != D->getDeclContext()) 255 OS << " parent " << cast<Decl>(D->getDeclContext()); 256 dumpPreviousDecl(OS, D); 257 dumpSourceRange(D->getSourceRange()); 258 OS << ' '; 259 dumpLocation(D->getLocation()); 260 if (D->isFromASTFile()) 261 OS << " imported"; 262 if (Module *M = D->getOwningModule()) 263 OS << " in " << M->getFullModuleName(); 264 if (auto *ND = dyn_cast<NamedDecl>(D)) 265 for (Module *M : D->getASTContext().getModulesWithMergedDefinition( 266 const_cast<NamedDecl *>(ND))) 267 AddChild([=] { OS << "also in " << M->getFullModuleName(); }); 268 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 269 if (!ND->isUnconditionallyVisible()) 270 OS << " hidden"; 271 if (D->isImplicit()) 272 OS << " implicit"; 273 274 if (D->isUsed()) 275 OS << " used"; 276 else if (D->isThisDeclarationReferenced()) 277 OS << " referenced"; 278 279 if (D->isInvalidDecl()) 280 OS << " invalid"; 281 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 282 if (FD->isConstexprSpecified()) 283 OS << " constexpr"; 284 if (FD->isConsteval()) 285 OS << " consteval"; 286 else if (FD->isImmediateFunction()) 287 OS << " immediate"; 288 if (FD->isMultiVersion()) 289 OS << " multiversion"; 290 } 291 292 if (!isa<FunctionDecl>(*D)) { 293 const auto *MD = dyn_cast<ObjCMethodDecl>(D); 294 if (!MD || !MD->isThisDeclarationADefinition()) { 295 const auto *DC = dyn_cast<DeclContext>(D); 296 if (DC && DC->hasExternalLexicalStorage()) { 297 ColorScope Color(OS, ShowColors, UndeserializedColor); 298 OS << " <undeserialized declarations>"; 299 } 300 } 301 } 302 303 ConstDeclVisitor<TextNodeDumper>::Visit(D); 304 } 305 306 void TextNodeDumper::Visit(const CXXCtorInitializer *Init) { 307 OS << "CXXCtorInitializer"; 308 if (Init->isAnyMemberInitializer()) { 309 OS << ' '; 310 dumpBareDeclRef(Init->getAnyMember()); 311 } else if (Init->isBaseInitializer()) { 312 dumpType(QualType(Init->getBaseClass(), 0)); 313 } else if (Init->isDelegatingInitializer()) { 314 dumpType(Init->getTypeSourceInfo()->getType()); 315 } else { 316 llvm_unreachable("Unknown initializer type"); 317 } 318 } 319 320 void TextNodeDumper::Visit(const BlockDecl::Capture &C) { 321 OS << "capture"; 322 if (C.isByRef()) 323 OS << " byref"; 324 if (C.isNested()) 325 OS << " nested"; 326 if (C.getVariable()) { 327 OS << ' '; 328 dumpBareDeclRef(C.getVariable()); 329 } 330 } 331 332 void TextNodeDumper::Visit(const OMPClause *C) { 333 if (!C) { 334 ColorScope Color(OS, ShowColors, NullColor); 335 OS << "<<<NULL>>> OMPClause"; 336 return; 337 } 338 { 339 ColorScope Color(OS, ShowColors, AttrColor); 340 StringRef ClauseName(llvm::omp::getOpenMPClauseName(C->getClauseKind())); 341 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper() 342 << ClauseName.drop_front() << "Clause"; 343 } 344 dumpPointer(C); 345 dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc())); 346 if (C->isImplicit()) 347 OS << " <implicit>"; 348 } 349 350 void TextNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) { 351 const TypeSourceInfo *TSI = A.getTypeSourceInfo(); 352 if (TSI) { 353 OS << "case "; 354 dumpType(TSI->getType()); 355 } else { 356 OS << "default"; 357 } 358 359 if (A.isSelected()) 360 OS << " selected"; 361 } 362 363 void TextNodeDumper::Visit(const concepts::Requirement *R) { 364 if (!R) { 365 ColorScope Color(OS, ShowColors, NullColor); 366 OS << "<<<NULL>>> Requirement"; 367 return; 368 } 369 370 { 371 ColorScope Color(OS, ShowColors, StmtColor); 372 switch (R->getKind()) { 373 case concepts::Requirement::RK_Type: 374 OS << "TypeRequirement"; 375 break; 376 case concepts::Requirement::RK_Simple: 377 OS << "SimpleRequirement"; 378 break; 379 case concepts::Requirement::RK_Compound: 380 OS << "CompoundRequirement"; 381 break; 382 case concepts::Requirement::RK_Nested: 383 OS << "NestedRequirement"; 384 break; 385 } 386 } 387 388 dumpPointer(R); 389 390 if (auto *ER = dyn_cast<concepts::ExprRequirement>(R)) { 391 if (ER->hasNoexceptRequirement()) 392 OS << " noexcept"; 393 } 394 395 if (R->isDependent()) 396 OS << " dependent"; 397 else 398 OS << (R->isSatisfied() ? " satisfied" : " unsatisfied"); 399 if (R->containsUnexpandedParameterPack()) 400 OS << " contains_unexpanded_pack"; 401 } 402 403 static double GetApproxValue(const llvm::APFloat &F) { 404 llvm::APFloat V = F; 405 bool ignored; 406 V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven, 407 &ignored); 408 return V.convertToDouble(); 409 } 410 411 /// True if the \p APValue \p Value can be folded onto the current line. 412 static bool isSimpleAPValue(const APValue &Value) { 413 switch (Value.getKind()) { 414 case APValue::None: 415 case APValue::Indeterminate: 416 case APValue::Int: 417 case APValue::Float: 418 case APValue::FixedPoint: 419 case APValue::ComplexInt: 420 case APValue::ComplexFloat: 421 case APValue::LValue: 422 case APValue::MemberPointer: 423 case APValue::AddrLabelDiff: 424 return true; 425 case APValue::Vector: 426 case APValue::Array: 427 case APValue::Struct: 428 return false; 429 case APValue::Union: 430 return isSimpleAPValue(Value.getUnionValue()); 431 } 432 llvm_unreachable("unexpected APValue kind!"); 433 } 434 435 /// Dump the children of the \p APValue \p Value. 436 /// 437 /// \param[in] Value The \p APValue to visit 438 /// \param[in] Ty The \p QualType passed to \p Visit 439 /// 440 /// \param[in] IdxToChildFun A function mapping an \p APValue and an index 441 /// to one of the child of the \p APValue 442 /// 443 /// \param[in] NumChildren \p IdxToChildFun will be called on \p Value with 444 /// the indices in the range \p [0,NumChildren( 445 /// 446 /// \param[in] LabelSingular The label to use on a line with a single child 447 /// \param[in] LabelPlurial The label to use on a line with multiple children 448 void TextNodeDumper::dumpAPValueChildren( 449 const APValue &Value, QualType Ty, 450 const APValue &(*IdxToChildFun)(const APValue &, unsigned), 451 unsigned NumChildren, StringRef LabelSingular, StringRef LabelPlurial) { 452 // To save some vertical space we print up to MaxChildrenPerLine APValues 453 // considered to be simple (by isSimpleAPValue) on a single line. 454 constexpr unsigned MaxChildrenPerLine = 4; 455 unsigned I = 0; 456 while (I < NumChildren) { 457 unsigned J = I; 458 while (J < NumChildren) { 459 if (isSimpleAPValue(IdxToChildFun(Value, J)) && 460 (J - I < MaxChildrenPerLine)) { 461 ++J; 462 continue; 463 } 464 break; 465 } 466 467 J = std::max(I + 1, J); 468 469 // Print [I,J) on a single line. 470 AddChild(J - I > 1 ? LabelPlurial : LabelSingular, [=]() { 471 for (unsigned X = I; X < J; ++X) { 472 Visit(IdxToChildFun(Value, X), Ty); 473 if (X + 1 != J) 474 OS << ", "; 475 } 476 }); 477 I = J; 478 } 479 } 480 481 void TextNodeDumper::Visit(const APValue &Value, QualType Ty) { 482 ColorScope Color(OS, ShowColors, ValueKindColor); 483 switch (Value.getKind()) { 484 case APValue::None: 485 OS << "None"; 486 return; 487 case APValue::Indeterminate: 488 OS << "Indeterminate"; 489 return; 490 case APValue::Int: 491 OS << "Int "; 492 { 493 ColorScope Color(OS, ShowColors, ValueColor); 494 OS << Value.getInt(); 495 } 496 return; 497 case APValue::Float: 498 OS << "Float "; 499 { 500 ColorScope Color(OS, ShowColors, ValueColor); 501 OS << GetApproxValue(Value.getFloat()); 502 } 503 return; 504 case APValue::FixedPoint: 505 OS << "FixedPoint "; 506 { 507 ColorScope Color(OS, ShowColors, ValueColor); 508 OS << Value.getFixedPoint(); 509 } 510 return; 511 case APValue::Vector: { 512 unsigned VectorLength = Value.getVectorLength(); 513 OS << "Vector length=" << VectorLength; 514 515 dumpAPValueChildren( 516 Value, Ty, 517 [](const APValue &Value, unsigned Index) -> const APValue & { 518 return Value.getVectorElt(Index); 519 }, 520 VectorLength, "element", "elements"); 521 return; 522 } 523 case APValue::ComplexInt: 524 OS << "ComplexInt "; 525 { 526 ColorScope Color(OS, ShowColors, ValueColor); 527 OS << Value.getComplexIntReal() << " + " << Value.getComplexIntImag() 528 << 'i'; 529 } 530 return; 531 case APValue::ComplexFloat: 532 OS << "ComplexFloat "; 533 { 534 ColorScope Color(OS, ShowColors, ValueColor); 535 OS << GetApproxValue(Value.getComplexFloatReal()) << " + " 536 << GetApproxValue(Value.getComplexFloatImag()) << 'i'; 537 } 538 return; 539 case APValue::LValue: 540 (void)Context; 541 OS << "LValue <todo>"; 542 return; 543 case APValue::Array: { 544 unsigned ArraySize = Value.getArraySize(); 545 unsigned NumInitializedElements = Value.getArrayInitializedElts(); 546 OS << "Array size=" << ArraySize; 547 548 dumpAPValueChildren( 549 Value, Ty, 550 [](const APValue &Value, unsigned Index) -> const APValue & { 551 return Value.getArrayInitializedElt(Index); 552 }, 553 NumInitializedElements, "element", "elements"); 554 555 if (Value.hasArrayFiller()) { 556 AddChild("filler", [=] { 557 { 558 ColorScope Color(OS, ShowColors, ValueColor); 559 OS << ArraySize - NumInitializedElements << " x "; 560 } 561 Visit(Value.getArrayFiller(), Ty); 562 }); 563 } 564 565 return; 566 } 567 case APValue::Struct: { 568 OS << "Struct"; 569 570 dumpAPValueChildren( 571 Value, Ty, 572 [](const APValue &Value, unsigned Index) -> const APValue & { 573 return Value.getStructBase(Index); 574 }, 575 Value.getStructNumBases(), "base", "bases"); 576 577 dumpAPValueChildren( 578 Value, Ty, 579 [](const APValue &Value, unsigned Index) -> const APValue & { 580 return Value.getStructField(Index); 581 }, 582 Value.getStructNumFields(), "field", "fields"); 583 584 return; 585 } 586 case APValue::Union: { 587 OS << "Union"; 588 { 589 ColorScope Color(OS, ShowColors, ValueColor); 590 if (const FieldDecl *FD = Value.getUnionField()) 591 OS << " ." << *cast<NamedDecl>(FD); 592 } 593 // If the union value is considered to be simple, fold it into the 594 // current line to save some vertical space. 595 const APValue &UnionValue = Value.getUnionValue(); 596 if (isSimpleAPValue(UnionValue)) { 597 OS << ' '; 598 Visit(UnionValue, Ty); 599 } else { 600 AddChild([=] { Visit(UnionValue, Ty); }); 601 } 602 603 return; 604 } 605 case APValue::MemberPointer: 606 OS << "MemberPointer <todo>"; 607 return; 608 case APValue::AddrLabelDiff: 609 OS << "AddrLabelDiff <todo>"; 610 return; 611 } 612 llvm_unreachable("Unknown APValue kind!"); 613 } 614 615 void TextNodeDumper::dumpPointer(const void *Ptr) { 616 ColorScope Color(OS, ShowColors, AddressColor); 617 OS << ' ' << Ptr; 618 } 619 620 void TextNodeDumper::dumpLocation(SourceLocation Loc) { 621 if (!SM) 622 return; 623 624 ColorScope Color(OS, ShowColors, LocationColor); 625 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc); 626 627 // The general format we print out is filename:line:col, but we drop pieces 628 // that haven't changed since the last loc printed. 629 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc); 630 631 if (PLoc.isInvalid()) { 632 OS << "<invalid sloc>"; 633 return; 634 } 635 636 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) { 637 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':' 638 << PLoc.getColumn(); 639 LastLocFilename = PLoc.getFilename(); 640 LastLocLine = PLoc.getLine(); 641 } else if (PLoc.getLine() != LastLocLine) { 642 OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); 643 LastLocLine = PLoc.getLine(); 644 } else { 645 OS << "col" << ':' << PLoc.getColumn(); 646 } 647 } 648 649 void TextNodeDumper::dumpSourceRange(SourceRange R) { 650 // Can't translate locations if a SourceManager isn't available. 651 if (!SM) 652 return; 653 654 OS << " <"; 655 dumpLocation(R.getBegin()); 656 if (R.getBegin() != R.getEnd()) { 657 OS << ", "; 658 dumpLocation(R.getEnd()); 659 } 660 OS << ">"; 661 662 // <t2.c:123:421[blah], t2.c:412:321> 663 } 664 665 void TextNodeDumper::dumpBareType(QualType T, bool Desugar) { 666 ColorScope Color(OS, ShowColors, TypeColor); 667 668 SplitQualType T_split = T.split(); 669 OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'"; 670 671 if (Desugar && !T.isNull()) { 672 // If the type is sugared, also dump a (shallow) desugared type. 673 SplitQualType D_split = T.getSplitDesugaredType(); 674 if (T_split != D_split) 675 OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'"; 676 } 677 } 678 679 void TextNodeDumper::dumpType(QualType T) { 680 OS << ' '; 681 dumpBareType(T); 682 } 683 684 void TextNodeDumper::dumpBareDeclRef(const Decl *D) { 685 if (!D) { 686 ColorScope Color(OS, ShowColors, NullColor); 687 OS << "<<<NULL>>>"; 688 return; 689 } 690 691 { 692 ColorScope Color(OS, ShowColors, DeclKindNameColor); 693 OS << D->getDeclKindName(); 694 } 695 dumpPointer(D); 696 697 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 698 ColorScope Color(OS, ShowColors, DeclNameColor); 699 OS << " '" << ND->getDeclName() << '\''; 700 } 701 702 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) 703 dumpType(VD->getType()); 704 } 705 706 void TextNodeDumper::dumpName(const NamedDecl *ND) { 707 if (ND->getDeclName()) { 708 ColorScope Color(OS, ShowColors, DeclNameColor); 709 OS << ' ' << ND->getDeclName(); 710 } 711 } 712 713 void TextNodeDumper::dumpAccessSpecifier(AccessSpecifier AS) { 714 const auto AccessSpelling = getAccessSpelling(AS); 715 if (AccessSpelling.empty()) 716 return; 717 OS << AccessSpelling; 718 } 719 720 void TextNodeDumper::dumpCleanupObject( 721 const ExprWithCleanups::CleanupObject &C) { 722 if (auto *BD = C.dyn_cast<BlockDecl *>()) 723 dumpDeclRef(BD, "cleanup"); 724 else if (auto *CLE = C.dyn_cast<CompoundLiteralExpr *>()) 725 AddChild([=] { 726 OS << "cleanup "; 727 { 728 ColorScope Color(OS, ShowColors, StmtColor); 729 OS << CLE->getStmtClassName(); 730 } 731 dumpPointer(CLE); 732 }); 733 else 734 llvm_unreachable("unexpected cleanup type"); 735 } 736 737 void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { 738 if (!D) 739 return; 740 741 AddChild([=] { 742 if (!Label.empty()) 743 OS << Label << ' '; 744 dumpBareDeclRef(D); 745 }); 746 } 747 748 const char *TextNodeDumper::getCommandName(unsigned CommandID) { 749 if (Traits) 750 return Traits->getCommandInfo(CommandID)->Name; 751 const comments::CommandInfo *Info = 752 comments::CommandTraits::getBuiltinCommandInfo(CommandID); 753 if (Info) 754 return Info->Name; 755 return "<not a builtin command>"; 756 } 757 758 void TextNodeDumper::printFPOptions(FPOptionsOverride FPO) { 759 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \ 760 if (FPO.has##NAME##Override()) \ 761 OS << " " #NAME "=" << FPO.get##NAME##Override(); 762 #include "clang/Basic/FPOptions.def" 763 } 764 765 void TextNodeDumper::visitTextComment(const comments::TextComment *C, 766 const comments::FullComment *) { 767 OS << " Text=\"" << C->getText() << "\""; 768 } 769 770 void TextNodeDumper::visitInlineCommandComment( 771 const comments::InlineCommandComment *C, const comments::FullComment *) { 772 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""; 773 switch (C->getRenderKind()) { 774 case comments::InlineCommandComment::RenderNormal: 775 OS << " RenderNormal"; 776 break; 777 case comments::InlineCommandComment::RenderBold: 778 OS << " RenderBold"; 779 break; 780 case comments::InlineCommandComment::RenderMonospaced: 781 OS << " RenderMonospaced"; 782 break; 783 case comments::InlineCommandComment::RenderEmphasized: 784 OS << " RenderEmphasized"; 785 break; 786 case comments::InlineCommandComment::RenderAnchor: 787 OS << " RenderAnchor"; 788 break; 789 } 790 791 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) 792 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; 793 } 794 795 void TextNodeDumper::visitHTMLStartTagComment( 796 const comments::HTMLStartTagComment *C, const comments::FullComment *) { 797 OS << " Name=\"" << C->getTagName() << "\""; 798 if (C->getNumAttrs() != 0) { 799 OS << " Attrs: "; 800 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) { 801 const comments::HTMLStartTagComment::Attribute &Attr = C->getAttr(i); 802 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\""; 803 } 804 } 805 if (C->isSelfClosing()) 806 OS << " SelfClosing"; 807 } 808 809 void TextNodeDumper::visitHTMLEndTagComment( 810 const comments::HTMLEndTagComment *C, const comments::FullComment *) { 811 OS << " Name=\"" << C->getTagName() << "\""; 812 } 813 814 void TextNodeDumper::visitBlockCommandComment( 815 const comments::BlockCommandComment *C, const comments::FullComment *) { 816 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""; 817 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) 818 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; 819 } 820 821 void TextNodeDumper::visitParamCommandComment( 822 const comments::ParamCommandComment *C, const comments::FullComment *FC) { 823 OS << " " 824 << comments::ParamCommandComment::getDirectionAsString(C->getDirection()); 825 826 if (C->isDirectionExplicit()) 827 OS << " explicitly"; 828 else 829 OS << " implicitly"; 830 831 if (C->hasParamName()) { 832 if (C->isParamIndexValid()) 833 OS << " Param=\"" << C->getParamName(FC) << "\""; 834 else 835 OS << " Param=\"" << C->getParamNameAsWritten() << "\""; 836 } 837 838 if (C->isParamIndexValid() && !C->isVarArgParam()) 839 OS << " ParamIndex=" << C->getParamIndex(); 840 } 841 842 void TextNodeDumper::visitTParamCommandComment( 843 const comments::TParamCommandComment *C, const comments::FullComment *FC) { 844 if (C->hasParamName()) { 845 if (C->isPositionValid()) 846 OS << " Param=\"" << C->getParamName(FC) << "\""; 847 else 848 OS << " Param=\"" << C->getParamNameAsWritten() << "\""; 849 } 850 851 if (C->isPositionValid()) { 852 OS << " Position=<"; 853 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) { 854 OS << C->getIndex(i); 855 if (i != e - 1) 856 OS << ", "; 857 } 858 OS << ">"; 859 } 860 } 861 862 void TextNodeDumper::visitVerbatimBlockComment( 863 const comments::VerbatimBlockComment *C, const comments::FullComment *) { 864 OS << " Name=\"" << getCommandName(C->getCommandID()) 865 << "\"" 866 " CloseName=\"" 867 << C->getCloseName() << "\""; 868 } 869 870 void TextNodeDumper::visitVerbatimBlockLineComment( 871 const comments::VerbatimBlockLineComment *C, 872 const comments::FullComment *) { 873 OS << " Text=\"" << C->getText() << "\""; 874 } 875 876 void TextNodeDumper::visitVerbatimLineComment( 877 const comments::VerbatimLineComment *C, const comments::FullComment *) { 878 OS << " Text=\"" << C->getText() << "\""; 879 } 880 881 void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { 882 OS << " null"; 883 } 884 885 void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { 886 OS << " type"; 887 dumpType(TA.getAsType()); 888 } 889 890 void TextNodeDumper::VisitDeclarationTemplateArgument( 891 const TemplateArgument &TA) { 892 OS << " decl"; 893 dumpDeclRef(TA.getAsDecl()); 894 } 895 896 void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { 897 OS << " nullptr"; 898 } 899 900 void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { 901 OS << " integral " << TA.getAsIntegral(); 902 } 903 904 void TextNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) { 905 if (TA.getAsTemplate().getKind() == TemplateName::UsingTemplate) 906 OS << " using"; 907 OS << " template "; 908 TA.getAsTemplate().dump(OS); 909 } 910 911 void TextNodeDumper::VisitTemplateExpansionTemplateArgument( 912 const TemplateArgument &TA) { 913 if (TA.getAsTemplateOrTemplatePattern().getKind() == 914 TemplateName::UsingTemplate) 915 OS << " using"; 916 OS << " template expansion "; 917 TA.getAsTemplateOrTemplatePattern().dump(OS); 918 } 919 920 void TextNodeDumper::VisitExpressionTemplateArgument(const TemplateArgument &) { 921 OS << " expr"; 922 } 923 924 void TextNodeDumper::VisitPackTemplateArgument(const TemplateArgument &) { 925 OS << " pack"; 926 } 927 928 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) { 929 if (Node->path_empty()) 930 return; 931 932 OS << " ("; 933 bool First = true; 934 for (CastExpr::path_const_iterator I = Node->path_begin(), 935 E = Node->path_end(); 936 I != E; ++I) { 937 const CXXBaseSpecifier *Base = *I; 938 if (!First) 939 OS << " -> "; 940 941 const auto *RD = 942 cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl()); 943 944 if (Base->isVirtual()) 945 OS << "virtual "; 946 OS << RD->getName(); 947 First = false; 948 } 949 950 OS << ')'; 951 } 952 953 void TextNodeDumper::VisitIfStmt(const IfStmt *Node) { 954 if (Node->hasInitStorage()) 955 OS << " has_init"; 956 if (Node->hasVarStorage()) 957 OS << " has_var"; 958 if (Node->hasElseStorage()) 959 OS << " has_else"; 960 if (Node->isConstexpr()) 961 OS << " constexpr"; 962 if (Node->isConsteval()) { 963 OS << " "; 964 if (Node->isNegatedConsteval()) 965 OS << "!"; 966 OS << "consteval"; 967 } 968 } 969 970 void TextNodeDumper::VisitSwitchStmt(const SwitchStmt *Node) { 971 if (Node->hasInitStorage()) 972 OS << " has_init"; 973 if (Node->hasVarStorage()) 974 OS << " has_var"; 975 } 976 977 void TextNodeDumper::VisitWhileStmt(const WhileStmt *Node) { 978 if (Node->hasVarStorage()) 979 OS << " has_var"; 980 } 981 982 void TextNodeDumper::VisitLabelStmt(const LabelStmt *Node) { 983 OS << " '" << Node->getName() << "'"; 984 if (Node->isSideEntry()) 985 OS << " side_entry"; 986 } 987 988 void TextNodeDumper::VisitGotoStmt(const GotoStmt *Node) { 989 OS << " '" << Node->getLabel()->getName() << "'"; 990 dumpPointer(Node->getLabel()); 991 } 992 993 void TextNodeDumper::VisitCaseStmt(const CaseStmt *Node) { 994 if (Node->caseStmtIsGNURange()) 995 OS << " gnu_range"; 996 } 997 998 void TextNodeDumper::VisitConstantExpr(const ConstantExpr *Node) { 999 if (Node->hasAPValueResult()) 1000 AddChild("value", 1001 [=] { Visit(Node->getAPValueResult(), Node->getType()); }); 1002 } 1003 1004 void TextNodeDumper::VisitCallExpr(const CallExpr *Node) { 1005 if (Node->usesADL()) 1006 OS << " adl"; 1007 if (Node->hasStoredFPFeatures()) 1008 printFPOptions(Node->getFPFeatures()); 1009 } 1010 1011 void TextNodeDumper::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node) { 1012 const char *OperatorSpelling = clang::getOperatorSpelling(Node->getOperator()); 1013 if (OperatorSpelling) 1014 OS << " '" << OperatorSpelling << "'"; 1015 1016 VisitCallExpr(Node); 1017 } 1018 1019 void TextNodeDumper::VisitCastExpr(const CastExpr *Node) { 1020 OS << " <"; 1021 { 1022 ColorScope Color(OS, ShowColors, CastColor); 1023 OS << Node->getCastKindName(); 1024 } 1025 dumpBasePath(OS, Node); 1026 OS << ">"; 1027 if (Node->hasStoredFPFeatures()) 1028 printFPOptions(Node->getFPFeatures()); 1029 } 1030 1031 void TextNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *Node) { 1032 VisitCastExpr(Node); 1033 if (Node->isPartOfExplicitCast()) 1034 OS << " part_of_explicit_cast"; 1035 } 1036 1037 void TextNodeDumper::VisitDeclRefExpr(const DeclRefExpr *Node) { 1038 OS << " "; 1039 dumpBareDeclRef(Node->getDecl()); 1040 if (Node->getDecl() != Node->getFoundDecl()) { 1041 OS << " ("; 1042 dumpBareDeclRef(Node->getFoundDecl()); 1043 OS << ")"; 1044 } 1045 switch (Node->isNonOdrUse()) { 1046 case NOUR_None: break; 1047 case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break; 1048 case NOUR_Constant: OS << " non_odr_use_constant"; break; 1049 case NOUR_Discarded: OS << " non_odr_use_discarded"; break; 1050 } 1051 if (Node->isImmediateEscalating()) 1052 OS << " immediate-escalating"; 1053 } 1054 1055 void TextNodeDumper::VisitUnresolvedLookupExpr( 1056 const UnresolvedLookupExpr *Node) { 1057 OS << " ("; 1058 if (!Node->requiresADL()) 1059 OS << "no "; 1060 OS << "ADL) = '" << Node->getName() << '\''; 1061 1062 UnresolvedLookupExpr::decls_iterator I = Node->decls_begin(), 1063 E = Node->decls_end(); 1064 if (I == E) 1065 OS << " empty"; 1066 for (; I != E; ++I) 1067 dumpPointer(*I); 1068 } 1069 1070 void TextNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) { 1071 { 1072 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1073 OS << " " << Node->getDecl()->getDeclKindName() << "Decl"; 1074 } 1075 OS << "='" << *Node->getDecl() << "'"; 1076 dumpPointer(Node->getDecl()); 1077 if (Node->isFreeIvar()) 1078 OS << " isFreeIvar"; 1079 } 1080 1081 void TextNodeDumper::VisitSYCLUniqueStableNameExpr( 1082 const SYCLUniqueStableNameExpr *Node) { 1083 dumpType(Node->getTypeSourceInfo()->getType()); 1084 } 1085 1086 void TextNodeDumper::VisitPredefinedExpr(const PredefinedExpr *Node) { 1087 OS << " " << PredefinedExpr::getIdentKindName(Node->getIdentKind()); 1088 } 1089 1090 void TextNodeDumper::VisitCharacterLiteral(const CharacterLiteral *Node) { 1091 ColorScope Color(OS, ShowColors, ValueColor); 1092 OS << " " << Node->getValue(); 1093 } 1094 1095 void TextNodeDumper::VisitIntegerLiteral(const IntegerLiteral *Node) { 1096 bool isSigned = Node->getType()->isSignedIntegerType(); 1097 ColorScope Color(OS, ShowColors, ValueColor); 1098 OS << " " << toString(Node->getValue(), 10, isSigned); 1099 } 1100 1101 void TextNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *Node) { 1102 ColorScope Color(OS, ShowColors, ValueColor); 1103 OS << " " << Node->getValueAsString(/*Radix=*/10); 1104 } 1105 1106 void TextNodeDumper::VisitFloatingLiteral(const FloatingLiteral *Node) { 1107 ColorScope Color(OS, ShowColors, ValueColor); 1108 OS << " " << Node->getValueAsApproximateDouble(); 1109 } 1110 1111 void TextNodeDumper::VisitStringLiteral(const StringLiteral *Str) { 1112 ColorScope Color(OS, ShowColors, ValueColor); 1113 OS << " "; 1114 Str->outputString(OS); 1115 } 1116 1117 void TextNodeDumper::VisitInitListExpr(const InitListExpr *ILE) { 1118 if (auto *Field = ILE->getInitializedFieldInUnion()) { 1119 OS << " field "; 1120 dumpBareDeclRef(Field); 1121 } 1122 } 1123 1124 void TextNodeDumper::VisitGenericSelectionExpr(const GenericSelectionExpr *E) { 1125 if (E->isResultDependent()) 1126 OS << " result_dependent"; 1127 } 1128 1129 void TextNodeDumper::VisitUnaryOperator(const UnaryOperator *Node) { 1130 OS << " " << (Node->isPostfix() ? "postfix" : "prefix") << " '" 1131 << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'"; 1132 if (!Node->canOverflow()) 1133 OS << " cannot overflow"; 1134 if (Node->hasStoredFPFeatures()) 1135 printFPOptions(Node->getStoredFPFeatures()); 1136 } 1137 1138 void TextNodeDumper::VisitUnaryExprOrTypeTraitExpr( 1139 const UnaryExprOrTypeTraitExpr *Node) { 1140 OS << " " << getTraitSpelling(Node->getKind()); 1141 1142 if (Node->isArgumentType()) 1143 dumpType(Node->getArgumentType()); 1144 } 1145 1146 void TextNodeDumper::VisitMemberExpr(const MemberExpr *Node) { 1147 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl(); 1148 dumpPointer(Node->getMemberDecl()); 1149 switch (Node->isNonOdrUse()) { 1150 case NOUR_None: break; 1151 case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break; 1152 case NOUR_Constant: OS << " non_odr_use_constant"; break; 1153 case NOUR_Discarded: OS << " non_odr_use_discarded"; break; 1154 } 1155 } 1156 1157 void TextNodeDumper::VisitExtVectorElementExpr( 1158 const ExtVectorElementExpr *Node) { 1159 OS << " " << Node->getAccessor().getNameStart(); 1160 } 1161 1162 void TextNodeDumper::VisitBinaryOperator(const BinaryOperator *Node) { 1163 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'"; 1164 if (Node->hasStoredFPFeatures()) 1165 printFPOptions(Node->getStoredFPFeatures()); 1166 } 1167 1168 void TextNodeDumper::VisitCompoundAssignOperator( 1169 const CompoundAssignOperator *Node) { 1170 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) 1171 << "' ComputeLHSTy="; 1172 dumpBareType(Node->getComputationLHSType()); 1173 OS << " ComputeResultTy="; 1174 dumpBareType(Node->getComputationResultType()); 1175 if (Node->hasStoredFPFeatures()) 1176 printFPOptions(Node->getStoredFPFeatures()); 1177 } 1178 1179 void TextNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) { 1180 OS << " " << Node->getLabel()->getName(); 1181 dumpPointer(Node->getLabel()); 1182 } 1183 1184 void TextNodeDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) { 1185 OS << " " << Node->getCastName() << "<" 1186 << Node->getTypeAsWritten().getAsString() << ">" 1187 << " <" << Node->getCastKindName(); 1188 dumpBasePath(OS, Node); 1189 OS << ">"; 1190 } 1191 1192 void TextNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) { 1193 OS << " " << (Node->getValue() ? "true" : "false"); 1194 } 1195 1196 void TextNodeDumper::VisitCXXThisExpr(const CXXThisExpr *Node) { 1197 if (Node->isImplicit()) 1198 OS << " implicit"; 1199 OS << " this"; 1200 } 1201 1202 void TextNodeDumper::VisitCXXFunctionalCastExpr( 1203 const CXXFunctionalCastExpr *Node) { 1204 OS << " functional cast to " << Node->getTypeAsWritten().getAsString() << " <" 1205 << Node->getCastKindName() << ">"; 1206 if (Node->hasStoredFPFeatures()) 1207 printFPOptions(Node->getFPFeatures()); 1208 } 1209 1210 void TextNodeDumper::VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node) { 1211 VisitCXXNamedCastExpr(Node); 1212 if (Node->hasStoredFPFeatures()) 1213 printFPOptions(Node->getFPFeatures()); 1214 } 1215 1216 void TextNodeDumper::VisitCXXUnresolvedConstructExpr( 1217 const CXXUnresolvedConstructExpr *Node) { 1218 dumpType(Node->getTypeAsWritten()); 1219 if (Node->isListInitialization()) 1220 OS << " list"; 1221 } 1222 1223 void TextNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) { 1224 CXXConstructorDecl *Ctor = Node->getConstructor(); 1225 dumpType(Ctor->getType()); 1226 if (Node->isElidable()) 1227 OS << " elidable"; 1228 if (Node->isListInitialization()) 1229 OS << " list"; 1230 if (Node->isStdInitListInitialization()) 1231 OS << " std::initializer_list"; 1232 if (Node->requiresZeroInitialization()) 1233 OS << " zeroing"; 1234 if (Node->isImmediateEscalating()) 1235 OS << " immediate-escalating"; 1236 } 1237 1238 void TextNodeDumper::VisitCXXBindTemporaryExpr( 1239 const CXXBindTemporaryExpr *Node) { 1240 OS << " (CXXTemporary"; 1241 dumpPointer(Node); 1242 OS << ")"; 1243 } 1244 1245 void TextNodeDumper::VisitCXXNewExpr(const CXXNewExpr *Node) { 1246 if (Node->isGlobalNew()) 1247 OS << " global"; 1248 if (Node->isArray()) 1249 OS << " array"; 1250 if (Node->getOperatorNew()) { 1251 OS << ' '; 1252 dumpBareDeclRef(Node->getOperatorNew()); 1253 } 1254 // We could dump the deallocation function used in case of error, but it's 1255 // usually not that interesting. 1256 } 1257 1258 void TextNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) { 1259 if (Node->isGlobalDelete()) 1260 OS << " global"; 1261 if (Node->isArrayForm()) 1262 OS << " array"; 1263 if (Node->getOperatorDelete()) { 1264 OS << ' '; 1265 dumpBareDeclRef(Node->getOperatorDelete()); 1266 } 1267 } 1268 1269 void TextNodeDumper::VisitTypeTraitExpr(const TypeTraitExpr *Node) { 1270 OS << " " << getTraitSpelling(Node->getTrait()); 1271 } 1272 1273 void TextNodeDumper::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node) { 1274 OS << " " << getTraitSpelling(Node->getTrait()); 1275 } 1276 1277 void TextNodeDumper::VisitExpressionTraitExpr(const ExpressionTraitExpr *Node) { 1278 OS << " " << getTraitSpelling(Node->getTrait()); 1279 } 1280 1281 void TextNodeDumper::VisitMaterializeTemporaryExpr( 1282 const MaterializeTemporaryExpr *Node) { 1283 if (const ValueDecl *VD = Node->getExtendingDecl()) { 1284 OS << " extended by "; 1285 dumpBareDeclRef(VD); 1286 } 1287 } 1288 1289 void TextNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) { 1290 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i) 1291 dumpCleanupObject(Node->getObject(i)); 1292 } 1293 1294 void TextNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) { 1295 dumpPointer(Node->getPack()); 1296 dumpName(Node->getPack()); 1297 } 1298 1299 void TextNodeDumper::VisitCXXDependentScopeMemberExpr( 1300 const CXXDependentScopeMemberExpr *Node) { 1301 OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember(); 1302 } 1303 1304 void TextNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) { 1305 OS << " selector="; 1306 Node->getSelector().print(OS); 1307 switch (Node->getReceiverKind()) { 1308 case ObjCMessageExpr::Instance: 1309 break; 1310 1311 case ObjCMessageExpr::Class: 1312 OS << " class="; 1313 dumpBareType(Node->getClassReceiver()); 1314 break; 1315 1316 case ObjCMessageExpr::SuperInstance: 1317 OS << " super (instance)"; 1318 break; 1319 1320 case ObjCMessageExpr::SuperClass: 1321 OS << " super (class)"; 1322 break; 1323 } 1324 } 1325 1326 void TextNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) { 1327 if (auto *BoxingMethod = Node->getBoxingMethod()) { 1328 OS << " selector="; 1329 BoxingMethod->getSelector().print(OS); 1330 } 1331 } 1332 1333 void TextNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) { 1334 if (!Node->getCatchParamDecl()) 1335 OS << " catch all"; 1336 } 1337 1338 void TextNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) { 1339 dumpType(Node->getEncodedType()); 1340 } 1341 1342 void TextNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) { 1343 OS << " "; 1344 Node->getSelector().print(OS); 1345 } 1346 1347 void TextNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) { 1348 OS << ' ' << *Node->getProtocol(); 1349 } 1350 1351 void TextNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) { 1352 if (Node->isImplicitProperty()) { 1353 OS << " Kind=MethodRef Getter=\""; 1354 if (Node->getImplicitPropertyGetter()) 1355 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1356 else 1357 OS << "(null)"; 1358 1359 OS << "\" Setter=\""; 1360 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter()) 1361 Setter->getSelector().print(OS); 1362 else 1363 OS << "(null)"; 1364 OS << "\""; 1365 } else { 1366 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() 1367 << '"'; 1368 } 1369 1370 if (Node->isSuperReceiver()) 1371 OS << " super"; 1372 1373 OS << " Messaging="; 1374 if (Node->isMessagingGetter() && Node->isMessagingSetter()) 1375 OS << "Getter&Setter"; 1376 else if (Node->isMessagingGetter()) 1377 OS << "Getter"; 1378 else if (Node->isMessagingSetter()) 1379 OS << "Setter"; 1380 } 1381 1382 void TextNodeDumper::VisitObjCSubscriptRefExpr( 1383 const ObjCSubscriptRefExpr *Node) { 1384 if (Node->isArraySubscriptRefExpr()) 1385 OS << " Kind=ArraySubscript GetterForArray=\""; 1386 else 1387 OS << " Kind=DictionarySubscript GetterForDictionary=\""; 1388 if (Node->getAtIndexMethodDecl()) 1389 Node->getAtIndexMethodDecl()->getSelector().print(OS); 1390 else 1391 OS << "(null)"; 1392 1393 if (Node->isArraySubscriptRefExpr()) 1394 OS << "\" SetterForArray=\""; 1395 else 1396 OS << "\" SetterForDictionary=\""; 1397 if (Node->setAtIndexMethodDecl()) 1398 Node->setAtIndexMethodDecl()->getSelector().print(OS); 1399 else 1400 OS << "(null)"; 1401 } 1402 1403 void TextNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) { 1404 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1405 } 1406 1407 void TextNodeDumper::VisitOMPIteratorExpr(const OMPIteratorExpr *Node) { 1408 OS << " "; 1409 for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) { 1410 Visit(Node->getIteratorDecl(I)); 1411 OS << " = "; 1412 const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I); 1413 OS << " begin "; 1414 Visit(Range.Begin); 1415 OS << " end "; 1416 Visit(Range.End); 1417 if (Range.Step) { 1418 OS << " step "; 1419 Visit(Range.Step); 1420 } 1421 } 1422 } 1423 1424 void TextNodeDumper::VisitConceptSpecializationExpr( 1425 const ConceptSpecializationExpr *Node) { 1426 OS << " "; 1427 dumpBareDeclRef(Node->getFoundDecl()); 1428 } 1429 1430 void TextNodeDumper::VisitRequiresExpr( 1431 const RequiresExpr *Node) { 1432 if (!Node->isValueDependent()) 1433 OS << (Node->isSatisfied() ? " satisfied" : " unsatisfied"); 1434 } 1435 1436 void TextNodeDumper::VisitRValueReferenceType(const ReferenceType *T) { 1437 if (T->isSpelledAsLValue()) 1438 OS << " written as lvalue reference"; 1439 } 1440 1441 void TextNodeDumper::VisitArrayType(const ArrayType *T) { 1442 switch (T->getSizeModifier()) { 1443 case ArrayType::Normal: 1444 break; 1445 case ArrayType::Static: 1446 OS << " static"; 1447 break; 1448 case ArrayType::Star: 1449 OS << " *"; 1450 break; 1451 } 1452 OS << " " << T->getIndexTypeQualifiers().getAsString(); 1453 } 1454 1455 void TextNodeDumper::VisitConstantArrayType(const ConstantArrayType *T) { 1456 OS << " " << T->getSize(); 1457 VisitArrayType(T); 1458 } 1459 1460 void TextNodeDumper::VisitVariableArrayType(const VariableArrayType *T) { 1461 OS << " "; 1462 dumpSourceRange(T->getBracketsRange()); 1463 VisitArrayType(T); 1464 } 1465 1466 void TextNodeDumper::VisitDependentSizedArrayType( 1467 const DependentSizedArrayType *T) { 1468 VisitArrayType(T); 1469 OS << " "; 1470 dumpSourceRange(T->getBracketsRange()); 1471 } 1472 1473 void TextNodeDumper::VisitDependentSizedExtVectorType( 1474 const DependentSizedExtVectorType *T) { 1475 OS << " "; 1476 dumpLocation(T->getAttributeLoc()); 1477 } 1478 1479 void TextNodeDumper::VisitVectorType(const VectorType *T) { 1480 switch (T->getVectorKind()) { 1481 case VectorType::GenericVector: 1482 break; 1483 case VectorType::AltiVecVector: 1484 OS << " altivec"; 1485 break; 1486 case VectorType::AltiVecPixel: 1487 OS << " altivec pixel"; 1488 break; 1489 case VectorType::AltiVecBool: 1490 OS << " altivec bool"; 1491 break; 1492 case VectorType::NeonVector: 1493 OS << " neon"; 1494 break; 1495 case VectorType::NeonPolyVector: 1496 OS << " neon poly"; 1497 break; 1498 case VectorType::SveFixedLengthDataVector: 1499 OS << " fixed-length sve data vector"; 1500 break; 1501 case VectorType::SveFixedLengthPredicateVector: 1502 OS << " fixed-length sve predicate vector"; 1503 break; 1504 case VectorType::RVVFixedLengthDataVector: 1505 OS << " fixed-length rvv data vector"; 1506 break; 1507 } 1508 OS << " " << T->getNumElements(); 1509 } 1510 1511 void TextNodeDumper::VisitFunctionType(const FunctionType *T) { 1512 auto EI = T->getExtInfo(); 1513 if (EI.getNoReturn()) 1514 OS << " noreturn"; 1515 if (EI.getProducesResult()) 1516 OS << " produces_result"; 1517 if (EI.getHasRegParm()) 1518 OS << " regparm " << EI.getRegParm(); 1519 OS << " " << FunctionType::getNameForCallConv(EI.getCC()); 1520 } 1521 1522 void TextNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) { 1523 auto EPI = T->getExtProtoInfo(); 1524 if (EPI.HasTrailingReturn) 1525 OS << " trailing_return"; 1526 if (T->isConst()) 1527 OS << " const"; 1528 if (T->isVolatile()) 1529 OS << " volatile"; 1530 if (T->isRestrict()) 1531 OS << " restrict"; 1532 if (T->getExtProtoInfo().Variadic) 1533 OS << " variadic"; 1534 switch (EPI.RefQualifier) { 1535 case RQ_None: 1536 break; 1537 case RQ_LValue: 1538 OS << " &"; 1539 break; 1540 case RQ_RValue: 1541 OS << " &&"; 1542 break; 1543 } 1544 // FIXME: Exception specification. 1545 // FIXME: Consumed parameters. 1546 VisitFunctionType(T); 1547 } 1548 1549 void TextNodeDumper::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 1550 dumpDeclRef(T->getDecl()); 1551 } 1552 1553 void TextNodeDumper::VisitUsingType(const UsingType *T) { 1554 dumpDeclRef(T->getFoundDecl()); 1555 if (!T->typeMatchesDecl()) 1556 OS << " divergent"; 1557 } 1558 1559 void TextNodeDumper::VisitTypedefType(const TypedefType *T) { 1560 dumpDeclRef(T->getDecl()); 1561 if (!T->typeMatchesDecl()) 1562 OS << " divergent"; 1563 } 1564 1565 void TextNodeDumper::VisitUnaryTransformType(const UnaryTransformType *T) { 1566 switch (T->getUTTKind()) { 1567 #define TRANSFORM_TYPE_TRAIT_DEF(Enum, Trait) \ 1568 case UnaryTransformType::Enum: \ 1569 OS << " " #Trait; \ 1570 break; 1571 #include "clang/Basic/TransformTypeTraits.def" 1572 } 1573 } 1574 1575 void TextNodeDumper::VisitTagType(const TagType *T) { 1576 dumpDeclRef(T->getDecl()); 1577 } 1578 1579 void TextNodeDumper::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 1580 OS << " depth " << T->getDepth() << " index " << T->getIndex(); 1581 if (T->isParameterPack()) 1582 OS << " pack"; 1583 dumpDeclRef(T->getDecl()); 1584 } 1585 1586 void TextNodeDumper::VisitSubstTemplateTypeParmType( 1587 const SubstTemplateTypeParmType *T) { 1588 dumpDeclRef(T->getAssociatedDecl()); 1589 VisitTemplateTypeParmDecl(T->getReplacedParameter()); 1590 if (auto PackIndex = T->getPackIndex()) 1591 OS << " pack_index " << *PackIndex; 1592 } 1593 1594 void TextNodeDumper::VisitSubstTemplateTypeParmPackType( 1595 const SubstTemplateTypeParmPackType *T) { 1596 dumpDeclRef(T->getAssociatedDecl()); 1597 VisitTemplateTypeParmDecl(T->getReplacedParameter()); 1598 } 1599 1600 void TextNodeDumper::VisitAutoType(const AutoType *T) { 1601 if (T->isDecltypeAuto()) 1602 OS << " decltype(auto)"; 1603 if (!T->isDeduced()) 1604 OS << " undeduced"; 1605 if (T->isConstrained()) { 1606 dumpDeclRef(T->getTypeConstraintConcept()); 1607 for (const auto &Arg : T->getTypeConstraintArguments()) 1608 VisitTemplateArgument(Arg); 1609 } 1610 } 1611 1612 void TextNodeDumper::VisitDeducedTemplateSpecializationType( 1613 const DeducedTemplateSpecializationType *T) { 1614 if (T->getTemplateName().getKind() == TemplateName::UsingTemplate) 1615 OS << " using"; 1616 } 1617 1618 void TextNodeDumper::VisitTemplateSpecializationType( 1619 const TemplateSpecializationType *T) { 1620 if (T->isTypeAlias()) 1621 OS << " alias"; 1622 if (T->getTemplateName().getKind() == TemplateName::UsingTemplate) 1623 OS << " using"; 1624 OS << " "; 1625 T->getTemplateName().dump(OS); 1626 } 1627 1628 void TextNodeDumper::VisitInjectedClassNameType( 1629 const InjectedClassNameType *T) { 1630 dumpDeclRef(T->getDecl()); 1631 } 1632 1633 void TextNodeDumper::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1634 dumpDeclRef(T->getDecl()); 1635 } 1636 1637 void TextNodeDumper::VisitPackExpansionType(const PackExpansionType *T) { 1638 if (auto N = T->getNumExpansions()) 1639 OS << " expansions " << *N; 1640 } 1641 1642 void TextNodeDumper::VisitLabelDecl(const LabelDecl *D) { dumpName(D); } 1643 1644 void TextNodeDumper::VisitTypedefDecl(const TypedefDecl *D) { 1645 dumpName(D); 1646 dumpType(D->getUnderlyingType()); 1647 if (D->isModulePrivate()) 1648 OS << " __module_private__"; 1649 } 1650 1651 void TextNodeDumper::VisitEnumDecl(const EnumDecl *D) { 1652 if (D->isScoped()) { 1653 if (D->isScopedUsingClassTag()) 1654 OS << " class"; 1655 else 1656 OS << " struct"; 1657 } 1658 dumpName(D); 1659 if (D->isModulePrivate()) 1660 OS << " __module_private__"; 1661 if (D->isFixed()) 1662 dumpType(D->getIntegerType()); 1663 } 1664 1665 void TextNodeDumper::VisitRecordDecl(const RecordDecl *D) { 1666 OS << ' ' << D->getKindName(); 1667 dumpName(D); 1668 if (D->isModulePrivate()) 1669 OS << " __module_private__"; 1670 if (D->isCompleteDefinition()) 1671 OS << " definition"; 1672 } 1673 1674 void TextNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) { 1675 dumpName(D); 1676 dumpType(D->getType()); 1677 } 1678 1679 void TextNodeDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) { 1680 dumpName(D); 1681 dumpType(D->getType()); 1682 1683 for (const auto *Child : D->chain()) 1684 dumpDeclRef(Child); 1685 } 1686 1687 void TextNodeDumper::VisitFunctionDecl(const FunctionDecl *D) { 1688 dumpName(D); 1689 dumpType(D->getType()); 1690 1691 StorageClass SC = D->getStorageClass(); 1692 if (SC != SC_None) 1693 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC); 1694 if (D->isInlineSpecified()) 1695 OS << " inline"; 1696 if (D->isVirtualAsWritten()) 1697 OS << " virtual"; 1698 if (D->isModulePrivate()) 1699 OS << " __module_private__"; 1700 1701 if (D->isPure()) 1702 OS << " pure"; 1703 if (D->isDefaulted()) { 1704 OS << " default"; 1705 if (D->isDeleted()) 1706 OS << "_delete"; 1707 } 1708 if (D->isDeletedAsWritten()) 1709 OS << " delete"; 1710 if (D->isTrivial()) 1711 OS << " trivial"; 1712 1713 if (D->isIneligibleOrNotSelected()) 1714 OS << (isa<CXXDestructorDecl>(D) ? " not_selected" : " ineligible"); 1715 1716 if (const auto *FPT = D->getType()->getAs<FunctionProtoType>()) { 1717 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); 1718 switch (EPI.ExceptionSpec.Type) { 1719 default: 1720 break; 1721 case EST_Unevaluated: 1722 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl; 1723 break; 1724 case EST_Uninstantiated: 1725 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate; 1726 break; 1727 } 1728 } 1729 1730 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 1731 if (MD->size_overridden_methods() != 0) { 1732 auto dumpOverride = [=](const CXXMethodDecl *D) { 1733 SplitQualType T_split = D->getType().split(); 1734 OS << D << " " << D->getParent()->getName() << "::" << D->getDeclName() 1735 << " '" << QualType::getAsString(T_split, PrintPolicy) << "'"; 1736 }; 1737 1738 AddChild([=] { 1739 auto Overrides = MD->overridden_methods(); 1740 OS << "Overrides: [ "; 1741 dumpOverride(*Overrides.begin()); 1742 for (const auto *Override : 1743 llvm::make_range(Overrides.begin() + 1, Overrides.end())) { 1744 OS << ", "; 1745 dumpOverride(Override); 1746 } 1747 OS << " ]"; 1748 }); 1749 } 1750 } 1751 1752 if (!D->isInlineSpecified() && D->isInlined()) { 1753 OS << " implicit-inline"; 1754 } 1755 // Since NumParams comes from the FunctionProtoType of the FunctionDecl and 1756 // the Params are set later, it is possible for a dump during debugging to 1757 // encounter a FunctionDecl that has been created but hasn't been assigned 1758 // ParmVarDecls yet. 1759 if (!D->param_empty() && !D->param_begin()) 1760 OS << " <<<NULL params x " << D->getNumParams() << ">>>"; 1761 } 1762 1763 void TextNodeDumper::VisitLifetimeExtendedTemporaryDecl( 1764 const LifetimeExtendedTemporaryDecl *D) { 1765 OS << " extended by "; 1766 dumpBareDeclRef(D->getExtendingDecl()); 1767 OS << " mangling "; 1768 { 1769 ColorScope Color(OS, ShowColors, ValueColor); 1770 OS << D->getManglingNumber(); 1771 } 1772 } 1773 1774 void TextNodeDumper::VisitFieldDecl(const FieldDecl *D) { 1775 dumpName(D); 1776 dumpType(D->getType()); 1777 if (D->isMutable()) 1778 OS << " mutable"; 1779 if (D->isModulePrivate()) 1780 OS << " __module_private__"; 1781 } 1782 1783 void TextNodeDumper::VisitVarDecl(const VarDecl *D) { 1784 dumpName(D); 1785 dumpType(D->getType()); 1786 StorageClass SC = D->getStorageClass(); 1787 if (SC != SC_None) 1788 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC); 1789 switch (D->getTLSKind()) { 1790 case VarDecl::TLS_None: 1791 break; 1792 case VarDecl::TLS_Static: 1793 OS << " tls"; 1794 break; 1795 case VarDecl::TLS_Dynamic: 1796 OS << " tls_dynamic"; 1797 break; 1798 } 1799 if (D->isModulePrivate()) 1800 OS << " __module_private__"; 1801 if (D->isNRVOVariable()) 1802 OS << " nrvo"; 1803 if (D->isInline()) 1804 OS << " inline"; 1805 if (D->isConstexpr()) 1806 OS << " constexpr"; 1807 if (D->hasInit()) { 1808 switch (D->getInitStyle()) { 1809 case VarDecl::CInit: 1810 OS << " cinit"; 1811 break; 1812 case VarDecl::CallInit: 1813 OS << " callinit"; 1814 break; 1815 case VarDecl::ListInit: 1816 OS << " listinit"; 1817 break; 1818 case VarDecl::ParenListInit: 1819 OS << " parenlistinit"; 1820 } 1821 } 1822 if (D->needsDestruction(D->getASTContext())) 1823 OS << " destroyed"; 1824 if (D->isParameterPack()) 1825 OS << " pack"; 1826 1827 if (D->hasInit()) { 1828 const Expr *E = D->getInit(); 1829 // Only dump the value of constexpr VarDecls for now. 1830 if (E && !E->isValueDependent() && D->isConstexpr() && 1831 !D->getType()->isDependentType()) { 1832 const APValue *Value = D->evaluateValue(); 1833 if (Value) 1834 AddChild("value", [=] { Visit(*Value, E->getType()); }); 1835 } 1836 } 1837 } 1838 1839 void TextNodeDumper::VisitBindingDecl(const BindingDecl *D) { 1840 dumpName(D); 1841 dumpType(D->getType()); 1842 } 1843 1844 void TextNodeDumper::VisitCapturedDecl(const CapturedDecl *D) { 1845 if (D->isNothrow()) 1846 OS << " nothrow"; 1847 } 1848 1849 void TextNodeDumper::VisitImportDecl(const ImportDecl *D) { 1850 OS << ' ' << D->getImportedModule()->getFullModuleName(); 1851 1852 for (Decl *InitD : 1853 D->getASTContext().getModuleInitializers(D->getImportedModule())) 1854 dumpDeclRef(InitD, "initializer"); 1855 } 1856 1857 void TextNodeDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) { 1858 OS << ' '; 1859 switch (D->getCommentKind()) { 1860 case PCK_Unknown: 1861 llvm_unreachable("unexpected pragma comment kind"); 1862 case PCK_Compiler: 1863 OS << "compiler"; 1864 break; 1865 case PCK_ExeStr: 1866 OS << "exestr"; 1867 break; 1868 case PCK_Lib: 1869 OS << "lib"; 1870 break; 1871 case PCK_Linker: 1872 OS << "linker"; 1873 break; 1874 case PCK_User: 1875 OS << "user"; 1876 break; 1877 } 1878 StringRef Arg = D->getArg(); 1879 if (!Arg.empty()) 1880 OS << " \"" << Arg << "\""; 1881 } 1882 1883 void TextNodeDumper::VisitPragmaDetectMismatchDecl( 1884 const PragmaDetectMismatchDecl *D) { 1885 OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\""; 1886 } 1887 1888 void TextNodeDumper::VisitOMPExecutableDirective( 1889 const OMPExecutableDirective *D) { 1890 if (D->isStandaloneDirective()) 1891 OS << " openmp_standalone_directive"; 1892 } 1893 1894 void TextNodeDumper::VisitOMPDeclareReductionDecl( 1895 const OMPDeclareReductionDecl *D) { 1896 dumpName(D); 1897 dumpType(D->getType()); 1898 OS << " combiner"; 1899 dumpPointer(D->getCombiner()); 1900 if (const auto *Initializer = D->getInitializer()) { 1901 OS << " initializer"; 1902 dumpPointer(Initializer); 1903 switch (D->getInitializerKind()) { 1904 case OMPDeclareReductionDecl::DirectInit: 1905 OS << " omp_priv = "; 1906 break; 1907 case OMPDeclareReductionDecl::CopyInit: 1908 OS << " omp_priv ()"; 1909 break; 1910 case OMPDeclareReductionDecl::CallInit: 1911 break; 1912 } 1913 } 1914 } 1915 1916 void TextNodeDumper::VisitOMPRequiresDecl(const OMPRequiresDecl *D) { 1917 for (const auto *C : D->clauselists()) { 1918 AddChild([=] { 1919 if (!C) { 1920 ColorScope Color(OS, ShowColors, NullColor); 1921 OS << "<<<NULL>>> OMPClause"; 1922 return; 1923 } 1924 { 1925 ColorScope Color(OS, ShowColors, AttrColor); 1926 StringRef ClauseName( 1927 llvm::omp::getOpenMPClauseName(C->getClauseKind())); 1928 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper() 1929 << ClauseName.drop_front() << "Clause"; 1930 } 1931 dumpPointer(C); 1932 dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc())); 1933 }); 1934 } 1935 } 1936 1937 void TextNodeDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) { 1938 dumpName(D); 1939 dumpType(D->getType()); 1940 } 1941 1942 void TextNodeDumper::VisitNamespaceDecl(const NamespaceDecl *D) { 1943 dumpName(D); 1944 if (D->isInline()) 1945 OS << " inline"; 1946 if (D->isNested()) 1947 OS << " nested"; 1948 if (!D->isOriginalNamespace()) 1949 dumpDeclRef(D->getOriginalNamespace(), "original"); 1950 } 1951 1952 void TextNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 1953 OS << ' '; 1954 dumpBareDeclRef(D->getNominatedNamespace()); 1955 } 1956 1957 void TextNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 1958 dumpName(D); 1959 dumpDeclRef(D->getAliasedNamespace()); 1960 } 1961 1962 void TextNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) { 1963 dumpName(D); 1964 dumpType(D->getUnderlyingType()); 1965 } 1966 1967 void TextNodeDumper::VisitTypeAliasTemplateDecl( 1968 const TypeAliasTemplateDecl *D) { 1969 dumpName(D); 1970 } 1971 1972 void TextNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) { 1973 VisitRecordDecl(D); 1974 if (!D->isCompleteDefinition()) 1975 return; 1976 1977 AddChild([=] { 1978 { 1979 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1980 OS << "DefinitionData"; 1981 } 1982 #define FLAG(fn, name) \ 1983 if (D->fn()) \ 1984 OS << " " #name; 1985 FLAG(isParsingBaseSpecifiers, parsing_base_specifiers); 1986 1987 FLAG(isGenericLambda, generic); 1988 FLAG(isLambda, lambda); 1989 1990 FLAG(isAnonymousStructOrUnion, is_anonymous); 1991 FLAG(canPassInRegisters, pass_in_registers); 1992 FLAG(isEmpty, empty); 1993 FLAG(isAggregate, aggregate); 1994 FLAG(isStandardLayout, standard_layout); 1995 FLAG(isTriviallyCopyable, trivially_copyable); 1996 FLAG(isPOD, pod); 1997 FLAG(isTrivial, trivial); 1998 FLAG(isPolymorphic, polymorphic); 1999 FLAG(isAbstract, abstract); 2000 FLAG(isLiteral, literal); 2001 2002 FLAG(hasUserDeclaredConstructor, has_user_declared_ctor); 2003 FLAG(hasConstexprNonCopyMoveConstructor, has_constexpr_non_copy_move_ctor); 2004 FLAG(hasMutableFields, has_mutable_fields); 2005 FLAG(hasVariantMembers, has_variant_members); 2006 FLAG(allowConstDefaultInit, can_const_default_init); 2007 2008 AddChild([=] { 2009 { 2010 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2011 OS << "DefaultConstructor"; 2012 } 2013 FLAG(hasDefaultConstructor, exists); 2014 FLAG(hasTrivialDefaultConstructor, trivial); 2015 FLAG(hasNonTrivialDefaultConstructor, non_trivial); 2016 FLAG(hasUserProvidedDefaultConstructor, user_provided); 2017 FLAG(hasConstexprDefaultConstructor, constexpr); 2018 FLAG(needsImplicitDefaultConstructor, needs_implicit); 2019 FLAG(defaultedDefaultConstructorIsConstexpr, defaulted_is_constexpr); 2020 }); 2021 2022 AddChild([=] { 2023 { 2024 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2025 OS << "CopyConstructor"; 2026 } 2027 FLAG(hasSimpleCopyConstructor, simple); 2028 FLAG(hasTrivialCopyConstructor, trivial); 2029 FLAG(hasNonTrivialCopyConstructor, non_trivial); 2030 FLAG(hasUserDeclaredCopyConstructor, user_declared); 2031 FLAG(hasCopyConstructorWithConstParam, has_const_param); 2032 FLAG(needsImplicitCopyConstructor, needs_implicit); 2033 FLAG(needsOverloadResolutionForCopyConstructor, 2034 needs_overload_resolution); 2035 if (!D->needsOverloadResolutionForCopyConstructor()) 2036 FLAG(defaultedCopyConstructorIsDeleted, defaulted_is_deleted); 2037 FLAG(implicitCopyConstructorHasConstParam, implicit_has_const_param); 2038 }); 2039 2040 AddChild([=] { 2041 { 2042 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2043 OS << "MoveConstructor"; 2044 } 2045 FLAG(hasMoveConstructor, exists); 2046 FLAG(hasSimpleMoveConstructor, simple); 2047 FLAG(hasTrivialMoveConstructor, trivial); 2048 FLAG(hasNonTrivialMoveConstructor, non_trivial); 2049 FLAG(hasUserDeclaredMoveConstructor, user_declared); 2050 FLAG(needsImplicitMoveConstructor, needs_implicit); 2051 FLAG(needsOverloadResolutionForMoveConstructor, 2052 needs_overload_resolution); 2053 if (!D->needsOverloadResolutionForMoveConstructor()) 2054 FLAG(defaultedMoveConstructorIsDeleted, defaulted_is_deleted); 2055 }); 2056 2057 AddChild([=] { 2058 { 2059 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2060 OS << "CopyAssignment"; 2061 } 2062 FLAG(hasSimpleCopyAssignment, simple); 2063 FLAG(hasTrivialCopyAssignment, trivial); 2064 FLAG(hasNonTrivialCopyAssignment, non_trivial); 2065 FLAG(hasCopyAssignmentWithConstParam, has_const_param); 2066 FLAG(hasUserDeclaredCopyAssignment, user_declared); 2067 FLAG(needsImplicitCopyAssignment, needs_implicit); 2068 FLAG(needsOverloadResolutionForCopyAssignment, needs_overload_resolution); 2069 FLAG(implicitCopyAssignmentHasConstParam, implicit_has_const_param); 2070 }); 2071 2072 AddChild([=] { 2073 { 2074 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2075 OS << "MoveAssignment"; 2076 } 2077 FLAG(hasMoveAssignment, exists); 2078 FLAG(hasSimpleMoveAssignment, simple); 2079 FLAG(hasTrivialMoveAssignment, trivial); 2080 FLAG(hasNonTrivialMoveAssignment, non_trivial); 2081 FLAG(hasUserDeclaredMoveAssignment, user_declared); 2082 FLAG(needsImplicitMoveAssignment, needs_implicit); 2083 FLAG(needsOverloadResolutionForMoveAssignment, needs_overload_resolution); 2084 }); 2085 2086 AddChild([=] { 2087 { 2088 ColorScope Color(OS, ShowColors, DeclKindNameColor); 2089 OS << "Destructor"; 2090 } 2091 FLAG(hasSimpleDestructor, simple); 2092 FLAG(hasIrrelevantDestructor, irrelevant); 2093 FLAG(hasTrivialDestructor, trivial); 2094 FLAG(hasNonTrivialDestructor, non_trivial); 2095 FLAG(hasUserDeclaredDestructor, user_declared); 2096 FLAG(hasConstexprDestructor, constexpr); 2097 FLAG(needsImplicitDestructor, needs_implicit); 2098 FLAG(needsOverloadResolutionForDestructor, needs_overload_resolution); 2099 if (!D->needsOverloadResolutionForDestructor()) 2100 FLAG(defaultedDestructorIsDeleted, defaulted_is_deleted); 2101 }); 2102 }); 2103 2104 for (const auto &I : D->bases()) { 2105 AddChild([=] { 2106 if (I.isVirtual()) 2107 OS << "virtual "; 2108 dumpAccessSpecifier(I.getAccessSpecifier()); 2109 dumpType(I.getType()); 2110 if (I.isPackExpansion()) 2111 OS << "..."; 2112 }); 2113 } 2114 } 2115 2116 void TextNodeDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 2117 dumpName(D); 2118 } 2119 2120 void TextNodeDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) { 2121 dumpName(D); 2122 } 2123 2124 void TextNodeDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) { 2125 dumpName(D); 2126 } 2127 2128 void TextNodeDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) { 2129 dumpName(D); 2130 } 2131 2132 void TextNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 2133 if (const auto *TC = D->getTypeConstraint()) { 2134 OS << " "; 2135 dumpBareDeclRef(TC->getNamedConcept()); 2136 if (TC->getNamedConcept() != TC->getFoundDecl()) { 2137 OS << " ("; 2138 dumpBareDeclRef(TC->getFoundDecl()); 2139 OS << ")"; 2140 } 2141 } else if (D->wasDeclaredWithTypename()) 2142 OS << " typename"; 2143 else 2144 OS << " class"; 2145 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 2146 if (D->isParameterPack()) 2147 OS << " ..."; 2148 dumpName(D); 2149 } 2150 2151 void TextNodeDumper::VisitNonTypeTemplateParmDecl( 2152 const NonTypeTemplateParmDecl *D) { 2153 dumpType(D->getType()); 2154 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 2155 if (D->isParameterPack()) 2156 OS << " ..."; 2157 dumpName(D); 2158 } 2159 2160 void TextNodeDumper::VisitTemplateTemplateParmDecl( 2161 const TemplateTemplateParmDecl *D) { 2162 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 2163 if (D->isParameterPack()) 2164 OS << " ..."; 2165 dumpName(D); 2166 } 2167 2168 void TextNodeDumper::VisitUsingDecl(const UsingDecl *D) { 2169 OS << ' '; 2170 if (D->getQualifier()) 2171 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 2172 OS << D->getDeclName(); 2173 } 2174 2175 void TextNodeDumper::VisitUsingEnumDecl(const UsingEnumDecl *D) { 2176 OS << ' '; 2177 dumpBareDeclRef(D->getEnumDecl()); 2178 } 2179 2180 void TextNodeDumper::VisitUnresolvedUsingTypenameDecl( 2181 const UnresolvedUsingTypenameDecl *D) { 2182 OS << ' '; 2183 if (D->getQualifier()) 2184 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 2185 OS << D->getDeclName(); 2186 } 2187 2188 void TextNodeDumper::VisitUnresolvedUsingValueDecl( 2189 const UnresolvedUsingValueDecl *D) { 2190 OS << ' '; 2191 if (D->getQualifier()) 2192 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 2193 OS << D->getDeclName(); 2194 dumpType(D->getType()); 2195 } 2196 2197 void TextNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) { 2198 OS << ' '; 2199 dumpBareDeclRef(D->getTargetDecl()); 2200 } 2201 2202 void TextNodeDumper::VisitConstructorUsingShadowDecl( 2203 const ConstructorUsingShadowDecl *D) { 2204 if (D->constructsVirtualBase()) 2205 OS << " virtual"; 2206 2207 AddChild([=] { 2208 OS << "target "; 2209 dumpBareDeclRef(D->getTargetDecl()); 2210 }); 2211 2212 AddChild([=] { 2213 OS << "nominated "; 2214 dumpBareDeclRef(D->getNominatedBaseClass()); 2215 OS << ' '; 2216 dumpBareDeclRef(D->getNominatedBaseClassShadowDecl()); 2217 }); 2218 2219 AddChild([=] { 2220 OS << "constructed "; 2221 dumpBareDeclRef(D->getConstructedBaseClass()); 2222 OS << ' '; 2223 dumpBareDeclRef(D->getConstructedBaseClassShadowDecl()); 2224 }); 2225 } 2226 2227 void TextNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) { 2228 switch (D->getLanguage()) { 2229 case LinkageSpecDecl::lang_c: 2230 OS << " C"; 2231 break; 2232 case LinkageSpecDecl::lang_cxx: 2233 OS << " C++"; 2234 break; 2235 } 2236 } 2237 2238 void TextNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) { 2239 OS << ' '; 2240 dumpAccessSpecifier(D->getAccess()); 2241 } 2242 2243 void TextNodeDumper::VisitFriendDecl(const FriendDecl *D) { 2244 if (TypeSourceInfo *T = D->getFriendType()) 2245 dumpType(T->getType()); 2246 } 2247 2248 void TextNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) { 2249 dumpName(D); 2250 dumpType(D->getType()); 2251 if (D->getSynthesize()) 2252 OS << " synthesize"; 2253 2254 switch (D->getAccessControl()) { 2255 case ObjCIvarDecl::None: 2256 OS << " none"; 2257 break; 2258 case ObjCIvarDecl::Private: 2259 OS << " private"; 2260 break; 2261 case ObjCIvarDecl::Protected: 2262 OS << " protected"; 2263 break; 2264 case ObjCIvarDecl::Public: 2265 OS << " public"; 2266 break; 2267 case ObjCIvarDecl::Package: 2268 OS << " package"; 2269 break; 2270 } 2271 } 2272 2273 void TextNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 2274 if (D->isInstanceMethod()) 2275 OS << " -"; 2276 else 2277 OS << " +"; 2278 dumpName(D); 2279 dumpType(D->getReturnType()); 2280 2281 if (D->isVariadic()) 2282 OS << " variadic"; 2283 } 2284 2285 void TextNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) { 2286 dumpName(D); 2287 switch (D->getVariance()) { 2288 case ObjCTypeParamVariance::Invariant: 2289 break; 2290 2291 case ObjCTypeParamVariance::Covariant: 2292 OS << " covariant"; 2293 break; 2294 2295 case ObjCTypeParamVariance::Contravariant: 2296 OS << " contravariant"; 2297 break; 2298 } 2299 2300 if (D->hasExplicitBound()) 2301 OS << " bounded"; 2302 dumpType(D->getUnderlyingType()); 2303 } 2304 2305 void TextNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) { 2306 dumpName(D); 2307 dumpDeclRef(D->getClassInterface()); 2308 dumpDeclRef(D->getImplementation()); 2309 for (const auto *P : D->protocols()) 2310 dumpDeclRef(P); 2311 } 2312 2313 void TextNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) { 2314 dumpName(D); 2315 dumpDeclRef(D->getClassInterface()); 2316 dumpDeclRef(D->getCategoryDecl()); 2317 } 2318 2319 void TextNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) { 2320 dumpName(D); 2321 2322 for (const auto *Child : D->protocols()) 2323 dumpDeclRef(Child); 2324 } 2325 2326 void TextNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) { 2327 dumpName(D); 2328 dumpDeclRef(D->getSuperClass(), "super"); 2329 2330 dumpDeclRef(D->getImplementation()); 2331 for (const auto *Child : D->protocols()) 2332 dumpDeclRef(Child); 2333 } 2334 2335 void TextNodeDumper::VisitObjCImplementationDecl( 2336 const ObjCImplementationDecl *D) { 2337 dumpName(D); 2338 dumpDeclRef(D->getSuperClass(), "super"); 2339 dumpDeclRef(D->getClassInterface()); 2340 } 2341 2342 void TextNodeDumper::VisitObjCCompatibleAliasDecl( 2343 const ObjCCompatibleAliasDecl *D) { 2344 dumpName(D); 2345 dumpDeclRef(D->getClassInterface()); 2346 } 2347 2348 void TextNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 2349 dumpName(D); 2350 dumpType(D->getType()); 2351 2352 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required) 2353 OS << " required"; 2354 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional) 2355 OS << " optional"; 2356 2357 ObjCPropertyAttribute::Kind Attrs = D->getPropertyAttributes(); 2358 if (Attrs != ObjCPropertyAttribute::kind_noattr) { 2359 if (Attrs & ObjCPropertyAttribute::kind_readonly) 2360 OS << " readonly"; 2361 if (Attrs & ObjCPropertyAttribute::kind_assign) 2362 OS << " assign"; 2363 if (Attrs & ObjCPropertyAttribute::kind_readwrite) 2364 OS << " readwrite"; 2365 if (Attrs & ObjCPropertyAttribute::kind_retain) 2366 OS << " retain"; 2367 if (Attrs & ObjCPropertyAttribute::kind_copy) 2368 OS << " copy"; 2369 if (Attrs & ObjCPropertyAttribute::kind_nonatomic) 2370 OS << " nonatomic"; 2371 if (Attrs & ObjCPropertyAttribute::kind_atomic) 2372 OS << " atomic"; 2373 if (Attrs & ObjCPropertyAttribute::kind_weak) 2374 OS << " weak"; 2375 if (Attrs & ObjCPropertyAttribute::kind_strong) 2376 OS << " strong"; 2377 if (Attrs & ObjCPropertyAttribute::kind_unsafe_unretained) 2378 OS << " unsafe_unretained"; 2379 if (Attrs & ObjCPropertyAttribute::kind_class) 2380 OS << " class"; 2381 if (Attrs & ObjCPropertyAttribute::kind_direct) 2382 OS << " direct"; 2383 if (Attrs & ObjCPropertyAttribute::kind_getter) 2384 dumpDeclRef(D->getGetterMethodDecl(), "getter"); 2385 if (Attrs & ObjCPropertyAttribute::kind_setter) 2386 dumpDeclRef(D->getSetterMethodDecl(), "setter"); 2387 } 2388 } 2389 2390 void TextNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 2391 dumpName(D->getPropertyDecl()); 2392 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 2393 OS << " synthesize"; 2394 else 2395 OS << " dynamic"; 2396 dumpDeclRef(D->getPropertyDecl()); 2397 dumpDeclRef(D->getPropertyIvarDecl()); 2398 } 2399 2400 void TextNodeDumper::VisitBlockDecl(const BlockDecl *D) { 2401 if (D->isVariadic()) 2402 OS << " variadic"; 2403 2404 if (D->capturesCXXThis()) 2405 OS << " captures_this"; 2406 } 2407 2408 void TextNodeDumper::VisitConceptDecl(const ConceptDecl *D) { 2409 dumpName(D); 2410 } 2411 2412 void TextNodeDumper::VisitCompoundStmt(const CompoundStmt *S) { 2413 VisitStmt(S); 2414 if (S->hasStoredFPFeatures()) 2415 printFPOptions(S->getStoredFPFeatures()); 2416 } 2417 2418 void TextNodeDumper::VisitHLSLBufferDecl(const HLSLBufferDecl *D) { 2419 if (D->isCBuffer()) 2420 OS << " cbuffer"; 2421 else 2422 OS << " tbuffer"; 2423 dumpName(D); 2424 } 2425