Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:44

0001 //===--- TextNodeDumper.h - Printing of AST nodes -------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements AST dumping of components of individual AST nodes.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_AST_TEXTNODEDUMPER_H
0014 #define LLVM_CLANG_AST_TEXTNODEDUMPER_H
0015 
0016 #include "clang/AST/ASTContext.h"
0017 #include "clang/AST/ASTDumperUtils.h"
0018 #include "clang/AST/AttrVisitor.h"
0019 #include "clang/AST/CommentCommandTraits.h"
0020 #include "clang/AST/CommentVisitor.h"
0021 #include "clang/AST/DeclVisitor.h"
0022 #include "clang/AST/ExprConcepts.h"
0023 #include "clang/AST/ExprCXX.h"
0024 #include "clang/AST/StmtVisitor.h"
0025 #include "clang/AST/TemplateArgumentVisitor.h"
0026 #include "clang/AST/Type.h"
0027 #include "clang/AST/TypeLocVisitor.h"
0028 #include "clang/AST/TypeVisitor.h"
0029 
0030 namespace clang {
0031 
0032 class APValue;
0033 
0034 class TextTreeStructure {
0035   raw_ostream &OS;
0036   const bool ShowColors;
0037 
0038   /// Pending[i] is an action to dump an entity at level i.
0039   llvm::SmallVector<std::function<void(bool IsLastChild)>, 32> Pending;
0040 
0041   /// Indicates whether we're at the top level.
0042   bool TopLevel = true;
0043 
0044   /// Indicates if we're handling the first child after entering a new depth.
0045   bool FirstChild = true;
0046 
0047   /// Prefix for currently-being-dumped entity.
0048   std::string Prefix;
0049 
0050 public:
0051   /// Add a child of the current node.  Calls DoAddChild without arguments
0052   template <typename Fn> void AddChild(Fn DoAddChild) {
0053     return AddChild("", DoAddChild);
0054   }
0055 
0056   /// Add a child of the current node with an optional label.
0057   /// Calls DoAddChild without arguments.
0058   template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) {
0059     // If we're at the top level, there's nothing interesting to do; just
0060     // run the dumper.
0061     if (TopLevel) {
0062       TopLevel = false;
0063       DoAddChild();
0064       while (!Pending.empty()) {
0065         Pending.back()(true);
0066         Pending.pop_back();
0067       }
0068       Prefix.clear();
0069       OS << "\n";
0070       TopLevel = true;
0071       return;
0072     }
0073 
0074     auto DumpWithIndent = [this, DoAddChild,
0075                            Label(Label.str())](bool IsLastChild) {
0076       // Print out the appropriate tree structure and work out the prefix for
0077       // children of this node. For instance:
0078       //
0079       //   A        Prefix = ""
0080       //   |-B      Prefix = "| "
0081       //   | `-C    Prefix = "|   "
0082       //   `-D      Prefix = "  "
0083       //     |-E    Prefix = "  | "
0084       //     `-F    Prefix = "    "
0085       //   G        Prefix = ""
0086       //
0087       // Note that the first level gets no prefix.
0088       {
0089         OS << '\n';
0090         ColorScope Color(OS, ShowColors, IndentColor);
0091         OS << Prefix << (IsLastChild ? '`' : '|') << '-';
0092         if (!Label.empty())
0093           OS << Label << ": ";
0094 
0095         this->Prefix.push_back(IsLastChild ? ' ' : '|');
0096         this->Prefix.push_back(' ');
0097       }
0098 
0099       FirstChild = true;
0100       unsigned Depth = Pending.size();
0101 
0102       DoAddChild();
0103 
0104       // If any children are left, they're the last at their nesting level.
0105       // Dump those ones out now.
0106       while (Depth < Pending.size()) {
0107         Pending.back()(true);
0108         this->Pending.pop_back();
0109       }
0110 
0111       // Restore the old prefix.
0112       this->Prefix.resize(Prefix.size() - 2);
0113     };
0114 
0115     if (FirstChild) {
0116       Pending.push_back(std::move(DumpWithIndent));
0117     } else {
0118       Pending.back()(false);
0119       Pending.back() = std::move(DumpWithIndent);
0120     }
0121     FirstChild = false;
0122   }
0123 
0124   TextTreeStructure(raw_ostream &OS, bool ShowColors)
0125       : OS(OS), ShowColors(ShowColors) {}
0126 };
0127 
0128 class TextNodeDumper
0129     : public TextTreeStructure,
0130       public comments::ConstCommentVisitor<TextNodeDumper, void,
0131                                            const comments::FullComment *>,
0132       public ConstAttrVisitor<TextNodeDumper>,
0133       public ConstTemplateArgumentVisitor<TextNodeDumper>,
0134       public ConstStmtVisitor<TextNodeDumper>,
0135       public TypeVisitor<TextNodeDumper>,
0136       public TypeLocVisitor<TextNodeDumper>,
0137       public ConstDeclVisitor<TextNodeDumper> {
0138   raw_ostream &OS;
0139   const bool ShowColors;
0140 
0141   /// Keep track of the last location we print out so that we can
0142   /// print out deltas from then on out.
0143   const char *LastLocFilename = "";
0144   unsigned LastLocLine = ~0U;
0145 
0146   /// \p Context, \p SM, and \p Traits can be null. This is because we want
0147   /// to be able to call \p dump() in a debugger without having to pass the
0148   /// \p ASTContext to \p dump. Not all parts of the AST dump output will be
0149   /// available without the \p ASTContext.
0150   const ASTContext *Context = nullptr;
0151   const SourceManager *SM = nullptr;
0152 
0153   /// The policy to use for printing; can be defaulted.
0154   PrintingPolicy PrintPolicy = LangOptions();
0155 
0156   const comments::CommandTraits *Traits = nullptr;
0157 
0158   const char *getCommandName(unsigned CommandID);
0159   void printFPOptions(FPOptionsOverride FPO);
0160 
0161   void dumpAPValueChildren(const APValue &Value, QualType Ty,
0162                            const APValue &(*IdxToChildFun)(const APValue &,
0163                                                            unsigned),
0164                            unsigned NumChildren, StringRef LabelSingular,
0165                            StringRef LabelPlurial);
0166 
0167 public:
0168   TextNodeDumper(raw_ostream &OS, const ASTContext &Context, bool ShowColors);
0169   TextNodeDumper(raw_ostream &OS, bool ShowColors);
0170 
0171   void Visit(const comments::Comment *C, const comments::FullComment *FC);
0172 
0173   void Visit(const Attr *A);
0174 
0175   void Visit(const TemplateArgument &TA, SourceRange R,
0176              const Decl *From = nullptr, StringRef Label = {});
0177 
0178   void Visit(const Stmt *Node);
0179 
0180   void Visit(const Type *T);
0181 
0182   void Visit(QualType T);
0183 
0184   void Visit(TypeLoc);
0185 
0186   void Visit(const Decl *D);
0187 
0188   void Visit(const CXXCtorInitializer *Init);
0189 
0190   void Visit(const OMPClause *C);
0191 
0192   void Visit(const OpenACCClause *C);
0193 
0194   void Visit(const BlockDecl::Capture &C);
0195 
0196   void Visit(const GenericSelectionExpr::ConstAssociation &A);
0197 
0198   void Visit(const ConceptReference *);
0199 
0200   void Visit(const concepts::Requirement *R);
0201 
0202   void Visit(const APValue &Value, QualType Ty);
0203 
0204   void dumpPointer(const void *Ptr);
0205   void dumpLocation(SourceLocation Loc);
0206   void dumpSourceRange(SourceRange R);
0207   void dumpBareType(QualType T, bool Desugar = true);
0208   void dumpType(QualType T);
0209   void dumpBareDeclRef(const Decl *D);
0210   void dumpName(const NamedDecl *ND);
0211   void dumpAccessSpecifier(AccessSpecifier AS);
0212   void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C);
0213   void dumpTemplateSpecializationKind(TemplateSpecializationKind TSK);
0214   void dumpNestedNameSpecifier(const NestedNameSpecifier *NNS);
0215   void dumpConceptReference(const ConceptReference *R);
0216   void dumpTemplateArgument(const TemplateArgument &TA);
0217   void dumpBareTemplateName(TemplateName TN);
0218   void dumpTemplateName(TemplateName TN, StringRef Label = {});
0219 
0220   void dumpDeclRef(const Decl *D, StringRef Label = {});
0221 
0222   void visitTextComment(const comments::TextComment *C,
0223                         const comments::FullComment *);
0224   void visitInlineCommandComment(const comments::InlineCommandComment *C,
0225                                  const comments::FullComment *);
0226   void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
0227                                 const comments::FullComment *);
0228   void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
0229                               const comments::FullComment *);
0230   void visitBlockCommandComment(const comments::BlockCommandComment *C,
0231                                 const comments::FullComment *);
0232   void visitParamCommandComment(const comments::ParamCommandComment *C,
0233                                 const comments::FullComment *FC);
0234   void visitTParamCommandComment(const comments::TParamCommandComment *C,
0235                                  const comments::FullComment *FC);
0236   void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
0237                                  const comments::FullComment *);
0238   void
0239   visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
0240                                 const comments::FullComment *);
0241   void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
0242                                 const comments::FullComment *);
0243 
0244 // Implements Visit methods for Attrs.
0245 #include "clang/AST/AttrTextNodeDump.inc"
0246 
0247   void VisitNullTemplateArgument(const TemplateArgument &TA);
0248   void VisitTypeTemplateArgument(const TemplateArgument &TA);
0249   void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
0250   void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
0251   void VisitIntegralTemplateArgument(const TemplateArgument &TA);
0252   void VisitTemplateTemplateArgument(const TemplateArgument &TA);
0253   void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
0254   void VisitExpressionTemplateArgument(const TemplateArgument &TA);
0255   void VisitPackTemplateArgument(const TemplateArgument &TA);
0256 
0257   void VisitIfStmt(const IfStmt *Node);
0258   void VisitSwitchStmt(const SwitchStmt *Node);
0259   void VisitWhileStmt(const WhileStmt *Node);
0260   void VisitLabelStmt(const LabelStmt *Node);
0261   void VisitGotoStmt(const GotoStmt *Node);
0262   void VisitCaseStmt(const CaseStmt *Node);
0263   void VisitReturnStmt(const ReturnStmt *Node);
0264   void VisitCoawaitExpr(const CoawaitExpr *Node);
0265   void VisitCoreturnStmt(const CoreturnStmt *Node);
0266   void VisitCompoundStmt(const CompoundStmt *Node);
0267   void VisitConstantExpr(const ConstantExpr *Node);
0268   void VisitCallExpr(const CallExpr *Node);
0269   void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node);
0270   void VisitCastExpr(const CastExpr *Node);
0271   void VisitImplicitCastExpr(const ImplicitCastExpr *Node);
0272   void VisitDeclRefExpr(const DeclRefExpr *Node);
0273   void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *Node);
0274   void VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *Node);
0275   void VisitPredefinedExpr(const PredefinedExpr *Node);
0276   void VisitCharacterLiteral(const CharacterLiteral *Node);
0277   void VisitIntegerLiteral(const IntegerLiteral *Node);
0278   void VisitFixedPointLiteral(const FixedPointLiteral *Node);
0279   void VisitFloatingLiteral(const FloatingLiteral *Node);
0280   void VisitStringLiteral(const StringLiteral *Str);
0281   void VisitInitListExpr(const InitListExpr *ILE);
0282   void VisitGenericSelectionExpr(const GenericSelectionExpr *E);
0283   void VisitUnaryOperator(const UnaryOperator *Node);
0284   void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
0285   void VisitMemberExpr(const MemberExpr *Node);
0286   void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
0287   void VisitBinaryOperator(const BinaryOperator *Node);
0288   void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
0289   void VisitAddrLabelExpr(const AddrLabelExpr *Node);
0290   void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
0291   void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
0292   void VisitCXXThisExpr(const CXXThisExpr *Node);
0293   void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
0294   void VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node);
0295   void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
0296   void VisitCXXConstructExpr(const CXXConstructExpr *Node);
0297   void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
0298   void VisitCXXNewExpr(const CXXNewExpr *Node);
0299   void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
0300   void VisitTypeTraitExpr(const TypeTraitExpr *Node);
0301   void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node);
0302   void VisitExpressionTraitExpr(const ExpressionTraitExpr *Node);
0303   void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *Node);
0304   void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *Node);
0305   void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
0306   void VisitExprWithCleanups(const ExprWithCleanups *Node);
0307   void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
0308   void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
0309   void
0310   VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
0311   void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
0312   void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
0313   void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
0314   void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
0315   void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
0316   void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
0317   void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
0318   void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
0319   void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
0320   void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
0321   void VisitOMPIteratorExpr(const OMPIteratorExpr *Node);
0322   void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *Node);
0323   void VisitRequiresExpr(const RequiresExpr *Node);
0324 
0325   void VisitRValueReferenceType(const ReferenceType *T);
0326   void VisitArrayType(const ArrayType *T);
0327   void VisitConstantArrayType(const ConstantArrayType *T);
0328   void VisitVariableArrayType(const VariableArrayType *T);
0329   void VisitDependentSizedArrayType(const DependentSizedArrayType *T);
0330   void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
0331   void VisitVectorType(const VectorType *T);
0332   void VisitFunctionType(const FunctionType *T);
0333   void VisitFunctionProtoType(const FunctionProtoType *T);
0334   void VisitUnresolvedUsingType(const UnresolvedUsingType *T);
0335   void VisitUsingType(const UsingType *T);
0336   void VisitTypedefType(const TypedefType *T);
0337   void VisitUnaryTransformType(const UnaryTransformType *T);
0338   void VisitTagType(const TagType *T);
0339   void VisitTemplateTypeParmType(const TemplateTypeParmType *T);
0340   void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
0341   void
0342   VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T);
0343   void VisitAutoType(const AutoType *T);
0344   void VisitDeducedTemplateSpecializationType(
0345       const DeducedTemplateSpecializationType *T);
0346   void VisitTemplateSpecializationType(const TemplateSpecializationType *T);
0347   void VisitInjectedClassNameType(const InjectedClassNameType *T);
0348   void VisitObjCInterfaceType(const ObjCInterfaceType *T);
0349   void VisitPackExpansionType(const PackExpansionType *T);
0350 
0351   void VisitTypeLoc(TypeLoc TL);
0352 
0353   void VisitLabelDecl(const LabelDecl *D);
0354   void VisitTypedefDecl(const TypedefDecl *D);
0355   void VisitEnumDecl(const EnumDecl *D);
0356   void VisitRecordDecl(const RecordDecl *D);
0357   void VisitEnumConstantDecl(const EnumConstantDecl *D);
0358   void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
0359   void VisitFunctionDecl(const FunctionDecl *D);
0360   void VisitCXXDeductionGuideDecl(const CXXDeductionGuideDecl *D);
0361   void VisitFieldDecl(const FieldDecl *D);
0362   void VisitVarDecl(const VarDecl *D);
0363   void VisitBindingDecl(const BindingDecl *D);
0364   void VisitCapturedDecl(const CapturedDecl *D);
0365   void VisitImportDecl(const ImportDecl *D);
0366   void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
0367   void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
0368   void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
0369   void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
0370   void VisitOMPRequiresDecl(const OMPRequiresDecl *D);
0371   void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
0372   void VisitNamespaceDecl(const NamespaceDecl *D);
0373   void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
0374   void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
0375   void VisitTypeAliasDecl(const TypeAliasDecl *D);
0376   void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
0377   void VisitCXXRecordDecl(const CXXRecordDecl *D);
0378   void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
0379   void VisitClassTemplateDecl(const ClassTemplateDecl *D);
0380   void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
0381   void VisitVarTemplateDecl(const VarTemplateDecl *D);
0382   void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
0383   void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
0384   void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
0385   void VisitUsingDecl(const UsingDecl *D);
0386   void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
0387   void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
0388   void VisitUsingEnumDecl(const UsingEnumDecl *D);
0389   void VisitUsingShadowDecl(const UsingShadowDecl *D);
0390   void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
0391   void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
0392   void VisitAccessSpecDecl(const AccessSpecDecl *D);
0393   void VisitFriendDecl(const FriendDecl *D);
0394   void VisitObjCIvarDecl(const ObjCIvarDecl *D);
0395   void VisitObjCMethodDecl(const ObjCMethodDecl *D);
0396   void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
0397   void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
0398   void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
0399   void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
0400   void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
0401   void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
0402   void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
0403   void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
0404   void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
0405   void VisitBlockDecl(const BlockDecl *D);
0406   void VisitConceptDecl(const ConceptDecl *D);
0407   void
0408   VisitLifetimeExtendedTemporaryDecl(const LifetimeExtendedTemporaryDecl *D);
0409   void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
0410   void VisitHLSLOutArgExpr(const HLSLOutArgExpr *E);
0411   void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
0412   void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *S);
0413   void VisitOpenACCCombinedConstruct(const OpenACCCombinedConstruct *S);
0414   void VisitOpenACCDataConstruct(const OpenACCDataConstruct *S);
0415   void VisitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct *S);
0416   void VisitOpenACCExitDataConstruct(const OpenACCExitDataConstruct *S);
0417   void VisitOpenACCHostDataConstruct(const OpenACCHostDataConstruct *S);
0418   void VisitOpenACCWaitConstruct(const OpenACCWaitConstruct *S);
0419   void VisitOpenACCInitConstruct(const OpenACCInitConstruct *S);
0420   void VisitOpenACCSetConstruct(const OpenACCSetConstruct *S);
0421   void VisitOpenACCShutdownConstruct(const OpenACCShutdownConstruct *S);
0422   void VisitOpenACCUpdateConstruct(const OpenACCUpdateConstruct *S);
0423   void VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *S);
0424   void VisitEmbedExpr(const EmbedExpr *S);
0425   void VisitAtomicExpr(const AtomicExpr *AE);
0426 };
0427 
0428 } // namespace clang
0429 
0430 #endif // LLVM_CLANG_AST_TEXTNODEDUMPER_H