1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
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/TypeVisitor.h" |
26 | |
27 | namespace clang { |
28 | |
29 | class TextTreeStructure { |
30 | raw_ostream &OS; |
31 | const bool ShowColors; |
32 | |
33 | |
34 | llvm::SmallVector<std::function<void(bool IsLastChild)>, 32> Pending; |
35 | |
36 | |
37 | bool TopLevel = true; |
38 | |
39 | |
40 | bool FirstChild = true; |
41 | |
42 | |
43 | std::string Prefix; |
44 | |
45 | public: |
46 | |
47 | template <typename Fn> void AddChild(Fn DoAddChild) { |
48 | return AddChild("", DoAddChild); |
49 | } |
50 | |
51 | |
52 | |
53 | template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild) { |
54 | |
55 | |
56 | if (TopLevel) { |
57 | TopLevel = false; |
58 | DoAddChild(); |
59 | while (!Pending.empty()) { |
60 | Pending.back()(true); |
61 | Pending.pop_back(); |
62 | } |
63 | Prefix.clear(); |
64 | OS << "\n"; |
65 | TopLevel = true; |
66 | return; |
67 | } |
68 | |
69 | |
70 | |
71 | std::string LabelStr = Label; |
72 | auto DumpWithIndent = [this, DoAddChild, LabelStr](bool IsLastChild) { |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | { |
86 | OS << '\n'; |
87 | ColorScope Color(OS, ShowColors, IndentColor); |
88 | OS << Prefix << (IsLastChild ? '`' : '|') << '-'; |
89 | if (!LabelStr.empty()) |
90 | OS << LabelStr << ": "; |
91 | |
92 | this->Prefix.push_back(IsLastChild ? ' ' : '|'); |
93 | this->Prefix.push_back(' '); |
94 | } |
95 | |
96 | FirstChild = true; |
97 | unsigned Depth = Pending.size(); |
98 | |
99 | DoAddChild(); |
100 | |
101 | |
102 | |
103 | while (Depth < Pending.size()) { |
104 | Pending.back()(true); |
105 | this->Pending.pop_back(); |
106 | } |
107 | |
108 | |
109 | this->Prefix.resize(Prefix.size() - 2); |
110 | }; |
111 | |
112 | if (FirstChild) { |
113 | Pending.push_back(std::move(DumpWithIndent)); |
114 | } else { |
115 | Pending.back()(false); |
116 | Pending.back() = std::move(DumpWithIndent); |
117 | } |
118 | FirstChild = false; |
119 | } |
120 | |
121 | TextTreeStructure(raw_ostream &OS, bool ShowColors) |
122 | : OS(OS), ShowColors(ShowColors) {} |
123 | }; |
124 | |
125 | class TextNodeDumper |
126 | : public TextTreeStructure, |
127 | public comments::ConstCommentVisitor<TextNodeDumper, void, |
128 | const comments::FullComment *>, |
129 | public ConstAttrVisitor<TextNodeDumper>, |
130 | public ConstTemplateArgumentVisitor<TextNodeDumper>, |
131 | public ConstStmtVisitor<TextNodeDumper>, |
132 | public TypeVisitor<TextNodeDumper>, |
133 | public ConstDeclVisitor<TextNodeDumper> { |
134 | raw_ostream &OS; |
135 | const bool ShowColors; |
136 | |
137 | |
138 | |
139 | const char *LastLocFilename = ""; |
140 | unsigned LastLocLine = ~0U; |
141 | |
142 | const SourceManager *SM; |
143 | |
144 | |
145 | PrintingPolicy PrintPolicy; |
146 | |
147 | const comments::CommandTraits *Traits; |
148 | |
149 | const char *getCommandName(unsigned CommandID); |
150 | |
151 | public: |
152 | TextNodeDumper(raw_ostream &OS, bool ShowColors, const SourceManager *SM, |
153 | const PrintingPolicy &PrintPolicy, |
154 | const comments::CommandTraits *Traits); |
155 | |
156 | void (const comments::Comment *C, const comments::FullComment *FC); |
157 | |
158 | void Visit(const Attr *A); |
159 | |
160 | void Visit(const TemplateArgument &TA, SourceRange R, |
161 | const Decl *From = nullptr, StringRef Label = {}); |
162 | |
163 | void Visit(const Stmt *Node); |
164 | |
165 | void Visit(const Type *T); |
166 | |
167 | void Visit(QualType T); |
168 | |
169 | void Visit(const Decl *D); |
170 | |
171 | void Visit(const CXXCtorInitializer *Init); |
172 | |
173 | void Visit(const OMPClause *C); |
174 | |
175 | void Visit(const BlockDecl::Capture &C); |
176 | |
177 | void Visit(const GenericSelectionExpr::ConstAssociation &A); |
178 | |
179 | void dumpPointer(const void *Ptr); |
180 | void dumpLocation(SourceLocation Loc); |
181 | void dumpSourceRange(SourceRange R); |
182 | void dumpBareType(QualType T, bool Desugar = true); |
183 | void dumpType(QualType T); |
184 | void dumpBareDeclRef(const Decl *D); |
185 | void dumpName(const NamedDecl *ND); |
186 | void dumpAccessSpecifier(AccessSpecifier AS); |
187 | |
188 | void dumpDeclRef(const Decl *D, StringRef Label = {}); |
189 | |
190 | void (const comments::TextComment *C, |
191 | const comments::FullComment *); |
192 | void visitInlineCommandComment(const comments::InlineCommandComment *C, |
193 | const comments::FullComment *); |
194 | void (const comments::HTMLStartTagComment *C, |
195 | const comments::FullComment *); |
196 | void (const comments::HTMLEndTagComment *C, |
197 | const comments::FullComment *); |
198 | void visitBlockCommandComment(const comments::BlockCommandComment *C, |
199 | const comments::FullComment *); |
200 | void visitParamCommandComment(const comments::ParamCommandComment *C, |
201 | const comments::FullComment *FC); |
202 | void visitTParamCommandComment(const comments::TParamCommandComment *C, |
203 | const comments::FullComment *FC); |
204 | void (const comments::VerbatimBlockComment *C, |
205 | const comments::FullComment *); |
206 | void |
207 | (const comments::VerbatimBlockLineComment *C, |
208 | const comments::FullComment *); |
209 | void (const comments::VerbatimLineComment *C, |
210 | const comments::FullComment *); |
211 | |
212 | |
213 | #include "clang/AST/AttrTextNodeDump.inc" |
214 | |
215 | void VisitNullTemplateArgument(const TemplateArgument &TA); |
216 | void VisitTypeTemplateArgument(const TemplateArgument &TA); |
217 | void VisitDeclarationTemplateArgument(const TemplateArgument &TA); |
218 | void VisitNullPtrTemplateArgument(const TemplateArgument &TA); |
219 | void VisitIntegralTemplateArgument(const TemplateArgument &TA); |
220 | void VisitTemplateTemplateArgument(const TemplateArgument &TA); |
221 | void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA); |
222 | void VisitExpressionTemplateArgument(const TemplateArgument &TA); |
223 | void VisitPackTemplateArgument(const TemplateArgument &TA); |
224 | |
225 | void VisitIfStmt(const IfStmt *Node); |
226 | void VisitSwitchStmt(const SwitchStmt *Node); |
227 | void VisitWhileStmt(const WhileStmt *Node); |
228 | void VisitLabelStmt(const LabelStmt *Node); |
229 | void VisitGotoStmt(const GotoStmt *Node); |
230 | void VisitCaseStmt(const CaseStmt *Node); |
231 | void VisitCallExpr(const CallExpr *Node); |
232 | void VisitCastExpr(const CastExpr *Node); |
233 | void VisitImplicitCastExpr(const ImplicitCastExpr *Node); |
234 | void VisitDeclRefExpr(const DeclRefExpr *Node); |
235 | void VisitPredefinedExpr(const PredefinedExpr *Node); |
236 | void VisitCharacterLiteral(const CharacterLiteral *Node); |
237 | void VisitIntegerLiteral(const IntegerLiteral *Node); |
238 | void VisitFixedPointLiteral(const FixedPointLiteral *Node); |
239 | void VisitFloatingLiteral(const FloatingLiteral *Node); |
240 | void VisitStringLiteral(const StringLiteral *Str); |
241 | void VisitInitListExpr(const InitListExpr *ILE); |
242 | void VisitGenericSelectionExpr(const GenericSelectionExpr *E); |
243 | void VisitUnaryOperator(const UnaryOperator *Node); |
244 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node); |
245 | void VisitMemberExpr(const MemberExpr *Node); |
246 | void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node); |
247 | void VisitBinaryOperator(const BinaryOperator *Node); |
248 | void VisitCompoundAssignOperator(const CompoundAssignOperator *Node); |
249 | void VisitAddrLabelExpr(const AddrLabelExpr *Node); |
250 | void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node); |
251 | void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node); |
252 | void VisitCXXThisExpr(const CXXThisExpr *Node); |
253 | void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node); |
254 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node); |
255 | void VisitCXXConstructExpr(const CXXConstructExpr *Node); |
256 | void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node); |
257 | void VisitCXXNewExpr(const CXXNewExpr *Node); |
258 | void VisitCXXDeleteExpr(const CXXDeleteExpr *Node); |
259 | void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node); |
260 | void VisitExprWithCleanups(const ExprWithCleanups *Node); |
261 | void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node); |
262 | void VisitSizeOfPackExpr(const SizeOfPackExpr *Node); |
263 | void |
264 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node); |
265 | void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node); |
266 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node); |
267 | void VisitObjCMessageExpr(const ObjCMessageExpr *Node); |
268 | void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node); |
269 | void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node); |
270 | void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node); |
271 | void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node); |
272 | void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node); |
273 | void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node); |
274 | void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node); |
275 | |
276 | void VisitRValueReferenceType(const ReferenceType *T); |
277 | void VisitArrayType(const ArrayType *T); |
278 | void VisitConstantArrayType(const ConstantArrayType *T); |
279 | void VisitVariableArrayType(const VariableArrayType *T); |
280 | void VisitDependentSizedArrayType(const DependentSizedArrayType *T); |
281 | void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T); |
282 | void VisitVectorType(const VectorType *T); |
283 | void VisitFunctionType(const FunctionType *T); |
284 | void VisitFunctionProtoType(const FunctionProtoType *T); |
285 | void VisitUnresolvedUsingType(const UnresolvedUsingType *T); |
286 | void VisitTypedefType(const TypedefType *T); |
287 | void VisitUnaryTransformType(const UnaryTransformType *T); |
288 | void VisitTagType(const TagType *T); |
289 | void VisitTemplateTypeParmType(const TemplateTypeParmType *T); |
290 | void VisitAutoType(const AutoType *T); |
291 | void VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
292 | void VisitInjectedClassNameType(const InjectedClassNameType *T); |
293 | void VisitObjCInterfaceType(const ObjCInterfaceType *T); |
294 | void VisitPackExpansionType(const PackExpansionType *T); |
295 | |
296 | void VisitLabelDecl(const LabelDecl *D); |
297 | void VisitTypedefDecl(const TypedefDecl *D); |
298 | void VisitEnumDecl(const EnumDecl *D); |
299 | void VisitRecordDecl(const RecordDecl *D); |
300 | void VisitEnumConstantDecl(const EnumConstantDecl *D); |
301 | void VisitIndirectFieldDecl(const IndirectFieldDecl *D); |
302 | void VisitFunctionDecl(const FunctionDecl *D); |
303 | void VisitFieldDecl(const FieldDecl *D); |
304 | void VisitVarDecl(const VarDecl *D); |
305 | void VisitBindingDecl(const BindingDecl *D); |
306 | void VisitCapturedDecl(const CapturedDecl *D); |
307 | void VisitImportDecl(const ImportDecl *D); |
308 | void (const PragmaCommentDecl *D); |
309 | void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D); |
310 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
311 | void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D); |
312 | void VisitOMPRequiresDecl(const OMPRequiresDecl *D); |
313 | void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D); |
314 | void VisitNamespaceDecl(const NamespaceDecl *D); |
315 | void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D); |
316 | void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); |
317 | void VisitTypeAliasDecl(const TypeAliasDecl *D); |
318 | void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D); |
319 | void VisitCXXRecordDecl(const CXXRecordDecl *D); |
320 | void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); |
321 | void VisitClassTemplateDecl(const ClassTemplateDecl *D); |
322 | void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D); |
323 | void VisitVarTemplateDecl(const VarTemplateDecl *D); |
324 | void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); |
325 | void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); |
326 | void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); |
327 | void VisitUsingDecl(const UsingDecl *D); |
328 | void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D); |
329 | void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D); |
330 | void VisitUsingShadowDecl(const UsingShadowDecl *D); |
331 | void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D); |
332 | void VisitLinkageSpecDecl(const LinkageSpecDecl *D); |
333 | void VisitAccessSpecDecl(const AccessSpecDecl *D); |
334 | void VisitFriendDecl(const FriendDecl *D); |
335 | void VisitObjCIvarDecl(const ObjCIvarDecl *D); |
336 | void VisitObjCMethodDecl(const ObjCMethodDecl *D); |
337 | void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D); |
338 | void VisitObjCCategoryDecl(const ObjCCategoryDecl *D); |
339 | void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D); |
340 | void VisitObjCProtocolDecl(const ObjCProtocolDecl *D); |
341 | void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D); |
342 | void VisitObjCImplementationDecl(const ObjCImplementationDecl *D); |
343 | void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D); |
344 | void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); |
345 | void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); |
346 | void VisitBlockDecl(const BlockDecl *D); |
347 | |
348 | private: |
349 | void dumpCXXTemporary(const CXXTemporary *Temporary); |
350 | }; |
351 | |
352 | } |
353 | |
354 | #endif |
355 | |