clang  9.0.0svn
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)->isHidden())
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  for (const auto *Child : D->specializations())
133  dumpTemplateDeclSpecialization(Child, DumpExplicitInst,
134  !D->isCanonicalDecl());
135 }
136 
138  // FIXME: We don't add a declaration of a function template specialization
139  // to its context when it's explicitly instantiated, so dump explicit
140  // instantiations when we dump the template itself.
141  dumpTemplateDecl(D, true);
142 }
143 
145  dumpTemplateDecl(D, false);
146 }
147 
149  dumpTemplateDecl(D, false);
150 }
151 
152 //===----------------------------------------------------------------------===//
153 // Type method implementations
154 //===----------------------------------------------------------------------===//
155 
156 void QualType::dump(const char *msg) const {
157  if (msg)
158  llvm::errs() << msg << ": ";
159  dump();
160 }
161 
162 LLVM_DUMP_METHOD void QualType::dump() const { dump(llvm::errs()); }
163 
164 LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS) const {
165  ASTDumper Dumper(OS, nullptr, nullptr);
166  Dumper.Visit(*this);
167 }
168 
169 LLVM_DUMP_METHOD void Type::dump() const { dump(llvm::errs()); }
170 
171 LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {
172  QualType(this, 0).dump(OS);
173 }
174 
175 //===----------------------------------------------------------------------===//
176 // Decl method implementations
177 //===----------------------------------------------------------------------===//
178 
179 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
180 
181 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize,
182  ASTDumpOutputFormat Format) const {
183  ASTContext &Ctx = getASTContext();
184  const SourceManager &SM = Ctx.getSourceManager();
185 
186  if (ADOF_JSON == Format) {
187  JSONDumper P(OS, SM, Ctx, Ctx.getPrintingPolicy(),
188  &Ctx.getCommentCommandTraits());
189  (void)Deserialize; // FIXME?
190  P.Visit(this);
191  } else {
192  ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &SM,
194  P.setDeserialize(Deserialize);
195  P.Visit(this);
196  }
197 }
198 
199 LLVM_DUMP_METHOD void Decl::dumpColor() const {
200  const ASTContext &Ctx = getASTContext();
201  ASTDumper P(llvm::errs(), &Ctx.getCommentCommandTraits(),
202  &Ctx.getSourceManager(), /*ShowColors*/ true,
203  Ctx.getPrintingPolicy());
204  P.Visit(this);
205 }
206 
207 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
208  dumpLookups(llvm::errs());
209 }
210 
211 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
212  bool DumpDecls,
213  bool Deserialize) const {
214  const DeclContext *DC = this;
215  while (!DC->isTranslationUnit())
216  DC = DC->getParent();
217  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
218  const SourceManager &SM = Ctx.getSourceManager();
221  P.setDeserialize(Deserialize);
222  P.dumpLookups(this, DumpDecls);
223 }
224 
225 //===----------------------------------------------------------------------===//
226 // Stmt method implementations
227 //===----------------------------------------------------------------------===//
228 
229 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
230  dump(llvm::errs(), SM);
231 }
232 
233 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
234  ASTDumper P(OS, nullptr, &SM);
235  P.Visit(this);
236 }
237 
238 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
239  ASTDumper P(OS, nullptr, nullptr);
240  P.Visit(this);
241 }
242 
243 LLVM_DUMP_METHOD void Stmt::dump() const {
244  ASTDumper P(llvm::errs(), nullptr, nullptr);
245  P.Visit(this);
246 }
247 
248 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
249  ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
250  P.Visit(this);
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // Comment method implementations
255 //===----------------------------------------------------------------------===//
256 
257 LLVM_DUMP_METHOD void Comment::dump() const {
258  dump(llvm::errs(), nullptr, nullptr);
259 }
260 
261 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
262  dump(llvm::errs(), &Context.getCommentCommandTraits(),
263  &Context.getSourceManager());
264 }
265 
266 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
267  const SourceManager *SM) const {
268  const FullComment *FC = dyn_cast<FullComment>(this);
269  if (!FC)
270  return;
271  ASTDumper D(OS, Traits, SM);
272  D.Visit(FC, FC);
273 }
274 
275 LLVM_DUMP_METHOD void Comment::dumpColor() const {
276  const FullComment *FC = dyn_cast<FullComment>(this);
277  if (!FC)
278  return;
279  ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
280  D.Visit(FC, FC);
281 }
void dump() const
Definition: ASTDumper.cpp:169
Defines the clang::ASTContext interface.
A (possibly-)qualified type.
Definition: Type.h:643
Defines the SourceManager interface.
Defines the clang::Module class, which describes a module in the source code.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
StringRef P
NamedDecl * getTemplatedDecl() const
Get the underlying, templated declaration.
Definition: DeclTemplate.h:457
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration, or NULL if there is no previous declaration.
Definition: DeclBase.h:960
Declaration of a variable template.
bool hasExternalVisibleStorage() const
Whether this DeclContext has external storage containing additional declarations that are visible in ...
Definition: DeclBase.h:2355
lookups_range noload_lookups(bool PreserveInternalState) const
Definition: DeclLookups.h:89
void Visit(const Decl *D)
void dumpColor() const
Definition: ASTDumper.cpp:199
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1195
void dumpLookups() const
Definition: ASTDumper.cpp:207
void dumpColor() const
dumpColor - same as dump(), but forces color highlighting.
Definition: ASTDumper.cpp:248
void dumpTemplateDeclSpecialization(const SpecializationDecl *D, bool DumpExplicitInst, bool DumpRefOnly)
Definition: ASTDumper.cpp:85
void VisitVarTemplateDecl(const VarTemplateDecl *D)
Definition: ASTDumper.cpp:148
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:657
static const TerminalColor DeclNameColor
DiagnosticsEngine & getDiagnostics() const
lookups_range lookups() const
Definition: DeclLookups.h:75
void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D)
Definition: ASTDumper.cpp:137
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:436
bool isCanonicalDecl() const
Whether this particular Decl is a canonical one.
Definition: DeclBase.h:883
ASTDumpOutputFormat
Used to specify the format for printing AST dump information.
void dumpLookups(const DeclContext *DC, bool DumpDecls)
Definition: ASTDumper.cpp:25
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:878
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1779
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:180
const SourceManager & SM
Definition: Format.cpp:1572
This class provides information about commands that can be used in comments.
void dumpTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst)
Definition: ASTDumper.cpp:127
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition: Specifiers.h:192
This template specialization was formed from a template-id but has not yet been declared, defined, or instantiated.
Definition: Specifiers.h:177
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:403
void dump() const
Definition: ASTDumper.cpp:162
This template specialization was instantiated from a template due to an explicit instantiation declar...
Definition: Specifiers.h:188
The name of a declaration.
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
Definition: ASTDumper.cpp:243
This template specialization was declared or defined by an explicit specialization (C++ [temp...
Definition: Specifiers.h:184
SourceManager & getSourceManager()
Definition: ASTContext.h:665
Declaration of a class template.
void VisitClassTemplateDecl(const ClassTemplateDecl *D)
Definition: ASTDumper.cpp:144
void dump(const char *s) const
Definition: ASTDumper.cpp:156
void dump() const
Definition: ASTDumper.cpp:179
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
Definition: DeclBase.cpp:1162
bool isTranslationUnit() const
Definition: DeclBase.h:1854
This class handles loading and caching of source files into memory.
Defines enum values for all the target-independent builtin functions.
Declaration of a template function.
A full comment attached to a declaration, contains block content.
Definition: Comment.h:1091
static const TerminalColor UndeserializedColor