1 //===--- TextNodeDumper.h - 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 #ifndef LLVM_CLANG_AST_TEXTNODEDUMPER_H 14 #define LLVM_CLANG_AST_TEXTNODEDUMPER_H 15 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTDumperUtils.h" 18 #include "clang/AST/AttrVisitor.h" 19 #include "clang/AST/CommentCommandTraits.h" 20 #include "clang/AST/CommentVisitor.h" 21 #include "clang/AST/DeclVisitor.h" 22 #include "clang/AST/ExprCXX.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/AST/TemplateArgumentVisitor.h" 25 #include "clang/AST/Type.h" 26 #include "clang/AST/TypeVisitor.h" 27 28 namespace clang { 29 30 class APValue; 31 32 class TextTreeStructure { 33 raw_ostream &OS; 34 const bool ShowColors; 35 36 /// Pending[i] is an action to dump an entity at level i. 37 llvm::SmallVector<std::function<void(bool IsLastChild)>, 32> Pending; 38 39 /// Indicates whether we're at the top level. 40 bool TopLevel = true; 41 42 /// Indicates if we're handling the first child after entering a new depth. 43 bool FirstChild = true; 44 45 /// Prefix for currently-being-dumped entity. 46 std::string Prefix; 47 48 public: 49 /// Add a child of the current node. Calls DoAddChild without arguments 50 template <typename Fn> void AddChild(Fn DoAddChild) { 51 return AddChild("", DoAddChild); 52 } 53 54 /// Add a child of the current node with an optional label. 55 /// Calls DoAddChild without arguments. 56 template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) { 57 // If we're at the top level, there's nothing interesting to do; just 58 // run the dumper. 59 if (TopLevel) { 60 TopLevel = false; 61 DoAddChild(); 62 while (!Pending.empty()) { 63 Pending.back()(true); 64 Pending.pop_back(); 65 } 66 Prefix.clear(); 67 OS << "\n"; 68 TopLevel = true; 69 return; 70 } 71 72 // We need to capture an owning-string in the lambda because the lambda 73 // is invoked in a deferred manner. 74 std::string LabelStr(Label); 75 auto DumpWithIndent = [this, DoAddChild, LabelStr](bool IsLastChild) { 76 // Print out the appropriate tree structure and work out the prefix for 77 // children of this node. For instance: 78 // 79 // A Prefix = "" 80 // |-B Prefix = "| " 81 // | `-C Prefix = "| " 82 // `-D Prefix = " " 83 // |-E Prefix = " | " 84 // `-F Prefix = " " 85 // G Prefix = "" 86 // 87 // Note that the first level gets no prefix. 88 { 89 OS << '\n'; 90 ColorScope Color(OS, ShowColors, IndentColor); 91 OS << Prefix << (IsLastChild ? '`' : '|') << '-'; 92 if (!LabelStr.empty()) 93 OS << LabelStr << ": "; 94 95 this->Prefix.push_back(IsLastChild ? ' ' : '|'); 96 this->Prefix.push_back(' '); 97 } 98 99 FirstChild = true; 100 unsigned Depth = Pending.size(); 101 102 DoAddChild(); 103 104 // If any children are left, they're the last at their nesting level. 105 // Dump those ones out now. 106 while (Depth < Pending.size()) { 107 Pending.back()(true); 108 this->Pending.pop_back(); 109 } 110 111 // Restore the old prefix. 112 this->Prefix.resize(Prefix.size() - 2); 113 }; 114 115 if (FirstChild) { 116 Pending.push_back(std::move(DumpWithIndent)); 117 } else { 118 Pending.back()(false); 119 Pending.back() = std::move(DumpWithIndent); 120 } 121 FirstChild = false; 122 } 123 124 TextTreeStructure(raw_ostream &OS, bool ShowColors) 125 : OS(OS), ShowColors(ShowColors) {} 126 }; 127 128 class TextNodeDumper 129 : public TextTreeStructure, 130 public comments::ConstCommentVisitor<TextNodeDumper, void, 131 const comments::FullComment *>, 132 public ConstAttrVisitor<TextNodeDumper>, 133 public ConstTemplateArgumentVisitor<TextNodeDumper>, 134 public ConstStmtVisitor<TextNodeDumper>, 135 public TypeVisitor<TextNodeDumper>, 136 public ConstDeclVisitor<TextNodeDumper> { 137 raw_ostream &OS; 138 const bool ShowColors; 139 140 /// Keep track of the last location we print out so that we can 141 /// print out deltas from then on out. 142 const char *LastLocFilename = ""; 143 unsigned LastLocLine = ~0U; 144 145 /// \p Context, \p SM, and \p Traits can be null. This is because we want 146 /// to be able to call \p dump() in a debugger without having to pass the 147 /// \p ASTContext to \p dump. Not all parts of the AST dump output will be 148 /// available without the \p ASTContext. 149 const ASTContext *Context = nullptr; 150 const SourceManager *SM = nullptr; 151 152 /// The policy to use for printing; can be defaulted. 153 PrintingPolicy PrintPolicy = LangOptions(); 154 155 const comments::CommandTraits *Traits = nullptr; 156 157 const char *getCommandName(unsigned CommandID); 158 void printFPOptions(FPOptionsOverride FPO); 159 160 void dumpAPValueChildren(const APValue &Value, QualType Ty, 161 const APValue &(*IdxToChildFun)(const APValue &, 162 unsigned), 163 unsigned NumChildren, StringRef LabelSingular, 164 StringRef LabelPlurial); 165 166 public: 167 TextNodeDumper(raw_ostream &OS, const ASTContext &Context, bool ShowColors); 168 TextNodeDumper(raw_ostream &OS, bool ShowColors); 169 170 void Visit(const comments::Comment *C, const comments::FullComment *FC); 171 172 void Visit(const Attr *A); 173 174 void Visit(const TemplateArgument &TA, SourceRange R, 175 const Decl *From = nullptr, StringRef Label = {}); 176 177 void Visit(const Stmt *Node); 178 179 void Visit(const Type *T); 180 181 void Visit(QualType T); 182 183 void Visit(const Decl *D); 184 185 void Visit(const CXXCtorInitializer *Init); 186 187 void Visit(const OMPClause *C); 188 189 void Visit(const BlockDecl::Capture &C); 190 191 void Visit(const GenericSelectionExpr::ConstAssociation &A); 192 193 void Visit(const APValue &Value, QualType Ty); 194 195 void dumpPointer(const void *Ptr); 196 void dumpLocation(SourceLocation Loc); 197 void dumpSourceRange(SourceRange R); 198 void dumpBareType(QualType T, bool Desugar = true); 199 void dumpType(QualType T); 200 void dumpBareDeclRef(const Decl *D); 201 void dumpName(const NamedDecl *ND); 202 void dumpAccessSpecifier(AccessSpecifier AS); 203 void dumpCleanupObject(const ExprWithCleanups::CleanupObject &C); 204 205 void dumpDeclRef(const Decl *D, StringRef Label = {}); 206 207 void visitTextComment(const comments::TextComment *C, 208 const comments::FullComment *); 209 void visitInlineCommandComment(const comments::InlineCommandComment *C, 210 const comments::FullComment *); 211 void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C, 212 const comments::FullComment *); 213 void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C, 214 const comments::FullComment *); 215 void visitBlockCommandComment(const comments::BlockCommandComment *C, 216 const comments::FullComment *); 217 void visitParamCommandComment(const comments::ParamCommandComment *C, 218 const comments::FullComment *FC); 219 void visitTParamCommandComment(const comments::TParamCommandComment *C, 220 const comments::FullComment *FC); 221 void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C, 222 const comments::FullComment *); 223 void 224 visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C, 225 const comments::FullComment *); 226 void visitVerbatimLineComment(const comments::VerbatimLineComment *C, 227 const comments::FullComment *); 228 229 // Implements Visit methods for Attrs. 230 #include "clang/AST/AttrTextNodeDump.inc" 231 232 void VisitNullTemplateArgument(const TemplateArgument &TA); 233 void VisitTypeTemplateArgument(const TemplateArgument &TA); 234 void VisitDeclarationTemplateArgument(const TemplateArgument &TA); 235 void VisitNullPtrTemplateArgument(const TemplateArgument &TA); 236 void VisitIntegralTemplateArgument(const TemplateArgument &TA); 237 void VisitTemplateTemplateArgument(const TemplateArgument &TA); 238 void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA); 239 void VisitExpressionTemplateArgument(const TemplateArgument &TA); 240 void VisitPackTemplateArgument(const TemplateArgument &TA); 241 242 void VisitIfStmt(const IfStmt *Node); 243 void VisitSwitchStmt(const SwitchStmt *Node); 244 void VisitWhileStmt(const WhileStmt *Node); 245 void VisitLabelStmt(const LabelStmt *Node); 246 void VisitGotoStmt(const GotoStmt *Node); 247 void VisitCaseStmt(const CaseStmt *Node); 248 void VisitConstantExpr(const ConstantExpr *Node); 249 void VisitCallExpr(const CallExpr *Node); 250 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node); 251 void VisitCastExpr(const CastExpr *Node); 252 void VisitImplicitCastExpr(const ImplicitCastExpr *Node); 253 void VisitDeclRefExpr(const DeclRefExpr *Node); 254 void VisitPredefinedExpr(const PredefinedExpr *Node); 255 void VisitCharacterLiteral(const CharacterLiteral *Node); 256 void VisitIntegerLiteral(const IntegerLiteral *Node); 257 void VisitFixedPointLiteral(const FixedPointLiteral *Node); 258 void VisitFloatingLiteral(const FloatingLiteral *Node); 259 void VisitStringLiteral(const StringLiteral *Str); 260 void VisitInitListExpr(const InitListExpr *ILE); 261 void VisitGenericSelectionExpr(const GenericSelectionExpr *E); 262 void VisitUnaryOperator(const UnaryOperator *Node); 263 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node); 264 void VisitMemberExpr(const MemberExpr *Node); 265 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node); 266 void VisitBinaryOperator(const BinaryOperator *Node); 267 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node); 268 void VisitAddrLabelExpr(const AddrLabelExpr *Node); 269 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node); 270 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node); 271 void VisitCXXThisExpr(const CXXThisExpr *Node); 272 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node); 273 void VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node); 274 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node); 275 void VisitCXXConstructExpr(const CXXConstructExpr *Node); 276 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node); 277 void VisitCXXNewExpr(const CXXNewExpr *Node); 278 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node); 279 void VisitTypeTraitExpr(const TypeTraitExpr *Node); 280 void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node); 281 void VisitExpressionTraitExpr(const ExpressionTraitExpr *Node); 282 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node); 283 void VisitExprWithCleanups(const ExprWithCleanups *Node); 284 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node); 285 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node); 286 void 287 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node); 288 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node); 289 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node); 290 void VisitObjCMessageExpr(const ObjCMessageExpr *Node); 291 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node); 292 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node); 293 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node); 294 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node); 295 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node); 296 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node); 297 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node); 298 void VisitOMPIteratorExpr(const OMPIteratorExpr *Node); 299 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *Node); 300 301 void VisitRValueReferenceType(const ReferenceType *T); 302 void VisitArrayType(const ArrayType *T); 303 void VisitConstantArrayType(const ConstantArrayType *T); 304 void VisitVariableArrayType(const VariableArrayType *T); 305 void VisitDependentSizedArrayType(const DependentSizedArrayType *T); 306 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T); 307 void VisitVectorType(const VectorType *T); 308 void VisitFunctionType(const FunctionType *T); 309 void VisitFunctionProtoType(const FunctionProtoType *T); 310 void VisitUnresolvedUsingType(const UnresolvedUsingType *T); 311 void VisitTypedefType(const TypedefType *T); 312 void VisitUnaryTransformType(const UnaryTransformType *T); 313 void VisitTagType(const TagType *T); 314 void VisitTemplateTypeParmType(const TemplateTypeParmType *T); 315 void VisitAutoType(const AutoType *T); 316 void VisitTemplateSpecializationType(const TemplateSpecializationType *T); 317 void VisitInjectedClassNameType(const InjectedClassNameType *T); 318 void VisitObjCInterfaceType(const ObjCInterfaceType *T); 319 void VisitPackExpansionType(const PackExpansionType *T); 320 321 void VisitLabelDecl(const LabelDecl *D); 322 void VisitTypedefDecl(const TypedefDecl *D); 323 void VisitEnumDecl(const EnumDecl *D); 324 void VisitRecordDecl(const RecordDecl *D); 325 void VisitEnumConstantDecl(const EnumConstantDecl *D); 326 void VisitIndirectFieldDecl(const IndirectFieldDecl *D); 327 void VisitFunctionDecl(const FunctionDecl *D); 328 void VisitFieldDecl(const FieldDecl *D); 329 void VisitVarDecl(const VarDecl *D); 330 void VisitBindingDecl(const BindingDecl *D); 331 void VisitCapturedDecl(const CapturedDecl *D); 332 void VisitImportDecl(const ImportDecl *D); 333 void VisitPragmaCommentDecl(const PragmaCommentDecl *D); 334 void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D); 335 void VisitOMPExecutableDirective(const OMPExecutableDirective *D); 336 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D); 337 void VisitOMPRequiresDecl(const OMPRequiresDecl *D); 338 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D); 339 void VisitNamespaceDecl(const NamespaceDecl *D); 340 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D); 341 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); 342 void VisitTypeAliasDecl(const TypeAliasDecl *D); 343 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D); 344 void VisitCXXRecordDecl(const CXXRecordDecl *D); 345 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); 346 void VisitClassTemplateDecl(const ClassTemplateDecl *D); 347 void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D); 348 void VisitVarTemplateDecl(const VarTemplateDecl *D); 349 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); 350 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); 351 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); 352 void VisitUsingDecl(const UsingDecl *D); 353 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D); 354 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D); 355 void VisitUsingShadowDecl(const UsingShadowDecl *D); 356 void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D); 357 void VisitLinkageSpecDecl(const LinkageSpecDecl *D); 358 void VisitAccessSpecDecl(const AccessSpecDecl *D); 359 void VisitFriendDecl(const FriendDecl *D); 360 void VisitObjCIvarDecl(const ObjCIvarDecl *D); 361 void VisitObjCMethodDecl(const ObjCMethodDecl *D); 362 void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D); 363 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D); 364 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D); 365 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D); 366 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D); 367 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D); 368 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D); 369 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); 370 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); 371 void VisitBlockDecl(const BlockDecl *D); 372 void VisitConceptDecl(const ConceptDecl *D); 373 void 374 VisitLifetimeExtendedTemporaryDecl(const LifetimeExtendedTemporaryDecl *D); 375 }; 376 377 } // namespace clang 378 379 #endif // LLVM_CLANG_AST_TEXTNODEDUMPER_H 380