1 | //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===// |
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 the AST dump methods, which dump out the |
10 | // AST in a form that exposes type details and other fields. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "clang/AST/ASTDumper.h" |
15 | #include "clang/AST/ASTConcept.h" |
16 | #include "clang/AST/ASTContext.h" |
17 | #include "clang/AST/DeclLookups.h" |
18 | #include "clang/AST/JSONNodeDumper.h" |
19 | #include "clang/Basic/Builtins.h" |
20 | #include "clang/Basic/Module.h" |
21 | #include "clang/Basic/SourceManager.h" |
22 | #include "llvm/Support/raw_ostream.h" |
23 | |
24 | using namespace clang; |
25 | using namespace clang::comments; |
26 | |
27 | void ASTDumper::dumpInvalidDeclContext(const DeclContext *DC) { |
28 | NodeDumper.AddChild(DoAddChild: [=] { |
29 | if (!DC) { |
30 | ColorScope Color(OS, ShowColors, NullColor); |
31 | OS << "<<<NULL>>>" ; |
32 | return; |
33 | } |
34 | // An invalid DeclContext is one for which a dyn_cast() from a DeclContext |
35 | // pointer to a Decl pointer would fail an assertion or otherwise fall prey |
36 | // to undefined behavior as a result of an invalid associated DeclKind. |
37 | // Such invalidity is not supposed to happen of course, but, when it does, |
38 | // the information provided below is intended to provide some hints about |
39 | // what might have gone awry. |
40 | { |
41 | ColorScope Color(OS, ShowColors, DeclKindNameColor); |
42 | OS << "DeclContext" ; |
43 | } |
44 | NodeDumper.dumpPointer(Ptr: DC); |
45 | OS << " <" ; |
46 | { |
47 | ColorScope Color(OS, ShowColors, DeclNameColor); |
48 | OS << "unrecognized Decl kind " << (unsigned)DC->getDeclKind(); |
49 | } |
50 | OS << ">" ; |
51 | }); |
52 | } |
53 | |
54 | void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { |
55 | NodeDumper.AddChild(DoAddChild: [=] { |
56 | OS << "StoredDeclsMap " ; |
57 | NodeDumper.dumpBareDeclRef(D: cast<Decl>(Val: DC)); |
58 | |
59 | const DeclContext *Primary = DC->getPrimaryContext(); |
60 | if (Primary != DC) { |
61 | OS << " primary" ; |
62 | NodeDumper.dumpPointer(Ptr: cast<Decl>(Val: Primary)); |
63 | } |
64 | |
65 | bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage(); |
66 | |
67 | auto Range = getDeserialize() |
68 | ? Primary->lookups() |
69 | : Primary->noload_lookups(/*PreserveInternalState=*/true); |
70 | for (auto I = Range.begin(), E = Range.end(); I != E; ++I) { |
71 | DeclarationName Name = I.getLookupName(); |
72 | DeclContextLookupResult R = *I; |
73 | |
74 | NodeDumper.AddChild(DoAddChild: [=] { |
75 | OS << "DeclarationName " ; |
76 | { |
77 | ColorScope Color(OS, ShowColors, DeclNameColor); |
78 | OS << '\'' << Name << '\''; |
79 | } |
80 | |
81 | for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end(); |
82 | RI != RE; ++RI) { |
83 | NodeDumper.AddChild(DoAddChild: [=] { |
84 | NodeDumper.dumpBareDeclRef(*RI); |
85 | |
86 | if (!(*RI)->isUnconditionallyVisible()) |
87 | OS << " hidden" ; |
88 | |
89 | // If requested, dump the redecl chain for this lookup. |
90 | if (DumpDecls) { |
91 | // Dump earliest decl first. |
92 | std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) { |
93 | if (Decl *Prev = D->getPreviousDecl()) |
94 | DumpWithPrev(Prev); |
95 | Visit(D); |
96 | }; |
97 | DumpWithPrev(*RI); |
98 | } |
99 | }); |
100 | } |
101 | }); |
102 | } |
103 | |
104 | if (HasUndeserializedLookups) { |
105 | NodeDumper.AddChild(DoAddChild: [=] { |
106 | ColorScope Color(OS, ShowColors, UndeserializedColor); |
107 | OS << "<undeserialized lookups>" ; |
108 | }); |
109 | } |
110 | }); |
111 | } |
112 | |
113 | template <typename SpecializationDecl> |
114 | void ASTDumper::dumpTemplateDeclSpecialization(const SpecializationDecl *D, |
115 | bool DumpExplicitInst, |
116 | bool DumpRefOnly) { |
117 | bool DumpedAny = false; |
118 | for (const auto *RedeclWithBadType : D->redecls()) { |
119 | // FIXME: The redecls() range sometimes has elements of a less-specific |
120 | // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives |
121 | // us TagDecls, and should give CXXRecordDecls). |
122 | auto *Redecl = cast<SpecializationDecl>(RedeclWithBadType); |
123 | switch (Redecl->getTemplateSpecializationKind()) { |
124 | case TSK_ExplicitInstantiationDeclaration: |
125 | case TSK_ExplicitInstantiationDefinition: |
126 | if (!DumpExplicitInst) |
127 | break; |
128 | [[fallthrough]]; |
129 | case TSK_Undeclared: |
130 | case TSK_ImplicitInstantiation: |
131 | if (DumpRefOnly) |
132 | NodeDumper.dumpDeclRef(D: Redecl); |
133 | else |
134 | Visit(Redecl); |
135 | DumpedAny = true; |
136 | break; |
137 | case TSK_ExplicitSpecialization: |
138 | break; |
139 | } |
140 | } |
141 | |
142 | // Ensure we dump at least one decl for each specialization. |
143 | if (!DumpedAny) |
144 | NodeDumper.dumpDeclRef(D); |
145 | } |
146 | |
147 | template <typename TemplateDecl> |
148 | void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) { |
149 | dumpTemplateParameters(TPL: D->getTemplateParameters()); |
150 | |
151 | Visit(D->getTemplatedDecl()); |
152 | |
153 | if (GetTraversalKind() == TK_AsIs) { |
154 | for (const auto *Child : D->specializations()) |
155 | dumpTemplateDeclSpecialization(Child, DumpExplicitInst, |
156 | !D->isCanonicalDecl()); |
157 | } |
158 | } |
159 | |
160 | void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { |
161 | // FIXME: We don't add a declaration of a function template specialization |
162 | // to its context when it's explicitly instantiated, so dump explicit |
163 | // instantiations when we dump the template itself. |
164 | dumpTemplateDecl(D, DumpExplicitInst: true); |
165 | } |
166 | |
167 | void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) { |
168 | dumpTemplateDecl(D, DumpExplicitInst: false); |
169 | } |
170 | |
171 | void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) { |
172 | dumpTemplateDecl(D, DumpExplicitInst: false); |
173 | } |
174 | |
175 | //===----------------------------------------------------------------------===// |
176 | // Type method implementations |
177 | //===----------------------------------------------------------------------===// |
178 | |
179 | void QualType::dump(const char *msg) const { |
180 | if (msg) |
181 | llvm::errs() << msg << ": " ; |
182 | dump(); |
183 | } |
184 | |
185 | LLVM_DUMP_METHOD void QualType::dump() const { |
186 | ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false); |
187 | Dumper.Visit(T: *this); |
188 | } |
189 | |
190 | LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS, |
191 | const ASTContext &Context) const { |
192 | ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors()); |
193 | Dumper.Visit(T: *this); |
194 | } |
195 | |
196 | LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); } |
197 | |
198 | LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS, |
199 | const ASTContext &Context) const { |
200 | QualType(this, 0).dump(OS, Context); |
201 | } |
202 | |
203 | //===----------------------------------------------------------------------===// |
204 | // TypeLoc method implementations |
205 | //===----------------------------------------------------------------------===// |
206 | |
207 | LLVM_DUMP_METHOD void TypeLoc::dump() const { |
208 | ASTDumper(llvm::errs(), /*ShowColors=*/false).Visit(T: *this); |
209 | } |
210 | |
211 | LLVM_DUMP_METHOD void TypeLoc::dump(llvm::raw_ostream &OS, |
212 | const ASTContext &Context) const { |
213 | ASTDumper(OS, Context, Context.getDiagnostics().getShowColors()).Visit(T: *this); |
214 | } |
215 | |
216 | //===----------------------------------------------------------------------===// |
217 | // Decl method implementations |
218 | //===----------------------------------------------------------------------===// |
219 | |
220 | LLVM_DUMP_METHOD void Decl::dump() const { dump(Out&: llvm::errs()); } |
221 | |
222 | LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize, |
223 | ASTDumpOutputFormat Format) const { |
224 | ASTContext &Ctx = getASTContext(); |
225 | const SourceManager &SM = Ctx.getSourceManager(); |
226 | |
227 | if (ADOF_JSON == Format) { |
228 | JSONDumper P(OS, SM, Ctx, Ctx.getPrintingPolicy(), |
229 | &Ctx.getCommentCommandTraits()); |
230 | (void)Deserialize; // FIXME? |
231 | P.Visit(D: this); |
232 | } else { |
233 | ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors()); |
234 | P.setDeserialize(Deserialize); |
235 | P.Visit(D: this); |
236 | } |
237 | } |
238 | |
239 | LLVM_DUMP_METHOD void Decl::dumpColor() const { |
240 | const ASTContext &Ctx = getASTContext(); |
241 | ASTDumper P(llvm::errs(), Ctx, /*ShowColors=*/true); |
242 | P.Visit(D: this); |
243 | } |
244 | |
245 | LLVM_DUMP_METHOD void DeclContext::dumpAsDecl() const { |
246 | dumpAsDecl(Ctx: nullptr); |
247 | } |
248 | |
249 | LLVM_DUMP_METHOD void DeclContext::dumpAsDecl(const ASTContext *Ctx) const { |
250 | // By design, DeclContext is required to be a base class of some class that |
251 | // derives from Decl. Thus, it should always be possible to dyn_cast() from |
252 | // a DeclContext pointer to a Decl pointer and Decl::castFromDeclContext() |
253 | // asserts that to be the case. Since this function is intended for use in a |
254 | // debugger, it performs an additional check in order to prevent a failed |
255 | // cast and assertion. If that check fails, then the (invalid) DeclContext |
256 | // is dumped with an indication of its invalidity. |
257 | if (hasValidDeclKind()) { |
258 | const auto *D = cast<Decl>(Val: this); |
259 | D->dump(); |
260 | } else { |
261 | // If an ASTContext is not available, a less capable ASTDumper is |
262 | // constructed for which color diagnostics are, regrettably, disabled. |
263 | ASTDumper P = Ctx ? ASTDumper(llvm::errs(), *Ctx, |
264 | Ctx->getDiagnostics().getShowColors()) |
265 | : ASTDumper(llvm::errs(), /*ShowColors*/ false); |
266 | P.dumpInvalidDeclContext(DC: this); |
267 | } |
268 | } |
269 | |
270 | LLVM_DUMP_METHOD void DeclContext::dumpLookups() const { |
271 | dumpLookups(OS&: llvm::errs()); |
272 | } |
273 | |
274 | LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS, |
275 | bool DumpDecls, |
276 | bool Deserialize) const { |
277 | const DeclContext *DC = this; |
278 | while (!DC->isTranslationUnit()) |
279 | DC = DC->getParent(); |
280 | const ASTContext &Ctx = cast<TranslationUnitDecl>(Val: DC)->getASTContext(); |
281 | ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors()); |
282 | P.setDeserialize(Deserialize); |
283 | P.dumpLookups(DC: this, DumpDecls); |
284 | } |
285 | |
286 | //===----------------------------------------------------------------------===// |
287 | // Stmt method implementations |
288 | //===----------------------------------------------------------------------===// |
289 | |
290 | LLVM_DUMP_METHOD void Stmt::dump() const { |
291 | ASTDumper P(llvm::errs(), /*ShowColors=*/false); |
292 | P.Visit(Node: this); |
293 | } |
294 | |
295 | LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, |
296 | const ASTContext &Context) const { |
297 | ASTDumper P(OS, Context, Context.getDiagnostics().getShowColors()); |
298 | P.Visit(Node: this); |
299 | } |
300 | |
301 | LLVM_DUMP_METHOD void Stmt::dumpColor() const { |
302 | ASTDumper P(llvm::errs(), /*ShowColors=*/true); |
303 | P.Visit(Node: this); |
304 | } |
305 | |
306 | //===----------------------------------------------------------------------===// |
307 | // Comment method implementations |
308 | //===----------------------------------------------------------------------===// |
309 | |
310 | LLVM_DUMP_METHOD void Comment::() const { |
311 | const auto *FC = dyn_cast<FullComment>(Val: this); |
312 | if (!FC) |
313 | return; |
314 | ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false); |
315 | Dumper.Visit(FC, FC); |
316 | } |
317 | |
318 | LLVM_DUMP_METHOD void Comment::(raw_ostream &OS, |
319 | const ASTContext &Context) const { |
320 | const auto *FC = dyn_cast<FullComment>(Val: this); |
321 | if (!FC) |
322 | return; |
323 | ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors()); |
324 | Dumper.Visit(FC, FC); |
325 | } |
326 | |
327 | LLVM_DUMP_METHOD void Comment::() const { |
328 | const auto *FC = dyn_cast<FullComment>(Val: this); |
329 | if (!FC) |
330 | return; |
331 | ASTDumper Dumper(llvm::errs(), /*ShowColors=*/true); |
332 | Dumper.Visit(FC, FC); |
333 | } |
334 | |
335 | //===----------------------------------------------------------------------===// |
336 | // APValue method implementations |
337 | //===----------------------------------------------------------------------===// |
338 | |
339 | LLVM_DUMP_METHOD void APValue::dump() const { |
340 | ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false); |
341 | Dumper.Visit(Value: *this, /*Ty=*/QualType()); |
342 | } |
343 | |
344 | LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS, |
345 | const ASTContext &Context) const { |
346 | ASTDumper Dumper(llvm::errs(), Context, |
347 | Context.getDiagnostics().getShowColors()); |
348 | Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy)); |
349 | } |
350 | |
351 | //===----------------------------------------------------------------------===// |
352 | // ConceptReference method implementations |
353 | //===----------------------------------------------------------------------===// |
354 | |
355 | LLVM_DUMP_METHOD void ConceptReference::dump() const { |
356 | dump(llvm::errs()); |
357 | } |
358 | |
359 | LLVM_DUMP_METHOD void ConceptReference::dump(raw_ostream &OS) const { |
360 | auto &Ctx = getNamedConcept()->getASTContext(); |
361 | ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors()); |
362 | P.Visit(R: this); |
363 | } |
364 | |