clang  14.0.0git
ASTDumper.cpp
Go to the documentation of this file.
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/ASTContext.h"
16 #include "clang/AST/DeclLookups.h"
18 #include "clang/Basic/Builtins.h"
19 #include "clang/Basic/Module.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
23 using namespace clang::comments;
24 
25 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
26  NodeDumper.AddChild([=] {
27  OS << "StoredDeclsMap ";
28  NodeDumper.dumpBareDeclRef(cast<Decl>(DC));
29 
30  const DeclContext *Primary = DC->getPrimaryContext();
31  if (Primary != DC) {
32  OS << " primary";
33  NodeDumper.dumpPointer(cast<Decl>(Primary));
34  }
35 
36  bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
37 
38  auto Range = getDeserialize()
39  ? Primary->lookups()
40  : Primary->noload_lookups(/*PreserveInternalState=*/true);
41  for (auto I = Range.begin(), E = Range.end(); I != E; ++I) {
42  DeclarationName Name = I.getLookupName();
44 
45  NodeDumper.AddChild([=] {
46  OS << "DeclarationName ";
47  {
48  ColorScope Color(OS, ShowColors, DeclNameColor);
49  OS << '\'' << Name << '\'';
50  }
51 
52  for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
53  RI != RE; ++RI) {
54  NodeDumper.AddChild([=] {
55  NodeDumper.dumpBareDeclRef(*RI);
56 
57  if (!(*RI)->isUnconditionallyVisible())
58  OS << " hidden";
59 
60  // If requested, dump the redecl chain for this lookup.
61  if (DumpDecls) {
62  // Dump earliest decl first.
63  std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
64  if (Decl *Prev = D->getPreviousDecl())
65  DumpWithPrev(Prev);
66  Visit(D);
67  };
68  DumpWithPrev(*RI);
69  }
70  });
71  }
72  });
73  }
74 
75  if (HasUndeserializedLookups) {
76  NodeDumper.AddChild([=] {
77  ColorScope Color(OS, ShowColors, UndeserializedColor);
78  OS << "<undeserialized lookups>";
79  });
80  }
81  });
82 }
83 
84 template <typename SpecializationDecl>
85 void ASTDumper::dumpTemplateDeclSpecialization(const SpecializationDecl *D,
86  bool DumpExplicitInst,
87  bool DumpRefOnly) {
88  bool DumpedAny = false;
89  for (const auto *RedeclWithBadType : D->redecls()) {
90  // FIXME: The redecls() range sometimes has elements of a less-specific
91  // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
92  // us TagDecls, and should give CXXRecordDecls).
93  auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
94  if (!Redecl) {
95  // Found the injected-class-name for a class template. This will be dumped
96  // as part of its surrounding class so we don't need to dump it here.
97  assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
98  "expected an injected-class-name");
99  continue;
100  }
101 
102  switch (Redecl->getTemplateSpecializationKind()) {
105  if (!DumpExplicitInst)
106  break;
107  LLVM_FALLTHROUGH;
108  case TSK_Undeclared:
110  if (DumpRefOnly)
111  NodeDumper.dumpDeclRef(Redecl);
112  else
113  Visit(Redecl);
114  DumpedAny = true;
115  break;
117  break;
118  }
119  }
120 
121  // Ensure we dump at least one decl for each specialization.
122  if (!DumpedAny)
123  NodeDumper.dumpDeclRef(D);
124 }
125 
126 template <typename TemplateDecl>
127 void ASTDumper::dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst) {
128  dumpTemplateParameters(D->getTemplateParameters());
129 
130  Visit(D->getTemplatedDecl());
131 
132  if (GetTraversalKind() == TK_AsIs) {
133  for (const auto *Child : D->specializations())
134  dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
135  !D->isCanonicalDecl());
136  }
137 }
138 
140  // FIXME: We don't add a declaration of a function template specialization
141  // to its context when it's explicitly instantiated, so dump explicit
142  // instantiations when we dump the template itself.
143  dumpTemplateDecl(D, true);
144 }
145 
147  dumpTemplateDecl(D, false);
148 }
149 
151  dumpTemplateDecl(D, false);
152 }
153 
154 //===----------------------------------------------------------------------===//
155 // Type method implementations
156 //===----------------------------------------------------------------------===//
157 
158 void QualType::dump(const char *msg) const {
159  if (msg)
160  llvm::errs() << msg << ": ";
161  dump();
162 }
163 
164 LLVM_DUMP_METHOD void QualType::dump() const {
165  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
166  Dumper.Visit(*this);
167 }
168 
169 LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS,
170  const ASTContext &Context) const {
171  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
172  Dumper.Visit(*this);
173 }
174 
175 LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
176 
177 LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS,
178  const ASTContext &Context) const {
179  QualType(this, 0).dump(OS, Context);
180 }
181 
182 //===----------------------------------------------------------------------===//
183 // Decl method implementations
184 //===----------------------------------------------------------------------===//
185 
186 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
187 
188 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize,
189  ASTDumpOutputFormat Format) const {
190  ASTContext &Ctx = getASTContext();
191  const SourceManager &SM = Ctx.getSourceManager();
192 
193  if (ADOF_JSON == Format) {
194  JSONDumper P(OS, SM, Ctx, Ctx.getPrintingPolicy(),
195  &Ctx.getCommentCommandTraits());
196  (void)Deserialize; // FIXME?
197  P.Visit(this);
198  } else {
199  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
200  P.setDeserialize(Deserialize);
201  P.Visit(this);
202  }
203 }
204 
205 LLVM_DUMP_METHOD void Decl::dumpColor() const {
206  const ASTContext &Ctx = getASTContext();
207  ASTDumper P(llvm::errs(), Ctx, /*ShowColors=*/true);
208  P.Visit(this);
209 }
210 
211 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
212  dumpLookups(llvm::errs());
213 }
214 
215 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
216  bool DumpDecls,
217  bool Deserialize) const {
218  const DeclContext *DC = this;
219  while (!DC->isTranslationUnit())
220  DC = DC->getParent();
221  const ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
222  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
223  P.setDeserialize(Deserialize);
224  P.dumpLookups(this, DumpDecls);
225 }
226 
227 //===----------------------------------------------------------------------===//
228 // Stmt method implementations
229 //===----------------------------------------------------------------------===//
230 
231 LLVM_DUMP_METHOD void Stmt::dump() const {
232  ASTDumper P(llvm::errs(), /*ShowColors=*/false);
233  P.Visit(this);
234 }
235 
236 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS,
237  const ASTContext &Context) const {
238  ASTDumper P(OS, Context, Context.getDiagnostics().getShowColors());
239  P.Visit(this);
240 }
241 
242 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
243  ASTDumper P(llvm::errs(), /*ShowColors=*/true);
244  P.Visit(this);
245 }
246 
247 //===----------------------------------------------------------------------===//
248 // Comment method implementations
249 //===----------------------------------------------------------------------===//
250 
251 LLVM_DUMP_METHOD void Comment::dump() const {
252  const auto *FC = dyn_cast<FullComment>(this);
253  if (!FC)
254  return;
255  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
256  Dumper.Visit(FC, FC);
257 }
258 
259 LLVM_DUMP_METHOD void Comment::dump(raw_ostream &OS,
260  const ASTContext &Context) const {
261  const auto *FC = dyn_cast<FullComment>(this);
262  if (!FC)
263  return;
264  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
265  Dumper.Visit(FC, FC);
266 }
267 
268 LLVM_DUMP_METHOD void Comment::dumpColor() const {
269  const auto *FC = dyn_cast<FullComment>(this);
270  if (!FC)
271  return;
272  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/true);
273  Dumper.Visit(FC, FC);
274 }
275 
276 //===----------------------------------------------------------------------===//
277 // APValue method implementations
278 //===----------------------------------------------------------------------===//
279 
280 LLVM_DUMP_METHOD void APValue::dump() const {
281  ASTDumper Dumper(llvm::errs(), /*ShowColors=*/false);
282  Dumper.Visit(*this, /*Ty=*/QualType());
283 }
284 
285 LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
286  const ASTContext &Context) const {
287  ASTDumper Dumper(llvm::errs(), Context,
288  Context.getDiagnostics().getShowColors());
289  Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
290 }
clang::TemplateDecl::getTemplatedDecl
NamedDecl * getTemplatedDecl() const
Get the underlying, templated declaration.
Definition: DeclTemplate.h:431
clang::APValue::dump
void dump() const
Definition: ASTDumper.cpp:280
clang::DeclContext::hasExternalVisibleStorage
bool hasExternalVisibleStorage() const
Whether this DeclContext has external storage containing additional declarations that are visible in ...
Definition: DeclBase.h:2442
Builtins.h
clang::ASTDumper::VisitClassTemplateDecl
void VisitClassTemplateDecl(const ClassTemplateDecl *D)
Definition: ASTDumper.cpp:146
clang::comments::Comment::dumpColor
void dumpColor() const
Definition: ASTDumper.cpp:268
DeclLookups.h
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1347
clang::ColorScope
Definition: ASTDumperUtils.h:86
clang::if
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
Definition: RecursiveASTVisitor.h:1002
clang::DeclContext::getPrimaryContext
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
Definition: DeclBase.cpp:1229
clang::TSK_Undeclared
@ TSK_Undeclared
This template specialization was formed from a template-id but has not yet been declared,...
Definition: Specifiers.h:167
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::DeclListNode::iterator
Definition: DeclBase.h:1244
Module.h
SourceManager.h
clang::JSONDumper
Definition: JSONNodeDumper.h:354
clang::Stmt::dump
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
Definition: ASTDumper.cpp:231
clang::DeclarationName
The name of a declaration.
Definition: DeclarationName.h:145
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:694
clang::DeclContextLookupResult::begin
iterator begin()
Definition: DeclBase.h:1306
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::FunctionTemplateDecl
Declaration of a template function.
Definition: DeclTemplate.h:974
clang::TSK_ExplicitInstantiationDeclaration
@ TSK_ExplicitInstantiationDeclaration
This template specialization was instantiated from a template due to an explicit instantiation declar...
Definition: Specifiers.h:178
ASTDumper.h
clang::DeclContext::lookups
lookups_range lookups() const
Definition: DeclLookups.h:75
clang::UndeserializedColor
static const TerminalColor UndeserializedColor
Definition: ASTDumperUtils.h:72
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:211
clang::ASTDumpOutputFormat
ASTDumpOutputFormat
Used to specify the format for printing AST dump information.
Definition: ASTDumperUtils.h:21
clang::ASTDumper::dumpTemplateDecl
void dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst)
Definition: ASTDumper.cpp:127
clang::DeclContext::isTranslationUnit
bool isTranslationUnit() const
Definition: DeclBase.h:1935
ASTContext.h
clang::QualType::dump
void dump() const
Definition: ASTDumper.cpp:164
clang::DeclContext::noload_lookups
lookups_range noload_lookups(bool PreserveInternalState) const
Definition: DeclLookups.h:89
clang::TSK_ExplicitInstantiationDefinition
@ TSK_ExplicitInstantiationDefinition
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition: Specifiers.h:182
clang::ASTContext::getCommentCommandTraits
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:918
clang::Decl::dump
void dump() const
Definition: ASTDumper.cpp:186
clang::QualType::dump
void dump(const char *s) const
Definition: ASTDumper.cpp:158
clang::TemplateDecl::getTemplateParameters
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:418
clang::ASTDumper::VisitVarTemplateDecl
void VisitVarTemplateDecl(const VarTemplateDecl *D)
Definition: ASTDumper.cpp:150
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::Decl::getPreviousDecl
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Definition: DeclBase.h:984
clang::ASTDumper::VisitFunctionTemplateDecl
void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D)
Definition: ASTDumper.cpp:139
clang::ClassTemplateDecl
Declaration of a class template.
Definition: DeclTemplate.h:2243
clang::TK_AsIs
@ TK_AsIs
Will traverse all child nodes.
Definition: ASTTypeTraits.h:39
dump
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Definition: CoverageMappingGen.cpp:1521
clang::DeclContext::getParent
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1860
clang::ASTContext::getPrintingPolicy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:686
clang::TemplateDecl
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
clang::DeclContext::dumpLookups
void dumpLookups() const
Definition: ASTDumper.cpp:211
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::DeclContextLookupResult::end
iterator end()
Definition: DeclBase.h:1307
clang::ASTContext::CharTy
CanQualType CharTy
Definition: ASTContext.h:1069
clang::ASTNodeTraverser::Visit
void Visit(const Decl *D)
Definition: ASTNodeTraverser.h:88
clang::TSK_ExplicitSpecialization
@ TSK_ExplicitSpecialization
This template specialization was declared or defined by an explicit specialization (C++ [temp....
Definition: Specifiers.h:174
clang::ASTDumper::dumpTemplateDeclSpecialization
void dumpTemplateDeclSpecialization(const SpecializationDecl *D, bool DumpExplicitInst, bool DumpRefOnly)
Definition: ASTDumper.cpp:85
clang::ASTDumper::dumpLookups
void dumpLookups(const DeclContext *DC, bool DumpDecls)
Definition: ASTDumper.cpp:25
clang::DiagnosticsEngine::getShowColors
bool getShowColors()
Definition: Diagnostic.h:706
clang::comments
Definition: ASTContext.h:151
clang::Type::dump
void dump() const
Definition: ASTDumper.cpp:175
clang
Definition: CalledOnceCheck.h:17
clang::DeclNameColor
static const TerminalColor DeclNameColor
Definition: ASTDumperUtils.h:81
clang::ADOF_JSON
@ ADOF_JSON
Definition: ASTDumperUtils.h:23
clang::DeclContextLookupResult
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1292
clang::ASTContext::getPointerType
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
Definition: ASTContext.cpp:3251
clang::ASTDumper
Definition: ASTDumper.h:18
clang::VarTemplateDecl
Declaration of a variable template.
Definition: DeclTemplate.h:3075
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::TSK_ImplicitInstantiation
@ TSK_ImplicitInstantiation
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:170
clang::comments::Comment::dump
void dump() const
Definition: ASTDumper.cpp:251
clang::Decl::isCanonicalDecl
bool isCanonicalDecl() const
Whether this particular Decl is a canonical one.
Definition: DeclBase.h:907
JSONNodeDumper.h
clang::Stmt::dumpColor
void dumpColor() const
dumpColor - same as dump(), but forces color highlighting.
Definition: ASTDumper.cpp:242
clang::Decl::dumpColor
void dumpColor() const
Definition: ASTDumper.cpp:205
clang::ASTContext::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: ASTContext.cpp:1501